rroonga 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. data/NEWS.ja.rdoc +46 -0
  2. data/NEWS.rdoc +46 -0
  3. data/README.ja.rdoc +2 -2
  4. data/README.rdoc +2 -3
  5. data/Rakefile +148 -11
  6. data/example/bookmark.rb +94 -91
  7. data/ext/groonga/extconf.rb +25 -25
  8. data/ext/groonga/groonga.def +2 -0
  9. data/ext/groonga/mkmf.log +7 -7
  10. data/ext/groonga/rb-grn-column.c +66 -0
  11. data/ext/groonga/rb-grn-context.c +92 -5
  12. data/ext/groonga/rb-grn-database.c +24 -1
  13. data/ext/groonga/rb-grn-exception.c +15 -0
  14. data/ext/groonga/rb-grn-hash.c +2 -2
  15. data/ext/groonga/rb-grn-index-column.c +8 -4
  16. data/ext/groonga/rb-grn-object.c +4 -2
  17. data/ext/groonga/rb-grn-patricia-trie.c +2 -2
  18. data/ext/groonga/rb-grn-snippet.c +1 -0
  19. data/ext/groonga/rb-grn-table-key-support.c +22 -19
  20. data/ext/groonga/rb-grn-table.c +63 -5
  21. data/ext/groonga/rb-grn.h +1 -1
  22. data/ext/groonga/rb-groonga.c +1 -0
  23. data/extconf.rb +1 -1
  24. data/html/developer.html +8 -2
  25. data/html/favicon.ico +0 -0
  26. data/html/favicon.svg +591 -0
  27. data/html/index.html +71 -8
  28. data/html/logo.svg +612 -0
  29. data/html/ranguba.css +92 -7
  30. data/html/readme.svg +256 -0
  31. data/lib/groonga/expression-builder.rb +8 -4
  32. data/lib/groonga/record.rb +77 -7
  33. data/lib/groonga/schema.rb +429 -100
  34. data/rroonga-build.rb +1 -1
  35. data/test/run-test.rb +1 -1
  36. data/test/test-array.rb +4 -0
  37. data/test/test-context.rb +8 -0
  38. data/test/test-database.rb +8 -1
  39. data/test/test-expression-builder.rb +14 -0
  40. data/test/test-fix-size-column.rb +4 -0
  41. data/test/test-hash.rb +10 -4
  42. data/test/test-index-column.rb +11 -0
  43. data/test/test-patricia-trie.rb +7 -1
  44. data/test/test-record.rb +14 -0
  45. data/test/test-remote.rb +3 -2
  46. data/test/test-schema-create-table.rb +32 -2
  47. data/test/test-schema.rb +108 -11
  48. data/test/test-table-select-normalize.rb +7 -3
  49. data/test/test-table-select.rb +12 -0
  50. data/test/test-table.rb +7 -0
  51. data/test/test-variable-size-column.rb +4 -0
  52. data/test-unit/Rakefile +40 -0
  53. data/test-unit/TODO +5 -0
  54. data/test-unit/bin/testrb +5 -0
  55. data/test-unit/html/classic.html +15 -0
  56. data/test-unit/html/index.html +25 -0
  57. data/test-unit/html/index.html.ja +27 -0
  58. data/test-unit/lib/test/unit/assertionfailederror.rb +25 -0
  59. data/test-unit/lib/test/unit/assertions.rb +1230 -0
  60. data/test-unit/lib/test/unit/attribute.rb +125 -0
  61. data/test-unit/lib/test/unit/autorunner.rb +360 -0
  62. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  63. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  64. data/test-unit/lib/test/unit/collector/load.rb +144 -0
  65. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  66. data/test-unit/lib/test/unit/collector.rb +36 -0
  67. data/test-unit/lib/test/unit/color-scheme.rb +102 -0
  68. data/test-unit/lib/test/unit/color.rb +96 -0
  69. data/test-unit/lib/test/unit/diff.rb +724 -0
  70. data/test-unit/lib/test/unit/error.rb +130 -0
  71. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  72. data/test-unit/lib/test/unit/failure.rb +136 -0
  73. data/test-unit/lib/test/unit/fixture.rb +176 -0
  74. data/test-unit/lib/test/unit/notification.rb +129 -0
  75. data/test-unit/lib/test/unit/omission.rb +191 -0
  76. data/test-unit/lib/test/unit/pending.rb +150 -0
  77. data/test-unit/lib/test/unit/priority.rb +180 -0
  78. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  79. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  80. data/test-unit/lib/test/unit/runner/tap.rb +8 -0
  81. data/test-unit/lib/test/unit/testcase.rb +476 -0
  82. data/test-unit/lib/test/unit/testresult.rb +89 -0
  83. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  84. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  85. data/test-unit/lib/test/unit/ui/console/testrunner.rb +466 -0
  86. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +63 -0
  87. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +92 -0
  88. data/test-unit/lib/test/unit/ui/testrunner.rb +28 -0
  89. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  90. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  91. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  92. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  93. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  94. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  95. data/test-unit/lib/test/unit/version.rb +7 -0
  96. data/test-unit/lib/test/unit.rb +323 -0
  97. data/test-unit/sample/adder.rb +13 -0
  98. data/test-unit/sample/subtracter.rb +12 -0
  99. data/test-unit/sample/test_adder.rb +20 -0
  100. data/test-unit/sample/test_subtracter.rb +20 -0
  101. data/test-unit/sample/test_user.rb +23 -0
  102. data/test-unit/test/collector/test-descendant.rb +133 -0
  103. data/test-unit/test/collector/test-load.rb +442 -0
  104. data/test-unit/test/collector/test_dir.rb +406 -0
  105. data/test-unit/test/collector/test_objectspace.rb +100 -0
  106. data/test-unit/test/run-test.rb +15 -0
  107. data/test-unit/test/test-attribute.rb +86 -0
  108. data/test-unit/test/test-color-scheme.rb +67 -0
  109. data/test-unit/test/test-color.rb +47 -0
  110. data/test-unit/test/test-diff.rb +518 -0
  111. data/test-unit/test/test-emacs-runner.rb +60 -0
  112. data/test-unit/test/test-fixture.rb +287 -0
  113. data/test-unit/test/test-notification.rb +33 -0
  114. data/test-unit/test/test-omission.rb +81 -0
  115. data/test-unit/test/test-pending.rb +70 -0
  116. data/test-unit/test/test-priority.rb +119 -0
  117. data/test-unit/test/test-testcase.rb +544 -0
  118. data/test-unit/test/test_assertions.rb +1151 -0
  119. data/test-unit/test/test_error.rb +26 -0
  120. data/test-unit/test/test_failure.rb +33 -0
  121. data/test-unit/test/test_testresult.rb +113 -0
  122. data/test-unit/test/test_testsuite.rb +129 -0
  123. data/test-unit/test/testunit-test-util.rb +14 -0
  124. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  125. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  126. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  127. data/test-unit/test/util/test_observable.rb +102 -0
  128. data/test-unit/test/util/test_procwrapper.rb +36 -0
  129. data/text/{TUTORIAL.ja.rdoc → tutorial.ja.rdoc} +165 -126
  130. metadata +106 -16
  131. data/html/favicon.xcf +0 -0
  132. data/html/logo.xcf +0 -0
  133. data/license/GPL +0 -340
  134. data/license/RUBY +0 -59
  135. data/pkg-config.rb +0 -333
