rroonga 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/NEWS.ja.rdoc +29 -0
  2. data/NEWS.rdoc +29 -0
  3. data/Rakefile +5 -3
  4. data/ext/groonga/rb-grn-array.c +1 -1
  5. data/ext/groonga/rb-grn-context.c +15 -28
  6. data/ext/groonga/rb-grn-exception.c +46 -1
  7. data/ext/groonga/rb-grn-expression.c +22 -13
  8. data/ext/groonga/rb-grn-fix-size-column.c +2 -8
  9. data/ext/groonga/rb-grn-hash.c +8 -1
  10. data/ext/groonga/rb-grn-object.c +1 -1
  11. data/ext/groonga/rb-grn-patricia-trie.c +23 -1
  12. data/ext/groonga/rb-grn-table-key-support.c +22 -0
  13. data/ext/groonga/rb-grn-table.c +37 -5
  14. data/ext/groonga/rb-grn-utils.c +20 -2
  15. data/ext/groonga/rb-groonga.c +76 -38
  16. data/extconf.rb +17 -1
  17. data/html/developer.html +32 -7
  18. data/html/footer.html.erb +5 -0
  19. data/html/heading-mark.svg +393 -0
  20. data/html/index.html +33 -3
  21. data/lib/groonga.rb +3 -7
  22. data/lib/groonga/context.rb +2 -13
  23. data/lib/groonga/expression-builder.rb +273 -67
  24. data/lib/groonga/pagination.rb +143 -0
  25. data/lib/groonga/record.rb +2 -0
  26. data/lib/groonga/schema.rb +140 -29
  27. data/rroonga-build.rb +2 -2
  28. data/test-unit/Rakefile +40 -0
  29. data/test-unit/TODO +5 -0
  30. data/test-unit/bin/testrb +5 -0
  31. data/test-unit/html/classic.html +15 -0
  32. data/test-unit/html/index.html +25 -0
  33. data/test-unit/html/index.html.ja +27 -0
  34. data/test-unit/lib/test/unit.rb +323 -0
  35. data/test-unit/lib/test/unit/assertionfailederror.rb +25 -0
  36. data/test-unit/lib/test/unit/assertions.rb +1230 -0
  37. data/test-unit/lib/test/unit/attribute.rb +125 -0
  38. data/test-unit/lib/test/unit/autorunner.rb +360 -0
  39. data/test-unit/lib/test/unit/collector.rb +36 -0
  40. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  41. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  42. data/test-unit/lib/test/unit/collector/load.rb +144 -0
  43. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  44. data/test-unit/lib/test/unit/color-scheme.rb +102 -0
  45. data/test-unit/lib/test/unit/color.rb +96 -0
  46. data/test-unit/lib/test/unit/diff.rb +724 -0
  47. data/test-unit/lib/test/unit/error.rb +130 -0
  48. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  49. data/test-unit/lib/test/unit/failure.rb +136 -0
  50. data/test-unit/lib/test/unit/fixture.rb +176 -0
  51. data/test-unit/lib/test/unit/notification.rb +129 -0
  52. data/test-unit/lib/test/unit/omission.rb +191 -0
  53. data/test-unit/lib/test/unit/pending.rb +150 -0
  54. data/test-unit/lib/test/unit/priority.rb +180 -0
  55. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  56. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  57. data/test-unit/lib/test/unit/runner/tap.rb +8 -0
  58. data/test-unit/lib/test/unit/testcase.rb +476 -0
  59. data/test-unit/lib/test/unit/testresult.rb +89 -0
  60. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  61. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  62. data/test-unit/lib/test/unit/ui/console/testrunner.rb +466 -0
  63. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +63 -0
  64. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +92 -0
  65. data/test-unit/lib/test/unit/ui/testrunner.rb +28 -0
  66. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  67. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  68. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  69. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  70. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  71. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  72. data/test-unit/lib/test/unit/version.rb +7 -0
  73. data/test-unit/sample/adder.rb +13 -0
  74. data/test-unit/sample/subtracter.rb +12 -0
  75. data/test-unit/sample/test_adder.rb +20 -0
  76. data/test-unit/sample/test_subtracter.rb +20 -0
  77. data/test-unit/sample/test_user.rb +23 -0
  78. data/test-unit/test/collector/test-descendant.rb +133 -0
  79. data/test-unit/test/collector/test-load.rb +442 -0
  80. data/test-unit/test/collector/test_dir.rb +406 -0
  81. data/test-unit/test/collector/test_objectspace.rb +100 -0
  82. data/test-unit/test/run-test.rb +15 -0
  83. data/test-unit/test/test-attribute.rb +86 -0
  84. data/test-unit/test/test-color-scheme.rb +67 -0
  85. data/test-unit/test/test-color.rb +47 -0
  86. data/test-unit/test/test-diff.rb +518 -0
  87. data/test-unit/test/test-emacs-runner.rb +60 -0
  88. data/test-unit/test/test-fixture.rb +287 -0
  89. data/test-unit/test/test-notification.rb +33 -0
  90. data/test-unit/test/test-omission.rb +81 -0
  91. data/test-unit/test/test-pending.rb +70 -0
  92. data/test-unit/test/test-priority.rb +119 -0
  93. data/test-unit/test/test-testcase.rb +544 -0
  94. data/test-unit/test/test_assertions.rb +1151 -0
  95. data/test-unit/test/test_error.rb +26 -0
  96. data/test-unit/test/test_failure.rb +33 -0
  97. data/test-unit/test/test_testresult.rb +113 -0
  98. data/test-unit/test/test_testsuite.rb +129 -0
  99. data/test-unit/test/testunit-test-util.rb +14 -0
  100. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  101. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  102. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  103. data/test-unit/test/util/test_observable.rb +102 -0
  104. data/test-unit/test/util/test_procwrapper.rb +36 -0
  105. data/test/groonga-test-utils.rb +3 -2
  106. data/test/run-test.rb +14 -2
  107. data/test/test-column.rb +7 -7
  108. data/test/test-context-select.rb +34 -11
  109. data/test/test-exception.rb +3 -0
  110. data/test/test-expression-builder.rb +11 -0
  111. data/test/test-expression.rb +3 -6
  112. data/test/test-gqtp.rb +3 -5
  113. data/test/test-pagination.rb +249 -0
  114. data/test/test-record.rb +36 -8
  115. data/test/test-remote.rb +11 -4
  116. data/test/test-schema-create-table.rb +251 -0
  117. data/test/test-schema.rb +4 -24
  118. data/test/test-table-offset-and-limit.rb +3 -5
  119. data/test/test-table-select-mecab.rb +80 -0
  120. data/test/test-table-select-weight.rb +104 -0
  121. data/test/test-table.rb +22 -4
  122. data/test/test-version.rb +1 -1
  123. data/text/TUTORIAL.ja.rdoc +2 -0
  124. data/text/expression.rdoc +1 -0
  125. metadata +96 -35
