Version bump to 0.4.0
[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 let(:v1) {Vertex.new :name => :v1}
7 let(:v2) {Vertex.new :name => :v2}
8 let(:v3) {Vertex.new :name => :v3}
9
10 describe '#initialize' do
11 it 'creates an empty edge' do
12 e = Edge.new
13 e.connections.should be_empty
14 end
15
16 it 'creates an edge with some parameters' do
17 e = Edge.new :value1 => 1, :value2 => 'value2', :value3 => :v3
18 e.value1.should == 1
19 e.value2.should == 'value2'
20 e.value3.should == :v3
21 e.value4.should be_nil
22 end
23 end # #initialize
24
25 describe 'adds attribues' do
26 it 'adds then reports arbitrary attributes' do
27 e.score = 15
28 e.score.should == 15
29 end
30 end # adds attributes
31
32 describe "#to_s" do
33 it "returns the string form of an edge" do
34 v1 = Vertex.new :name => :v1
35 v2 = Vertex.new :name => :v2
36 e.type = :test
37 e << v1 << v2
38 e.to_s.should == '<E: test [<V: v1>, <V: v2>] >'
39 end
40 end
41
42 describe "#<<" do
43 it "adds a new vertex to an edge (with a connection)" do
44 e.connections.should be_empty
45 e << v1
46 e.should have(1).connections
47 e.should have(1).vertices
48 e.vertices.should include(v1)
49 v1.edges.should include(e)
50 e << v2
51 e.should have(2).connections
52 e.should have(2).vertices
53 e.vertices.should include(v1)
54 e.vertices.should include(v2)
55 v2.edges.should include(e)
56 end
57
58 it 'adds several vertices to an edge' do
59 e.connections.should be_empty
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 e << v1 << v1
69 e.vertices.should include(v1)
70 e.should have(2).vertices
71 e.vertices.uniq.length.should == 1
72 end
73 end # #<<
74
75 describe 'connection_at' do
76 it 'returns the connection that links to a vertex' do
77 e.connections.should be_empty
78 e << v1 << v2
79
80 e.connection_at(v1).end.should be v1
81 e.connection_at(v2).end.should be v2
82 end
83
84 it 'returns nil if there is no connection to that vertex' do
85 e.connections.should be_empty
86 e << v1 << v2
87
88 e.connection_at(v3).should be nil
89 end
90
91 it 'returns the vertex for a self-loop' do
92 e.connections.should be_empty
93 e << v1 << v1
94
95 e.connection_at(v1).end.should be v1
96 end
97 end # #connection_at
98
99 describe 'other_end' do
100 it 'returns the vertex at the other end of the given one' do
101 e.connections.should be_empty
102 e << v1 << v2
103
104 e.other_end(v1).should be v2
105 e.other_end(v2).should be v1
106 end
107
108 it 'returns the same vertex in a self-loop' do
109 e.connections.should be_empty
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 e << v1 << v2
118
119 [v1, v2].should include e.other_end(v3)
120 end
121
122 it 'returns nil if it cannot return something sensible' do
123 e.other_end(v1).should be_nil
124 e << v1
125 e.other_end(v1).should be_nil
126 end
127 end # other_end
128
129 describe '#to_dot' do
130 it 'describes an edge in dot notation' do
131 e << v1 << v2
132 e.to_dot.should == "#{v1.object_id.to_s} -- #{v2.object_id.to_s};"
133 end
134
135 it 'describes an edge in dot notation, using given attributes' do
136 e << v1 << v2
137 e.name = 'Edge name'
138 edot = e.to_dot :label => :name
139 edot.should == "#{v1.object_id.to_s} -- #{v2.object_id.to_s} {label = \"#{e.name}\"};"
140 end
141
142 it 'describes an edge in dot notation, given a block' do
143 e << v1 << v2
144 e.to_dot {|e| e.object_id.to_s}.should == e.object_id.to_s
145 end
146 end # dot
147 end # Edge
148
149 describe Connection do
150 let (:c) {Connection.new }
151
152 describe 'adds attribues' do
153 it 'adds then reports arbitrary attributes' do
154 c.score = 15
155 c.score.should == 15
156 end
157 end # adds attributes
158 end # Connection
159
160 end