@@ -0,0 +1,1230 @@
1
+ # Author:: Nathaniel Talbott.
2
+ # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
3
+ # Copyright (c) 2009 Kouhei Sutou.
4
+ # License:: Ruby license.
5
+
6
+ require 'test/unit/assertionfailederror'
7
+ require 'test/unit/util/backtracefilter'
8
+ require 'test/unit/util/method-owner-finder'
9
+ require 'test/unit/diff'
10
+
11
+ module Test
12
+ module Unit
13
+
14
+ ##
15
+ # Test::Unit::Assertions contains the standard Test::Unit assertions.
16
+ # Assertions is included in Test::Unit::TestCase.
17
+ #
18
+ # To include it in your own code and use its functionality, you simply
19
+ # need to rescue Test::Unit::AssertionFailedError. Additionally you may
20
+ # override add_assertion to get notified whenever an assertion is made.
21
+ #
22
+ # Notes:
23
+ # * The message to each assertion, if given, will be propagated with the
24
+ # failure.
25
+ # * It is easy to add your own assertions based on assert_block().
26
+ #
27
+ # = Example Custom Assertion
28
+ #
29
+ # def deny(boolean, message = nil)
30
+ # message = build_message message, '<?> is not false or nil.', boolean
31
+ # assert_block message do
32
+ # not boolean
33
+ # end
34
+ # end
35
+
36
+ module Assertions
37
+
38
+ ##
39
+ # The assertion upon which all other assertions are based. Passes if the
40
+ # block yields true.
41
+ #
42
+ # Example:
43
+ # assert_block "Couldn't do the thing" do
44
+ # do_the_thing
45
+ # end
46
+
47
+ public
48
+ def assert_block(message="assert_block failed.") # :yields:
49
+ _wrap_assertion do
50
+ if (! yield)
51
+ raise AssertionFailedError.new(message.to_s)
52
+ end
53
+ end
54
+ end
55
+
56
+ ##
57
+ # Asserts that +boolean+ is not false or nil.
58
+ #
59
+ # Example:
60
+ # assert [1, 2].include?(5)
61
+
62
+ public
63
+ def assert(boolean, message=nil)
64
+ _wrap_assertion do
65
+ assert_block("assert should not be called with a block.") { !block_given? }
66
+ assert_block(build_message(message, "<?> is not true.", boolean)) { boolean }
67
+ end
68
+ end
69
+
70
+ ##
71
+ # Passes if +expected+ == +actual.
72
+ #
73
+ # Note that the ordering of arguments is important, since a helpful
74
+ # error message is generated when this one fails that tells you the
75
+ # values of expected and actual.
76
+ #
77
+ # Example:
78
+ # assert_equal 'MY STRING', 'my string'.upcase
79
+
80
+ public
81
+ def assert_equal(expected, actual, message=nil)
82
+ diff = AssertionMessage.delayed_diff(expected, actual)
83
+ full_message = build_message(message, <<EOT, expected, actual, diff)
84
+ <?> expected but was
85
+ <?>.?
86
+ EOT
87
+ begin
88
+ assert_block(full_message) { expected == actual }
89
+ rescue AssertionFailedError => failure
90
+ failure.expected = expected
91
+ failure.actual = actual
92
+ failure.inspected_expected = AssertionMessage.convert(expected)
93
+ failure.inspected_actual = AssertionMessage.convert(actual)
94
+ failure.user_message = message
95
+ raise
96
+ end
97
+ end
98
+
99
+ ##
100
+ # Passes if the block raises one of the expected
101
+ # exceptions. When an expected exception is an Exception
102
+ # object, passes if expected_exception == actual_exception.
103
+ #
104
+ # Example:
105
+ # assert_raise(RuntimeError, LoadError) do
106
+ # raise 'Boom!!!'
107
+ # end # -> pass
108
+ #
109
+ # assert_raise do
110
+ # raise Exception, 'Any exception should be raised!!!'
111
+ # end # -> pass
112
+ #
113
+ # assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
114
+ # assert_raise(MyError.new("XXX")) {raise "XXX"} # -> fail
115
+ # assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
116
+ public
117
+ def assert_raise(*args, &block)
118
+ assert_expected_exception = Proc.new do |*_args|
119
+ message, assert_exception_helper, actual_exception = _args
120
+ expected = assert_exception_helper.expected_exceptions
121
+ full_message = build_message(message,
122
+ "<?> exception expected but was\n?",
123
+ expected, actual_exception)
124
+ assert_block(full_message) do
125
+ expected == [] or assert_exception_helper.expected?(actual_exception)
126
+ end
127
+ end
128
+ _assert_raise(assert_expected_exception, *args, &block)
129
+ end
130
+
131
+ ##
132
+ # Alias of assert_raise.
133
+ #
134
+ # Will be deprecated in 1.9, and removed in 2.0.
135
+
136
+ public
137
+ def assert_raises(*args, &block)
138
+ assert_raise(*args, &block)
139
+ end
140
+
141
+ ##
142
+ # Passes if the block raises one of the given
143
+ # exceptions or sub exceptions of the given exceptions.
144
+ #
145
+ # Example:
146
+ # assert_raise_kind_of(SystemCallError) do
147
+ # raise Errno::EACCES
148
+ # end
149
+ def assert_raise_kind_of(*args, &block)
150
+ assert_expected_exception = Proc.new do |*_args|
151
+ message, assert_exception_helper, actual_exception = _args
152
+ expected = assert_exception_helper.expected_exceptions
153
+ full_message = build_message(message,
154
+ "<?> family exception expected " +
155
+ "but was\n?",
156
+ expected, actual_exception)
157
+ assert_block(full_message) do
158
+ assert_exception_helper.expected?(actual_exception, :kind_of?)
159
+ end
160
+ end
161
+ _assert_raise(assert_expected_exception, *args, &block)
162
+ end
163
+
164
+
165
+ ##
166
+ # Passes if +object+.instance_of?(+klass+). When +klass+ is
167
+ # an array of classes, it passes if any class
168
+ # satisfies +object.instance_of?(class).
169
+ #
170
+ # Example:
171
+ # assert_instance_of(String, 'foo') # -> pass
172
+ # assert_instance_of([Fixnum, NilClass], 100) # -> pass
173
+ # assert_instance_of([Numeric, NilClass], 100) # -> fail
174
+
175
+ public
176
+ def assert_instance_of(klass, object, message="")
177
+ _wrap_assertion do
178
+ klasses = nil
179
+ klasses = klass if klass.is_a?(Array)
180
+ assert_block("The first parameter to assert_instance_of should be " +
181
+ "a Class or an Array of Class.") do
182
+ if klasses
183
+ klasses.all? {|k| k.is_a?(Class)}
184
+ else
185
+ klass.is_a?(Class)
186
+ end
187
+ end
188
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
189
+ "<#{value}>"
190
+ end
191
+ full_message = build_message(message, <<EOT, object, klass_message, object.class)
192
+ <?> expected to be an instance of
193
+ ? but was
194
+ <?>.
195
+ EOT
196
+ assert_block(full_message) do
197
+ if klasses
198
+ klasses.any? {|k| object.instance_of?(k)}
199
+ else
200
+ object.instance_of?(klass)
201
+ end
202
+ end
203
+ end
204
+ end
205
+
206
+ ##
207
+ # Passes if +object+ is nil.
208
+ #
209
+ # Example:
210
+ # assert_nil [1, 2].uniq!
211
+
212
+ public
213
+ def assert_nil(object, message="")
214
+ full_message = build_message(message, <<EOT, object)
215
+ <?> expected to be nil.
216
+ EOT
217
+ assert_block(full_message) { object.nil? }
218
+ end
219
+
220
+ ##
221
+ # Passes if +object+.kind_of?(+klass+). When +klass+ is
222
+ # an array of classes or modules, it passes if any
223
+ # class or module satisfies +object.kind_of?(class_or_module).
224
+ #
225
+ # Example:
226
+ # assert_kind_of(Object, 'foo') # -> pass
227
+ # assert_kind_of([Fixnum, NilClass], 100) # -> pass
228
+ # assert_kind_of([Fixnum, NilClass], "string") # -> fail
229
+
230
+ public
231
+ def assert_kind_of(klass, object, message="")
232
+ _wrap_assertion do
233
+ klasses = nil
234
+ klasses = klass if klass.is_a?(Array)
235
+ assert_block("The first parameter to assert_kind_of should be " +
236
+ "a kind_of Module or an Array of a kind_of Module.") do
237
+ if klasses
238
+ klasses.all? {|k| k.kind_of?(Module)}
239
+ else
240
+ klass.kind_of?(Module)
241
+ end
242
+ end
243
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
244
+ "<#{value}>"
245
+ end
246
+ full_message = build_message(message,
247
+ "<?> expected to be kind_of\\?\n" +
248
+ "? but was\n" +
249
+ "<?>.",
250
+ object,
251
+ klass_message,
252
+ object.class)
253
+ assert_block(full_message) do
254
+ if klasses
255
+ klasses.any? {|k| object.kind_of?(k)}
256
+ else
257
+ object.kind_of?(klass)
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ ##
264
+ # Passes if +object+ .respond_to? +method+
265
+ #
266
+ # Example:
267
+ # assert_respond_to 'bugbear', :slice
268
+
269
+ public
270
+ def assert_respond_to(object, method, message="")
271
+ _wrap_assertion do
272
+ full_message = build_message(message,
273
+ "<?>.kind_of\\?(Symbol) or\n" +
274
+ "<?>.respond_to\\?(:to_str) expected",
275
+ method, method)
276
+ assert_block(full_message) do
277
+ method.kind_of?(Symbol) or method.respond_to?(:to_str)
278
+ end
279
+ full_message = build_message(message,
280
+ "<?>.respond_to\\?(?) expected\n" +
281
+ "(Class: <?>)",
282
+ object, method, object.class)
283
+ assert_block(full_message) {object.respond_to?(method)}
284
+ end
285
+ end
286
+
287
+ ##
288
+ # Passes if +string+ =~ +pattern+.
289
+ #
290
+ # Example:
291
+ # assert_match(/\d+/, 'five, 6, seven')
292
+
293
+ public
294
+ def assert_match(pattern, string, message="")
295
+ _wrap_assertion do
296
+ pattern = case(pattern)
297
+ when String
298
+ Regexp.new(Regexp.escape(pattern))
299
+ else
300
+ pattern
301
+ end
302
+ full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
303
+ assert_block(full_message) { string =~ pattern }
304
+ end
305
+ end
306
+
307
+ ##
308
+ # Passes if +actual+ .equal? +expected+ (i.e. they are the same
309
+ # instance).
310
+ #
311
+ # Example:
312
+ # o = Object.new
313
+ # assert_same o, o
314
+
315
+ public
316
+ def assert_same(expected, actual, message="")
317
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
318
+ <?>
319
+ with id <?> expected to be equal\\? to
320
+ <?>
321
+ with id <?>.
322
+ EOT
323
+ assert_block(full_message) { actual.equal?(expected) }
324
+ end
325
+
326
+ ##
327
+ # Compares the +object1+ with +object2+ using +operator+.
328
+ #
329
+ # Passes if object1.__send__(operator, object2) is true.
330
+ #
331
+ # Example:
332
+ # assert_operator 5, :>=, 4
333
+
334
+ public
335
+ def assert_operator(object1, operator, object2, message="")
336
+ _wrap_assertion do
337
+ full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
338
+ assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
339
+ full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
340
+ <?> expected to be
341
+ ?
342
+ <?>.
343
+ EOT
344
+ assert_block(full_message) { object1.__send__(operator, object2) }
345
+ end
346
+ end
347
+
348
+ ##
349
+ # Passes if block does not raise an exception.
350
+ #
351
+ # Example:
352
+ # assert_nothing_raised do
353
+ # [1, 2].uniq
354
+ # end
355
+
356
+ public
357
+ def assert_nothing_raised(*args)
358
+ _wrap_assertion do
359
+ if args.last.is_a?(String)
360
+ message = args.pop
361
+ else
362
+ message = ""
363
+ end
364
+
365
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
366
+ begin
367
+ yield
368
+ rescue Exception => e
369
+ if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
370
+ assert_exception_helper.expected?(e))
371
+ failure_message = build_message(message, "Exception raised:\n?", e)
372
+ assert_block(failure_message) {false}
373
+ else
374
+ raise
375
+ end
376
+ end
377
+ nil
378
+ end
379
+ end
380
+
381
+ ##
382
+ # Flunk always fails.
383
+ #
384
+ # Example:
385
+ # flunk 'Not done testing yet.'
386
+
387
+ public
388
+ def flunk(message="Flunked")
389
+ assert_block(build_message(message)){false}
390
+ end
391
+
392
+ ##
393
+ # Passes if ! +actual+ .equal? +expected+
394
+ #
395
+ # Example:
396
+ # assert_not_same Object.new, Object.new
397
+
398
+ public
399
+ def assert_not_same(expected, actual, message="")
400
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
401
+ <?>
402
+ with id <?> expected to not be equal\\? to
403
+ <?>
404
+ with id <?>.
405
+ EOT
406
+ assert_block(full_message) { !actual.equal?(expected) }
407
+ end
408
+
409
+ ##
410
+ # Passes if +expected+ != +actual+
411
+ #
412
+ # Example:
413
+ # assert_not_equal 'some string', 5
414
+
415
+ public
416
+ def assert_not_equal(expected, actual, message="")
417
+ full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
418
+ assert_block(full_message) { expected != actual }
419
+ end
420
+
421
+ ##
422
+ # Passes if ! +object+ .nil?
423
+ #
424
+ # Example:
425
+ # assert_not_nil '1 two 3'.sub!(/two/, '2')
426
+
427
+ public
428
+ def assert_not_nil(object, message="")
429
+ full_message = build_message(message, "<?> expected to not be nil.", object)
430
+ assert_block(full_message){!object.nil?}
431
+ end
432
+
433
+ ##
434
+ # Passes if +regexp+ !~ +string+
435
+ #
436
+ # Example:
437
+ # assert_no_match(/two/, 'one 2 three')
438
+
439
+ public
440
+ def assert_no_match(regexp, string, message="")
441
+ _wrap_assertion do
442
+ assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
443
+ full_message = build_message(message, "<?> expected to not match\n<?>.", regexp, string)
444
+ assert_block(full_message) { regexp !~ string }
445
+ end
446
+ end
447
+
448
+ UncaughtThrow = {
449
+ NameError => /^uncaught throw \`(.+)\'$/,
450
+ ArgumentError => /^uncaught throw (.+)$/,
451
+ ThreadError => /^uncaught throw \`(.+)\' in thread /
452
+ } #`
453
+
454
+ ##
455
+ # Passes if the block throws +expected_object+
456
+ #
457
+ # Example:
458
+ # assert_throw(:done) do
459
+ # throw(:done)
460
+ # end
461
+
462
+ public
463
+ def assert_throw(expected_object, message="", &proc)
464
+ _wrap_assertion do
465
+ begin
466
+ catch([]) {}
467
+ rescue TypeError
468
+ assert_instance_of(Symbol, expected_object,
469
+ "assert_throws expects the symbol that should be thrown for its first argument")
470
+ end
471
+ assert_block("Should have passed a block to assert_throw.") do
472
+ block_given?
473
+ end
474
+ caught = true
475
+ begin
476
+ catch(expected_object) do
477
+ proc.call
478
+ caught = false
479
+ end
480
+ full_message = build_message(message,
481
+ "<?> should have been thrown.",
482
+ expected_object)
483
+ assert_block(full_message) {caught}
484
+ rescue NameError, ArgumentError, ThreadError => error
485
+ raise unless UncaughtThrow[error.class] =~ error.message
486
+ tag = $1
487
+ tag = tag[1..-1].intern if tag[0, 1] == ":"
488
+ full_message = build_message(message,
489
+ "<?> expected to be thrown but\n" +
490
+ "<?> was thrown.",
491
+ expected_object, tag)
492
+ flunk(full_message)
493
+ end
494
+ end
495
+ end
496
+
497
+ ##
498
+ # Alias of assert_throw.
499
+ #
500
+ # Will be deprecated in 1.9, and removed in 2.0.
501
+ def assert_throws(*args, &block)
502
+ assert_throw(*args, &block)
503
+ end
504
+
505
+ ##
506
+ # Passes if block does not throw anything.
507
+ #
508
+ # Example:
509
+ # assert_nothing_thrown do
510
+ # [1, 2].uniq
511
+ # end
512
+
513
+ public
514
+ def assert_nothing_thrown(message="", &proc)
515
+ _wrap_assertion do
516
+ assert(block_given?, "Should have passed a block to assert_nothing_thrown")
517
+ begin
518
+ proc.call
519
+ rescue NameError, ArgumentError, ThreadError => error
520
+ raise unless UncaughtThrow[error.class] =~ error.message
521
+ tag = $1
522
+ tag = tag[1..-1].intern if tag[0, 1] == ":"
523
+ full_message = build_message(message,
524
+ "<?> was thrown when nothing was expected",
525
+ tag)
526
+ flunk(full_message)
527
+ end
528
+ assert(true, "Expected nothing to be thrown")
529
+ end
530
+ end
531
+
532
+ ##
533
+ # Passes if +expected_float+ and +actual_float+ are equal
534
+ # within +delta+ tolerance.
535
+ #
536
+ # Example:
537
+ # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
538
+
539
+ public
540
+ def assert_in_delta(expected_float, actual_float, delta, message="")
541
+ _wrap_assertion do
542
+ {expected_float => "first float", actual_float => "second float", delta => "delta"}.each do |float, name|
543
+ assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not")
544
+ end
545
+ assert_operator(delta, :>=, 0.0, "The delta should not be negative")
546
+ full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
547
+ <?> and
548
+ <?> expected to be within
549
+ <?> of each other.
550
+ EOT
551
+ assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
552
+ end
553
+ end
554
+
555
+ ##
556
+ # Passes if the method send returns a true value.
557
+ #
558
+ # +send_array+ is composed of:
559
+ # * A receiver
560
+ # * A method
561
+ # * Arguments to the method
562
+ #
563
+ # Example:
564
+ # assert_send [[1, 2], :include?, 4]
565
+
566
+ public
567
+ def assert_send(send_array, message="")
568
+ _wrap_assertion do
569
+ assert_instance_of(Array, send_array, "assert_send requires an array of send information")
570
+ assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
571
+ full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
572
+ <?> expected to respond to
573
+ <?(?)> with a true value.
574
+ EOT
575
+ assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
576
+ end
577
+ end
578
+
579
+ ##
580
+ # Passes if +actual+ is a boolean value.
581
+ #
582
+ # Example:
583
+ # assert_boolean(true) # -> pass
584
+ # assert_boolean(nil) # -> fail
585
+ def assert_boolean(actual, message=nil)
586
+ _wrap_assertion do
587
+ assert_block(build_message(message,
588
+ "<true> or <false> expected but was\n<?>",
589
+ actual)) do
590
+ [true, false].include?(actual)
591
+ end
592
+ end
593
+ end
594
+
595
+ ##
596
+ # Passes if +actual+ is true.
597
+ #
598
+ # Example:
599
+ # assert_true(true) # -> pass
600
+ # assert_true(:true) # -> fail
601
+ def assert_true(actual, message=nil)
602
+ _wrap_assertion do
603
+ assert_block(build_message(message,
604
+ "<true> expected but was\n<?>",
605
+ actual)) do
606
+ actual == true
607
+ end
608
+ end
609
+ end
610
+
611
+ ##
612
+ # Passes if +actual+ is false.
613
+ #
614
+ # Example:
615
+ # assert_false(false) # -> pass
616
+ # assert_false(nil) # -> fail
617
+ def assert_false(actual, message=nil)
618
+ _wrap_assertion do
619
+ assert_block(build_message(message,
620
+ "<false> expected but was\n<?>",
621
+ actual)) do
622
+ actual == false
623
+ end
624
+ end
625
+ end
626
+
627
+ ##
628
+ # Passes if expression "+expected+ +operator+
629
+ # +actual+" is true.
630
+ #
631
+ # Example:
632
+ # assert_compare(1, "<", 10) # -> pass
633
+ # assert_compare(1, ">=", 10) # -> fail
634
+ def assert_compare(expected, operator, actual, message=nil)
635
+ _wrap_assertion do
636
+ assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
637
+ case operator.to_s
638
+ when "<"
639
+ operator_description = "less than"
640
+ when "<="
641
+ operator_description = "less than or equal to"
642
+ when ">"
643
+ operator_description = "greater than"
644
+ when ">="
645
+ operator_description = "greater than or equal to"
646
+ end
647
+ template = <<-EOT
648
+ <?> #{operator} <?> should be true
649
+ <?> expected #{operator_description}
650
+ <?>.
651
+ EOT
652
+ full_message = build_message(message, template,
653
+ expected, actual,
654
+ expected, actual)
655
+ assert_block(full_message) do
656
+ expected.send(operator, actual)
657
+ end
658
+ end
659
+ end
660
+
661
+ ##
662
+ # Passes if assertion is failed in block.
663
+ #
664
+ # Example:
665
+ # assert_fail_assertion {assert_equal("A", "B")} # -> pass
666
+ # assert_fail_assertion {assert_equal("A", "A")} # -> fail
667
+ def assert_fail_assertion(message=nil)
668
+ _wrap_assertion do
669
+ full_message = build_message(message,
670
+ "Failed assertion was expected.")
671
+ assert_block(full_message) do
672
+ begin
673
+ yield
674
+ false
675
+ rescue AssertionFailedError
676
+ true
677
+ end
678
+ end
679
+ end
680
+ end
681
+
682
+ ##
683
+ # Passes if an exception is raised in block and its
684
+ # message is +expected+.
685
+ #
686
+ # Example:
687
+ # assert_raise_message("exception") {raise "exception"} # -> pass
688
+ # assert_raise_message(/exc/i) {raise "exception"} # -> pass
689
+ # assert_raise_message("exception") {raise "EXCEPTION"} # -> fail
690
+ # assert_raise_message("exception") {} # -> fail
691
+ def assert_raise_message(expected, message=nil)
692
+ _wrap_assertion do
693
+ full_message = build_message(message,
694
+ "<?> exception message expected " +
695
+ "but none was thrown.",
696
+ expected)
697
+ exception = nil
698
+ assert_block(full_message) do
699
+ begin
700
+ yield
701
+ false
702
+ rescue Exception => exception
703
+ true
704
+ end
705
+ end
706
+
707
+ actual = exception.message
708
+ diff = AssertionMessage.delayed_diff(expected, actual)
709
+ full_message =
710
+ build_message(message,
711
+ "<?> exception message expected but was\n" +
712
+ "<?>.?", expected, actual, diff)
713
+ assert_block(full_message) do
714
+ if expected.is_a?(Regexp)
715
+ expected =~ actual
716
+ else
717
+ expected == actual
718
+ end
719
+ end
720
+ end
721
+ end
722
+
723
+ ##
724
+ # Passes if +object+.const_defined?(+constant_name+)
725
+ #
726
+ # Example:
727
+ # assert_const_defined(Test, :Unit) # -> pass
728
+ # assert_const_defined(Object, :Nonexistent) # -> fail
729
+ def assert_const_defined(object, constant_name, message=nil)
730
+ _wrap_assertion do
731
+ full_message = build_message(message,
732
+ "<?>.const_defined\\?(<?>) expected.",
733
+ object, constant_name)
734
+ assert_block(full_message) do
735
+ object.const_defined?(constant_name)
736
+ end
737
+ end
738
+ end
739
+
740
+ ##
741
+ # Passes if !+object+.const_defined?(+constant_name+)
742
+ #
743
+ # Example:
744
+ # assert_not_const_defined(Object, :Nonexistent) # -> pass
745
+ # assert_not_const_defined(Test, :Unit) # -> fail
746
+ def assert_not_const_defined(object, constant_name, message=nil)
747
+ _wrap_assertion do
748
+ full_message = build_message(message,
749
+ "!<?>.const_defined\\?(<?>) expected.",
750
+ object, constant_name)
751
+ assert_block(full_message) do
752
+ !object.const_defined?(constant_name)
753
+ end
754
+ end
755
+ end
756
+
757
+ ##
758
+ # Passes if +object+.+predicate+
759
+ #
760
+ # Example:
761
+ # assert_predicate([], :empty?) # -> pass
762
+ # assert_predicate([1], :empty?) # -> fail
763
+ def assert_predicate(object, predicate, message=nil)
764
+ _wrap_assertion do
765
+ assert_respond_to(object, predicate, message)
766
+ actual = object.send(predicate)
767
+ full_message = build_message(message,
768
+ "<?>.? is true value expected but was\n" +
769
+ "<?>",
770
+ object,
771
+ AssertionMessage.literal(predicate),
772
+ actual)
773
+ assert_block(full_message) do
774
+ actual
775
+ end
776
+ end
777
+ end
778
+
779
+ ##
780
+ # Passes if +object+.+predicate+
781
+ #
782
+ # Example:
783
+ # assert_not_predicate([1], :empty?) # -> pass
784
+ # assert_not_predicate([], :empty?) # -> fail
785
+ def assert_not_predicate(object, predicate, message=nil)
786
+ _wrap_assertion do
787
+ assert_respond_to(object, predicate, message)
788
+ actual = object.send(predicate)
789
+ full_message = build_message(message,
790
+ "<?>.? is false value expected but was\n" +
791
+ "<?>",
792
+ object,
793
+ AssertionMessage.literal(predicate),
794
+ actual)
795
+ assert_block(full_message) do
796
+ not actual
797
+ end
798
+ end
799
+ end
800
+
801
+ ##
802
+ # Passes if +object+#+alias_name+ is an alias method of
803
+ # +object+#+original_name+.
804
+ #
805
+ # Example:
806
+ # assert_alias_method([], :length, :size) # -> pass
807
+ # assert_alias_method([], :size, :length) # -> pass
808
+ # assert_alias_method([], :each, :size) # -> fail
809
+ def assert_alias_method(object, alias_name, original_name, message=nil)
810
+ _wrap_assertion do
811
+ find_method_failure_message = Proc.new do |method_name|
812
+ build_message(message,
813
+ "<?>.? doesn't exist\n" +
814
+ "(Class: <?>)",
815
+ object,
816
+ AssertionMessage.literal(method_name),
817
+ object.class)
818
+ end
819
+
820
+ alias_method = original_method = nil
821
+ assert_block(find_method_failure_message.call(alias_name)) do
822
+ begin
823
+ alias_method = object.method(alias_name)
824
+ true
825
+ rescue NameError
826
+ false
827
+ end
828
+ end
829
+ assert_block(find_method_failure_message.call(original_name)) do
830
+ begin
831
+ original_method = object.method(original_name)
832
+ true
833
+ rescue NameError
834
+ false
835
+ end
836
+ end
837
+
838
+ full_message = build_message(message,
839
+ "<?> is alias of\n" +
840
+ "<?> expected",
841
+ alias_method,
842
+ original_method)
843
+ assert_block(full_message) do
844
+ alias_method == original_method
845
+ end
846
+ end
847
+ end
848
+
849
+ ##
850
+ # Builds a failure message. +head+ is added before the +template+ and
851
+ # +arguments+ replaces the '?'s positionally in the template.
852
+
853
+ public
854
+ def build_message(head, template=nil, *arguments)
855
+ template &&= template.chomp
856
+ return AssertionMessage.new(head, template, arguments)
857
+ end
858
+
859
+ private
860
+ def _wrap_assertion
861
+ @_assertion_wrapped ||= false
862
+ unless (@_assertion_wrapped)
863
+ @_assertion_wrapped = true
864
+ begin
865
+ add_assertion
866
+ return yield
867
+ ensure
868
+ @_assertion_wrapped = false
869
+ end
870
+ else
871
+ return yield
872
+ end
873
+ end
874
+
875
+ ##
876
+ # Called whenever an assertion is made. Define this in classes that
877
+ # include Test::Unit::Assertions to record assertion counts.
878
+
879
+ private
880
+ def add_assertion
881
+ end
882
+
883
+ ##
884
+ # Select whether or not to use the pretty-printer. If this option is set
885
+ # to false before any assertions are made, pp.rb will not be required.
886
+
887
+ public
888
+ def self.use_pp=(value)
889
+ AssertionMessage.use_pp = value
890
+ end
891
+
892
+ # :stopdoc:
893
+ private
894
+ def _assert_raise(assert_expected_exception, *args, &block)
895
+ _wrap_assertion do
896
+ if args.last.is_a?(String)
897
+ message = args.pop
898
+ else
899
+ message = ""
900
+ end
901
+
902
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
903
+ expected = assert_exception_helper.expected_exceptions
904
+ actual_exception = nil
905
+ full_message = build_message(message,
906
+ "<?> exception expected " +
907
+ "but none was thrown.",
908
+ expected)
909
+ assert_block(full_message) do
910
+ begin
911
+ yield
912
+ false
913
+ rescue Exception => actual_exception
914
+ true
915
+ end
916
+ end
917
+ assert_expected_exception.call(message, assert_exception_helper,
918
+ actual_exception)
919
+ actual_exception
920
+ end
921
+ end
922
+
923
+ class AssertionMessage
924
+ @use_pp = true
925
+ class << self
926
+ attr_accessor :use_pp
927
+
928
+ def literal(value)
929
+ Literal.new(value)
930
+ end
931
+
932
+ def delayed_literal(&block)
933
+ DelayedLiteral.new(block)
934
+ end
935
+
936
+ def maybe_container(value, &formatter)
937
+ MaybeContainer.new(value, &formatter)
938
+ end
939
+
940
+ MAX_DIFF_TARGET_STRING_SIZE = 1000
941
+ def max_diff_target_string_size
942
+ size = ENV["TEST_UNIT_MAX_DIFF_TARGET_STRING_SIZE"]
943
+ if size
944
+ begin
945
+ size = Integer(size)
946
+ rescue ArgumentError
947
+ size = nil
948
+ end
949
+ end
950
+ size || MAX_DIFF_TARGET_STRING_SIZE
951
+ end
952
+
953
+ def diff_target_string?(string)
954
+ if string.respond_to?(:bytesize)
955
+ string.bytesize < max_diff_target_string_size
956
+ else
957
+ string.size < max_diff_target_string_size
958
+ end
959
+ end
960
+
961
+ def prepare_for_diff(from, to)
962
+ if !from.is_a?(String) or !to.is_a?(String)
963
+ from = convert(from)
964
+ to = convert(to)
965
+ end
966
+
967
+ if diff_target_string?(from) and diff_target_string?(to)
968
+ [from, to]
969
+ else
970
+ [nil, nil]
971
+ end
972
+ end
973
+
974
+ def delayed_diff(from, to)
975
+ delayed_literal do
976
+ from, to = prepare_for_diff(from, to)
977
+
978
+ diff = "" if from.nil? or to.nil?
979
+ diff ||= Diff.readable(from, to)
980
+ if /^[-+]/ !~ diff
981
+ diff = ""
982
+ elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
983
+ diff = "\n\ndiff:\n#{diff}"
984
+ else
985
+ diff = ""
986
+ end
987
+
988
+ if Diff.need_fold?(diff)
989
+ folded_diff = Diff.folded_readable(from, to)
990
+ diff << "\n\nfolded diff:\n#{folded_diff}"
991
+ end
992
+
993
+ diff
994
+ end
995
+ end
996
+
997
+ def convert(object)
998
+ case object
999
+ when Exception
1000
+ <<EOM.chop
1001
+ Class: <#{convert(object.class)}>
1002
+ Message: <#{convert(object.message)}>
1003
+ ---Backtrace---
1004
+ #{Util::BacktraceFilter.filter_backtrace(object.backtrace).join("\n")}
1005
+ ---------------
1006
+ EOM
1007
+ else
1008
+ if use_pp
1009
+ begin
1010
+ require 'pp' unless defined?(PP)
1011
+ begin
1012
+ return PP.pp(object, '').chomp
1013
+ rescue NameError
1014
+ end
1015
+ rescue LoadError
1016
+ self.use_pp = false
1017
+ end
1018
+ end
1019
+ object.inspect
1020
+ end
1021
+ end
1022
+ end
1023
+
1024
+ class Literal
1025
+ def initialize(value)
1026
+ @value = value
1027
+ end
1028
+
1029
+ def inspect
1030
+ @value.to_s
1031
+ end
1032
+ end
1033
+
1034
+ class DelayedLiteral
1035
+ def initialize(value)
1036
+ @value = value
1037
+ end
1038
+
1039
+ def inspect
1040
+ @value.call.to_s
1041
+ end
1042
+ end
1043
+
1044
+ class MaybeContainer
1045
+ def initialize(value, &formatter)
1046
+ @value = value
1047
+ @formatter = formatter
1048
+ end
1049
+
1050
+ def inspect
1051
+ if @value.is_a?(Array)
1052
+ values = @value.collect do |value|
1053
+ @formatter.call(AssertionMessage.convert(value))
1054
+ end
1055
+ "[#{values.join(', ')}]"
1056
+ else
1057
+ @formatter.call(AssertionMessage.convert(@value))
1058
+ end
1059
+ end
1060
+ end
1061
+
1062
+ class Template
1063
+ def self.create(string)
1064
+ parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
1065
+ self.new(parts)
1066
+ end
1067
+
1068
+ attr_reader :count
1069
+
1070
+ def initialize(parts)
1071
+ @parts = parts
1072
+ @count = parts.find_all{|e| e == '?'}.size
1073
+ end
1074
+
1075
+ def result(parameters)
1076
+ raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
1077
+ params = parameters.dup
1078
+ @parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
1079
+ end
1080
+ end
1081
+
1082
+ include Util::BacktraceFilter
1083
+
1084
+ def initialize(head, template_string, parameters)
1085
+ @head = head
1086
+ @template_string = template_string
1087
+ @parameters = parameters
1088
+ end
1089
+
1090
+ def convert(object)
1091
+ self.class.convert(object)
1092
+ end
1093
+
1094
+ def template
1095
+ @template ||= Template.create(@template_string)
1096
+ end
1097
+
1098
+ def add_period(string)
1099
+ (string =~ /\.\Z/ ? string : string + '.')
1100
+ end
1101
+
1102
+ def to_s
1103
+ message_parts = []
1104
+ if (@head)
1105
+ head = @head.to_s
1106
+ unless(head.empty?)
1107
+ message_parts << add_period(head)
1108
+ end
1109
+ end
1110
+ tail = template.result(@parameters.collect{|e| convert(e)})
1111
+ message_parts << tail unless(tail.empty?)
1112
+ message_parts.join("\n")
1113
+ end
1114
+ end
1115
+
1116
+ class AssertExceptionHelper
1117
+ class WrappedException
1118
+ def initialize(exception)
1119
+ @exception = exception
1120
+ end
1121
+
1122
+ def inspect
1123
+ if default_inspect?
1124
+ "#{@exception.class.inspect}(#{@exception.message.inspect})"
1125
+ else
1126
+ @exception.inspect
1127
+ end
1128
+ end
1129
+
1130
+ def method_missing(name, *args, &block)
1131
+ @exception.send(name, *args, &block)
1132
+ end
1133
+
1134
+ private
1135
+ def default_inspect?
1136
+ inspect_method = @exception.method(:inspect)
1137
+ if inspect_method.respond_to?(:owner) and
1138
+ inspect_method.owner == Exception
1139
+ true
1140
+ else
1141
+ default_inspect_method = Exception.instance_method(:inspect)
1142
+ default_inspect_method.bind(@exception).call == @exception.inspect
1143
+ end
1144
+ end
1145
+ end
1146
+
1147
+ def initialize(test_case, expected_exceptions)
1148
+ @test_case = test_case
1149
+ @expected_exceptions = expected_exceptions
1150
+ @expected_classes, @expected_modules, @expected_objects =
1151
+ split_expected_exceptions(expected_exceptions)
1152
+ end
1153
+
1154
+ def expected_exceptions
1155
+ exceptions = @expected_exceptions.collect do |exception|
1156
+ if exception.is_a?(Exception)
1157
+ WrappedException.new(exception)
1158
+ else
1159
+ exception
1160
+ end
1161
+ end
1162
+ if exceptions.size == 1
1163
+ exceptions[0]
1164
+ else
1165
+ exceptions
1166
+ end
1167
+ end
1168
+
1169
+ def expected?(actual_exception, equality=nil)
1170
+ equality ||= :instance_of?
1171
+ expected_class?(actual_exception, equality) or
1172
+ expected_module?(actual_exception) or
1173
+ expected_object?(actual_exception)
1174
+ end
1175
+
1176
+ private
1177
+ def split_expected_exceptions(expected_exceptions)
1178
+ exception_modules = []
1179
+ exception_objects = []
1180
+ exception_classes = []
1181
+ expected_exceptions.each do |exception_type|
1182
+ if exception_type.instance_of?(Module)
1183
+ exception_modules << exception_type
1184
+ elsif exception_type.is_a?(Exception)
1185
+ exception_objects << exception_type
1186
+ else
1187
+ @test_case.send(:assert,
1188
+ Exception >= exception_type,
1189
+ "Should expect a class of exception, " +
1190
+ "#{exception_type}")
1191
+ exception_classes << exception_type
1192
+ end
1193
+ end
1194
+ [exception_classes, exception_modules, exception_objects]
1195
+ end
1196
+
1197
+ def expected_class?(actual_exception, equality)
1198
+ @expected_classes.any? do |expected_class|
1199
+ actual_exception.send(equality, expected_class)
1200
+ end
1201
+ end
1202
+
1203
+ def expected_module?(actual_exception)
1204
+ @expected_modules.any? do |expected_module|
1205
+ actual_exception.is_a?(expected_module)
1206
+ end
1207
+ end
1208
+
1209
+ def expected_object?(actual_exception)
1210
+ @expected_objects.any? do |expected_object|
1211
+ expected_object == actual_exception or
1212
+ fallback_exception_object_equal(expected_object, actual_exception)
1213
+ end
1214
+ end
1215
+
1216
+ def fallback_exception_object_equal(expected_object, actual_exception)
1217
+ owner = Util::MethodOwnerFinder.find(expected_object, :==)
1218
+ if owner == Kernel or owner == Exception
1219
+ expected_object.class == actual_exception.class and
1220
+ expected_object.message == actual_exception.message
1221
+ else
1222
+ false
1223
+ end
1224
+ end
1225
+ end
1226
+
1227
+ # :startdoc:
1228
+ end
1229
+ end
1230
+ end