test-unit 3.1.5 → 3.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +5 -5
  2. data/BSDL +24 -0
  3. data/COPYING +41 -41
  4. data/README.md +24 -17
  5. data/Rakefile +21 -24
  6. data/doc/text/getting-started.md +246 -0
  7. data/doc/text/news.md +797 -56
  8. data/lib/test/unit/assertion-failed-error.rb +35 -0
  9. data/lib/test/unit/assertions.rb +542 -220
  10. data/lib/test/unit/attribute.rb +78 -4
  11. data/lib/test/unit/auto-runner-loader.rb +17 -0
  12. data/lib/test/unit/autorunner.rb +175 -78
  13. data/lib/test/unit/code-snippet-fetcher.rb +7 -7
  14. data/lib/test/unit/collector/descendant.rb +1 -0
  15. data/lib/test/unit/collector/dir.rb +4 -2
  16. data/lib/test/unit/collector/load.rb +25 -15
  17. data/lib/test/unit/collector/objectspace.rb +1 -0
  18. data/lib/test/unit/collector.rb +31 -0
  19. data/lib/test/unit/color-scheme.rb +29 -2
  20. data/lib/test/unit/data-sets.rb +127 -0
  21. data/lib/test/unit/data.rb +121 -12
  22. data/lib/test/unit/diff.rb +10 -11
  23. data/lib/test/unit/fixture.rb +77 -27
  24. data/lib/test/unit/notification.rb +9 -7
  25. data/lib/test/unit/omission.rb +34 -31
  26. data/lib/test/unit/pending.rb +12 -11
  27. data/lib/test/unit/priority.rb +7 -5
  28. data/lib/test/unit/runner/console.rb +20 -1
  29. data/lib/test/unit/test-suite-creator.rb +30 -9
  30. data/lib/test/unit/testcase.rb +349 -196
  31. data/lib/test/unit/testresult.rb +7 -0
  32. data/lib/test/unit/testsuite.rb +1 -1
  33. data/lib/test/unit/ui/console/testrunner.rb +171 -60
  34. data/lib/test/unit/ui/emacs/testrunner.rb +5 -5
  35. data/lib/test/unit/ui/testrunnermediator.rb +9 -7
  36. data/lib/test/unit/util/backtracefilter.rb +17 -5
  37. data/lib/test/unit/util/memory-usage.rb +47 -0
  38. data/lib/test/unit/util/observable.rb +2 -2
  39. data/lib/test/unit/util/output.rb +5 -4
  40. data/lib/test/unit/util/procwrapper.rb +4 -4
  41. data/lib/test/unit/version.rb +1 -1
  42. data/lib/test/unit/warning.rb +3 -0
  43. data/lib/test/unit.rb +177 -161
  44. data/lib/test-unit.rb +2 -17
  45. metadata +20 -94
  46. data/GPL +0 -339
  47. data/LGPL +0 -502
  48. data/test/collector/test-descendant.rb +0 -178
  49. data/test/collector/test-load.rb +0 -442
  50. data/test/collector/test_dir.rb +0 -406
  51. data/test/collector/test_objectspace.rb +0 -100
  52. data/test/fixtures/header-label.csv +0 -3
  53. data/test/fixtures/header-label.tsv +0 -3
  54. data/test/fixtures/header.csv +0 -3
  55. data/test/fixtures/header.tsv +0 -3
  56. data/test/fixtures/no-header.csv +0 -2
  57. data/test/fixtures/no-header.tsv +0 -2
  58. data/test/fixtures/plus.csv +0 -3
  59. data/test/run-test.rb +0 -22
  60. data/test/test-assertions.rb +0 -2157
  61. data/test/test-attribute-matcher.rb +0 -38
  62. data/test/test-attribute.rb +0 -123
  63. data/test/test-code-snippet.rb +0 -37
  64. data/test/test-color-scheme.rb +0 -82
  65. data/test/test-color.rb +0 -47
  66. data/test/test-data.rb +0 -281
  67. data/test/test-diff.rb +0 -518
  68. data/test/test-emacs-runner.rb +0 -60
  69. data/test/test-error.rb +0 -26
  70. data/test/test-failure.rb +0 -33
  71. data/test/test-fault-location-detector.rb +0 -163
  72. data/test/test-fixture.rb +0 -659
  73. data/test/test-notification.rb +0 -33
  74. data/test/test-omission.rb +0 -81
  75. data/test/test-pending.rb +0 -70
  76. data/test/test-priority.rb +0 -173
  77. data/test/test-test-case.rb +0 -1171
  78. data/test/test-test-result.rb +0 -113
  79. data/test/test-test-suite-creator.rb +0 -97
  80. data/test/test-test-suite.rb +0 -150
  81. data/test/testunit-test-util.rb +0 -31
  82. data/test/ui/test_testrunmediator.rb +0 -20
  83. data/test/util/test-method-owner-finder.rb +0 -38
  84. data/test/util/test-output.rb +0 -11
  85. data/test/util/test_backtracefilter.rb +0 -41
  86. data/test/util/test_observable.rb +0 -102
  87. data/test/util/test_procwrapper.rb +0 -36
@@ -5,6 +5,7 @@
5
5
 
6
6
  require 'test/unit/assertion-failed-error'
7
7
  require 'test/unit/util/backtracefilter'
8
+ require 'test/unit/util/memory-usage'
8
9
  require 'test/unit/util/method-owner-finder'
9
10
  require 'test/unit/diff'
10
11
 
@@ -32,9 +33,9 @@ module Test
32
33
  #
33
34
  # @example Example Custom Assertion
34
35
  #
35
- # def deny(boolean, message = nil)
36
- # message = build_message message, '<?> is not false or nil.', boolean
37
- # assert_block message do
36
+ # def deny(boolean, message=nil)
37
+ # message = build_message(message, '<?> is not false or nil.', boolean)
38
+ # assert_block(message) do
38
39
  # not boolean
39
40
  # end
40
41
  # end
@@ -43,7 +44,7 @@ module Test
43
44
 
44
45
  ##
45
46
  # The assertion upon which all other assertions are based. Passes if the
46
- # block yields true.
47
+ # block yields not false nor nil.
47
48
  #
48
49
  # @example
49
50
  # assert_block "Couldn't do the thing" do
@@ -52,7 +53,11 @@ module Test
52
53
  def assert_block(message="assert_block failed.")
53
54
  _wrap_assertion do
54
55
  if (! yield)
55
- raise AssertionFailedError.new(message.to_s)
56
+ options = {}
57
+ if message.respond_to?(:user_message)
58
+ options[:user_message] = message.user_message
59
+ end
60
+ raise AssertionFailedError.new(message.to_s, options)
56
61
  end
57
62
  end
58
63
  end
@@ -153,8 +158,8 @@ module Test
153
158
  assertion_message = message
154
159
  else
155
160
  error_message = "assertion message must be String, Proc or "
156
- error_message << "#{AssertionMessage}: "
157
- error_message << "<#{message.inspect}>(<#{message.class}>)"
161
+ error_message += "#{AssertionMessage}: "
162
+ error_message += "<#{message.inspect}>(<#{message.class}>)"
158
163
  raise ArgumentError, error_message, filter_backtrace(caller)
159
164
  end
