Product graphs working initially
[graph.njae.git] / spec / graph / edge_spec.rb
1 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
3 module GraphNjae
4 describe Edge do
5 let (:e) { Edge.new }
6 describe "#initialize" do
7 it "creates an empty edge" do
8 e = Edge.new
9 e.connections.should be_empty
10 end
11
12 it "creates an edge with some parameters" do
13 e = Edge.new :value1 => 1, :value2 => "value2", :value3 => :v3
14 e.value1.should == 1
15 e.value2.should == "value2"
16 e.value3.should == :v3
17 e.value4.should be_nil
18 end
19
20 end # #initialize
21
22 describe "adds attribues" do
23 it "adds then reports arbitrary attributes" do
24 e.score = 15
25 e.score.should == 15
26 end
27 end # adds attributes
28
29 describe "#<<" do
30 it "adds a new vertex to an edge (with a connection)" do
31 e.connections.should be_empty
32 v1 = Vertex.new
33 v2 = Vertex.new
34 e << v1
35 e.should have(1).connections
36 e.should have(1).vertices
37 e.vertices.should include(v1)
38 v1.edges.should include(e)
39 e << v2
40 e.should have(2).connections
41 e.should have(2).vertices
42 e.vertices.should include(v1)
43 e.vertices.should include(v2)
44 v2.edges.should include(e)
45 end
46
47 it "adds several vertices to an edge" do
48 e.connections.should be_empty
49 v1 = Vertex.new
50 v2 = Vertex.new
51 e << v1 << v2
52 e.vertices.should include(v1)
53 e.vertices.should include(v2)
54 e.should have(2).vertices
55 end
56
57 it "adds a self-loop" do
58 e.connections.should be_empty
59 v1 = Vertex.new
60 e << v1 << v1
61 e.vertices.should include(v1)
62 e.should have(2).vertices
63 e.vertices.uniq.length.should == 1
64 end
65 end # #<<
66
67 describe "connection_at" do
68 it "returns the connection that links to a vertex" do
69 e.connections.should be_empty
70 v1 = Vertex.new
71 v2 = Vertex.new
72 e << v1 << v2
73
74 e.connection_at(v1).end.should be v1
75 e.connection_at(v2).end.should be v2
76 end
77
78 it "returns nil if there is no connection to that vertex" do
79 e.connections.should be_empty
80 v1 = Vertex.new
81 v2 = Vertex.new
82 v3 = Vertex.new
83 e << v1 << v2
84
85 e.connection_at(v3).should be nil
86 end
87
88 it "returns the vertex for a self-loop" do
89 e.connections.should be_empty
90 v1 = Vertex.new
91 e << v1 << v1
92
93 e.connection_at(v1).end.should be v1
94 end
95
96
97 end # #connection_at
98 end # Edge
99
100 describe Connection do
101 let (:c) {Connection.new }
102
103 describe "adds attribues" do
104 it "adds then reports arbitrary attributes" do
105 c.score = 15
106 c.score.should == 15
107 end
108 end # adds attributes
109 end # Connection
110
111 end