activegroonga 0.0.1

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