extensions 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/object'
4
+ require 'set'
5
+
6
+ class TC_Object < Test::Unit::TestCase
7
+
8
+ def test_singleton_class
9
+ obj = "xyz"
10
+ expected = class << obj; self; end
11
+ actual = obj.singleton_class
12
+ assert_same(expected, actual)
13
+ end
14
+
15
+ def test_in
16
+ assert(2.in?([1,2,3]))
17
+ assert(2.in?(Set[1,2,3]))
18
+ assert("two\n".in?("one\ntwo\nthree\n"))
19
+ assert(5.in?("one\ntwo\nthree\n") == false)
20
+ a = [ [1,3], ["a","b"] ]
21
+ assert(["a","b"].in?(a))
22
+ end
23
+
24
+ def test_pp_s
25
+ # Can't so much, as the code is so simple; all the work is done by PP.
26
+ # Any testing of its output would be rendered incorrect by changes to that
27
+ # library. Just do a sanity check with a simple value.
28
+ assert_equal(%{"one"\n}, "one".pp_s)
29
+ end
30
+
31
+ def test_not_nil?
32
+ assert_equal(true, 5.not_nil?)
33
+ assert_equal(true, :x.not_nil?)
34
+ assert_equal(false, nil.not_nil?)
35
+ assert_equal(true, false.not_nil?)
36
+ end
37
+
38
+ def test_non_nil?
39
+ assert_equal(true, 5.non_nil?)
40
+ assert_equal(true, :x.non_nil?)
41
+ assert_equal(false, nil.non_nil?)
42
+ assert_equal(true, false.non_nil?)
43
+ end
44
+
45
+ def test_define_method_0
46
+ o = Object.new
47
+ o.define_method(:foobar) do |x, y|
48
+ x + y
49
+ end
50
+ assert_equal(9, o.foobar(5,4))
51
+ assert_raise(ArgumentError) { o.foobar() }
52
+ assert_raise(ArgumentError) { o.foobar(1) }
53
+ assert_raise(ArgumentError) { o.foobar(1,2,3) }
54
+ end
55
+
56
+ def test_define_method_1
57
+ o = Object.new
58
+ p = proc { |a,b,c| a + b + c }
59
+ o.define_method(:foobar, p)
60
+ assert_equal("xyz", o.foobar('x','y','z'))
61
+ assert_raise(ArgumentError) { o.foobar(1,2) }
62
+ end
63
+
64
+ def test_define_method_2
65
+ s = 'hello'
66
+ s.define_method(:foobar, s.method(:reverse))
67
+ assert_equal("olleh", s.foobar)
68
+ assert_raise(ArgumentError) { s.foobar(1,2,3) }
69
+ end
70
+
71
+ end # class TC_Object
72
+
@@ -0,0 +1,60 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/ostruct'
4
+
5
+ class TC_OStruct < Test::Unit::TestCase
6
+ class Person < OpenStruct; end
7
+
8
+ def test_1_old_functionality
9
+ o = OpenStruct.new
10
+ assert_nil(o.foo)
11
+ o.foo = :bar
12
+ assert_equal(:bar, o.foo)
13
+ o.delete_field(:foo)
14
+ assert_nil(o.foo)
15
+ o1 = OpenStruct.new(:x => 1, :y => 2)
16
+ assert_equal(1, o1.x)
17
+ assert_equal(2, o1.y)
18
+ o2 = OpenStruct.new(:x => 1, :y => 2)
19
+ assert(o1 == o2)
20
+ end
21
+
22
+ def test_2_new_functionality
23
+ person = OpenStruct.new do |p|
24
+ p.name = 'John Smith'
25
+ p.gender = :M
26
+ p.age = 71
27
+ end
28
+ assert_equal('John Smith', person.name)
29
+ assert_equal(:M, person.gender)
30
+ assert_equal(71, person.age)
31
+ assert_equal(nil, person.address)
32
+ person = OpenStruct.new(:gender => :M, :age => 71) do |p|
33
+ p.name = 'John Smith'
34
+ end
35
+ assert_equal('John Smith', person.name)
36
+ assert_equal(:M, person.gender)
37
+ assert_equal(71, person.age)
38
+ assert_equal(nil, person.address)
39
+ end
40
+
41
+ def test_3_subclass
42
+ person = Person.new do |p|
43
+ p.name = 'John Smith'
44
+ p.gender = :M
45
+ p.age = 71
46
+ end
47
+ assert_equal('John Smith', person.name)
48
+ assert_equal(:M, person.gender)
49
+ assert_equal(71, person.age)
50
+ assert_equal(nil, person.address)
51
+ person = Person.new(:gender => :M, :age => 71) do |p|
52
+ p.name = 'John Smith'
53
+ end
54
+ assert_equal('John Smith', person.name)
55
+ assert_equal(:M, person.gender)
56
+ assert_equal(71, person.age)
57
+ assert_equal(nil, person.address)
58
+ end
59
+ end # class TC_Array
60
+
@@ -0,0 +1,438 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/string'
4
+
5
+ #
6
+ # Create the setup method for the unit tests, so they can share the
7
+ # same data.
8
+ #
9
+ module TC_String_Setup
10
+ def setup
11
+ @tabs = <<-EOF
12
+
13
+ \tOne tab
14
+ \tOne space and one tab
15
+ \t Six spaces, a tab, and a space
16
+ EOF
17
+
18
+ @usage = %{
19
+ | usage: prog [-o dir] [-h] file...
20
+ | where
21
+ | -o dir outputs to DIR
22
+ | -h prints this message
23
+ }
24
+
25
+ @poem1 = <<-EOF
26
+ I must go down to the seas again
27
+ The lonely sea and the sky
28
+ And all I want is a tall ship
29
+ And a star to steer her by
30
+ EOF
31
+
32
+ @poem2 = <<-EOF
33
+ "Eek!"
34
+ She cried
35
+ As the mouse quietly scurried
36
+ by.
37
+ EOF
38
+
39
+ @poem3 = <<-EOF
40
+ * I must go down to the seas again
41
+ * The lonely sea and the sky
42
+ * And all I want is a tall ship
43
+ * And a star to steer her by
44
+ EOF
45
+ end # def setup
46
+ end # module TC_String_Setup
47
+
48
+ #
49
+ # Test the expand_tabs method.
50
+ #
51
+ class TC_String_1 < Test::Unit::TestCase
52
+
53
+ include TC_String_Setup
54
+
55
+ def test_expand_tabs_1
56
+ expected = <<-EOF
57
+
58
+ One tab
59
+ One space and one tab
60
+ Six spaces, a tab, and a space
61
+ EOF
62
+ assert_equal(expected, @tabs.expand_tabs)
63
+ assert_equal(expected, @tabs.expand_tabs(8))
64
+ end
65
+
66
+ def test_expand_tabs_2
67
+ expected = <<-EOF
68
+
69
+ One tab
70
+ One space and one tab
71
+ Six spaces, a tab, and a space
72
+ EOF
73
+ assert_equal(expected, @tabs.expand_tabs(4))
74
+ end
75
+
76
+ def test_expand_tabs_3
77
+ expected = <<-EOF
78
+
79
+ One tab
80
+ One space and one tab
81
+ Six spaces, a tab, and a space
82
+ EOF
83
+ assert_equal(expected, @tabs.expand_tabs(16))
84
+ end
85
+
86
+ def test_expand_tabs_4
87
+ expected = <<-EOF
88
+
89
+ One tab
90
+ One space and one tab
91
+ Six spaces, a tab, and a space
92
+ EOF
93
+ assert_equal(expected, @tabs.expand_tabs(1))
94
+ end
95
+
96
+ def test_expand_tabs_5
97
+ expected = <<-EOF
98
+
99
+ One tab
100
+ One space and one tab
101
+ Six spaces, a tab, and a space
102
+ EOF
103
+ assert_equal(expected, @tabs.expand_tabs(0))
104
+ end
105
+ end # class TC_String_1
106
+
107
+
108
+ # ===========================================================================
109
+
110
+
111
+ #
112
+ # Test the indent-style methods.
113
+ #
114
+ class TC_String_2 < Test::Unit::TestCase
115
+
116
+ include TC_String_Setup
117
+
118
+ def test_indent_0
119
+ assert_equal("xyz", "xyz".indent(-1))
120
+ assert_equal("xyz", "xyz".indent(0))
121
+ assert_equal(" xyz", "xyz".indent(1))
122
+ assert_equal(" xyz", "xyz".indent(2))
123
+ assert_equal(" xyz", "xyz".indent(3))
124
+ assert_equal(" xyz", "xyz".indent(3).indent(2))
125
+ end
126
+
127
+ def test_indent_1
128
+ expected = <<-EOF
129
+ I must go down to the seas again
130
+ The lonely sea and the sky
131
+ And all I want is a tall ship
132
+ And a star to steer her by
133
+ EOF
134
+ actual = @poem1.indent(4)
135
+ assert_equal(expected, actual)
136
+ end
137
+
138
+ def test_indent_2
139
+ expected = <<-EOF
140
+ I must go down to the seas again
141
+ The lonely sea and the sky
142
+ And all I want is a tall ship
143
+ And a star to steer her by
144
+ EOF
145
+ actual = @poem1.indent(-1)
146
+ assert_equal(expected, actual)
147
+ end
148
+
149
+ def test_outdent_0
150
+ assert_equal(" xyz", " xyz".outdent(-1))
151
+ assert_equal(" xyz", " xyz".outdent(0))
152
+ assert_equal(" xyz", " xyz".outdent(1))
153
+ assert_equal(" xyz", " xyz".outdent(2))
154
+ assert_equal("xyz", " xyz".outdent(3))
155
+ assert_equal("xyz", " xyz".outdent(4))
156
+ end
157
+
158
+ def test_outdent_1
159
+ expected = <<-EOF
160
+ I must go down to the seas again
161
+ The lonely sea and the sky
162
+ And all I want is a tall ship
163
+ And a star to steer her by
164
+ EOF
165
+ actual = @poem1.outdent(1)
166
+ assert_equal(expected, actual)
167
+ end
168
+
169
+ def test_outdent_2
170
+ expected = <<-EOF
171
+ I must go down to the seas again
172
+ The lonely sea and the sky
173
+ And all I want is a tall ship
174
+ And a star to steer her by
175
+ EOF
176
+ actual = @poem1.outdent(6)
177
+ assert_equal(expected, actual)
178
+ end
179
+
180
+ def test_outdent_3
181
+ str = @poem1.gsub(/^ *\|/, "") # Remove |s to test relative indentation
182
+ expected = <<-EOF
183
+ I must go down to the seas again
184
+ The lonely sea and the sky
185
+ And all I want is a tall ship
186
+ And a star to steer her by
187
+ EOF
188
+ actual = str.outdent(6)
189
+ assert_equal(expected, actual)
190
+ end
191
+
192
+ def test_outdent_4
193
+ expected = <<-EOF
194
+ "Eek!"
195
+ She cried
196
+ As the mouse quietly scurried
197
+ by.
198
+ EOF
199
+ actual = @poem2.outdent(100)
200
+ assert_equal(expected, actual)
201
+ end
202
+
203
+ def test_tabto_0
204
+ assert_equal("xyz", "xyz".tabto(0))
205
+ assert_equal(" xyz", "xyz".tabto(1))
206
+ assert_equal(" xyz", "xyz".tabto(2))
207
+ assert_equal(" xyz", " xyz".tabto(2))
208
+ end
209
+
210
+ def test_tabto_1
211
+ expected = <<-EOF
212
+ I must go down to the seas again
213
+ The lonely sea and the sky
214
+ And all I want is a tall ship
215
+ And a star to steer her by
216
+ EOF
217
+ actual = @poem1.tabto(3)
218
+ assert_equal(expected, actual)
219
+ end
220
+
221
+ def test_tabto_2
222
+ expected = <<-EOF
223
+ "Eek!"
224
+ She cried
225
+ As the mouse quietly scurried
226
+ by.
227
+ EOF
228
+ actual = @poem2.tabto(-5)
229
+ assert_equal(expected, actual)
230
+ end
231
+
232
+ def test_tabto_3
233
+ expected = <<-EOF
234
+ "Eek!"
235
+ She cried
236
+ As the mouse quietly scurried
237
+ by.
238
+ EOF
239
+ actual = @poem2.tabto(10)
240
+ assert_equal(expected, actual)
241
+ end
242
+
243
+ def test_tabto_4
244
+ # Note blank lines in data. Should not prevent #tabto from doing its work.
245
+ str = <<-EOF
246
+
247
+ abc
248
+
249
+ 123
250
+ EOF
251
+ expected = <<-EOF
252
+
253
+ abc
254
+
255
+ 123
256
+ EOF
257
+ actual = str.tabto(2)
258
+ assert_equal(expected, actual)
259
+ end
260
+
261
+ def test_tabto_5
262
+ # Note blank lines in data. Should not prevent #tabto from doing its work.
263
+ str = <<EOF
264
+
265
+ 1. All files (summary) in alphabetical order.
266
+ * General report on everything.
267
+
268
+ 2. All 'core' files (summary) in alphabetical order.
269
+ * Shows what we've achieved and what's left to be done.
270
+ EOF
271
+
272
+ expected = <<EOF
273
+
274
+ 1. All files (summary) in alphabetical order.
275
+ * General report on everything.
276
+
277
+ 2. All 'core' files (summary) in alphabetical order.
278
+ * Shows what we've achieved and what's left to be done.
279
+ EOF
280
+ assert_equal(expected, str.tabto(2))
281
+ end
282
+
283
+ def test_taballto_0
284
+ assert_equal("xyz", " xyz".taballto(-1))
285
+ assert_equal("xyz", "xyz".taballto(0))
286
+ assert_equal("xyz", " xyz".taballto(0))
287
+ assert_equal(" foo\n bar\n", "foo\n bar\n".taballto(3))
288
+ end
289
+
290
+ def test_taballto_1
291
+ expected = <<-EOF
292
+ "Eek!"
293
+ She cried
294
+ As the mouse quietly scurried
295
+ by.
296
+ EOF
297
+ assert_equal(expected, @poem2.taballto(6))
298
+ end
299
+
300
+ end # class TC_String_2
301
+
302
+
303
+ # ===========================================================================
304
+
305
+
306
+ #
307
+ # Test the trim method.
308
+ #
309
+ class TC_String_3 < Test::Unit::TestCase
310
+
311
+ include TC_String_Setup
312
+
313
+ def test_trim_0
314
+ assert_equal("", "".trim)
315
+ assert_equal("xyz", "xyz".trim)
316
+ assert_equal("xyz", "\nxyz".trim)
317
+ assert_equal("\nxyz", "\n\nxyz".trim)
318
+ assert_equal("xyz", " \nxyz".trim)
319
+ assert_equal(" xyz", " xyz ".trim)
320
+ assert_equal("yz", "\n xyz".trim('x'))
321
+ assert_equal("yz", "\nxyz".trim('x'))
322
+ assert_equal("xyz", "|xyz".trim('|'))
323
+ assert_equal("xyz", "| xyz".trim('|'))
324
+ assert_equal(" xyz", "| xyz".trim('|'))
325
+ assert_equal("xyz", "| xyz".trim('| '))
326
+ assert_equal("", " \n | ".trim("| "))
327
+ assert_equal(" xyz\n xyz\n", " \n xyz\n xyz\n".trim('|'))
328
+ end
329
+
330
+ def test_trim_1
331
+ expected = <<-EOF
332
+ I must go down to the seas again
333
+ The lonely sea and the sky
334
+ And all I want is a tall ship
335
+ And a star to steer her by
336
+ EOF
337
+ actual = @poem3.trim('*')
338
+ assert_equal(expected, actual)
339
+ end
340
+
341
+ def test_trim_2
342
+ expected = <<EOF
343
+ usage: prog [-o dir] [-h] file...
344
+ where
345
+ -o dir outputs to DIR
346
+ -h prints this message
347
+ EOF
348
+ assert_equal(expected, @usage.trim("|"))
349
+ end
350
+
351
+ def test_trim_3
352
+ expected = <<EOF
353
+ | usage: prog [-o dir] [-h] file...
354
+ | where
355
+ | -o dir outputs to DIR
356
+ | -h prints this message
357
+ EOF
358
+ assert_equal(expected, @usage.trim)
359
+ end
360
+
361
+ end # class TC_String_3
362
+
363
+
364
+ # ===========================================================================
365
+
366
+
367
+ #
368
+ # Test the starts_with?, ends_with?, line, cmp, and join methods.
369
+ #
370
+ class TC_String_4 < Test::Unit::TestCase
371
+
372
+ include TC_String_Setup
373
+
374
+ def test_starts_with
375
+ assert(@poem1.strip.starts_with?("I must go down"))
376
+ assert(! @poem1.strip.starts_with?("Hello stranger"))
377
+ end
378
+
379
+ def test_ends_with
380
+ assert(@poem1.strip.ends_with?("steer her by"))
381
+ assert(! @poem1.strip.ends_with?("I must be going"))
382
+ end
383
+
384
+ def test_line
385
+ data = <<-EOF
386
+ 1 o'clock, 2 o'clock, 3 o'clock, rock!
387
+ 5, 6, 7 o'clock, 8 o'clock, rock!
388
+ 9, 10, 11 o'clock, 12 o'clock, rock!
389
+ We're gonna rock around the clock tonight!
390
+ EOF
391
+ expected = "9, 10, 11 o'clock, 12 o'clock, rock!"
392
+ assert_equal(expected, data.line(2))
393
+ assert_equal([expected], data.line(2, 1))
394
+
395
+ expected = [
396
+ "9, 10, 11 o'clock, 12 o'clock, rock!",
397
+ "We're gonna rock around the clock tonight!",
398
+ ]
399
+ assert_equal(expected, data.line(2..3))
400
+ assert_equal(expected, data.line(2..7))
401
+ assert_equal(expected, data.line(2, 2))
402
+ assert_equal(expected, data.line(2, 3))
403
+ assert_equal(expected, data.line(2, 4))
404
+
405
+ assert_equal(nil, data.line(10))
406
+ assert_equal(nil, data.line(10, 10))
407
+ assert_equal(nil, data.line(10..20))
408
+
409
+ assert_raises(TypeError) { data.line("fish") }
410
+ assert_raises(ArgumentError) { data.line(0,1,2) }
411
+ end
412
+
413
+ def test_cmp
414
+ assert_equal(nil, @usage.cmp(@usage))
415
+ assert_equal(@usage.size, @usage.cmp(@usage + "XYZ"))
416
+ assert_equal(@usage.size, (@usage + "XYZ").cmp(@usage))
417
+ usage2 = @usage.dup
418
+ usage2[5] = '$'
419
+ assert_equal(5, usage2.cmp(@usage))
420
+ assert_equal(5, @usage.cmp(usage2))
421
+ end
422
+
423
+ def test_join
424
+ assert_equal('', ''.join)
425
+ assert_equal('x', 'x'.join)
426
+ assert_equal("x x", "x \t\n\n x \t \n ".join)
427
+ assert_equal("x", " \t\n\n x \t \n ".join)
428
+ assert_equal('One fine day she came my way', "One fine day she \n came my way".join)
429
+ assert_equal('One fine day she came my way', "One fine day she \n came my way".join)
430
+ assert_equal('One fine day she came my way', " One fine day she \n came my way".join)
431
+ assert_equal('One fine day she came my way', "\nOne fine day she came my way".join)
432
+ assert_equal('One fine day she came my way', "\nOne fine day she \n came my way".join)
433
+ assert_equal('One fine day she came my way', "One fine day she \n\n\n \n came my way".join)
434
+ assert_equal('One fine day she came my way', "One fine day she \n\n\n \n came my way\n\n".join)
435
+ end
436
+
437
+ end # class TC_String_4
438
+