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
"returns the string form of a vertex" do
32 v
.to_s
.should
== '<V: v1>'
38 it
'adds a single edge between vertices' do
39 v
.neighbours
.should be_empty
40 v
.edges
.should be_empty
45 v1
.id
= :v1 # Need this to ensure that v != v1
47 v
.should
have(1).edges
48 v1
.should
have(1).edges
50 v1
.edges
.should
include(e
)
52 v
.should
have(1).neighbours
53 v
.neighbours
.should
include(v1
)
54 v
.neighbours
.should_not
include(v
)
55 v1
.should
have(1).neighbours
56 v1
.neighbours
.should
include(v
)
57 v1
.neighbours
.should_not
include(v1
)
60 it
'adds a single edge as a self-loop' do
61 v
.neighbours
.should be_empty
62 v
.edges
.should be_empty
66 v
.should
have(1).edges
67 v
.should
have(1).neighbours
68 v
.neighbours
.should
include(v
)
73 it
'connects two vertices' do
75 v1
.id
= :v1 # Need this to ensure that v != v1
79 v
.should
have(1).neighbours
80 v
.neighbours
.should
include(v1
)
81 v
.neighbours
.should_not
include(v
)
83 v1
.should
have(1).neighbours
84 v1
.neighbours
.should
include(v
)
85 v1
.neighbours
.should_not
include(v1
)
87 v
.should
have(1).edges
88 v
.edges
.should
include(e
)
89 v1
.should
have(1).edges
90 v1
.edges
.should
include(e
)
92 e
.should
have(2).vertices
93 e
.vertices
.should
include(v
)
94 e
.vertices
.should
include(v1
)
96 e
.should
have(2).connections
99 it
'connects two vertices by an edge with attributes' do
102 e
= v
.connect(v1
, {:type => :edge_type})
103 e
.type
.should
== :edge_type
106 it
'creates a self-connection' do
109 v
.should
have(1).neighbours
110 v
.neighbours
.should
include(v
)
112 v
.should
have(1).edges
113 v
.edges
.should
include(e
)
115 e
.should
have(2).vertices
116 e
.vertices
.uniq
.length
.should
== 1
117 e
.vertices
.should
include(v
)
119 e
.should
have(2).connections
122 it
'creates a self-connection with an edge with attributes' do
123 e
= v
.connect(v
, {:type => :edge_type})
124 e
.type
.should
== :edge_type
129 describe
'#neighbours' do
130 it
'finds neighbours of a self loop' do
132 v
.should
have(1).neighbours
133 v
.neighbours
.should
include v
136 it
'finds neighbours on an edge' do
139 v
.should
have(1).neighbours
140 v
.neighbours
.should
include v1
141 v1
.should
have(1).neighbours
142 v1
.neighbours
.should
include v
145 it
'finds neighbours with multiple edges' do
150 v
.should
have(2).neighbours
151 v
.neighbours
.should
include v1
152 v
.neighbours
.should_not
include v
153 v1
.should
have(2).neighbours
154 v1
.neighbours
.should
include v
155 v1
.neighbours
.should_not
include v1
158 it
'finds neighbours with multiple self loops' do
160 v
.should
have(3).neighbours
161 v
.neighbours
.should
include v
162 v
.neighbours
.uniq
.length
.should
== 1
165 it
'finds neighbours with all sorts of edges' do
166 v1
= Vertex
.new
; v1
.id
= :v1
167 v2
= Vertex
.new
; v2
.id
= :v2
168 v3
= Vertex
.new
; v3
.id
= :v3
174 v
.should
have(2).neighbours
175 v
.neighbours
.should
include v1
176 v
.neighbours
.should
include v2
178 v1
.should
have(1).neighbours
179 v1
.neighbours
.should
include v
181 v2
.should
have(3).neighbours
182 v2
.neighbours
.should
include v
183 v2
.neighbours
.should
include v2
184 v2
.neighbours
.should
include v3
186 v3
.should
have(1).neighbours
187 v3
.neighbours
.should
include v2
190 it
'finds neighbours in graphs with several vertices' do
191 v1
= Vertex
.new
; v1
.id
= :v1
192 v2
= Vertex
.new
; v2
.id
= :v2
193 v3
= Vertex
.new
; v3
.id
= :v3
194 v4
= Vertex
.new
; v4
.id
= :v4
200 v
.should
have(2).neighbours
201 v
.neighbours
.should
include v1
202 v
.neighbours
.should
include v3
203 v
.neighbours
.should_not
include v
204 v1
.should
have(2).neighbours
205 v1
.neighbours
.should
include v
206 v1
.neighbours
.should
include v2
207 v1
.neighbours
.should_not
include v1
208 v2
.should
have(2).neighbours
209 v2
.neighbours
.should
include v1
210 v2
.neighbours
.should
include v3
211 v2
.neighbours
.should_not
include v2
212 v3
.should
have(3).neighbours
213 v3
.neighbours
.should
include v
214 v3
.neighbours
.should
include v2
215 v3
.neighbours
.should
include v4
216 v3
.neighbours
.should_not
include v3
217 v4
.should
have(1).neighbours
218 v4
.neighbours
.should
include v3
219 v4
.neighbours
.should_not
include v4
222 it
'finds neighbours with multiple edges between vertices' do
223 v1
= Vertex
.new
; v1
.id
= :v1
224 v2
= Vertex
.new
; v2
.id
= :v2
225 v3
= Vertex
.new
; v3
.id
= :v3
233 v
.should
have(3).neighbours
234 v
.neighbours
.should
include v1
235 v
.neighbours
.should
include v2
236 v
.neighbours
.should
include v
238 v1
.should
have(2).neighbours
239 v1
.neighbours
.should
include v
240 v1
.neighbours
.should
include v1
242 v2
.should
have(4).neighbours
243 v2
.neighbours
.should
include v
244 v2
.neighbours
.should
include v2
245 v2
.neighbours
.should
include v3
246 v2
.neighbours
.uniq
.length
.should
== 3
248 v3
.should
have(2).neighbours
249 v3
.neighbours
.should
include v2
250 v3
.neighbours
.uniq
.length
.should
== 1
254 describe
'#to_dot' do
255 it
'describes a vertex in dot notation' do
257 v
.to_dot
.should
== "#{v.object_id.to_s};"
260 it
'describes a vertex in dot notation, using given attributes' do
264 vdot
= v
.to_dot
:label => :name, :shape => :shape
265 vdot
.should
== "#{v.object_id.to_s} {label = \"#{v.name}\", shape = \"#{v.shape}\"};"
268 it
'describes a vertex using a block' do
271 vdot
= v
.to_dot
{|v
| v
.field1
+ ';'}
272 vdot
.should
== "#{v.field1};"