@@ -0,0 +1,1151 @@
1
+ # Author:: Nathaniel Talbott.
2
+ # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
3
+ # Copyright (c) 2009 Kouhei Sutou.
4
+ # License:: Ruby license.
5
+
6
+ require 'test/unit'
7
+
8
+ module Test
9
+ module Unit
10
+ class TC_Assertions < TestCase
11
+ backtrace_pre = "---Backtrace---"
12
+ backtrace_post = "---------------"
13
+ BACKTRACE_RE = /#{backtrace_pre}\n.+\n#{backtrace_post}/m
14
+
15
+ def check(value, message="")
16
+ add_assertion
17
+ raise AssertionFailedError.new(message) unless value
18
+ end
19
+
20
+ def check_assertions(expect_fail, expected_message="",
21
+ return_value_expected=false)
22
+ @actual_assertion_count = 0
23
+ failed = true
24
+ actual_message = nil
25
+ @catch_assertions = true
26
+ return_value = nil
27
+ begin
28
+ return_value = yield
29
+ failed = false
30
+ rescue AssertionFailedError => error
31
+ actual_message = error.message
32
+ end
33
+ @catch_assertions = false
34
+
35
+ if expect_fail
36
+ message = "Should have failed, but didn't"
37
+ else
38
+ message = "Should not have failed, but did with message\n" +
39
+ "<#{actual_message}>"
40
+ end
41
+ check(expect_fail == failed, message)
42
+
43
+ message = "Should have made one assertion but made\n" +
44
+ "<#{@actual_assertion_count}>"
45
+ check(1 == @actual_assertion_count, message)
46
+
47
+ if expect_fail
48
+ case expected_message
49
+ when String
50
+ check(actual_message == expected_message,
51
+ "Should have the correct message.\n" +
52
+ "<#{expected_message.inspect}> expected but was\n" +
53
+ "<#{actual_message.inspect}>")
54
+ when Regexp
55
+ check(actual_message =~ expected_message,
56
+ "The message should match correctly.\n" +
57
+ "</#{expected_message.source}/> expected to match\n" +
58
+ "<#{actual_message.inspect}>")
59
+ else
60
+ check(false,
61
+ "Incorrect expected message type in assert_nothing_failed")
62
+ end
63
+ else
64
+ if return_value_expected
65
+ check(!return_value.nil?, "Should return a value")
66
+ else
67
+ check(return_value.nil?,
68
+ "Should not return a value but returned <#{return_value}>")
69
+ end
70
+ end
71
+
72
+ return_value
73
+ end
74
+
75
+ def check_nothing_fails(return_value_expected=false, &proc)
76
+ check_assertions(false, "", return_value_expected, &proc)
77
+ end
78
+
79
+ def check_fails(expected_message="", &proc)
80
+ check_assertions(true, expected_message, &proc)
81
+ end
82
+
83
+ def inspect_tag(tag)
84
+ begin
85
+ throw tag
86
+ rescue NameError
87
+ tag.to_s.inspect
88
+ rescue ArgumentError
89
+ tag.inspect
90
+ end
91
+ end
92
+
93
+ def test_assert_block
94
+ check_nothing_fails {
95
+ assert_block {true}
96
+ }
97
+ check_nothing_fails {
98
+ assert_block("successful assert_block") {true}
99
+ }
100
+ check_nothing_fails {
101
+ assert_block("successful assert_block") {true}
102
+ }
103
+ check_fails("assert_block failed.") {
104
+ assert_block {false}
105
+ }
106
+ check_fails("failed assert_block") {
107
+ assert_block("failed assert_block") {false}
108
+ }
109
+ end
110
+
111
+ def test_assert
112
+ check_nothing_fails{assert("a")}
113
+ check_nothing_fails{assert(true)}
114
+ check_nothing_fails{assert(true, "successful assert")}
115
+ check_fails("<nil> is not true."){assert(nil)}
116
+ check_fails("<false> is not true."){assert(false)}
117
+ check_fails("failed assert.\n<false> is not true."){assert(false, "failed assert")}
118
+ end
119
+
120
+ def test_assert_equal
121
+ check_nothing_fails {
122
+ assert_equal("string1", "string1")
123
+ }
124
+ check_nothing_fails {
125
+ assert_equal("string1", "string1", "successful assert_equal")
126
+ }
127
+
128
+ message = <<-EOM.chomp
129
+ <"string1"> expected but was
130
+ <"string2">.
131
+
132
+ diff:
133
+ - string1
134
+ ? ^
135
+ + string2
136
+ ? ^
137
+ EOM
138
+ check_fails(message) {
139
+ assert_equal("string1", "string2")
140
+ }
141
+
142
+ message = <<-EOM.chomp
143
+ failed assert_equal.
144
+ <"string1"> expected but was
145
+ <"string2">.
146
+
147
+ diff:
148
+ - string1
149
+ ? ^
150
+ + string2
151
+ ? ^
152
+ EOM
153
+ check_fails(message) {
154
+ assert_equal("string1", "string2", "failed assert_equal")
155
+ }
156
+
157
+ message = <<-EOM.chomp
158
+ <"111111"> expected but was
159
+ <111111>.
160
+
161
+ diff:
162
+ - "111111"
163
+ ? - -
164
+ + 111111
165
+ EOM
166
+ check_fails(message) do
167
+ assert_equal("111111", 111111)
168
+ end
169
+ end
170
+
171
+ def test_assert_equal_with_long_line
172
+ expected = ["0123456789",
173
+ "1123456789",
174
+ "2123456789",
175
+ "3123456789",
176
+ "4123456789",
177
+ "5123456789",
178
+ "6123456789",
179
+ "7123456789",
180
+ "8123456789"].join
181
+ actual = ["0000000000",
182
+ "1123456789",
183
+ "2123456789",
184
+ "3123456789",
185
+ "4123456789",
186
+ "5123456789",
187
+ "6123456789",
188
+ "7123456789",
189
+ "8123456789"].join
190
+ message = <<-EOM.chomp
191
+ <"#{expected}"> expected but was
192
+ <"#{actual}">.
193
+
194
+ diff:
195
+ - #{expected}
196
+ ? ^^^^^^^^^
197
+ + #{actual}
198
+ ? ^^^^^^^^^
199
+
200
+ folded diff:
201
+ - 012345678911234567892123456789312345678941234567895123456789612345678971234567
202
+ ? ^^^^^^^^^
203
+ + 000000000011234567892123456789312345678941234567895123456789612345678971234567
204
+ ? ^^^^^^^^^
205
+ 898123456789
206
+ EOM
207
+ check_fails(message) do
208
+ assert_equal(expected, actual)
209
+ end
210
+ end
211
+
212
+ def test_assert_equal_for_too_small_difference
213
+ message = <<-EOM.chomp
214
+ <1> expected but was
215
+ <2>.
216
+ EOM
217
+ check_fails(message) do
218
+ assert_equal(1, 2)
219
+ end
220
+ end
221
+
222
+ def test_assert_equal_for_same_inspected_objects
223
+ now = Time.now
224
+ now_without_usec = Time.at(now.to_i)
225
+ message = <<-EOM.chomp
226
+ <#{now.inspect}> expected but was
227
+ <#{now.inspect}>.
228
+ EOM
229
+ check_fails(message) do
230
+ assert_equal(now, now_without_usec)
231
+ end
232
+ end
233
+
234
+ def test_assert_equal_with_multi_lines_result
235
+ message = <<-EOM.chomp
236
+ <#{"a\nb".inspect}> expected but was
237
+ <#{"x".inspect}>.
238
+
239
+ diff:
240
+ + x
241
+ - a
242
+ - b
243
+ EOM
244
+ check_fails(message) do
245
+ assert_equal("a\nb", "x")
246
+ end
247
+ end
248
+
249
+ def test_assert_equal_with_large_string
250
+ message = <<-EOM.chomp
251
+ <#{("a\n" + "x" * 997).inspect}> expected but was
252
+ <#{"x".inspect}>.
253
+
254
+ diff:
255
+ + x
256
+ - a
257
+ - #{"x" * 997}
258
+
259
+ folded diff:
260
+ + x
261
+ - a
262
+ #{(["- " + ("x" * 78)] * 12).join("\n")}
263
+ - #{"x" * 61}
264
+ EOM
265
+ check_fails(message) do
266
+ assert_equal("a\n" + "x" * 997, "x")
267
+ end
268
+
269
+ message = <<-EOM.chomp
270
+ <#{("a\n" + "x" * 998).inspect}> expected but was
271
+ <#{"x".inspect}>.
272
+ EOM
273
+ check_fails(message) do
274
+ assert_equal("a\n" + "x" * 998, "x")
275
+ end
276
+ end
277
+
278
+ def test_assert_equal_with_max_diff_target_string_size
279
+ key = "TEST_UNIT_MAX_DIFF_TARGET_STRING_SIZE"
280
+ before_value = ENV[key]
281
+ ENV[key] = "100"
282
+ begin
283
+ message = <<-EOM.chomp
284
+ <#{("a\n" + "x" * 97).inspect}> expected but was
285
+ <#{"x".inspect}>.
286
+
287
+ diff:
288
+ + x
289
+ - a
290
+ - #{"x" * 97}
291
+
292
+ folded diff:
293
+ + x
294
+ - a
295
+ #{(["- " + ("x" * 78)]).join("\n")}
296
+ - #{"x" * 19}
297
+ EOM
298
+ check_fails(message) do
299
+ assert_equal("a\n" + "x" * 97, "x")
300
+ end
301
+
302
+ message = <<-EOM.chomp
303
+ <#{("a\n" + "x" * 98).inspect}> expected but was
304
+ <#{"x".inspect}>.
305
+ EOM
306
+ check_fails(message) do
307
+ assert_equal("a\n" + "x" * 98, "x")
308
+ end
309
+ ensure
310
+ ENV[key] = before_value
311
+ end
312
+ end
313
+
314
+ def test_assert_raise_success
315
+ return_value = nil
316
+ check_nothing_fails(true) do
317
+ return_value = assert_raise(RuntimeError) do
318
+ raise "Error"
319
+ end
320
+ end
321
+ check(return_value.kind_of?(Exception),
322
+ "Should have returned the exception " +
323
+ "from a successful assert_raise")
324
+ check(return_value.message == "Error",
325
+ "Should have returned the correct exception " +
326
+ "from a successful assert_raise")
327
+
328
+ check_nothing_fails(true) do
329
+ assert_raise(ArgumentError, "successful assert_raise") do
330
+ raise ArgumentError.new("Error")
331
+ end
332
+ end
333
+
334
+ check_nothing_fails(true) do
335
+ assert_raise(RuntimeError) do
336
+ raise "Error"
337
+ end
338
+ end
339
+
340
+ check_nothing_fails(true) do
341
+ assert_raise(RuntimeError, "successful assert_raise") do
342
+ raise "Error"
343
+ end
344
+ end
345
+
346
+ check_nothing_fails(true) do
347
+ assert_raise do
348
+ raise Exception, "Any exception"
349
+ end
350
+ end
351
+ end
352
+
353
+ def test_assert_raise_fail
354
+ check_fails("<RuntimeError> exception expected but none was thrown.") do
355
+ assert_raise(RuntimeError) do
356
+ 1 + 1
357
+ end
358
+ end
359
+
360
+ message = <<-EOM
361
+ failed assert_raise.
362
+ <ArgumentError> exception expected but was
363
+ Class: <RuntimeError>
364
+ Message: <"Error">
365
+ EOM
366
+ check_fails(/\A#{message}#{BACKTRACE_RE}\Z/m) do
367
+ assert_raise(ArgumentError, "failed assert_raise") do
368
+ raise "Error"
369
+ end
370
+ end
371
+
372
+ message = <<-EOM
373
+ Should expect a class of exception, Object.
374
+ <false> is not true.
375
+ EOM
376
+ check_fails(message.chomp) do
377
+ assert_nothing_raised(Object) do
378
+ 1 + 1
379
+ end
380
+ end
381
+ end
382
+
383
+ def test_assert_raise_module
384
+ exceptions = [ArgumentError, TypeError]
385
+ modules = [Math, Comparable]
386
+ rescues = exceptions + modules
387
+
388
+ exceptions.each do |exc|
389
+ return_value = nil
390
+ check_nothing_fails(true) do
391
+ return_value = assert_raise(*rescues) do
392
+ raise exc, "Error"
393
+ end
394
+ end
395
+ check(return_value.instance_of?(exc),
396
+ "Should have returned #{exc} but was #{return_value.class}")
397
+ check(return_value.message == "Error",
398
+ "Should have returned the correct exception " +
399
+ "from a successful assert_raise")
400
+ end
401
+
402
+ modules.each do |mod|
403
+ return_value = nil
404
+ check_nothing_fails(true) do
405
+ return_value = assert_raise(*rescues) do
406
+ raise Exception.new("Error").extend(mod)
407
+ end
408
+ end
409
+ check(mod === return_value,
410
+ "Should have returned #{mod}")
411
+ check(return_value.message == "Error",
412
+ "Should have returned the correct exception " +
413
+ "from a successful assert_raise")
414
+ end
415
+
416
+ check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception " +
417
+ "expected but none was thrown.") do
418
+ assert_raise(*rescues) do
419
+ 1 + 1
420
+ end
421
+ end
422
+
423
+ message = <<-EOM
424
+ failed assert_raise.
425
+ <[ArgumentError, TypeError]> exception expected but was
426
+ Class: <RuntimeError>
427
+ Message: <"Error">
428
+ EOM
429
+ message = Regexp.escape(message)
430
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/m) do
431
+ assert_raise(ArgumentError, TypeError, "failed assert_raise") do
432
+ raise "Error"
433
+ end
434
+ end
435
+ end
436
+
437
+ def test_assert_raise_instance
438
+ return_value = nil
439
+ check_nothing_fails(true) do
440
+ return_value = assert_raise(RuntimeError.new("Error")) do
441
+ raise "Error"
442
+ end
443
+ end
444
+ check(return_value.kind_of?(Exception),
445
+ "Should have returned the exception " +
446
+ "from a successful assert_raise")
447
+ check(return_value.message == "Error",
448
+ "Should have returned the correct exception " +
449
+ "from a successful assert_raise")
450
+
451
+ message = <<-EOM
452
+ <RuntimeError("XXX")> exception expected but was
453
+ Class: <RuntimeError>
454
+ Message: <"Error">
455
+ EOM
456
+ message = Regexp.escape(message)
457
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/) do
458
+ return_value = assert_raise(RuntimeError.new("XXX")) do
459
+ raise "Error"
460
+ end
461
+ end
462
+
463
+ different_error_class = Class.new(StandardError)
464
+ message = <<-EOM
465
+ <\#<Class:[xa-f\\d]+>\\("Error"\\)> exception expected but was
466
+ Class: <RuntimeError>
467
+ Message: <"Error">
468
+ EOM
469
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/) do
470
+ assert_raise(different_error_class.new("Error")) do
471
+ raise "Error"
472
+ end
473
+ end
474
+
475
+ different_error = different_error_class.new("Error")
476
+ def different_error.inspect
477
+ "DifferentError: \"Error\""
478
+ end
479
+ message = <<-EOM
480
+ <\DifferentError: \\"Error\\"> exception expected but was
481
+ Class: <RuntimeError>
482
+ Message: <"Error">
483
+ EOM
484
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/) do
485
+ assert_raise(different_error) do
486
+ raise "Error"
487
+ end
488
+ end
489
+
490
+ check_nothing_fails(true) do
491
+ assert_raise(different_error_class.new("Error"),
492
+ RuntimeError.new("Error"),
493
+ RuntimeError.new("XXX")) do
494
+ raise "Error"
495
+ end
496
+ end
497
+ end
498
+
499
+ def test_assert_instance_of
500
+ check_nothing_fails {
501
+ assert_instance_of(String, "string")
502
+ }
503
+ check_nothing_fails {
504
+ assert_instance_of(String, "string", "successful assert_instance_of")
505
+ }
506
+ check_nothing_fails {
507
+ assert_instance_of(String, "string", "successful assert_instance_of")
508
+ }
509
+ check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
510
+ assert_instance_of(Hash, "string")
511
+ }
512
+ check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
513
+ assert_instance_of(Hash, "string", "failed assert_instance_of")
514
+ }
515
+
516
+ check_nothing_fails do
517
+ assert_instance_of([Fixnum, NilClass], 100)
518
+ end
519
+ check_fails(%Q{<"string"> expected to be an instance of\n[<Fixnum>, <NilClass>] but was\n<String>.}) do
520
+ assert_instance_of([Fixnum, NilClass], "string")
521
+ end
522
+ check_fails(%Q{<100> expected to be an instance of\n[<Numeric>, <NilClass>] but was\n<Fixnum>.}) do
523
+ assert_instance_of([Numeric, NilClass], 100)
524
+ end
525
+ end
526
+
527
+ def test_assert_nil
528
+ check_nothing_fails {
529
+ assert_nil(nil)
530
+ }
531
+ check_nothing_fails {
532
+ assert_nil(nil, "successful assert_nil")
533
+ }
534
+ check_nothing_fails {
535
+ assert_nil(nil, "successful assert_nil")
536
+ }
537
+ check_fails(%Q{<"string"> expected to be nil.}) {
538
+ assert_nil("string")
539
+ }
540
+ check_fails(%Q{failed assert_nil.\n<"string"> expected to be nil.}) {
541
+ assert_nil("string", "failed assert_nil")
542
+ }
543
+ end
544
+
545
+ def test_assert_not_nil
546
+ check_nothing_fails{assert_not_nil(false)}
547
+ check_nothing_fails{assert_not_nil(false, "message")}
548
+ check_fails("<nil> expected to not be nil."){assert_not_nil(nil)}
549
+ check_fails("message.\n<nil> expected to not be nil.") {assert_not_nil(nil, "message")}
550
+ end
551
+
552
+ def test_assert_kind_of
553
+ check_nothing_fails {
554
+ assert_kind_of(Module, Array)
555
+ }
556
+ check_nothing_fails {
557
+ assert_kind_of(Object, "string", "successful assert_kind_of")
558
+ }
559
+ check_nothing_fails {
560
+ assert_kind_of(Object, "string", "successful assert_kind_of")
561
+ }
562
+ check_nothing_fails {
563
+ assert_kind_of(Comparable, 1)
564
+ }
565
+ check_fails(%Q{<"string"> expected to be kind_of?\n<Class> but was\n<String>.}) {
566
+ assert_kind_of(Class, "string")
567
+ }
568
+ check_fails(%Q{failed assert_kind_of.\n<"string"> expected to be kind_of?\n<Class> but was\n<String>.}) {
569
+ assert_kind_of(Class, "string", "failed assert_kind_of")
570
+ }
571
+
572
+ check_nothing_fails do
573
+ assert_kind_of([Fixnum, NilClass], 100)
574
+ end
575
+ check_fails(%Q{<"string"> expected to be kind_of?\n[<Fixnum>, <NilClass>] but was\n<String>.}) do
576
+ assert_kind_of([Fixnum, NilClass], "string")
577
+ end
578
+ end
579
+
580
+ def test_assert_match
581
+ check_nothing_fails {
582
+ assert_match(/strin./, "string")
583
+ }
584
+ check_nothing_fails {
585
+ assert_match("strin", "string")
586
+ }
587
+ check_nothing_fails {
588
+ assert_match(/strin./, "string", "successful assert_match")
589
+ }
590
+ check_nothing_fails {
591
+ assert_match(/strin./, "string", "successful assert_match")
592
+ }
593
+ check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) {
594
+ assert_match(/slin./, "string")
595
+ }
596
+ check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) {
597
+ assert_match("strin.", "string")
598
+ }
599
+ check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) {
600
+ assert_match(/slin./, "string", "failed assert_match")
601
+ }
602
+ end
603
+
604
+ def test_assert_same
605
+ thing = "thing"
606
+ check_nothing_fails {
607
+ assert_same(thing, thing)
608
+ }
609
+ check_nothing_fails {
610
+ assert_same(thing, thing, "successful assert_same")
611
+ }
612
+ check_nothing_fails {
613
+ assert_same(thing, thing, "successful assert_same")
614
+ }
615
+ thing2 = "thing"
616
+ check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
617
+ assert_same(thing, thing2)
618
+ }
619
+ check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
620
+ assert_same(thing, thing2, "failed assert_same")
621
+ }
622
+ end
623
+
624
+ def test_assert_nothing_raised
625
+ check_nothing_fails {
626
+ assert_nothing_raised {
627
+ 1 + 1
628
+ }
629
+ }
630
+ check_nothing_fails {
631
+ assert_nothing_raised("successful assert_nothing_raised") {
632
+ 1 + 1
633
+ }
634
+ }
635
+ check_nothing_fails {
636
+ assert_nothing_raised("successful assert_nothing_raised") {
637
+ 1 + 1
638
+ }
639
+ }
640
+ check_nothing_fails {
641
+ begin
642
+ assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
643
+ raise ZeroDivisionError.new("ArgumentError")
644
+ }
645
+ rescue ZeroDivisionError
646
+ end
647
+ }
648
+ check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
649
+ assert_nothing_raised(Object) {
650
+ 1 + 1
651
+ }
652
+ }
653
+ check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
654
+ assert_nothing_raised {
655
+ raise "Error"
656
+ }
657
+ }
658
+ check_fails(%r{\Afailed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
659
+ assert_nothing_raised("failed assert_nothing_raised") {
660
+ raise "Error"
661
+ }
662
+ }
663
+ check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
664
+ assert_nothing_raised(StandardError, RuntimeError) {
665
+ raise "Error"
666
+ }
667
+ }
668
+ check_fails("Failure.") do
669
+ assert_nothing_raised do
670
+ flunk("Failure")
671
+ end
672
+ end
673
+ end
674
+
675
+ def test_flunk
676
+ check_fails("Flunked.") {
677
+ flunk
678
+ }
679
+ check_fails("flunk message.") {
680
+ flunk("flunk message")
681
+ }
682
+ end
683
+
684
+ def test_assert_not_same
685
+ thing = "thing"
686
+ thing2 = "thing"
687
+ check_nothing_fails {
688
+ assert_not_same(thing, thing2)
689
+ }
690
+ check_nothing_fails {
691
+ assert_not_same(thing, thing2, "message")
692
+ }
693
+ check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
694
+ assert_not_same(thing, thing)
695
+ }
696
+ check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
697
+ assert_not_same(thing, thing, "message")
698
+ }
699
+ end
700
+
701
+ def test_assert_not_equal
702
+ check_nothing_fails {
703
+ assert_not_equal("string1", "string2")
704
+ }
705
+ check_nothing_fails {
706
+ assert_not_equal("string1", "string2", "message")
707
+ }
708
+ check_fails(%Q{<"string"> expected to be != to\n<"string">.}) {
709
+ assert_not_equal("string", "string")
710
+ }
711
+ check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) {
712
+ assert_not_equal("string", "string", "message")
713
+ }
714
+ end
715
+
716
+ def test_assert_no_match
717
+ check_nothing_fails{assert_no_match(/sling/, "string")}
718
+ check_nothing_fails{assert_no_match(/sling/, "string", "message")}
719
+ check_fails(%Q{The first argument to assert_no_match should be a Regexp.\n<"asdf"> expected to be an instance of\n<Regexp> but was\n<String>.}) do
720
+ assert_no_match("asdf", "asdf")
721
+ end
722
+ check_fails(%Q{</string/> expected to not match\n<"string">.}) do
723
+ assert_no_match(/string/, "string")
724
+ end
725
+ check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) do
726
+ assert_no_match(/string/, "string", "message")
727
+ end
728
+ end
729
+
730
+ def test_assert_throw
731
+ check_nothing_fails do
732
+ assert_throw(:thing, "message") do
733
+ throw :thing
734
+ end
735
+ end
736
+
737
+ tag = :thing2
738
+ check_fails("message.\n" +
739
+ "<:thing> expected to be thrown but\n" +
740
+ "<#{inspect_tag(tag)}> was thrown.") do
741
+ assert_throw(:thing, "message") do
742
+ throw :thing2
743
+ end
744
+ end
745
+ check_fails("message.\n" +
746
+ "<:thing> should have been thrown.") do
747
+ assert_throw(:thing, "message") do
748
+ 1 + 1
749
+ end
750
+ end
751
+ end
752
+
753
+ def test_assert_nothing_thrown
754
+ check_nothing_fails do
755
+ assert_nothing_thrown("message") do
756
+ 1 + 1
757
+ end
758
+ end
759
+
760
+ tag = :thing
761
+ inspected = inspect_tag(tag)
762
+ check_fails("message.\n" +
763
+ "<#{inspected}> was thrown when nothing was expected.") do
764
+ assert_nothing_thrown("message") do
765
+ throw tag
766
+ end
767
+ end
768
+ end
769
+
770
+ def test_assert_operator
771
+ check_nothing_fails {
772
+ assert_operator("thing", :==, "thing", "message")
773
+ }
774
+ check_fails(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do
775
+ assert_operator("thing", 0.15, "thing")
776
+ end
777
+ check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) {
778
+ assert_operator("thing1", :==, "thing2", "message")
779
+ }
780
+ end
781
+
782
+ def test_assert_respond_to
783
+ check_nothing_fails {
784
+ assert_respond_to("thing", :to_s, "message")
785
+ }
786
+ check_nothing_fails {
787
+ assert_respond_to("thing", "to_s", "message")
788
+ }
789
+ check_fails("<0.15>.kind_of?(Symbol) or\n" +
790
+ "<0.15>.respond_to?(:to_str) expected") {
791
+ assert_respond_to("thing", 0.15)
792
+ }
793
+ check_fails("message.\n" +
794
+ "<:symbol>.respond_to?(:non_existent) expected\n" +
795
+ "(Class: <Symbol>)") {
796
+ assert_respond_to(:symbol, :non_existent, "message")
797
+ }
798
+ end
799
+
800
+ def test_assert_in_delta
801
+ check_nothing_fails {
802
+ assert_in_delta(1.4, 1.4, 0)
803
+ }
804
+ check_nothing_fails {
805
+ assert_in_delta(0.5, 0.4, 0.1, "message")
806
+ }
807
+ check_nothing_fails {
808
+ float_thing = Object.new
809
+ def float_thing.to_f
810
+ 0.2
811
+ end
812
+ assert_in_delta(0.1, float_thing, 0.1)
813
+ }
814
+ check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other.") {
815
+ assert_in_delta(0.5, 0.4, 0.05, "message")
816
+ }
817
+ object = Object.new
818
+ check_fails("The arguments must respond to to_f; " +
819
+ "the first float did not.\n" +
820
+ "<#{object.inspect}>.respond_to?(:to_f) expected\n" +
821
+ "(Class: <Object>)") {
822
+ assert_in_delta(object, 0.4, 0.1)
823
+ }
824
+ check_fails("The delta should not be negative.\n" +
825
+ "<-0.1> expected to be\n>=\n<0.0>.") {
826
+ assert_in_delta(0.5, 0.4, -0.1, "message")
827
+ }
828
+ end
829
+
830
+ def test_assert_send
831
+ object = Object.new
832
+ class << object
833
+ private
834
+ def return_argument(argument, bogus)
835
+ return argument
836
+ end
837
+ end
838
+ check_nothing_fails {
839
+ assert_send([object, :return_argument, true, "bogus"], "message")
840
+ }
841
+ check_fails(%r{\Amessage\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value.\Z}) {
842
+ assert_send([object, :return_argument, false, "bogus"], "message")
843
+ }
844
+ end
845
+
846
+ def test_condition_invariant
847
+ object = Object.new
848
+ def object.inspect
849
+ @changed = true
850
+ end
851
+ def object.==(other)
852
+ @changed ||= false
853
+ return (!@changed)
854
+ end
855
+ check_nothing_fails do
856
+ assert_equal(object, object, "message")
857
+ end
858
+ end
859
+
860
+ def test_assert_boolean
861
+ check_nothing_fails do
862
+ assert_boolean(true)
863
+ end
864
+ check_nothing_fails do
865
+ assert_boolean(false)
866
+ end
867
+
868
+ check_fails("<true> or <false> expected but was\n<1>") do
869
+ assert_boolean(1)
870
+ end
871
+
872
+ check_fails("<true> or <false> expected but was\n<nil>") do
873
+ assert_boolean(nil)
874
+ end
875
+
876
+ check_fails("message.\n<true> or <false> expected but was\n<\"XXX\">") do
877
+ assert_boolean("XXX", "message")
878
+ end
879
+ end
880
+
881
+ def test_assert_true
882
+ check_nothing_fails do
883
+ assert_true(true)
884
+ end
885
+
886
+ check_fails("<true> expected but was\n<false>") do
887
+ assert_true(false)
888
+ end
889
+
890
+ check_fails("<true> expected but was\n<1>") do
891
+ assert_true(1)
892
+ end
893
+
894
+ check_fails("message.\n<true> expected but was\n<nil>") do
895
+ assert_true(nil, "message")
896
+ end
897
+ end
898
+
899
+ def test_assert_false
900
+ check_nothing_fails do
901
+ assert_false(false)
902
+ end
903
+
904
+ check_fails("<false> expected but was\n<true>") do
905
+ assert_false(true)
906
+ end
907
+
908
+ check_fails("<false> expected but was\n<nil>") do
909
+ assert_false(nil)
910
+ end
911
+
912
+ check_fails("message.\n<false> expected but was\n<:false>") do
913
+ assert_false(:false, "message")
914
+ end
915
+ end
916
+
917
+ def test_assert_compare
918
+ check_nothing_fails do
919
+ assert_compare(1.4, "<", 10.0)
920
+ end
921
+
922
+ check_nothing_fails do
923
+ assert_compare(2, "<=", 2)
924
+ end
925
+
926
+ check_nothing_fails do
927
+ assert_compare(14, ">=", 10.0)
928
+ end
929
+
930
+ check_nothing_fails do
931
+ assert_compare(14, ">", 13.9)
932
+ end
933
+
934
+ expected_message = <<-EOM
935
+ <15> < <10> should be true
936
+ <15> expected less than
937
+ <10>.
938
+ EOM
939
+ check_fails(expected_message.chomp) do
940
+ assert_compare(15, "<", 10)
941
+ end
942
+
943
+ expected_message = <<-EOM
944
+ <15> <= <10> should be true
945
+ <15> expected less than or equal to
946
+ <10>.
947
+ EOM
948
+ check_fails(expected_message.chomp) do
949
+ assert_compare(15, "<=", 10)
950
+ end
951
+
952
+ expected_message = <<-EOM
953
+ <10> > <15> should be true
954
+ <10> expected greater than
955
+ <15>.
956
+ EOM
957
+ check_fails(expected_message.chomp) do
958
+ assert_compare(10, ">", 15)
959
+ end
960
+
961
+ expected_message = <<-EOM
962
+ <10> >= <15> should be true
963
+ <10> expected greater than or equal to
964
+ <15>.
965
+ EOM
966
+ check_fails(expected_message.chomp) do
967
+ assert_compare(10, ">=", 15)
968
+ end
969
+ end
970
+
971
+ def test_assert_fail_assertion
972
+ check_nothing_fails do
973
+ assert_fail_assertion do
974
+ flunk
975
+ end
976
+ end
977
+
978
+ check_fails("Failed assertion was expected.") do
979
+ assert_fail_assertion do
980
+ end
981
+ end
982
+ end
983
+
984
+ def test_assert_raise_message
985
+ check_nothing_fails do
986
+ assert_raise_message("Raise!") do
987
+ raise "Raise!"
988
+ end
989
+ end
990
+
991
+ check_nothing_fails do
992
+ assert_raise_message("Raise!") do
993
+ raise Exception, "Raise!"
994
+ end
995
+ end
996
+
997
+ check_nothing_fails do
998
+ assert_raise_message(/raise/i) do
999
+ raise "Raise!"
1000
+ end
1001
+ end
1002
+
1003
+ expected_message = <<-EOM
1004
+ <"Expected message"> exception message expected but was
1005
+ <"Actual message">.
1006
+ EOM
1007
+ check_fails(expected_message.chomp) do
1008
+ assert_raise_message("Expected message") do
1009
+ raise "Actual message"
1010
+ end
1011
+ end
1012
+
1013
+ expected_message = <<-EOM
1014
+ <"Expected message"> exception message expected but none was thrown.
1015
+ EOM
1016
+ check_fails(expected_message.chomp) do
1017
+ assert_raise_message("Expected message") do
1018
+ end
1019
+ end
1020
+ end
1021
+
1022
+ def test_assert_raise_kind_of
1023
+ check_nothing_fails(true) do
1024
+ assert_raise_kind_of(SystemCallError) do
1025
+ raise Errno::EACCES
1026
+ end
1027
+ end
1028
+
1029
+ expected_message = <<-EOM
1030
+ <SystemCallError> family exception expected but was
1031
+ Class: <RuntimeError>
1032
+ Message: <"XXX">
1033
+ ---Backtrace---
1034
+ EOM
1035
+ check_fails(/\A#{Regexp.escape(expected_message)}(?m).+\z/) do
1036
+ assert_raise_kind_of(SystemCallError) do
1037
+ raise RuntimeError, "XXX"
1038
+ end
1039
+ end
1040
+ end
1041
+
1042
+ def test_assert_const_defined
1043
+ check_nothing_fails do
1044
+ assert_const_defined(Test, :Unit)
1045
+ end
1046
+
1047
+ check_nothing_fails do
1048
+ assert_const_defined(Test, "Unit")
1049
+ end
1050
+
1051
+ check_fails("<Test>.const_defined?(<:Nonexistence>) expected.") do
1052
+ assert_const_defined(Test, :Nonexistence)
1053
+ end
1054
+ end
1055
+
1056
+ def test_assert_not_const_defined
1057
+ check_nothing_fails do
1058
+ assert_not_const_defined(Test, :Nonexistence)
1059
+ end
1060
+
1061
+ check_fails("!<Test>.const_defined?(<:Unit>) expected.") do
1062
+ assert_not_const_defined(Test, :Unit)
1063
+ end
1064
+
1065
+ check_fails("!<Test>.const_defined?(<\"Unit\">) expected.") do
1066
+ assert_not_const_defined(Test, "Unit")
1067
+ end
1068
+ end
1069
+
1070
+ def test_assert_predicate
1071
+ check_nothing_fails do
1072
+ assert_predicate([], :empty?)
1073
+ end
1074
+
1075
+ check_fails("<[1]>.empty? is true value expected but was\n<false>") do
1076
+ assert_predicate([1], :empty?)
1077
+ end
1078
+
1079
+ check_fails("<[1]>.respond_to?(:nonexistent?) expected\n" +
1080
+ "(Class: <Array>)") do
1081
+ assert_predicate([1], :nonexistent?)
1082
+ end
1083
+ end
1084
+
1085
+ def test_assert_not_predicate
1086
+ check_nothing_fails do
1087
+ assert_not_predicate([1], :empty?)
1088
+ end
1089
+
1090
+ check_fails("<[]>.empty? is false value expected but was\n<true>") do
1091
+ assert_not_predicate([], :empty?)
1092
+ end
1093
+
1094
+ check_fails("<[]>.respond_to?(:nonexistent?) expected\n" +
1095
+ "(Class: <Array>)") do
1096
+ assert_not_predicate([], :nonexistent?)
1097
+ end
1098
+ end
1099
+
1100
+ def test_assert_alias_method
1101
+ object = Object.new
1102
+ class << object
1103
+ def original_method
1104
+ end
1105
+ alias_method :alias_method, :original_method
1106
+
1107
+ def other
1108
+ end
1109
+ end
1110
+
1111
+ check_nothing_fails do
1112
+ assert_alias_method(object, :alias_method, :original_method)
1113
+ end
1114
+
1115
+ check_nothing_fails do
1116
+ assert_alias_method(object, :original_method, :alias_method)
1117
+ end
1118
+
1119
+ check_fails("<#{object.method(:other).inspect}> is alias of\n" +
1120
+ "<#{object.method(:original_method).inspect}> expected") do
1121
+ assert_alias_method(object, :other, :original_method)
1122
+ end
1123
+
1124
+ check_fails("<#{object.inspect}>.nonexistent doesn't exist\n" +
1125
+ "(Class: <Object>)") do
1126
+ assert_alias_method(object, :nonexistent, :original_method)
1127
+ end
1128
+
1129
+ check_fails("<#{object.inspect}>.nonexistent doesn't exist\n" +
1130
+ "(Class: <Object>)") do
1131
+ assert_alias_method(object, :alias_method, :nonexistent)
1132
+ end
1133
+ end
1134
+
1135
+ private
1136
+ def add_failure(message, location=caller, options=nil)
1137
+ unless @catch_assertions
1138
+ super
1139
+ end
1140
+ end
1141
+
1142
+ def add_assertion
1143
+ if @catch_assertions
1144
+ @actual_assertion_count += 1
1145
+ else
1146
+ super
1147
+ end
1148
+ end
1149
+ end
1150
+ end
1151
+ end