160
165
  assertion_message ||= build_message(message,
@@ -167,7 +172,7 @@ module Test
167
172
  end
168
173
  end
169
174
 
170
- # Asserts that +object+ is false or nil.
175
+ # Asserts that `object` is false or nil.
171
176
  #
172
177
  # @note Just for minitest compatibility. :<
173
178
  #
@@ -192,8 +197,8 @@ module Test
192
197
  assertion_message = message
193
198
  else
194
199
  error_message = "assertion message must be String, Proc or "
195
- error_message << "#{AssertionMessage}: "
196
- error_message << "<#{message.inspect}>(<#{message.class}>)"
200
+ error_message += "#{AssertionMessage}: "
201
+ error_message += "<#{message.inspect}>(<#{message.class}>)"
197
202
  raise ArgumentError, error_message, filter_backtrace(caller)
198
203
  end
199
204
  assert_block("refute should not be called with a block.") do
@@ -209,7 +214,7 @@ module Test
209
214
  end
210
215
 
211
216
  ##
212
- # Passes if +expected+ == +actual+.
217
+ # Passes if `expected` == `actual`.
213
218
  #
214
219
  # Note that the ordering of arguments is important, since a helpful
215
220
  # error message is generated when this one fails that tells you the
@@ -239,7 +244,7 @@ EOT
239
244
  begin
240
245
  assert_block(full_message) { expected == actual }
241
246
  rescue AssertionFailedError => failure
242
- _set_failed_information(failure, expected, actual, message)
247
+ _set_failed_information(failure, expected, actual)
243
248
  raise failure # For JRuby. :<
244
249
  end
245
250
  end
@@ -275,8 +280,7 @@ EOT
275
280
  assert_exception_helper.expected?(actual_exception)
276
281
  end
277
282
  rescue AssertionFailedError => failure
278
- _set_failed_information(failure, expected, actual_exception,
279
- message)
283
+ _set_failed_information(failure, expected, actual_exception)
280
284
  raise failure # For JRuby. :<
281
285
  end
282
286
  end
@@ -286,6 +290,71 @@ EOT
286
290
  # Just for minitest compatibility. :<
287
291
  alias_method :assert_raises, :assert_raise
288
292
 
293
+ # Passes if the block raises `expected_exception` with
294
+ # `expected_message`. `expected_message` can be a `String` or
295
+ # `Regexp`.
296
+ #
297
+ # @example Pass pattern: String
298
+ # assert_raise_with_message(RuntimeError, "Boom!!!") do
299
+ # raise "Boom!!!"
300
+ # end # -> pass
301
+ #
302
+ # @example Pass pattern: Regexp
303
+ # assert_raise_with_message(RuntimeError, /!!!/) do
304
+ # raise "Boom!!!"
305
+ # end # -> pass
306
+ #
307
+ # @example Failure pattern: Exception class isn't matched
308
+ # assert_raise_with_message(RuntimeError, "Boom!!!") do
309
+ # raise ArgumentError, "Boom!!!"
310
+ # end # -> failure
311
+ #
312
+ # @example Failure pattern: Exception message isn't matched
313
+ # assert_raise_with_message(RuntimeError, "Boom!!!") do
314
+ # raise "Hello"
315
+ # end # -> failure
316
+ #
317
+ # @since 3.4.3
318
+ def assert_raise_with_message(expected_exception_class,
319
+ expected_message,
320
+ message=nil,
321
+ &block)
322
+ assert_expected_exception = Proc.new do |*_args|
323
+ _message, assert_exception_helper, actual_exception = _args
324
+ diff = AssertionMessage.delayed_diff([
325
+ expected_exception_class,
326
+ expected_message,
327
+ ],
328
+ [
329
+ actual_exception.class,
330
+ actual_exception.message,
331
+ ])
332
+ full_message = build_message(message,
333
+ "<?>(<?>) exception expected but was\n" +
334
+ "<?>(<?>).?",
335
+ expected_exception_class,
336
+ expected_message,
337
+ actual_exception.class,
338
+ actual_exception.message,
339
+ diff)
340
+ begin
341
+ assert_block(full_message) do
342
+ assert_exception_helper.expected?(actual_exception) and
343
+ expected_message === actual_exception.message
344
+ end
345
+ rescue AssertionFailedError => failure
346
+ _set_failed_information(failure,
347
+ expected_exception_class,
348
+ actual_exception)
349
+ raise failure # For JRuby. :<
350
+ end
351
+ actual_exception
352
+ end
353
+ args = [expected_exception_class]
354
+ args << message if message
355
+ _assert_raise(assert_expected_exception, *args, &block)
356
+ end
357
+
289
358
  ##
290
359
  # Passes if the block raises one of the given
291
360
  # exceptions or sub exceptions of the given exceptions.
@@ -311,7 +380,7 @@ EOT
311
380
 
312
381
 
313
382
  ##
314
- # Passes if +object+.instance_of?(+klass+). When +klass+ is
383
+ # Passes if `object`.instance_of?(`klass`). When `klass` is
315
384
  # an array of classes, it passes if any class
316
385
  # satisfies +object.instance_of?(class).
317
386
  #
@@ -319,7 +388,7 @@ EOT
319
388
  # assert_instance_of(String, 'foo') # -> pass
320
389
  # assert_instance_of([Fixnum, NilClass], 100) # -> pass
321
390
  # assert_instance_of([Numeric, NilClass], 100) # -> fail
322
- def assert_instance_of(klass, object, message="")
391
+ def assert_instance_of(klass, object, message=nil)
323
392
  _wrap_assertion do
324
393
  if klass.is_a?(Array)
325
394
  klasses = klass
@@ -334,7 +403,7 @@ EOT
334
403
  "<#{value}>"
335
404
  end
336
405
  full_message = build_message(message, <<EOT, object, klass_message, object.class)
337
- <?> expected to be instance_of\\?
406
+ <?> was expected to be instance_of\\?
338
407
  ? but was
339
408
  <?>.
340
409
  EOT
@@ -345,8 +414,8 @@ EOT
345
414
  end
346
415
 
347
416
  ##
348
- # Passes if +object+.instance_of?(+klass+) does not hold.
349
- # When +klass+ is an array of classes, it passes if no class
417
+ # Passes if `object`.instance_of?(`klass`) does not hold.
418
+ # When `klass` is an array of classes, it passes if no class
350
419
  # satisfies +object.instance_of?(class).
351
420
  #
352
421
  # @example
@@ -355,14 +424,14 @@ EOT
355
424
  # assert_not_instance_of([Numeric, NilClass], 100) # -> fail
356
425
  #
357
426
  # @since 3.0.0
358
- def assert_not_instance_of(klass, object, message="")
427
+ def assert_not_instance_of(klass, object, message=nil)
359
428
  _wrap_assertion do
360
429
  if klass.is_a?(Array)
361
430
  klasses = klass
362
431
  else
363
432
  klasses = [klass]
364
433
  end
365
- assert_block("The first parameter to assert_not_instance_of should be " <<
434
+ assert_block("The first parameter to assert_not_instance_of should be " +
366
435
  "a Class or an Array of Class.") do
367
436
  klasses.all? {|k| k.is_a?(Class)}
368
437
  end
@@ -370,7 +439,7 @@ EOT
370
439
  "<#{value}>"
371
440
  end
372
441
  full_message = build_message(message,
373
- "<?> expected to not be instance_of\\?\n" +
442
+ "<?> was expected to not be instance_of\\?\n" +
374
443
  "? but was.",
375
444
  object,
376
445
  klass_message)
@@ -386,19 +455,19 @@ EOT
386
455
  alias_method :refute_instance_of, :assert_not_instance_of
387
456
 
388
457
  ##
389
- # Passes if +object+ is nil.
458
+ # Passes if `object`.nil?.
390
459
  #
391
460
  # @example
392
461
  # assert_nil [1, 2].uniq!
393
- def assert_nil(object, message="")
462
+ def assert_nil(object, message=nil)
394
463
  full_message = build_message(message, <<EOT, object)
395
- <?> expected to be nil.
464
+ <?> was expected to be nil.
396
465
  EOT
397
466
  assert_block(full_message) { object.nil? }
398
467
  end
399
468
 
400
469
  ##
401
- # Passes if +object+.kind_of?(+klass+). When +klass+ is
470
+ # Passes if `object`.kind_of?(`klass`). When `klass` is
402
471
  # an array of classes or modules, it passes if any
403
472
  # class or module satisfies +object.kind_of?(class_or_module).
404
473
  #
@@ -406,7 +475,7 @@ EOT
406
475
  # assert_kind_of(Object, 'foo') # -> pass
407
476
  # assert_kind_of([Fixnum, NilClass], 100) # -> pass
408
477
  # assert_kind_of([Fixnum, NilClass], "string") # -> fail
409
- def assert_kind_of(klass, object, message="")
478
+ def assert_kind_of(klass, object, message=nil)
410
479
  _wrap_assertion do
411
480
  if klass.is_a?(Array)
412
481
  klasses = klass
@@ -421,7 +490,7 @@ EOT
421
490
  "<#{value}>"
422
491
  end
423
492
  full_message = build_message(message,
424
- "<?> expected to be kind_of\\?\n" +
493
+ "<?> was expected to be kind_of\\?\n" +
425
494
  "? but was\n" +
426
495
  "<?>.",
427
496
  object,
@@ -434,8 +503,8 @@ EOT
434
503
  end
435
504
 
436
505
  ##
437
- # Passes if +object+.kind_of?(+klass+) does not hold.
438
- # When +klass+ is an array of classes or modules, it passes only if all
506
+ # Passes if `object`.kind_of?(`klass`) does not hold.
507
+ # When `klass` is an array of classes or modules, it passes only if all
439
508
  # classes (and modules) do not satisfy +object.kind_of?(class_or_module).
440
509
  #
441
510
  # @example
@@ -444,7 +513,7 @@ EOT
444
513
  # assert_not_kind_of([Fixnum, NilClass], 100) # -> fail
445
514
  #
446
515
  # @since 3.0.0
447
- def assert_not_kind_of(klass, object, message="")
516
+ def assert_not_kind_of(klass, object, message=nil)
448
517
  _wrap_assertion do
449
518
  if klass.is_a?(Array)
450
519
  klasses = klass
@@ -459,7 +528,7 @@ EOT
459
528
  "<#{value}>"
460
529
  end
461
530
  full_message = build_message(message,
462
- "<?> expected to not be kind_of\\?\n" +
531
+ "<?> was expected to not be kind_of\\?\n" +
463
532
  "? but was.",
464
533
  object,
465
534
  klass_message)
@@ -475,11 +544,11 @@ EOT
475
544
  alias_method :refute_kind_of, :assert_not_kind_of
476
545
 
477
546
  ##
478
- # Passes if +object+ .respond_to? +method+
547
+ # Passes if `object` .respond_to? `method`
479
548
  #
480
549
  # @example
481
550
  # assert_respond_to 'bugbear', :slice
482
- def assert_respond_to(object, method, message="")
551
+ def assert_respond_to(object, method, message=nil)
483
552
  _wrap_assertion do
484
553
  full_message = build_message(message,
485
554
  "<?>.kind_of\\?(Symbol) or\n" +
@@ -497,12 +566,12 @@ EOT
497
566
  end
498
567
 
499
568
  ##
500
- # Passes if +object+ does not .respond_to? +method+.
569
+ # Passes if `object` does not .respond_to? `method`.
501
570
  #
502
571
  # @example
503
572
  # assert_not_respond_to('bugbear', :nonexistence) # -> pass
504
573
  # assert_not_respond_to('bugbear', :size) # -> fail
505
- def assert_not_respond_to(object, method, message="")
574
+ def assert_not_respond_to(object, method, message=nil)
506
575
  _wrap_assertion do
507
576
  full_message = build_message(message,
508
577
  "<?>.kind_of\\?(Symbol) or\n" +
@@ -525,35 +594,31 @@ EOT
525
594
  alias_method :refute_respond_to, :assert_not_respond_to
526
595
 
527
596
  ##
528
- # Passes if +pattern+ =~ +string+.
597
+ # Passes if `pattern` =~ `string`.
529
598
  #
530
599
  # @example
531
600
  # assert_match(/\d+/, 'five, 6, seven')
532
- def assert_match(pattern, string, message="")
601
+ def assert_match(pattern, string, message=nil)
533
602
  _wrap_assertion do
534
- pattern = case(pattern)
535
- when String
536
- Regexp.new(Regexp.escape(pattern))
537
- else
538
- pattern
539
- end
540
- full_message = build_message(message, "<?> expected to be =~\n<?>.",
603
+ pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a?(String)
604
+ full_message = build_message(message,
605
+ "<?> was expected to be =~\n<?>.",
541
606
  pattern, string)
542
607
  assert_block(full_message) { pattern =~ string }
543
608
  end
544
609
  end
545
610
 
546
611
  ##
547
- # Passes if +actual+ .equal? +expected+ (i.e. they are the same
612
+ # Passes if `actual` .equal? `expected` (i.e. they are the same
548
613
  # instance).
549
614
  #
550
615
  # @example
551
616
  # o = Object.new
552
617
  # assert_same o, o
553
- def assert_same(expected, actual, message="")
618
+ def assert_same(expected, actual, message=nil)
554
619
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
555
620
  <?>
556
- with id <?> expected to be equal\\? to
621
+ with id <?> was expected to be equal\\? to
557
622
  <?>
558
623
  with id <?>.
559
624
  EOT
@@ -561,18 +626,18 @@ EOT
561
626
  end
562
627
 
563
628
  ##
564
- # Compares the +object1+ with +object2+ using +operator+.
629
+ # Compares the `object1` with `object2` using `operator`.
565
630
  #
566
- # Passes if object1.__send__(operator, object2) is true.
631
+ # Passes if object1.__send__(operator, object2) is not false nor nil.
567
632
  #
568
633
  # @example
569
634
  # assert_operator 5, :>=, 4
570
- def assert_operator(object1, operator, object2, message="")
635
+ def assert_operator(object1, operator, object2, message=nil)
571
636
  _wrap_assertion do
572
637
  full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
573
638
  assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
574
639
  full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
575
- <?> expected to be
640
+ <?> was expected to be
576
641
  ?
577
642
  <?>.
578
643
  EOT
@@ -581,21 +646,21 @@ EOT
581
646
  end
582
647
 
583
648
  ##
584
- # Compares the +object1+ with +object2+ using +operator+.
649
+ # Compares the `object1` with `object2` using `operator`.
585
650
  #
586
- # Passes if object1.__send__(operator, object2) is not true.
651
+ # Passes if object1.__send__(operator, object2) is false or nil.
587
652
  #
588
653
  # @example
589
654
  # assert_not_operator(5, :<, 4) # => pass
590
655
  # assert_not_operator(5, :>, 4) # => fail
591
656
  #
592
657
  # @since 3.0.0
593
- def assert_not_operator(object1, operator, object2, message="")
658
+ def assert_not_operator(object1, operator, object2, message=nil)
594
659
  _wrap_assertion do
595
660
  full_message = build_message(nil, "<?>\ngiven as the operator for #assert_not_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
596
661
  assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
597
662
  full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
598
- <?> expected to not be
663
+ <?> was expected to not be
599
664
  ?
600
665
  <?>.
601
666
  EOT
@@ -635,7 +700,6 @@ EOT
635
700
  raise
636
701
  end
637
702
  end
638
- nil
639
703
  end
640
704
  end
641
705
 
@@ -649,14 +713,14 @@ EOT
649
713
  end
650
714
 
651
715
  ##
652
- # Passes if ! +actual+ .equal? +expected+
716
+ # Passes if ! `actual` .equal? `expected`
653
717
  #
654
718
  # @example
655
719
  # assert_not_same Object.new, Object.new
656
- def assert_not_same(expected, actual, message="")
720
+ def assert_not_same(expected, actual, message=nil)
657
721
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
658
722
  <?>
659
- with id <?> expected to not be equal\\? to
723
+ with id <?> was expected to not be equal\\? to
660
724
  <?>
661
725
  with id <?>.
662
726
  EOT
@@ -669,12 +733,14 @@ EOT
669
733
  alias_method :refute_same, :assert_not_same
670
734
 
671
735
  ##
672
- # Passes if +expected+ != +actual+
736
+ # Passes if `expected` != `actual`
673
737
  #
674
738
  # @example
675
739
  # assert_not_equal 'some string', 5
676
- def assert_not_equal(expected, actual, message="")
677
- full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
740
+ def assert_not_equal(expected, actual, message=nil)
741
+ full_message = build_message(message,
742
+ "<?> was expected to be != to\n<?>.",
743
+ expected, actual)
678
744
  assert_block(full_message) { expected != actual }
679
745
  end
680
746
 
@@ -684,12 +750,14 @@ EOT
684
750
  alias_method :refute_equal, :assert_not_equal
685
751
 
686
752
  ##
687
- # Passes if ! +object+ .nil?
753
+ # Passes if ! `object` .nil?
688
754
  #
689
755
  # @example
690
756
  # assert_not_nil '1 two 3'.sub!(/two/, '2')
691
- def assert_not_nil(object, message="")
692
- full_message = build_message(message, "<?> expected to not be nil.", object)
757
+ def assert_not_nil(object, message=nil)
758
+ full_message = build_message(message,
759
+ "<?> was expected to not be nil.",
760
+ object)
693
761
  assert_block(full_message){!object.nil?}
694
762
  end
695
763
 
@@ -699,20 +767,18 @@ EOT
699
767
  alias_method :refute_nil, :assert_not_nil
700
768
 
701
769
  ##
702
- # Passes if +regexp+ !~ +string+
770
+ # Passes if `regexp` !~ `string`
703
771
  #
704
772
  # @example
705
773
  # assert_not_match(/two/, 'one 2 three') # -> pass
706
774
  # assert_not_match(/three/, 'one 2 three') # -> fail
707
- def assert_not_match(regexp, string, message="")
775
+ def assert_not_match(pattern, string, message=nil)
708
776
  _wrap_assertion do
709
- assert_instance_of(Regexp, regexp,
710
- "<REGEXP> in assert_not_match(<REGEXP>, ...) " +
711
- "should be a Regexp.")
777
+ pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a?(String)
712
778
  full_message = build_message(message,
713
- "<?> expected to not match\n<?>.",
714
- regexp, string)
715
- assert_block(full_message) { regexp !~ string }
779
+ "<?> was expected to not match\n<?>.",
780
+ pattern, string)
781
+ assert_block(full_message) { pattern !~ string }
716
782
  end
717
783
  end
718
784
 
@@ -722,9 +788,9 @@ EOT
722
788
  alias_method :refute_match, :assert_not_match
723
789
 
724
790
  ##
725
- # Deprecated. Use #assert_not_match instead.
791
+ # @deprecated Use {#assert_not_match} instead.
726
792
  #
727
- # Passes if +regexp+ !~ +string+
793
+ # Passes if `regexp` !~ `string`
728
794
  #
729
795
  # @example
730
796
  # assert_no_match(/two/, 'one 2 three') # -> pass
@@ -782,13 +848,13 @@ EOT
782
848
  end
783
849
 
784
850
  ##
785
- # Passes if the block throws +expected_object+
851
+ # Passes if the block throws `expected_object`
786
852
  #
787
853
  # @example
788
854
  # assert_throw(:done) do
789
855
  # throw(:done)
790
856
  # end
791
- def assert_throw(expected_object, message="", &proc)
857
+ def assert_throw(expected_object, message=nil, &proc)
792
858
  _wrap_assertion do
793
859
  begin
794
860
  catch([]) {}
@@ -814,7 +880,7 @@ EOT
814
880
  tag = extractor.extract_tag
815
881
  raise if tag.nil?
816
882
  full_message = build_message(message,
817
- "<?> expected to be thrown but\n" +
883
+ "<?> was expected to be thrown but\n" +
818
884
  "<?> was thrown.",
819
885
  expected_object, tag)
820
886
  flunk(full_message)
@@ -834,7 +900,7 @@ EOT
834
900
  # assert_nothing_thrown do
835
901
  # [1, 2].uniq
836
902
  # end
837
- def assert_nothing_thrown(message="", &proc)
903
+ def assert_nothing_thrown(message=nil, &proc)
838
904
  _wrap_assertion do
839
905
  assert(block_given?, "Should have passed a block to assert_nothing_thrown")
840
906
  begin
@@ -853,29 +919,51 @@ EOT
853
919
  end
854
920
 
855
921
  ##
856
- # Passes if +expected_float+ and +actual_float+ are equal
857
- # within +delta+ tolerance.
922
+ # Passes if `expected_float` and `actual_float` are equal
923
+ # within `delta` tolerance.
858
924
  #
859
925
  # @example
860
926
  # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
861
927
  def assert_in_delta(expected_float, actual_float, delta=0.001, message="")
862
928
  _wrap_assertion do
863
- _assert_in_delta_validate_arguments(expected_float,
864
- actual_float,
865
- delta)
866
- full_message = _assert_in_delta_message(expected_float,
867
- actual_float,
868
- delta,
869
- message)
929
+ begin
930
+ pass = delta >= (expected_float - actual_float).abs
931
+ assert_operator(delta, :>=, 0.0, "The delta should not be negative")
932
+ full_message = _assert_in_delta_message(expected_float,
933
+ expected_float,
934
+ actual_float,
935
+ actual_float,
936
+ delta,
937
+ delta,
938
+ message)
939
+ rescue Test::Unit::AssertionFailedError
940
+ # for the above assert_operator
941
+ raise
942
+ rescue
943
+ _assert_in_delta_validate_arguments(expected_float,
944
+ actual_float,
945
+ delta)
946
+ normalized_expected = expected_float.to_f
947
+ normalized_actual = actual_float.to_f
948
+ normalized_delta = delta.to_f
949
+ pass = (normalized_expected - normalized_actual).abs <= normalized_delta
950
+ full_message = _assert_in_delta_message(expected_float,
951
+ normalized_expected,
952
+ actual_float,
953
+ normalized_actual,
954
+ delta,
955
+ normalized_delta,
956
+ message)
957
+ end
870
958
  assert_block(full_message) do
871
- (expected_float.to_f - actual_float.to_f).abs <= delta.to_f
959
+ pass
872
960
  end
873
961
  end
874
962
  end
875
963
 
876
964
  ##
877
- # Passes if +expected_float+ and +actual_float+ are
878
- # not equal within +delta+ tolerance.
965
+ # Passes if `expected_float` and `actual_float` are
966
+ # not equal within `delta` tolerance.
879
967
  #
880
968
  # @example
881
969
  # assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
@@ -885,13 +973,32 @@ EOT
885
973
  _assert_in_delta_validate_arguments(expected_float,
886
974
  actual_float,
887
975
  delta)
888
- full_message = _assert_in_delta_message(expected_float,
889
- actual_float,
890
- delta,
891
- message,
892
- :negative_assertion => true)
976
+ begin
977
+ pass = (expected_float - actual_float).abs > delta
978
+ full_message = _assert_in_delta_message(expected_float,
979
+ expected_float,
980
+ actual_float,
981
+ actual_float,
982
+ delta,
983
+ delta,
984
+ message,
985
+ :negative_assertion => true)
986
+ rescue
987
+ normalized_expected = expected_float.to_f
988
+ normalized_actual = actual_float.to_f
989
+ normalized_delta = delta.to_f
990
+ pass = (normalized_expected - normalized_actual).abs > normalized_delta
991
+ full_message = _assert_in_delta_message(expected_float,
992
+ normalized_expected,
993
+ actual_float,
994
+ normalized_actual,
995
+ delta,
996
+ normalized_delta,
997
+ message,
998
+ :negative_assertion => true)
999
+ end
893
1000
  assert_block(full_message) do
894
- (expected_float.to_f - actual_float.to_f).abs > delta.to_f
1001
+ pass
895
1002
  end
896
1003
  end
897
1004
  end
@@ -918,23 +1025,22 @@ EOT
918
1025
  assert_operator(delta, :>=, 0.0, "The delta should not be negative")
919
1026
  end
920
1027
 
921
- def _assert_in_delta_message(expected_float, actual_float, delta,
1028
+ def _assert_in_delta_message(expected_float, normalized_expected,
1029
+ actual_float, normalized_actual,
1030
+ delta, normalized_delta,
922
1031
  message, options={})
923
1032
  if options[:negative_assertion]
924
1033
  format = <<-EOT
925
- <?> -/+ <?> expected to not include
1034
+ <?> -/+ <?> was expected to not include
926
1035
  <?>.
927
1036
  EOT
928
1037
  else
929
1038
  format = <<-EOT
930
- <?> -/+ <?> expected to include
1039
+ <?> -/+ <?> was expected to include
931
1040
  <?>.
932
1041
  EOT
933
1042
  end
934
1043
  arguments = [expected_float, delta, actual_float]
935
- normalized_expected = expected_float.to_f
936
- normalized_actual = actual_float.to_f
937
- normalized_delta = delta.to_f
938
1044
  relation_format = nil
939
1045
  relation_arguments = nil
940
1046
  if normalized_actual < normalized_expected - normalized_delta
@@ -961,7 +1067,7 @@ EOT
961
1067
  end
962
1068
 
963
1069
  if relation_format
964
- format << <<-EOT
1070
+ format += <<-EOT
965
1071
 
966
1072
  Relation:
967
1073
  #{relation_format}
@@ -974,8 +1080,8 @@ EOT
974
1080
 
975
1081
  public
976
1082
  ##
977
- # Passes if +expected_float+ and +actual_float+ are equal
978
- # within +epsilon+ relative error of +expected_float+.
1083
+ # Passes if `expected_float` and `actual_float` are equal
1084
+ # within `epsilon` relative error of `expected_float`.
979
1085
  #
980
1086
  # @example
981
1087
  # assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
@@ -983,30 +1089,60 @@ EOT
983
1089
  def assert_in_epsilon(expected_float, actual_float, epsilon=0.001,
984
1090
  message="")
985
1091
  _wrap_assertion do
986
- _assert_in_epsilon_validate_arguments(expected_float,
987
- actual_float,
988
- epsilon)
989
- full_message = _assert_in_epsilon_message(expected_float,
990
- actual_float,
991
- epsilon,
992
- message)
993
- assert_block(full_message) do
994
- normalized_expected_float = expected_float.to_f
995
- if normalized_expected_float.zero?
996
- delta = epsilon.to_f ** 2
1092
+ begin
1093
+ zero_p = expected_float.zero? rescue expected_float == 0
1094
+ if zero_p
1095
+ delta = epsilon ** 2
1096
+ else
1097
+ delta = expected_float * epsilon
1098
+ end
1099
+ delta = delta.abs
1100
+ pass = (expected_float - actual_float).abs <= delta
1101
+ assert_operator(epsilon, :>=, 0.0, "The epsilon should not be negative")
1102
+ full_message = _assert_in_epsilon_message(expected_float,
1103
+ expected_float,
1104
+ actual_float,
1105
+ actual_float,
1106
+ epsilon,
1107
+ epsilon,
1108
+ delta,
1109
+ message)
1110
+ rescue Test::Unit::AssertionFailedError
1111
+ # for the above assert_operator
1112
+ raise
1113
+ rescue
1114
+ _assert_in_epsilon_validate_arguments(expected_float,
1115
+ actual_float,
1116
+ epsilon)
1117
+ normalized_expected = expected_float.to_f
1118
+ normalized_actual = actual_float.to_f
1119
+ normalized_epsilon = epsilon.to_f
1120
+ if normalized_expected.zero?
1121
+ delta = normalized_epsilon ** 2
997
1122
  else
998
- delta = normalized_expected_float * epsilon.to_f
1123
+ delta = normalized_expected * normalized_epsilon
999
1124
  end
1000
1125
  delta = delta.abs
1001
- (normalized_expected_float - actual_float.to_f).abs <= delta
1126
+ full_message = _assert_in_epsilon_message(expected_float,
1127
+ normalized_expected,
1128
+ actual_float,
1129
+ normalized_actual,
1130
+ epsilon,
1131
+ normalized_epsilon,
1132
+ delta,
1133
+ message)
1134
+ pass = (normalized_expected - normalized_actual).abs <= delta
1135
+ end
1136
+ assert_block(full_message) do
1137
+ pass
1002
1138
  end
1003
1139
  end
1004
1140
  end
1005
1141
 
1006
1142
  ##
1007
- # Passes if +expected_float+ and +actual_float+ are
1008
- # not equal within +epsilon+ relative error of
1009
- # +expected_float+.
1143
+ # Passes if `expected_float` and `actual_float` are
1144
+ # not equal within `epsilon` relative error of
1145
+ # `expected_float`.
1010
1146
  #
1011
1147
  # @example
1012
1148
  # assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
@@ -1014,18 +1150,43 @@ EOT
1014
1150
  def assert_not_in_epsilon(expected_float, actual_float, epsilon=0.001,
1015
1151
  message="")
1016
1152
  _wrap_assertion do
1017
- _assert_in_epsilon_validate_arguments(expected_float,
1018
- actual_float,
1019
- epsilon)
1020
- full_message = _assert_in_epsilon_message(expected_float,
1021
- actual_float,
1022
- epsilon,
1023
- message,
1024
- :negative_assertion => true)
1153
+ begin
1154
+ delta = expected_float * epsilon
1155
+ pass = (expected_float - actual_float).abs > delta
1156
+ assert_operator(epsilon, :>=, 0.0, "The epsilon should not be negative")
1157
+ full_message = _assert_in_epsilon_message(expected_float,
1158
+ expected_float,
1159
+ actual_float,
1160
+ actual_float,
1161
+ epsilon,
1162
+ epsilon,
1163
+ delta,
1164
+ message,
1165
+ :negative_assertion => true)
1166
+ rescue Test::Unit::AssertionFailedError
1167
+ # for the above assert_operator
1168
+ raise
1169
+ rescue
1170
+ _assert_in_epsilon_validate_arguments(expected_float,
1171
+ actual_float,
1172
+ epsilon)
1173
+ normalized_expected = expected_float.to_f
1174
+ normalized_actual = actual_float.to_f
1175
+ normalized_epsilon = epsilon.to_f
1176
+ delta = normalized_expected * normalized_epsilon
1177
+ pass = (normalized_expected - normalized_actual).abs > delta
1178
+ full_message = _assert_in_epsilon_message(expected_float,
1179
+ normalized_expected,
1180
+ actual_float,
1181
+ normalized_actual,
1182
+ epsilon,
1183
+ normalized_epsilon,
1184
+ delta,
1185
+ message,
1186
+ :negative_assertion => true)
1187
+ end
1025
1188
  assert_block(full_message) do
1026
- normalized_expected_float = expected_float.to_f
1027
- delta = normalized_expected_float * epsilon.to_f
1028
- (normalized_expected_float - actual_float.to_f).abs > delta
1189
+ pass
1029
1190
  end
1030
1191
  end
1031
1192
  end
@@ -1052,21 +1213,18 @@ EOT
1052
1213
  assert_operator(epsilon, :>=, 0.0, "The epsilon should not be negative")
1053
1214
  end
1054
1215
 
1055
- def _assert_in_epsilon_message(expected_float, actual_float, epsilon,
1056
- message, options={})
1057
- normalized_expected = expected_float.to_f
1058
- normalized_actual = actual_float.to_f
1059
- normalized_epsilon = epsilon.to_f
1060
- delta = normalized_expected * normalized_epsilon
1061
-
1216
+ def _assert_in_epsilon_message(expected_float, normalized_expected,
1217
+ actual_float, normalized_actual,
1218
+ epsilon, normalized_epsilon,
1219
+ delta, message, options={})
1062
1220
  if options[:negative_assertion]
1063
1221
  format = <<-EOT
1064
- <?> -/+ (<?> * <?>)[?] expected to not include
1222
+ <?> -/+ (<?> * <?>)[?] was expected to not include
1065
1223
  <?>.
1066
1224
  EOT
1067
1225
  else
1068
1226
  format = <<-EOT
1069
- <?> -/+ (<?> * <?>)[?] expected to include
1227
+ <?> -/+ (<?> * <?>)[?] was expected to include
1070
1228
  <?>.
1071
1229
  EOT
1072
1230
  end
@@ -1099,7 +1257,7 @@ EOT
1099
1257
  end
1100
1258
 
1101
1259
  if relation_format
1102
- format << <<-EOT
1260
+ format += <<-EOT
1103
1261
 
1104
1262
  Relation:
1105
1263
  #{relation_format}
@@ -1112,9 +1270,9 @@ EOT
1112
1270
 
1113
1271
  public
1114
1272
  ##
1115
- # Passes if the method send returns a true value.
1273
+ # Passes if the method `__send__` returns not false nor nil.
1116
1274
  #
1117
- # +send_array+ is composed of:
1275
+ # `send_array` is composed of:
1118
1276
  # * A receiver
1119
1277
  # * A method
1120
1278
  # * Arguments to the method
@@ -1131,7 +1289,7 @@ EOT
1131
1289
  "assert_send requires at least a receiver " +
1132
1290
  "and a message name")
1133
1291
  format = <<EOT
1134
- <?> expected to respond to
1292
+ <?> was expected to respond to
1135
1293
  <?(*?)> with a true value but was
1136
1294
  <?>.
1137
1295
  EOT
@@ -1152,9 +1310,9 @@ EOT
1152
1310
  end
1153
1311
 
1154
1312
  ##
1155
- # Passes if the method send doesn't return a true value.
1313
+ # Passes if the method `__send__` returns false or nil.
1156
1314
  #
1157
- # +send_array+ is composed of:
1315
+ # `send_array` is composed of:
1158
1316
  # * A receiver
1159
1317
  # * A method
1160
1318
  # * Arguments to the method
@@ -1171,7 +1329,7 @@ EOT
1171
1329
  "assert_not_send requires at least a receiver " +
1172
1330
  "and a message name")
1173
1331
  format = <<EOT
1174
- <?> expected to respond to
1332
+ <?> was expected to respond to
1175
1333
  <?(*?)> with not a true value but was
1176
1334
  <?>.
1177
1335
  EOT
@@ -1192,7 +1350,7 @@ EOT
1192
1350
  end
1193
1351
 
1194
1352
  ##
1195
- # Passes if +actual+ is a boolean value.
1353
+ # Passes if `actual` is a boolean value.
1196
1354
  #
1197
1355
  # @example
1198
1356
  # assert_boolean(true) # -> pass
@@ -1202,13 +1360,13 @@ EOT
1202
1360
  assert_block(build_message(message,
1203
1361
  "<true> or <false> expected but was\n<?>",
1204
1362
  actual)) do
1205
- [true, false].include?(actual)
1363
+ true == actual || false == actual
1206
1364
  end
1207
1365
  end
1208
1366
  end
1209
1367
 
1210
1368
  ##
1211
- # Passes if +actual+ is true.
1369
+ # Passes if `actual` is true.
1212
1370
  #
1213
1371
  # @example
1214
1372
  # assert_true(true) # -> pass
@@ -1218,13 +1376,13 @@ EOT
1218
1376
  assert_block(build_message(message,
1219
1377
  "<true> expected but was\n<?>",
1220
1378
  actual)) do
1221
- actual == true
1379
+ true == actual
1222
1380
  end
1223
1381
  end
1224
1382
  end
1225
1383
 
1226
1384
  ##
1227
- # Passes if +actual+ is false.
1385
+ # Passes if `actual` is false.
1228
1386
  #
1229
1387
  # @example
1230
1388
  # assert_false(false) # -> pass
@@ -1234,14 +1392,14 @@ EOT
1234
1392
  assert_block(build_message(message,
1235
1393
  "<false> expected but was\n<?>",
1236
1394
  actual)) do
1237
- actual == false
1395
+ false == actual
1238
1396
  end
1239
1397
  end
1240
1398
  end
1241
1399
 
1242
1400
  ##
1243
- # Passes if expression "+expected+ +operator+
1244
- # +actual+" is true.
1401
+ # Passes if expression "`expected` `operator`
1402
+ # `actual`" is not false nor nil.
1245
1403
  #
1246
1404
  # @example
1247
1405
  # assert_compare(1, "<", 10) # -> pass
@@ -1261,7 +1419,7 @@ EOT
1261
1419
  end
1262
1420
  template = <<-EOT
1263
1421
  <?> #{operator} <?> should be true
1264
- <?> expected #{operator_description}
1422
+ <?> was expected to be #{operator_description}
1265
1423
  <?>.
1266
1424
  EOT
1267
1425
  full_message = build_message(message, template,
@@ -1296,7 +1454,7 @@ EOT
1296
1454
 
1297
1455
  ##
1298
1456
  # Passes if an exception is raised in block and its
1299
- # message is +expected+.
1457
+ # message is `expected`.
1300
1458
  #
1301
1459
  # @example
1302
1460
  # assert_raise_message("exception") {raise "exception"} # -> pass
@@ -1306,7 +1464,7 @@ EOT
1306
1464
  def assert_raise_message(expected, message=nil)
1307
1465
  _wrap_assertion do
1308
1466
  full_message = build_message(message,
1309
- "<?> exception message expected " +
1467
+ "<?> exception message was expected " +
1310
1468
  "but none was thrown.",
1311
1469
  expected)
1312
1470
  exception = nil
@@ -1336,7 +1494,7 @@ EOT
1336
1494
  end
1337
1495
 
1338
1496
  ##
1339
- # Passes if +object+.const_defined?(+constant_name+)
1497
+ # Passes if `object`.const_defined?(`constant_name`)
1340
1498
  #
1341
1499
  # @example
1342
1500
  # assert_const_defined(Test, :Unit) # -> pass
@@ -1353,7 +1511,7 @@ EOT
1353
1511
  end
1354
1512
 
1355
1513
  ##
1356
- # Passes if !+object+.const_defined?(+constant_name+)
1514
+ # Passes if !`object`.const_defined?(`constant_name`)
1357
1515
  #
1358
1516
  # @example
1359
1517
  # assert_not_const_defined(Object, :Nonexistent) # -> pass
@@ -1370,7 +1528,7 @@ EOT
1370
1528
  end
1371
1529
 
1372
1530
  ##
1373
- # Passes if +object+.+predicate+ is _true_.
1531
+ # Passes if `object`.`predicate` is not false nor nil.
1374
1532
  #
1375
1533
  # @example
1376
1534
  # assert_predicate([], :empty?) # -> pass
@@ -1392,7 +1550,7 @@ EOT
1392
1550
  end
1393
1551
 
1394
1552
  ##
1395
- # Passes if +object+.+predicate+ is not _true_.
1553
+ # Passes if `object`.`predicate` is false or nil.
1396
1554
  #
1397
1555
  # @example
1398
1556
  # assert_not_predicate([1], :empty?) # -> pass
@@ -1419,8 +1577,8 @@ EOT
1419
1577
  alias_method :refute_predicate, :assert_not_predicate
1420
1578
 
1421
1579
  ##
1422
- # Passes if +object+#+alias_name+ is an alias method of
1423
- # +object+#+original_name+.
1580
+ # Passes if `object`#`alias_name` is an alias method of
1581
+ # `object`#`original_name`.
1424
1582
  #
1425
1583
  # @example
1426
1584
  # assert_alias_method([], :length, :size) # -> pass
@@ -1467,7 +1625,7 @@ EOT
1467
1625
  end
1468
1626
 
1469
1627
  ##
1470
- # Passes if +path+ exists.
1628
+ # Passes if `path` exists.
1471
1629
  #
1472
1630
  # @example
1473
1631
  # assert_path_exist("/tmp") # -> pass
@@ -1476,7 +1634,7 @@ EOT
1476
1634
  def assert_path_exist(path, message=nil)
1477
1635
  _wrap_assertion do
1478
1636
  failure_message = build_message(message,
1479
- "<?> expected to exist",
1637
+ "<?> was expected to exist",
1480
1638
  path)
1481
1639
  assert_block(failure_message) do
1482
1640
  File.exist?(path)
@@ -1485,7 +1643,7 @@ EOT
1485
1643
  end
1486
1644
 
1487
1645
  ##
1488
- # Passes if +path+ doesn't exist.
1646
+ # Passes if `path` doesn't exist.
1489
1647
  #
1490
1648
  # @example
1491
1649
  # assert_path_not_exist("/nonexistent") # -> pass
@@ -1494,7 +1652,7 @@ EOT
1494
1652
  def assert_path_not_exist(path, message=nil)
1495
1653
  _wrap_assertion do
1496
1654
  failure_message = build_message(message,
1497
- "<?> expected to not exist",
1655
+ "<?> was expected to not exist",
1498
1656
  path)
1499
1657
  assert_block(failure_message) do
1500
1658
  not File.exist?(path)
@@ -1503,7 +1661,7 @@ EOT
1503
1661
  end
1504
1662
 
1505
1663
  ##
1506
- # Passes if +collection+ includes +object+.
1664
+ # Passes if `collection` includes `object`.
1507
1665
  #
1508
1666
  # @example
1509
1667
  # assert_include([1, 10], 1) # -> pass
@@ -1515,7 +1673,7 @@ EOT
1515
1673
  assert_respond_to(collection, :include?,
1516
1674
  "The collection must respond to :include?.")
1517
1675
  full_message = build_message(message,
1518
- "<?> expected to include\n<?>.",
1676
+ "<?> was expected to include\n<?>.",
1519
1677
  collection,
1520
1678
  object)
1521
1679
  assert_block(full_message) do
@@ -1530,7 +1688,7 @@ EOT
1530
1688
  alias_method :assert_includes, :assert_include
1531
1689
 
1532
1690
  ##
1533
- # Passes if +collection+ doesn't include +object+.
1691
+ # Passes if `collection` doesn't include `object`.
1534
1692
  #
1535
1693
  # @example
1536
1694
  # assert_not_include([1, 10], 5) # -> pass
@@ -1542,7 +1700,7 @@ EOT
1542
1700
  assert_respond_to(collection, :include?,
1543
1701
  "The collection must respond to :include?.")
1544
1702
  full_message = build_message(message,
1545
- "<?> expected to not include\n<?>.",
1703
+ "<?> was expected to not include\n<?>.",
1546
1704
  collection,
1547
1705
  object)
1548
1706
  assert_block(full_message) do
@@ -1562,7 +1720,7 @@ EOT
1562
1720
  alias_method :refute_includes, :assert_not_include
1563
1721
 
1564
1722
  ##
1565
- # Passes if +object+ is empty.
1723
+ # Passes if `object` is empty.
1566
1724
  #
1567
1725
  # @example
1568
1726
  # assert_empty("") # -> pass
@@ -1576,7 +1734,7 @@ EOT
1576
1734
  assert_respond_to(object, :empty?,
1577
1735
  "The object must respond to :empty?.")
1578
1736
  full_message = build_message(message,
1579
- "<?> expected to be empty.",
1737
+ "<?> was expected to be empty.",
1580
1738
  object)
1581
1739
  assert_block(full_message) do
1582
1740
  object.empty?
@@ -1585,7 +1743,7 @@ EOT
1585
1743
  end
1586
1744
 
1587
1745
  ##
1588
- # Passes if +object+ is not empty.
1746
+ # Passes if `object` is not empty.
1589
1747
  #
1590
1748
  # @example
1591
1749
  # assert_not_empty(" ") # -> pass
@@ -1599,7 +1757,7 @@ EOT
1599
1757
  assert_respond_to(object, :empty?,
1600
1758
  "The object must respond to :empty?.")
1601
1759
  full_message = build_message(message,
1602
- "<?> expected to not be empty.",
1760
+ "<?> was expected to not be empty.",
1603
1761
  object)
1604
1762
  assert_block(full_message) do
1605
1763
  not object.empty?
@@ -1612,12 +1770,140 @@ EOT
1612
1770
  # @since 3.0.0
1613
1771
  alias_method :refute_empty, :assert_not_empty
1614
1772
 
1773
+ # @overload assert_all(collection, message=nil, &block)
1774
+ #
1775
+ # Asserts that all `block.call(item)` where `item` is each
1776
+ # item in `collection` are not false nor nil.
1777
+ #
1778
+ # If `collection` is empty, this assertion is always passed
1779
+ # with any `block`.
1780
+ #
1781
+ # @example Pass patterns
1782
+ # assert_all([1, 2, 3]) {|item| item > 0} # => pass
1783
+ # assert_all([1, 2, 3], &:positive?) # => pass
1784
+ # assert_all([]) {|item| false} # => pass
1785
+ #
1786
+ # @example Failure pattern
1787
+ # assert_all([0, 1, 2], &:zero?) # => failure
1788
+ #
1789
+ # @param [#each] collection The check target.
1790
+ # @param [String] message The additional user message. It is
1791
+ # showed when the assertion is failed.
1792
+ # @yield [Object] Give each item in `collection` to the block.
1793
+ # @yieldreturn [Object] The checked object.
1794
+ # @return [void]
1795
+ #
1796
+ # @since 3.4.4
1797
+ def assert_all(collection, message=nil)
1798
+ _wrap_assertion do
1799
+ failed = false
1800
+ result = {}
1801
+ collection.each do |item|
1802
+ element_result = yield(item)
1803
+ failed = true unless element_result
1804
+ result[item] = element_result
1805
+ end
1806
+ format = <<-FORMAT
1807
+ <?> was expected to be all true values with the given block but was
1808
+ <?>
1809
+ FORMAT
1810
+ full_message = build_message(message,
1811
+ format,
1812
+ collection,
1813
+ result)
1814
+ assert_block(full_message) do
1815
+ not failed
1816
+ end
1817
+ end
1818
+ end
1819
+
1820
+ # Just for Ruby's tool/lib/test/unit compatibility.
1821
+ #
1822
+ # @since 3.4.3
1823
+ alias_method :assert_all?, :assert_all
1824
+
1825
+ # @overload assert_nothing_leaked_memory(max_increasable_size, target=:physical, message=nil, &block)
1826
+ #
1827
+ # Asserts that increased memory usage by `block.call` is less
1828
+ # than `max_increasable_size`. `GC.start` is called before and
1829
+ # after `block.call`.
1830
+ #
1831
+ # This assertion may be fragile. Because memory usage is
1832
+ # depends on the current Ruby process's memory
1833
+ # usage. Launching a new Ruby process for this will produce
1834
+ # more stable result but we need to specify target code as
1835
+ # `String` instead of block for the approach. We choose easy
1836
+ # to write API approach rather than more stable result
1837
+ # approach for this case.
1838
+ #
1839
+ # @example Pass pattern
1840
+ # require "objspace"
1841
+ # size_per_object = ObjectSpace.memsize_of("Hello")
1842
+ # # If memory isn't leaked, physical memory of almost created objects
1843
+ # # (1000 - 10 objects) must be freed.
1844
+ # assert_nothing_leaked_memory(size_per_object * 10) do
1845
+ # 1_000.times do
1846
+ # "Hello".dup
1847
+ # end
1848
+ # end # => pass
1849
+ #
1850
+ # @example Failure pattern
1851
+ # require "objspace"
1852
+ # size_per_object = ObjectSpace.memsize_of("Hello")
1853
+ # strings = []
1854
+ # assert_nothing_leaked_memory(size_per_object * 10) do
1855
+ # 10_000.times do
1856
+ # # Created objects aren't GC-ed because they are referred.
1857
+ # strings << "Hello".dup
1858
+ # end
1859
+ # end # => failure
1860
+ #
1861
+ # @param target [:physical, :virtual] which memory usage is
1862
+ # used for comparing. `:physical` means physical memory usage
1863
+ # also known as Resident Set Size (RSS). `:virtual` means
1864
+ # virtual memory usage.
1865
+ # @yield [] do anything you want to measure memory usage
1866
+ # in the block.
1867
+ # @yieldreturn [void]
1868
+ # @return [void]
1869
+ #
1870
+ # @since 3.4.5
1871
+ def assert_nothing_leaked_memory(max_increasable_size,
1872
+ target=:physical,
1873
+ message=nil)
1874
+ _wrap_assertion do
1875
+ GC.start
1876
+ before = Util::MemoryUsage.new
1877
+ unless before.collected?
1878
+ omit("memory usage collection isn't supported on this platform")
1879
+ end
1880
+ yield
1881
+ GC.start
1882
+ after = Util::MemoryUsage.new
1883
+ before_value = before.__send__(target)
1884
+ after_value = after.__send__(target)
1885
+ actual_increased_size = after_value - before_value
1886
+ template = <<-TEMPLATE
1887
+ <?> was expected to be less than
1888
+ <?>.
1889
+ TEMPLATE
1890
+ full_message = build_message(message,
1891
+ template,
1892
+ actual_increased_size,
1893
+ max_increasable_size)
1894
+ assert_block(full_message) do
1895
+ actual_increased_size < max_increasable_size
1896
+ end
1897
+ end
1898
+ end
1899
+
1615
1900
  ##
1616
- # Builds a failure message. +head+ is added before the +template+ and
1617
- # +arguments+ replaces the '?'s positionally in the template.
1618
- def build_message(head, template=nil, *arguments)
1901
+ # Builds a failure message. `user_message` is added before the
1902
+ # `template` and `arguments` replaces the '?'s positionally in
1903
+ # the template.
1904
+ def build_message(user_message, template=nil, *arguments)
1619
1905
  template &&= template.chomp
1620
- return AssertionMessage.new(head, template, arguments)
1906
+ return AssertionMessage.new(user_message, template, arguments)
1621
1907
  end
1622
1908
 
1623
1909
  private
@@ -1667,7 +1953,7 @@ EOT
1667
1953
  expected = assert_exception_helper.expected_exceptions
1668
1954
  actual_exception = nil
1669
1955
  full_message = build_message(message,
1670
- "<?> exception expected " +
1956
+ "<?> exception was expected " +
1671
1957
  "but none was thrown.",
1672
1958
  expected)
1673
1959
  assert_block(full_message) do
@@ -1684,12 +1970,11 @@ EOT
1684
1970
  end
1685
1971
  end
1686
1972
 
1687
- def _set_failed_information(failure, expected, actual, user_message)
1973
+ def _set_failed_information(failure, expected, actual)
1688
1974
  failure.expected = expected
1689
1975
  failure.actual = actual
1690
1976
  failure.inspected_expected = AssertionMessage.convert(expected)
1691
1977
  failure.inspected_actual = AssertionMessage.convert(actual)
1692
- failure.user_message = user_message
1693
1978
  end
1694
1979
 
1695
1980
  class AssertionMessage
@@ -1776,7 +2061,7 @@ EOT
1776
2061
 
1777
2062
  if Diff.need_fold?(diff)
1778
2063
  folded_diff = Diff.folded_readable(from, to)
1779
- diff << "\n\nfolded diff:\n#{folded_diff}"
2064
+ diff += "\n\nfolded diff:\n#{folded_diff}"
1780
2065
  end
1781
2066
 
1782
2067
  diff
@@ -1790,9 +2075,9 @@ EOT
1790
2075
  inspector = Inspector.new(object)
1791
2076
  if use_pp
1792
2077
  begin
1793
- require 'pp' unless defined?(PP)
2078
+ require "pp" unless defined?(PP)
1794
2079
  begin
1795
- return PP.pp(inspector, '').chomp
2080
+ return PP.pp(inspector, String.new).chomp
1796
2081
  rescue NameError
1797
2082
  end
1798
2083
  rescue LoadError
@@ -2041,34 +2326,34 @@ EOT
2041
2326
  expanded_template = ""
2042
2327
  @parts.each do |part|
2043
2328
  if part == '?'
2044
- encoding_safe_concat(expanded_template, params.shift)
2329
+ param = params.shift
2330
+ if Object.const_defined?(:Encoding)
2331
+ expanded_template += concatenatable(param,
2332
+ expanded_template.encoding)
2333
+ else
2334
+ expanded_template += param
2335
+ end
2045
2336
  else
2046
- expanded_template << part.gsub(/\\\?/m, '?')
2337
+ expanded_template += part.gsub(/\\\?/m, '?')
2047
2338
  end
2048
2339
  end
2049
2340
  expanded_template
2050
2341
  end
2051
2342
 
2052
2343
  private
2053
- if Object.const_defined?(:Encoding)
2054
- def encoding_safe_concat(buffer, parameter)
2055
- if Encoding.compatible?(buffer, parameter)
2056
- buffer << parameter
2057
- else
2058
- buffer << parameter.dup.force_encoding(buffer.encoding)
2059
- end
2060
- end
2061
- else
2062
- def encoding_safe_concat(buffer, parameter)
2063
- buffer << parameter
2344
+ def concatenatable(text, encoding)
2345
+ if Encoding.compatible?(text, encoding)
2346
+ text
2347
+ else
2348
+ text.dup.force_encoding(encoding)
2064
2349
  end
2065
2350
  end
2066
2351
  end
2067
2352
 
2068
2353
  include Util::BacktraceFilter
2069
2354
 
2070
- def initialize(head, template_string, parameters)
2071
- @head = head
2355
+ def initialize(user_message, template_string, parameters)
2356
+ @user_message = user_message
2072
2357
  @template_string = template_string
2073
2358
  @parameters = parameters
2074
2359
  end
@@ -2081,24 +2366,28 @@ EOT
2081
2366
  @template ||= Template.create(@template_string)
2082
2367
  end
2083
2368
 
2084
- def add_period(string)
2085
- (string =~ /\.\Z/ ? string : string + '.')
2369
+ def user_message
2370
+ return nil unless @user_message
2371
+ message = @user_message
2372
+ message = message.call if message.respond_to?(:call)
2373
+ message.to_s
2086
2374
  end
2087
2375
 
2088
2376
  def to_s
2089
2377
  message_parts = []
2090
- if (@head)
2091
- head = @head
2092
- head = head.call if head.respond_to?(:call)
2093
- head = head.to_s
2094
- unless(head.empty?)
2095
- message_parts << add_period(head)
2096
- end
2378
+ head = user_message
2379
+ if head and not head.empty?
2380
+ message_parts << add_period(head)
2097
2381
  end
2098
2382
  tail = template.result(@parameters.collect{|e| convert(e)})
2099
2383
  message_parts << tail unless(tail.empty?)
2100
2384
  message_parts.join("\n")
2101
2385
  end
2386
+
2387
+ private
2388
+ def add_period(string)
2389
+ (string =~ /\.\Z/ ? string : string + '.')
2390
+ end
2102
2391
  end
2103
2392
 
2104
2393
  class AssertExceptionHelper
@@ -2110,7 +2399,14 @@ EOT
2110
2399
 
2111
2400
  def inspect
2112
2401
  if default_inspect?
2113
- "#{@exception.class.inspect}(<#{@exception.message}>)"
2402
+ inspected = "#{@exception.class.inspect}(<#{@exception.message}>)"
2403
+ unless (@exception.backtrace || []).empty?
2404
+ inspected += "\n"
2405
+ @exception.backtrace.each do |trace|
2406
+ inspected << "#{trace}\n"
2407
+ end
2408
+ end
2409
+ inspected
2114
2410
  else
2115
2411
  @exception.inspect
2116
2412
  end
@@ -2170,19 +2466,45 @@ EOT
2170
2466
  expected_exceptions.each do |exception_type|
2171
2467
  if exception_type.instance_of?(Module)
2172
2468
  exception_modules << exception_type
2173
- elsif exception_type.is_a?(Exception)
2469
+ elsif exception_object?(exception_type)
2174
2470
  exception_objects << exception_type
2175
- else
2176
- @test_case.__send__(:assert,
2177
- Exception >= exception_type,
2178
- "Should expect a class of exception, " +
2179
- "#{exception_type}")
2471
+ elsif exception_class?(exception_type)
2180
2472
  exception_classes << exception_type
2473
+ else
2474
+ full_message =
2475
+ @test_case.__send__(:build_message,
2476
+ nil,
2477
+ "<?> must be " +
2478
+ "a subclass of Exception, " +
2479
+ "an object of Exception subclasses " +
2480
+ "or a Module",
2481
+ exception_type)
2482
+ @test_case.flunk(full_message)
2181
2483
  end
2182
2484
  end
2183
2485
  [exception_classes, exception_modules, exception_objects]
2184
2486
  end
2185
2487
 
2488
+ def exception_object?(exception_type)
2489
+ return true if exception_type.is_a?(Exception)
2490
+
2491
+ if Object.const_defined?(:Java)
2492
+ return true if exception_type.is_a?(Java::JavaLang::Throwable)
2493
+ end
2494
+
2495
+ false
2496
+ end
2497
+
2498
+ def exception_class?(exception_type)
2499
+ return true if exception_type <= Exception
2500
+
2501
+ if Object.const_defined?(:Java)
2502
+ return true if exception_type <= Java::JavaLang::Throwable
2503
+ end
2504
+
2505
+ false
2506
+ end
2507
+
2186
2508
  def expected_class?(actual_exception, equality)
2187
2509
  @expected_classes.any? do |expected_class|
2188
2510
  actual_exception.__send__(equality, expected_class)