yargi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,360 @@
1
+ require 'test/unit'
2
+ require 'yargi'
3
+
4
+ module Yargi
5
+ class DigraphTest < Test::Unit::TestCase
6
+
7
+ module Until; end
8
+ module If; end
9
+
10
+ # Creates a digraph instance under @digraph
11
+ def setup
12
+ @digraph = Yargi::Digraph.new
13
+ end
14
+
15
+ def test_to_vertices
16
+ untils = @digraph.add_n_vertices(5, Until)
17
+ ifs = @digraph.add_n_vertices(5, If)
18
+ assert_equal [untils[0]], @digraph.send(:to_vertices, untils[0])
19
+ assert_equal [untils[0], untils[1]], @digraph.send(:to_vertices, [untils[0], untils[1]])
20
+ assert_equal [untils[0], untils[1]], @digraph.send(:to_vertices, untils[0], untils[1])
21
+ assert_equal untils, @digraph.send(:to_vertices, Until)
22
+ assert_equal ifs, @digraph.send(:to_vertices, If)
23
+ assert_equal (untils+ifs).sort, @digraph.send(:to_vertices, [Until, If])
24
+ assert_equal (untils+ifs).sort, @digraph.send(:to_vertices, Until, If)
25
+ assert_equal (untils+ifs).sort, @digraph.send(:to_vertices, [Until, If, untils[0]])
26
+ assert_equal (untils+ifs).sort, @digraph.send(:to_vertices, Until, If, untils[0])
27
+ end
28
+
29
+ def test_to_edges
30
+ untils = @digraph.add_n_vertices(5, Until)
31
+ ifs = @digraph.add_n_vertices(5, If)
32
+ untils_to_ifs = @digraph.connect(untils, ifs)
33
+ ifs_to_untils = @digraph.connect(ifs, untils)
34
+ assert_equal [untils_to_ifs[0]], @digraph.send(:to_edges, untils_to_ifs[0])
35
+ assert_equal [untils_to_ifs[0], untils_to_ifs[1]], @digraph.send(:to_edges, untils_to_ifs[0], untils_to_ifs[1])
36
+ assert_equal [untils_to_ifs[0], untils_to_ifs[1]], @digraph.send(:to_edges, [untils_to_ifs[0], untils_to_ifs[1]])
37
+ assert_equal @digraph.edges, @digraph.send(:to_edges, untils_to_ifs, ifs_to_untils)
38
+ assert_equal untils_to_ifs, @digraph.send(:to_edges, Proc.new{|e| Until===e.source})
39
+ assert_equal ifs_to_untils, @digraph.send(:to_edges, Proc.new{|e| If===e.source})
40
+ end
41
+
42
+ def test_extend_returns_self_hypothese
43
+ assert_equal self, self.extend(Until)
44
+ end
45
+
46
+ def test_vertices
47
+ v1 = @digraph.add_vertex({:kind => :point})
48
+ v2 = @digraph.add_vertex({:kind => :point})
49
+ v3 = @digraph.add_vertex({:kind => :end})
50
+ assert_equal :point, v1.kind
51
+ assert_equal :end, v3.kind
52
+ assert_equal VertexSet[v1, v2, v3], @digraph.vertices
53
+ assert_equal v1, @digraph.vertices[0]
54
+ assert_equal VertexSet[v1], @digraph.vertices {|v| v.index==0}
55
+ assert_equal VertexSet[v1, v3], @digraph.vertices {|v| v.index==0 or v.index==2}
56
+ assert_equal VertexSet[v1, v2], @digraph.vertices {|v| v[:kind]==:point}
57
+ assert_nothing_raised { @digraph.send(:check_sanity) }
58
+ end
59
+
60
+ def test_vertices_with_mods
61
+ untils = @digraph.add_n_vertices(5, Until)
62
+ ifs = @digraph.add_n_vertices(5, If)
63
+ assert_equal untils, @digraph.vertices(Until)
64
+ assert_equal ifs, @digraph.vertices(If)
65
+ assert_equal (untils+ifs), @digraph.vertices(Yargi::NONE|Until|If)
66
+ end
67
+
68
+ def test_vertices_with_both
69
+ untils = @digraph.add_n_vertices(5, Until)
70
+ ifs = @digraph.add_n_vertices(5, If)
71
+ assert_equal(VertexSet[@digraph.vertices[0]], @digraph.vertices(Until) do |v|
72
+ v.index==0
73
+ end)
74
+ assert_equal(VertexSet[], @digraph.vertices(If) do |v|
75
+ v.index==0
76
+ end)
77
+ end
78
+
79
+ def test_module_extended_vertices
80
+ v1 = @digraph.add_vertex(Until)
81
+ v2 = @digraph.add_vertex(If)
82
+ assert Until===v1
83
+ assert If===v2
84
+ assert_equal VertexSet[v1], @digraph.vertices {|v| Until===v}
85
+ assert_equal VertexSet[v2], @digraph.vertices {|v| If===v}
86
+ end
87
+
88
+ def test_tag
89
+ v1, v2 = @digraph.add_n_vertices(2)
90
+ v1.tag(Until)
91
+ v2.tag(If, Until)
92
+ assert Until===v1
93
+ assert If===v2 and Until===v2
94
+ end
95
+
96
+ def test_edges
97
+ v1, v2, v3 = @digraph.add_n_vertices(3)
98
+ e12, e23, e32, e21 = @digraph.connect_all([v1, v2], [v2, v3], [v3, v2], [v2, v1])
99
+ assert_equal EdgeSet[e12, e23], @digraph.edges {|e| e.index<=1}
100
+ assert_nothing_raised { @digraph.send(:check_sanity) }
101
+ end
102
+
103
+ def test_each_vertex
104
+ v1, v2, v3 = @digraph.add_n_vertices(3)
105
+ seen = []
106
+ @digraph.each_vertex {|v| seen << v}
107
+ assert_equal [v1, v2, v3], seen
108
+ end
109
+
110
+ def test_each_vertex_with_filter
111
+ v1, v2, v3, v4, v5 = @digraph.add_n_vertices(5)
112
+ seen = []
113
+ filter = Yargi.predicate {|elm| elm.index<3}
114
+ @digraph.each_vertex(filter) {|v| seen << v}
115
+ assert_equal [v1, v2, v3], seen
116
+ seen = []
117
+ filter = Yargi.predicate {|elm| elm.index>=3}
118
+ @digraph.each_vertex(filter) {|v| seen << v}
119
+ assert_equal [v4, v5], seen
120
+ end
121
+
122
+ def test_each_edge
123
+ v1, v2, v3 = @digraph.add_n_vertices(3)
124
+ edges = @digraph.connect_all([v1, v2], [v2, v3], [v3, v2], [v2, v1])
125
+ seen = []
126
+ @digraph.each_edge {|e| seen << e}
127
+ assert_equal edges, seen
128
+ end
129
+
130
+ def test_add_vertex
131
+ v1 = @digraph.add_vertex({:style => :begin})
132
+ assert_not_nil v1
133
+ assert_equal @digraph, v1.graph
134
+ assert_equal :begin, v1[:style]
135
+ assert_equal VertexSet[v1], @digraph.vertices
136
+ assert_equal 0, v1.index
137
+
138
+ v2 = @digraph.add_vertex({:style => :end})
139
+ assert_not_nil v2
140
+ assert_equal :end, v2[:style]
141
+ assert_equal VertexSet[v1, v2], @digraph.vertices
142
+ assert_equal 0, v1.index
143
+ assert_equal 1, v2.index
144
+ assert_nothing_raised { @digraph.send(:check_sanity) }
145
+ end
146
+
147
+ def test_add_n_vertices
148
+ v1, v2, v3 = @digraph.add_n_vertices(3, {:hello => "world"})
149
+ assert_equal VertexSet[v1, v2, v3], @digraph.vertices
150
+ assert_equal [0, 1, 2], [v1.index, v2.index, v3.index]
151
+ v1[:hello] = "world1"
152
+ v2[:hello] = "world2"
153
+ v3[:hello] = "world3"
154
+ assert_equal ["world1", "world2", "world3"], @digraph.vertices.collect{|v| v[:hello]}
155
+ assert_nothing_raised { @digraph.send(:check_sanity) }
156
+ end
157
+
158
+ def test_add_n_vertices_with_block
159
+ vertices = @digraph.add_n_vertices(5, Until) do |v,i|
160
+ v.set_mark(:mark, i)
161
+ end
162
+ vertices.each_with_index do |v,i|
163
+ assert_equal i, v.mark
164
+ assert Until===v
165
+ end
166
+ assert_equal [0, 1, 2, 3, 4], vertices.get_mark(:mark)
167
+ end
168
+
169
+ def test_connect
170
+ v1, v2 = @digraph.add_n_vertices(2)
171
+ edge = @digraph.connect(v1, v2, {:label => "hello"})
172
+ assert_equal @digraph, edge.graph
173
+ assert_not_nil edge
174
+ assert_equal 0, edge.index
175
+ assert_equal "hello", edge[:label]
176
+ assert_equal v1, edge.source
177
+ assert_equal v2, edge.target
178
+ assert_equal EdgeSet[edge], @digraph.edges
179
+
180
+ assert_equal EdgeSet[], v1.in_edges
181
+ assert_equal EdgeSet[edge], v1.out_edges
182
+ assert_equal EdgeSet[], v2.out_edges
183
+ assert_equal EdgeSet[edge], v2.in_edges
184
+ assert_nothing_raised { @digraph.send(:check_sanity) }
185
+ end
186
+
187
+ def test_connect_all
188
+ v1, v2 = @digraph.add_n_vertices(2)
189
+ e1, e2 = @digraph.connect_all([v1, v2], [v2, v1])
190
+ assert_equal VertexSet[v1, v2], @digraph.vertices
191
+ assert_equal EdgeSet[e1, e2], @digraph.edges
192
+ assert_equal VertexSet[v1, v2], e1.extremities
193
+ assert_equal VertexSet[v2, v1], e2.extremities
194
+ assert_equal EdgeSet[e1], v1.out_edges
195
+ assert_equal EdgeSet[e2], v1.in_edges
196
+ assert_equal EdgeSet[e2], v2.out_edges
197
+ assert_equal EdgeSet[e1], v1.out_edges
198
+ assert_nothing_raised { @digraph.send(:check_sanity) }
199
+ end
200
+
201
+ def test_connect_all_with_marks
202
+ v1, v2 = @digraph.add_n_vertices(2)
203
+ e1, e2 = @digraph.connect_all([v1, v2, {:hello => "world"}], [v2, v1, {:hello => "world"}])
204
+ assert_equal VertexSet[v1, v2], @digraph.vertices
205
+ assert_equal EdgeSet[e1, e2], @digraph.edges
206
+ assert_equal VertexSet[v1, v2], e1.extremities
207
+ assert_equal VertexSet[v2, v1], e2.extremities
208
+ assert_equal EdgeSet[e1], v1.out_edges
209
+ assert_equal EdgeSet[e2], v1.in_edges
210
+ assert_equal EdgeSet[e2], v2.out_edges
211
+ assert_equal EdgeSet[e1], v1.out_edges
212
+ assert_equal "world", e1[:hello]
213
+ assert_equal "world", e2[:hello]
214
+ e1[:hello] = "world1"
215
+ assert_equal "world", e2[:hello]
216
+ assert_nothing_raised { @digraph.send(:check_sanity) }
217
+ end
218
+
219
+ def test_remove_edge
220
+ v1, v2 = @digraph.add_n_vertices(2)
221
+ edge = @digraph.connect(v1, v2)
222
+ @digraph.remove_edge(edge)
223
+ assert_equal VertexSet[v1, v2], @digraph.vertices
224
+ assert_equal EdgeSet[], @digraph.edges
225
+ assert_equal EdgeSet[], v1.in_edges
226
+ assert_equal EdgeSet[], v1.out_edges
227
+ assert_equal EdgeSet[], v2.in_edges
228
+ assert_equal EdgeSet[], v2.out_edges
229
+
230
+ e1 = @digraph.connect(v1, v2)
231
+ e2 = @digraph.connect(v2, v1)
232
+ assert_equal VertexSet[v1, v2], @digraph.vertices
233
+ assert_equal EdgeSet[e1, e2], @digraph.edges
234
+ assert_equal EdgeSet[e2], v1.in_edges
235
+ assert_equal EdgeSet[e1], v1.out_edges
236
+ assert_equal EdgeSet[e1], v2.in_edges
237
+ assert_equal EdgeSet[e2], v2.out_edges
238
+ @digraph.remove_edge(e1)
239
+ assert_equal VertexSet[v1, v2], @digraph.vertices
240
+ assert_equal EdgeSet[e2], @digraph.edges
241
+ assert_equal EdgeSet[e2], v1.in_edges
242
+ assert_equal EdgeSet[], v1.out_edges
243
+ assert_equal EdgeSet[], v2.in_edges
244
+ assert_equal EdgeSet[e2], v2.out_edges
245
+ assert_equal 0, e2.index
246
+ assert_nothing_raised { @digraph.send(:check_sanity) }
247
+ end
248
+
249
+ def test_remove_edges
250
+ v1, v2, v3 = @digraph.add_n_vertices(3)
251
+ e12 = @digraph.connect(v1, v2)
252
+ e23 = @digraph.connect(v2, v3)
253
+ e32 = @digraph.connect(v3, v2)
254
+ e21 = @digraph.connect(v2, v1)
255
+ @digraph.remove_edges(e12, e23, e32, e21)
256
+ assert_equal EdgeSet[], @digraph.edges
257
+ [v1, v2, v3].each do |v|
258
+ assert_equal EdgeSet[], v.in_edges
259
+ assert_equal EdgeSet[], v.out_edges
260
+ end
261
+ assert_equal [-1], [e12, e23, e32, e21].collect {|e| e.index}.uniq
262
+
263
+ e12 = @digraph.connect(v1, v2)
264
+ e23 = @digraph.connect(v2, v3)
265
+ e32 = @digraph.connect(v3, v2)
266
+ e21 = @digraph.connect(v2, v1)
267
+ @digraph.remove_edges(e12, e32)
268
+ assert_equal [-1], [e12, e32].collect {|e| e.index}.uniq
269
+ assert_equal EdgeSet[e23, e21], @digraph.edges
270
+ assert_equal 0, e23.index
271
+ assert_equal 1, e21.index
272
+ assert_equal EdgeSet[e23, e21], v2.out_edges
273
+ assert_equal EdgeSet[e23], v3.in_edges
274
+ assert_equal EdgeSet[e21], v1.in_edges
275
+ assert_nothing_raised { @digraph.send(:check_sanity) }
276
+ end
277
+
278
+ def test_remove_vertex
279
+ v1, v2, v3 = @digraph.add_n_vertices(3)
280
+ e12 = @digraph.connect(v1, v2)
281
+ e23 = @digraph.connect(v2, v3)
282
+ e32 = @digraph.connect(v3, v2)
283
+ e21 = @digraph.connect(v2, v1)
284
+ @digraph.remove_vertex(v1)
285
+ assert_equal -1, v1.index
286
+ assert_equal VertexSet[v2, v3], @digraph.vertices
287
+ assert_equal [0, 1], @digraph.vertices.collect{|v| v.index}
288
+ assert_equal EdgeSet[e23, e32], @digraph.edges
289
+ assert_equal EdgeSet[e23], v2.out_edges
290
+ assert_equal EdgeSet[e32], v2.in_edges
291
+ assert_nothing_raised { @digraph.send(:check_sanity) }
292
+ end
293
+
294
+ def test_reconnect
295
+ v1, v2 = @digraph.add_n_vertices(2)
296
+ edge = @digraph.connect(v1, v2)
297
+ @digraph.reconnect(edge, v2, v1)
298
+ assert_equal VertexSet[v1, v2], @digraph.vertices
299
+ assert_equal EdgeSet[edge], @digraph.edges
300
+ assert_equal v2, edge.source
301
+ assert_equal v1, edge.target
302
+ assert_equal EdgeSet[edge], v1.in_edges
303
+ assert_equal EdgeSet[edge], v2.out_edges
304
+ assert_nothing_raised { @digraph.send(:check_sanity) }
305
+ end
306
+
307
+ def test_connect_returns_flatten_set
308
+ sources = @digraph.add_n_vertices(5)
309
+ edges = @digraph.connect(sources, sources)
310
+ assert_equal 25, edges.length
311
+ end
312
+
313
+ # def test_remove_vertex_with_block
314
+ # v1, v2, v3 = @digraph.add_n_vertices(3)
315
+ # e12 = @digraph.connect(v1, v2)
316
+ # e23 = @digraph.connect(v2, v3)
317
+ # e32 = @digraph.connect(v3, v2)
318
+ # e21 = @digraph.connect(v2, v1)
319
+ # @digraph.remove_vertex(v3) do |g,ine,oute|
320
+ # g.reconnect(ine, nil, v1)
321
+ # g.reconnect(oute, v1, nil)
322
+ # assert (v3.in_edges+v3.out_edges).empty?
323
+ # end
324
+ # assert_equal [e12, e23, e32, e21], @digraph.edges
325
+ # assert_equal [v1, v2], e12.extremities
326
+ # assert_equal [v2, v1], e23.extremities
327
+ # assert_equal [v1, v2], e32.extremities
328
+ # assert_equal [v2, v1], e21.extremities
329
+ # assert_equal [e21, e23], v1.in_edges
330
+ # assert_equal [e12, e32], v1.out_edges
331
+ # assert_equal [e12, e32], v2.in_edges
332
+ # assert_equal [e23, e21], v2.out_edges
333
+ # assert_nothing_raised { @digraph.send(:check_sanity) }
334
+ # end
335
+ #
336
+ # def test_remove_vertex_with_block_using_edge_shortcuts
337
+ # v1, v2, v3 = @digraph.add_n_vertices(3)
338
+ # e12 = @digraph.connect(v1, v2)
339
+ # e23 = @digraph.connect(v2, v3)
340
+ # e32 = @digraph.connect(v3, v2)
341
+ # e21 = @digraph.connect(v2, v1)
342
+ # @digraph.remove_vertex(v3) do |g,ine,oute|
343
+ # ine.each do |e| e.target = v1 end
344
+ # oute.each do |e| e.source = v1 end
345
+ # assert (v3.in_edges+v3.out_edges).empty?
346
+ # end
347
+ # assert_equal [e12, e23, e32, e21], @digraph.edges
348
+ # assert_equal [v1, v2], e12.extremities
349
+ # assert_equal [v2, v1], e23.extremities
350
+ # assert_equal [v1, v2], e32.extremities
351
+ # assert_equal [v2, v1], e21.extremities
352
+ # assert_equal [e21, e23], v1.in_edges
353
+ # assert_equal [e12, e32], v1.out_edges
354
+ # assert_equal [e12, e32], v2.in_edges
355
+ # assert_equal [e23, e21], v2.out_edges
356
+ # assert_nothing_raised { @digraph.send(:check_sanity) }
357
+ # end
358
+
359
+ end
360
+ end
@@ -0,0 +1,61 @@
1
+ require 'test/unit'
2
+ require 'yargi'
3
+
4
+ module Yargi
5
+ class Digraph
6
+
7
+ class VertexTest < Test::Unit::TestCase
8
+
9
+ module Center; end
10
+ module AtOne; end
11
+ module AtTwo; end
12
+
13
+ # Builds the star graph under @star
14
+ def setup
15
+ @star = Yargi::Digraph.new
16
+ @center = @star.add_vertex(Center)
17
+ @atone = @star.add_n_vertices(10, AtOne)
18
+ @attwo = @star.add_n_vertices(10, AtTwo)
19
+ @center_to_one = @star.connect(@center, @atone)
20
+ @one_to_two = @atone.zip(@attwo).collect do |pair|
21
+ @star.connect(pair[0], pair[1])
22
+ end
23
+ @one_to_two = VertexSet.new(@one_to_two)
24
+ end
25
+
26
+ def test_in_and_out_edges
27
+ assert_equal EdgeSet[], @center.in_edges
28
+ assert_equal @center_to_one, @center.out_edges
29
+ assert_equal @center_to_one, @atone.collect{|v| v.in_edges}.flatten
30
+ end
31
+
32
+ def test_in_adjacent
33
+ @atone.each {|v| assert_equal VertexSet[@center], v.in_adjacent}
34
+ @atone.each {|v| assert_equal VertexSet[@center], v.in_adjacent(Center)}
35
+ @atone.each {|v| assert_equal VertexSet[], v.in_adjacent(AtOne)}
36
+ assert_equal VertexSet[@center], @atone.in_adjacent
37
+ assert @atone.in_adjacent(AtOne).empty?
38
+ assert_equal VertexSet[@center], @atone.in_adjacent(Center)
39
+ end
40
+
41
+ def test_out_adjacent
42
+ @atone.each_with_index {|v,i| assert_equal @attwo[i,1], v.out_adjacent}
43
+ @atone.each_with_index {|v,i| assert_equal @attwo[i,1], v.out_adjacent(AtTwo)}
44
+ @atone.each_with_index {|v,i| assert_equal VertexSet[], v.in_adjacent(AtOne)}
45
+ assert_equal @attwo, @atone.out_adjacent
46
+ assert @atone.out_adjacent(AtOne).empty?
47
+ assert_equal @attwo, @atone.out_adjacent(AtTwo)
48
+ end
49
+
50
+ def test_adjacent
51
+ assert_equal @atone, @center.adjacent
52
+ assert_equal (@attwo+[@center]).sort, @atone.adjacent.sort
53
+ assert_equal @atone.sort, @attwo.adjacent.sort
54
+ assert_equal @star.vertices, (@atone+[@center]).adjacent.sort
55
+ assert_equal @atone, (@atone+[@center]).adjacent(AtOne).sort
56
+ end
57
+
58
+ end # class VertexTest
59
+
60
+ end
61
+ end
@@ -0,0 +1,44 @@
1
+ require 'test/unit'
2
+ require 'yargi'
3
+
4
+ module Yargi
5
+
6
+ # Here to check that what's in the documentation is correct
7
+ class DocumentationTest < Test::Unit::TestCase
8
+
9
+ module Source; end
10
+ module Sink; end
11
+
12
+ def test_README_example
13
+ # create a directed graph
14
+ digraph = Yargi::Digraph.new
15
+
16
+ # create 10 source and 5 sink vertices, tag them with user modules
17
+ sources = digraph.add_n_vertices(5, Source)
18
+ assert VertexSet===sources
19
+
20
+ # connect source to sink states
21
+ edges = digraph.connect(sources, sources)
22
+ assert EdgeSet===edges
23
+
24
+ # put some dot attributes
25
+ sources.add_marks(:shape => 'circle', :label => '')
26
+ edges.add_marks do |e|
27
+ {:label => "From #{e.source.index} to #{e.target.index}"}
28
+ end
29
+
30
+ # and print it
31
+ dir = File.expand_path(File.dirname(__FILE__))
32
+ dotfile = File.join(dir,"README-example.dot")
33
+ gitfile = File.join(dir,"README-example.gif")
34
+ File.open(dotfile, 'w') {|f| f << digraph.to_dot}
35
+ begin
36
+ `dot -Tgif -o #{gitfile} #{dotfile}`
37
+ rescue => ex
38
+ $STDERR << "dot test failed, probably not installed\n#{ex.message}"
39
+ end
40
+ end
41
+
42
+ end # class DocumentationTest
43
+
44
+ end
@@ -0,0 +1,17 @@
1
+ require 'test/unit'
2
+ require 'yargi'
3
+
4
+ module Yargi
5
+ class ElementSetTest < Test::Unit::TestCase
6
+
7
+ def test_flatten
8
+ e = ElementSet[ElementSet[1, 2, 3], ElementSet[4, 5, 6]]
9
+ assert_equal ElementSet[1, 2, 3, 4, 5, 6], e.flatten
10
+
11
+ e = EdgeSet[EdgeSet[1, 2, 3], EdgeSet[4, 5, 6], 7]
12
+ assert EdgeSet===e
13
+ assert_equal EdgeSet[1, 2, 3, 4, 5, 6, 7], e.flatten
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ require 'test/unit'
2
+
3
+ module Yargi
4
+
5
+ # Checks some hypotheses that we make about Ruby
6
+ class HypothesesTest < Test::Unit::TestCase
7
+
8
+ def test_method_missing_handles_block_as_expected
9
+ p = Object.new
10
+ def p.say_hello
11
+ who = block_given? ? yield : "anonymous"
12
+ "Hello #{who}"
13
+ end
14
+ o = Object.new
15
+ def o.set_obj(obj)
16
+ @obj = obj
17
+ end
18
+ def o.method_missing(name, *args, &block)
19
+ @obj.send(name, *args, &block)
20
+ end
21
+ assert_equal "Hello anonymous", p.say_hello
22
+ assert_equal "Hello blambeau", p.say_hello {"blambeau"}
23
+ o.set_obj(p)
24
+ assert_equal "Hello anonymous", o.say_hello
25
+ assert_equal "Hello blambeau", o.say_hello {"blambeau"}
26
+ end
27
+
28
+ end # class HypothesesTest
29
+
30
+ end
@@ -0,0 +1,56 @@
1
+ require 'test/unit'
2
+ require 'yargi'
3
+
4
+ module Yargi
5
+ class MarkableTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @object = Object.new
9
+ @object.extend Yargi::Markable
10
+ end
11
+
12
+ def test_mark_set_and_get
13
+ @object.set_mark("hello", "world")
14
+ assert_equal "world", @object.get_mark("hello")
15
+ @object.set_mark("hello", "world1")
16
+ assert_equal "world1", @object.get_mark("hello")
17
+ end
18
+
19
+ def test_mark_set_and_get_though_hash_api
20
+ @object["hello"] = "world"
21
+ assert_equal "world", @object["hello"]
22
+ @object["hello"] = "world1"
23
+ assert_equal "world1", @object["hello"]
24
+ end
25
+
26
+ def test_friendly_methods
27
+ @object.set_mark(:first, 1)
28
+ assert_equal 1, @object.first
29
+ @object[:first] = 1
30
+ assert_equal 1, @object.first
31
+ @object.first = 2
32
+ assert_equal 2, @object.first
33
+ end
34
+
35
+ def test_it_is_not_intrusive
36
+ @object[:merge_marks] = 1
37
+ assert_nothing_raised do
38
+ @object.merge_marks "hello" => "marks"
39
+ end
40
+ assert_equal 1, @object[:merge_marks]
41
+ assert_equal "marks", @object["hello"]
42
+ end
43
+
44
+ def test_to_h
45
+ @object.set_mark(:me, "blambeau")
46
+ @object.add_marks(:hello => "world", :who => "yarvi", :nil => nil)
47
+ expected = {:me => "blambeau", :hello => "world", :who => "yarvi", :nil => nil}
48
+ assert_equal expected, @object.to_h(false)
49
+ expected = {:me => "blambeau", :hello => "world", :who => "yarvi"}
50
+ assert_equal expected, @object.to_h(true)
51
+ expected = {:me => "blambeau", :hello => "world", :who => "yarvi"}
52
+ assert_equal expected, @object.to_h()
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,90 @@
1
+ require 'test/unit'
2
+ require 'yargi'
3
+
4
+ module Yargi
5
+
6
+ # Tests all predidate classes
7
+ class PredicateTest < Test::Unit::TestCase
8
+
9
+ def test_to_predicate
10
+ assert TruePredicate===Predicate.to_predicate(nil)
11
+ assert TruePredicate===Predicate.to_predicate(true)
12
+ assert FalsePredicate===Predicate.to_predicate(false)
13
+ assert TagPredicate===Predicate.to_predicate(Yargi)
14
+ assert TagPredicate===Predicate.to_predicate(PredicateTest)
15
+ assert LambdaPredicate===(Predicate.to_predicate {|elm| true})
16
+ proc = Proc.new {|elm| true}
17
+ assert LambdaPredicate===Predicate.to_predicate(proc)
18
+ assert AndPredicate === (Predicate.to_predicate(Yargi) {|elm| true})
19
+ end
20
+
21
+ def test_tag_predicate
22
+ p = TagPredicate.new(Test::Unit::TestCase)
23
+ assert p===self
24
+ assert_equal false, p===p
25
+ end
26
+
27
+ def test_lambda_predicate
28
+ p = LambdaPredicate.new {|elm| Test::Unit::TestCase===elm}
29
+ assert p===self
30
+ assert_equal false, p===p
31
+ end
32
+
33
+ def test_not_predicate
34
+ p = TagPredicate.new(Test::Unit::TestCase)
35
+ p = NotPredicate.new(p)
36
+ assert_equal false, p===self
37
+ assert_equal true, p===p
38
+ end
39
+
40
+ def test_and_predicate
41
+ assert_equal true, Object===self
42
+ p1 = TagPredicate.new(Object)
43
+ p2 = LambdaPredicate.new {|elm| elm.respond_to?(:test_and_predicate)}
44
+ anded = AndPredicate.new(p1, p2)
45
+ assert_equal true, anded===self
46
+ assert_equal false, anded==="hello"
47
+ end
48
+
49
+ def test_or_predicate
50
+ p1 = LambdaPredicate.new {|elm| elm.respond_to?(:upcase)}
51
+ p2 = LambdaPredicate.new {|elm| elm.respond_to?(:test_and_predicate)}
52
+ ored = OrPredicate.new(p1, p2)
53
+ assert_equal true, ored===self
54
+ assert_equal true, ored==="hello"
55
+ assert_equal false, ored===12
56
+ end
57
+
58
+ def test_not_accepts_modules
59
+ p = NotPredicate.new(Test::Unit::TestCase)
60
+ assert_equal false, p===self
61
+ assert_equal true, p==="hello"
62
+ end
63
+
64
+ def test_ruby_shortcuts
65
+ all = Yargi::ALL
66
+ none = Yargi::NONE
67
+ p = all & Test::Unit::TestCase
68
+ assert_equal true, p===self
69
+ assert_equal false, p==="hello"
70
+ p = p.not()
71
+ assert_equal false, p===self
72
+ assert_equal true, p==="hello"
73
+
74
+ respond = LambdaPredicate.new {|elm| elm.respond_to?(:test_and_predicate)}
75
+ assert_equal true, respond===self
76
+ p = all & Test::Unit::TestCase & respond
77
+ assert_equal true, p===self
78
+
79
+ ored = none|all
80
+ assert_equal true, ored===self
81
+
82
+ ored = none|respond|String
83
+ assert_equal true, ored===self
84
+ assert_equal true, ored==="hello"
85
+ assert_equal false, ored===12
86
+ end
87
+
88
+ end # class PredicateTest
89
+
90
+ end