8fc71ba76c5bfa42b7ee3843e32d144c3a111e5a
[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 end # #initialize
20
21 describe "adds attribues" do
22 it "adds then reports arbitrary attributes" do
23 e.score = 15
24 e.score.should == 15
25 end
26 end # adds attributes
27
28 describe "#<<" do
29 it "adds a new vertex to an edge (with a connection)" do
30 e.connections.should be_empty
31 v1 = Vertex.new :name => :v1
32 v2 = Vertex.new :name => :v2
33 e << v1
34 e.should have(1).connections
35 e.should have(1).vertices
36 e.vertices.should include(v1)
37 v1.edges.should include(e)
38 e << v2
39 e.should have(2).connections
40 e.should have(2).vertices
41 e.vertices.should include(v1)
42 e.vertices.should include(v2)
43 v2.edges.should include(e)
44 end
45
46 it "adds several vertices to an edge" do
47 e.connections.should be_empty
48 v1 = Vertex.new :name => :v1
49 v2 = Vertex.new :name => :v2
50 e << v1 << v2
51 e.vertices.should include(v1)
52 e.vertices.should include(v2)
53 e.should have(2).vertices
54 end
55
56 it "adds a self-loop" do
57 e.connections.should be_empty
58 v1 = Vertex.new
59 e << v1 << v1
60 e.vertices.should include(v1)
61 e.should have(2).vertices
62 e.vertices.uniq.length.should == 1
63 end
64 end # #<<
65
66 describe "connection_at" do
67 it "returns the connection that links to a vertex" do
68 e.connections.should be_empty
69 v1 = Vertex.new :name => :v1
70 v2 = Vertex.new :name => :v2
71 e << v1 << v2
72
73 e.connection_at(v1).end.should be v1
74 e.connection_at(v2).end.should be v2
75 end
76
77 it "returns nil if there is no connection to that vertex" do
78 e.connections.should be_empty
79 v1 = Vertex.new :name => :v1
80 v2 = Vertex.new :name => :v2
81 v3 = Vertex.new :name => :v3
82 e << v1 << v2
83
84 e.connection_at(v3).should be nil
85 end
86
87 it "returns the vertex for a self-loop" do
88 e.connections.should be_empty
89 v1 = Vertex.new :name => :v1
90 e << v1 << v1
91
92 e.connection_at(v1).end.should be v1
93 end
94 end # #connection_at
95
96 describe "other_end" do
97 it "returns the vertex at the other end of the given one" do
98 e.connections.should be_empty
99 v1 = Vertex.new :name => :v1
100 v2 = Vertex.new :name => :v2
101 e << v1 << v2
102
103 e.other_end(v1).should be v2
104 e.other_end(v2).should be v1
105 end
106
107 it "returns the same vertex in a self-loop" do
108 e.connections.should be_empty
109 v1 = Vertex.new :name => :v1
110 e << v1 << v1
111
112 e.other_end(v1).should be v1
113 end
114
115 it "returns one of the connected edges if given a vertex not connected to it" do
116 e.connections.should be_empty
117 v1 = Vertex.new :name => :v1
118 v2 = Vertex.new :name => :v2
119 v3 = Vertex.new :name => :v3
120 e << v1 << v2
121
122 [v1, v2].should include e.other_end(v3)
123 end
124
125 it "returns nil if it can't return something sensible" do
126 v1 = Vertex.new :name => :v1
127 e.other_end(v1).should be_nil
128 e << v1
129 e.other_end(v1).should be_nil
130 end
131 end # other_end
132 end # Edge
133
134 describe Connection do
135 let (:c) {Connection.new }
136
137 describe "adds attribues" do
138 it "adds then reports arbitrary attributes" do
139 c.score = 15
140 c.score.should == 15
141 end
142 end # adds attributes
143 end # Connection
144
145 end