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