1 require File
.expand_path(File
.dirname(__FILE__
) + '/../spec_helper')
5 let (:v) { Vertex
.new
}
7 describe
'#initialize' do
8 it
'creates an empty vertex' do
10 v
.edges
.should be_empty
13 it
'creates a vertex with some parameters' do
14 v
= Vertex
.new
:value1 => 1, :value2 => 'value2', :value3 => :v3
16 v
.value2
.should
== 'value2'
17 v
.value3
.should
== :v3
18 v
.value4
.should be_nil
22 describe
'adds attribues' do
23 it
'adds then reports arbitrary attributes' do
30 it
'adds a single edge between vertices' do
31 v
.neighbours
.should be_empty
32 v
.edges
.should be_empty
37 v1
.id
= :v1 # Need this to ensure that v != v1
39 v
.should
have(1).edges
40 v1
.should
have(1).edges
42 v1
.edges
.should
include(e
)
44 v
.should
have(1).neighbours
45 v
.neighbours
.should
include(v1
)
46 v
.neighbours
.should_not
include(v
)
47 v1
.should
have(1).neighbours
48 v1
.neighbours
.should
include(v
)
49 v1
.neighbours
.should_not
include(v1
)
52 it
'adds a single edge as a self-loop' do
53 v
.neighbours
.should be_empty
54 v
.edges
.should be_empty
58 v
.should
have(1).edges
59 v
.should
have(1).neighbours
60 v
.neighbours
.should
include(v
)
65 it
'connects two vertices' do
67 v1
.id
= :v1 # Need this to ensure that v != v1
71 v
.should
have(1).neighbours
72 v
.neighbours
.should
include(v1
)
73 v
.neighbours
.should_not
include(v
)
75 v1
.should
have(1).neighbours
76 v1
.neighbours
.should
include(v
)
77 v1
.neighbours
.should_not
include(v1
)
79 v
.should
have(1).edges
80 v
.edges
.should
include(e
)
81 v1
.should
have(1).edges
82 v1
.edges
.should
include(e
)
84 e
.should
have(2).vertices
85 e
.vertices
.should
include(v
)
86 e
.vertices
.should
include(v1
)
88 e
.should
have(2).connections
91 it
'connects two vertices by an edge with attributes' do
94 e
= v
.connect(v1
, {:type => :edge_type})
95 e
.type
.should
== :edge_type
98 it
'creates a self-connection' do
101 v
.should
have(1).neighbours
102 v
.neighbours
.should
include(v
)
104 v
.should
have(1).edges
105 v
.edges
.should
include(e
)
107 e
.should
have(2).vertices
108 e
.vertices
.uniq
.length
.should
== 1
109 e
.vertices
.should
include(v
)
111 e
.should
have(2).connections
114 it
'creates a self-connection with an edge with attributes' do
115 e
= v
.connect(v
, {:type => :edge_type})
116 e
.type
.should
== :edge_type
121 describe
'#neighbours' do
122 it
'finds neighbours of a self loop' do
124 v
.should
have(1).neighbours
125 v
.neighbours
.should
include v
128 it
'finds neighbours on an edge' do
131 v
.should
have(1).neighbours
132 v
.neighbours
.should
include v1
133 v1
.should
have(1).neighbours
134 v1
.neighbours
.should
include v
137 it
'finds neighbours with multiple edges' do
142 v
.should
have(2).neighbours
143 v
.neighbours
.should
include v1
144 v
.neighbours
.should_not
include v
145 v1
.should
have(2).neighbours
146 v1
.neighbours
.should
include v
147 v1
.neighbours
.should_not
include v1
150 it
'finds neighbours with multiple self loops' do
152 v
.should
have(3).neighbours
153 v
.neighbours
.should
include v
154 v
.neighbours
.uniq
.length
.should
== 1
157 it
'finds neighbours with all sorts of edges' do
158 v1
= Vertex
.new
; v1
.id
= :v1
159 v2
= Vertex
.new
; v2
.id
= :v2
160 v3
= Vertex
.new
; v3
.id
= :v3
166 v
.should
have(2).neighbours
167 v
.neighbours
.should
include v1
168 v
.neighbours
.should
include v2
170 v1
.should
have(1).neighbours
171 v1
.neighbours
.should
include v
173 v2
.should
have(3).neighbours
174 v2
.neighbours
.should
include v
175 v2
.neighbours
.should
include v2
176 v2
.neighbours
.should
include v3
178 v3
.should
have(1).neighbours
179 v3
.neighbours
.should
include v2
182 it
'finds neighbours in graphs with several vertices' do
183 v1
= Vertex
.new
; v1
.id
= :v1
184 v2
= Vertex
.new
; v2
.id
= :v2
185 v3
= Vertex
.new
; v3
.id
= :v3
186 v4
= Vertex
.new
; v4
.id
= :v4
192 v
.should
have(2).neighbours
193 v
.neighbours
.should
include v1
194 v
.neighbours
.should
include v3
195 v
.neighbours
.should_not
include v
196 v1
.should
have(2).neighbours
197 v1
.neighbours
.should
include v
198 v1
.neighbours
.should
include v2
199 v1
.neighbours
.should_not
include v1
200 v2
.should
have(2).neighbours
201 v2
.neighbours
.should
include v1
202 v2
.neighbours
.should
include v3
203 v2
.neighbours
.should_not
include v2
204 v3
.should
have(3).neighbours
205 v3
.neighbours
.should
include v
206 v3
.neighbours
.should
include v2
207 v3
.neighbours
.should
include v4
208 v3
.neighbours
.should_not
include v3
209 v4
.should
have(1).neighbours
210 v4
.neighbours
.should
include v3
211 v4
.neighbours
.should_not
include v4
214 it
'finds neighbours with multiple edges between vertices' do
215 v1
= Vertex
.new
; v1
.id
= :v1
216 v2
= Vertex
.new
; v2
.id
= :v2
217 v3
= Vertex
.new
; v3
.id
= :v3
225 v
.should
have(3).neighbours
226 v
.neighbours
.should
include v1
227 v
.neighbours
.should
include v2
228 v
.neighbours
.should
include v
230 v1
.should
have(2).neighbours
231 v1
.neighbours
.should
include v
232 v1
.neighbours
.should
include v1
234 v2
.should
have(4).neighbours
235 v2
.neighbours
.should
include v
236 v2
.neighbours
.should
include v2
237 v2
.neighbours
.should
include v3
238 v2
.neighbours
.uniq
.length
.should
== 3
240 v3
.should
have(2).neighbours
241 v3
.neighbours
.should
include v2
242 v3
.neighbours
.uniq
.length
.should
== 1
246 describe
'#to_dot' do
247 it
'describes a vertex in dot notation' do
249 v
.to_dot
.should
== "#{v.object_id.to_s};"
252 it
'describes a vertex in dot notation, using given attributes' do
256 vdot
= v
.to_dot
:label => :name, :shape => :shape
257 vdot
.should
== "#{v.object_id.to_s} {label = \"#{v.name}\", shape = \"#{v.shape}\"};"
260 it
'describes a vertex using a block' do
263 vdot
= v
.to_dot
{|v
| v
.field1
+ ';'}
264 vdot
.should
== "#{v.field1};"