test-unit 3.2.0 → 3.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/COPYING +4 -1
  3. data/README.md +11 -11
  4. data/Rakefile +10 -1
  5. data/doc/text/getting-started.md +246 -0
  6. data/doc/text/news.md +372 -1
  7. data/lib/test/unit.rb +171 -157
  8. data/lib/test/unit/assertions.rb +187 -149
  9. data/lib/test/unit/attribute.rb +71 -2
  10. data/lib/test/unit/autorunner.rb +65 -32
  11. data/lib/test/unit/code-snippet-fetcher.rb +7 -7
  12. data/lib/test/unit/collector/load.rb +8 -13
  13. data/lib/test/unit/data-sets.rb +116 -0
  14. data/lib/test/unit/data.rb +121 -12
  15. data/lib/test/unit/diff.rb +11 -11
  16. data/lib/test/unit/fixture.rb +3 -0
  17. data/lib/test/unit/notification.rb +9 -7
  18. data/lib/test/unit/omission.rb +34 -31
  19. data/lib/test/unit/pending.rb +12 -11
  20. data/lib/test/unit/priority.rb +7 -3
  21. data/lib/test/unit/runner/console.rb +25 -0
  22. data/lib/test/unit/test-suite-creator.rb +22 -8
  23. data/lib/test/unit/testcase.rb +270 -182
  24. data/lib/test/unit/ui/console/testrunner.rb +90 -35
  25. data/lib/test/unit/ui/emacs/testrunner.rb +5 -5
  26. data/lib/test/unit/util/observable.rb +2 -2
  27. data/lib/test/unit/util/output.rb +5 -4
  28. data/lib/test/unit/util/procwrapper.rb +4 -4
  29. data/lib/test/unit/version.rb +1 -1
  30. data/test/collector/test-descendant.rb +4 -0
  31. data/test/collector/test-load.rb +35 -2
  32. data/test/collector/test_dir.rb +5 -4
  33. data/test/collector/test_objectspace.rb +7 -5
  34. data/test/test-assertions.rb +128 -101
  35. data/test/test-code-snippet.rb +42 -0
  36. data/test/test-data.rb +195 -79
  37. data/test/test-priority.rb +19 -8
  38. data/test/test-test-case.rb +111 -3
  39. data/test/test-test-suite.rb +1 -0
  40. data/test/testunit-test-util.rb +2 -0
  41. metadata +38 -37
@@ -32,9 +32,9 @@ module Test
32
32
  #
33
33
  # @example Example Custom Assertion
34
34
  #
35
- # def deny(boolean, message = nil)
36
- # message = build_message message, '<?> is not false or nil.', boolean
37
- # assert_block message do
35
+ # def deny(boolean, message=nil)
36
+ # message = build_message(message, '<?> is not false or nil.', boolean)
37
+ # assert_block(message) do
38
38
  # not boolean
39
39
  # end
40
40
  # end
@@ -52,7 +52,11 @@ module Test
52
52
  def assert_block(message="assert_block failed.")
53
53
  _wrap_assertion do
54
54
  if (! yield)
55
- raise AssertionFailedError.new(message.to_s)
55
+ options = {}
56
+ if message.respond_to?(:user_message)
57
+ options[:user_message] = message.user_message
58
+ end
59
+ raise AssertionFailedError.new(message.to_s, options)
56
60
  end
57
61
  end
58
62
  end
@@ -153,8 +157,8 @@ module Test
153
157
  assertion_message = message
154
158
  else
155
159
  error_message = "assertion message must be String, Proc or "
156
- error_message << "#{AssertionMessage}: "
157
- error_message << "<#{message.inspect}>(<#{message.class}>)"
160
+ error_message += "#{AssertionMessage}: "
161
+ error_message += "<#{message.inspect}>(<#{message.class}>)"
158
162
  raise ArgumentError, error_message, filter_backtrace(caller)
159
163
  end
160
164
  assertion_message ||= build_message(message,
@@ -167,7 +171,7 @@ module Test
167
171
  end
168
172
  end
169
173
 
170
- # Asserts that +object+ is false or nil.
174
+ # Asserts that `object` is false or nil.
171
175
  #
172
176
  # @note Just for minitest compatibility. :<
173
177
  #
@@ -192,8 +196,8 @@ module Test
192
196
  assertion_message = message
193
197
  else
194
198
  error_message = "assertion message must be String, Proc or "
195
- error_message << "#{AssertionMessage}: "
196
- error_message << "<#{message.inspect}>(<#{message.class}>)"
199
+ error_message += "#{AssertionMessage}: "
200
+ error_message += "<#{message.inspect}>(<#{message.class}>)"
197
201
  raise ArgumentError, error_message, filter_backtrace(caller)
198
202
  end
199
203
  assert_block("refute should not be called with a block.") do
@@ -209,7 +213,7 @@ module Test
209
213
  end
210
214
 
211
215
  ##
212
- # Passes if +expected+ == +actual+.
216
+ # Passes if `expected` == `actual`.
213
217
  #
214
218
  # Note that the ordering of arguments is important, since a helpful
215
219
  # error message is generated when this one fails that tells you the
@@ -239,7 +243,7 @@ EOT
239
243
  begin
240
244
  assert_block(full_message) { expected == actual }
241
245
  rescue AssertionFailedError => failure
242
- _set_failed_information(failure, expected, actual, message)
246
+ _set_failed_information(failure, expected, actual)
243
247
  raise failure # For JRuby. :<
244
248
  end
245
249
  end
@@ -275,8 +279,7 @@ EOT
275
279
  assert_exception_helper.expected?(actual_exception)
276
280
  end
277
281
  rescue AssertionFailedError => failure
278
- _set_failed_information(failure, expected, actual_exception,
279
- message)
282
+ _set_failed_information(failure, expected, actual_exception)
280
283
  raise failure # For JRuby. :<
281
284
  end
282
285
  end
@@ -311,7 +314,7 @@ EOT
311
314
 
312
315
 
313
316
  ##
314
- # Passes if +object+.instance_of?(+klass+). When +klass+ is
317
+ # Passes if `object`.instance_of?(`klass`). When `klass` is
315
318
  # an array of classes, it passes if any class
316
319
  # satisfies +object.instance_of?(class).
317
320
  #
@@ -319,7 +322,7 @@ EOT
319
322
  # assert_instance_of(String, 'foo') # -> pass
320
323
  # assert_instance_of([Fixnum, NilClass], 100) # -> pass
321
324
  # assert_instance_of([Numeric, NilClass], 100) # -> fail
322
- def assert_instance_of(klass, object, message="")
325
+ def assert_instance_of(klass, object, message=nil)
323
326
  _wrap_assertion do
324
327
  if klass.is_a?(Array)
325
328
  klasses = klass
@@ -334,7 +337,7 @@ EOT
334
337
  "<#{value}>"
335
338
  end
336
339
  full_message = build_message(message, <<EOT, object, klass_message, object.class)
337
- <?> expected to be instance_of\\?
340
+ <?> was expected to be instance_of\\?
338
341
  ? but was
339
342
  <?>.
340
343
  EOT
@@ -345,8 +348,8 @@ EOT
345
348
  end
346
349
 
347
350
  ##
348
- # Passes if +object+.instance_of?(+klass+) does not hold.
349
- # When +klass+ is an array of classes, it passes if no class
351
+ # Passes if `object`.instance_of?(`klass`) does not hold.
352
+ # When `klass` is an array of classes, it passes if no class
350
353
  # satisfies +object.instance_of?(class).
351
354
  #
352
355
  # @example
@@ -355,14 +358,14 @@ EOT
355
358
  # assert_not_instance_of([Numeric, NilClass], 100) # -> fail
356
359
  #
357
360
  # @since 3.0.0
358
- def assert_not_instance_of(klass, object, message="")
361
+ def assert_not_instance_of(klass, object, message=nil)
359
362
  _wrap_assertion do
360
363
  if klass.is_a?(Array)
361
364
  klasses = klass
362
365
  else
363
366
  klasses = [klass]
364
367
  end
365
- assert_block("The first parameter to assert_not_instance_of should be " <<
368
+ assert_block("The first parameter to assert_not_instance_of should be " +
366
369
  "a Class or an Array of Class.") do
367
370
  klasses.all? {|k| k.is_a?(Class)}
368
371
  end
@@ -370,7 +373,7 @@ EOT
370
373
  "<#{value}>"
371
374
  end
372
375
  full_message = build_message(message,
373
- "<?> expected to not be instance_of\\?\n" +
376
+ "<?> was expected to not be instance_of\\?\n" +
374
377
  "? but was.",
375
378
  object,
376
379
  klass_message)
@@ -386,19 +389,19 @@ EOT
386
389
  alias_method :refute_instance_of, :assert_not_instance_of
387
390
 
388
391
  ##
389
- # Passes if +object+ is nil.
392
+ # Passes if `object` is nil.
390
393
  #
391
394
  # @example
392
395
  # assert_nil [1, 2].uniq!
393
- def assert_nil(object, message="")
396
+ def assert_nil(object, message=nil)
394
397
  full_message = build_message(message, <<EOT, object)
395
- <?> expected to be nil.
398
+ <?> was expected to be nil.
396
399
  EOT
397
400
  assert_block(full_message) { object.nil? }
398
401
  end
399
402
 
400
403
  ##
401
- # Passes if +object+.kind_of?(+klass+). When +klass+ is
404
+ # Passes if `object`.kind_of?(`klass`). When `klass` is
402
405
  # an array of classes or modules, it passes if any
403
406
  # class or module satisfies +object.kind_of?(class_or_module).
404
407
  #
@@ -406,7 +409,7 @@ EOT
406
409
  # assert_kind_of(Object, 'foo') # -> pass
407
410
  # assert_kind_of([Fixnum, NilClass], 100) # -> pass
408
411
  # assert_kind_of([Fixnum, NilClass], "string") # -> fail
409
- def assert_kind_of(klass, object, message="")
412
+ def assert_kind_of(klass, object, message=nil)
410
413
  _wrap_assertion do
411
414
  if klass.is_a?(Array)
412
415
  klasses = klass
@@ -421,7 +424,7 @@ EOT
421
424
  "<#{value}>"
422
425
  end
423
426
  full_message = build_message(message,
424
- "<?> expected to be kind_of\\?\n" +
427
+ "<?> was expected to be kind_of\\?\n" +
425
428
  "? but was\n" +
426
429
  "<?>.",
427
430
  object,
@@ -434,8 +437,8 @@ EOT
434
437
  end
435
438
 
436
439
  ##
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
440
+ # Passes if `object`.kind_of?(`klass`) does not hold.
441
+ # When `klass` is an array of classes or modules, it passes only if all
439
442
  # classes (and modules) do not satisfy +object.kind_of?(class_or_module).
440
443
  #
441
444
  # @example
@@ -444,7 +447,7 @@ EOT
444
447
  # assert_not_kind_of([Fixnum, NilClass], 100) # -> fail
445
448
  #
446
449
  # @since 3.0.0
447
- def assert_not_kind_of(klass, object, message="")
450
+ def assert_not_kind_of(klass, object, message=nil)
448
451
  _wrap_assertion do
449
452
  if klass.is_a?(Array)
450
453
  klasses = klass
@@ -459,7 +462,7 @@ EOT
459
462
  "<#{value}>"
460
463
  end
461
464
  full_message = build_message(message,
462
- "<?> expected to not be kind_of\\?\n" +
465
+ "<?> was expected to not be kind_of\\?\n" +
463
466
  "? but was.",
464
467
  object,
465
468
  klass_message)
@@ -475,11 +478,11 @@ EOT
475
478
  alias_method :refute_kind_of, :assert_not_kind_of
476
479
 
477
480
  ##
478
- # Passes if +object+ .respond_to? +method+
481
+ # Passes if `object` .respond_to? `method`
479
482
  #
480
483
  # @example
481
484
  # assert_respond_to 'bugbear', :slice
482
- def assert_respond_to(object, method, message="")
485
+ def assert_respond_to(object, method, message=nil)
483
486
  _wrap_assertion do
484
487
  full_message = build_message(message,
485
488
  "<?>.kind_of\\?(Symbol) or\n" +
@@ -497,12 +500,12 @@ EOT
497
500
  end
498
501
 
499
502
  ##
500
- # Passes if +object+ does not .respond_to? +method+.
503
+ # Passes if `object` does not .respond_to? `method`.
501
504
  #
502
505
  # @example
503
506
  # assert_not_respond_to('bugbear', :nonexistence) # -> pass
504
507
  # assert_not_respond_to('bugbear', :size) # -> fail
505
- def assert_not_respond_to(object, method, message="")
508
+ def assert_not_respond_to(object, method, message=nil)
506
509
  _wrap_assertion do
507
510
  full_message = build_message(message,
508
511
  "<?>.kind_of\\?(Symbol) or\n" +
@@ -525,11 +528,11 @@ EOT
525
528
  alias_method :refute_respond_to, :assert_not_respond_to
526
529
 
527
530
  ##
528
- # Passes if +pattern+ =~ +string+.
531
+ # Passes if `pattern` =~ `string`.
529
532
  #
530
533
  # @example
531
534
  # assert_match(/\d+/, 'five, 6, seven')
532
- def assert_match(pattern, string, message="")
535
+ def assert_match(pattern, string, message=nil)
533
536
  _wrap_assertion do
534
537
  pattern = case(pattern)
535
538
  when String
@@ -537,23 +540,24 @@ EOT
537
540
  else
538
541
  pattern
539
542
  end
540
- full_message = build_message(message, "<?> expected to be =~\n<?>.",
543
+ full_message = build_message(message,
544
+ "<?> was expected to be =~\n<?>.",
541
545
  pattern, string)
542
546
  assert_block(full_message) { pattern =~ string }
543
547
  end
544
548
  end
545
549
 
546
550
  ##
547
- # Passes if +actual+ .equal? +expected+ (i.e. they are the same
551
+ # Passes if `actual` .equal? `expected` (i.e. they are the same
548
552
  # instance).
549
553
  #
550
554
  # @example
551
555
  # o = Object.new
552
556
  # assert_same o, o
553
- def assert_same(expected, actual, message="")
557
+ def assert_same(expected, actual, message=nil)
554
558
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
555
559
  <?>
556
- with id <?> expected to be equal\\? to
560
+ with id <?> was expected to be equal\\? to
557
561
  <?>
558
562
  with id <?>.
559
563
  EOT
@@ -561,18 +565,18 @@ EOT
561
565
  end
562
566
 
563
567
  ##
564
- # Compares the +object1+ with +object2+ using +operator+.
568
+ # Compares the `object1` with `object2` using `operator`.
565
569
  #
566
570
  # Passes if object1.__send__(operator, object2) is true.
567
571
  #
568
572
  # @example
569
573
  # assert_operator 5, :>=, 4
570
- def assert_operator(object1, operator, object2, message="")
574
+ def assert_operator(object1, operator, object2, message=nil)
571
575
  _wrap_assertion do
572
576
  full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
573
577
  assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
574
578
  full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
575
- <?> expected to be
579
+ <?> was expected to be
576
580
  ?
577
581
  <?>.
578
582
  EOT
@@ -581,7 +585,7 @@ EOT
581
585
  end
582
586
 
583
587
  ##
584
- # Compares the +object1+ with +object2+ using +operator+.
588
+ # Compares the `object1` with `object2` using `operator`.
585
589
  #
586
590
  # Passes if object1.__send__(operator, object2) is not true.
587
591
  #
@@ -590,12 +594,12 @@ EOT
590
594
  # assert_not_operator(5, :>, 4) # => fail
591
595
  #
592
596
  # @since 3.0.0
593
- def assert_not_operator(object1, operator, object2, message="")
597
+ def assert_not_operator(object1, operator, object2, message=nil)
594
598
  _wrap_assertion do
595
599
  full_message = build_message(nil, "<?>\ngiven as the operator for #assert_not_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
596
600
  assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
597
601
  full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
598
- <?> expected to not be
602
+ <?> was expected to not be
599
603
  ?
600
604
  <?>.
601
605
  EOT
@@ -648,14 +652,14 @@ EOT
648
652
  end
649
653
 
650
654
  ##
651
- # Passes if ! +actual+ .equal? +expected+
655
+ # Passes if ! `actual` .equal? `expected`
652
656
  #
653
657
  # @example
654
658
  # assert_not_same Object.new, Object.new
655
- def assert_not_same(expected, actual, message="")
659
+ def assert_not_same(expected, actual, message=nil)
656
660
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
657
661
  <?>
658
- with id <?> expected to not be equal\\? to
662
+ with id <?> was expected to not be equal\\? to
659
663
  <?>
660
664
  with id <?>.
661
665
  EOT
@@ -668,12 +672,14 @@ EOT
668
672
  alias_method :refute_same, :assert_not_same
669
673
 
670
674
  ##
671
- # Passes if +expected+ != +actual+
675
+ # Passes if `expected` != `actual`
672
676
  #
673
677
  # @example
674
678
  # assert_not_equal 'some string', 5
675
- def assert_not_equal(expected, actual, message="")
676
- full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
679
+ def assert_not_equal(expected, actual, message=nil)
680
+ full_message = build_message(message,
681
+ "<?> was expected to be != to\n<?>.",
682
+ expected, actual)
677
683
  assert_block(full_message) { expected != actual }
678
684
  end
679
685
 
@@ -683,12 +689,14 @@ EOT
683
689
  alias_method :refute_equal, :assert_not_equal
684
690
 
685
691
  ##
686
- # Passes if ! +object+ .nil?
692
+ # Passes if ! `object` .nil?
687
693
  #
688
694
  # @example
689
695
  # assert_not_nil '1 two 3'.sub!(/two/, '2')
690
- def assert_not_nil(object, message="")
691
- full_message = build_message(message, "<?> expected to not be nil.", object)
696
+ def assert_not_nil(object, message=nil)
697
+ full_message = build_message(message,
698
+ "<?> was expected to not be nil.",
699
+ object)
692
700
  assert_block(full_message){!object.nil?}
693
701
  end
694
702
 
@@ -698,18 +706,18 @@ EOT
698
706
  alias_method :refute_nil, :assert_not_nil
699
707
 
700
708
  ##
701
- # Passes if +regexp+ !~ +string+
709
+ # Passes if `regexp` !~ `string`
702
710
  #
703
711
  # @example
704
712
  # assert_not_match(/two/, 'one 2 three') # -> pass
705
713
  # assert_not_match(/three/, 'one 2 three') # -> fail
706
- def assert_not_match(regexp, string, message="")
714
+ def assert_not_match(regexp, string, message=nil)
707
715
  _wrap_assertion do
708
716
  assert_instance_of(Regexp, regexp,
709
717
  "<REGEXP> in assert_not_match(<REGEXP>, ...) " +
710
718
  "should be a Regexp.")
711
719
  full_message = build_message(message,
712
- "<?> expected to not match\n<?>.",
720
+ "<?> was expected to not match\n<?>.",
713
721
  regexp, string)
714
722
  assert_block(full_message) { regexp !~ string }
715
723
  end
@@ -723,7 +731,7 @@ EOT
723
731
  ##
724
732
  # Deprecated. Use #assert_not_match instead.
725
733
  #
726
- # Passes if +regexp+ !~ +string+
734
+ # Passes if `regexp` !~ `string`
727
735
  #
728
736
  # @example
729
737
  # assert_no_match(/two/, 'one 2 three') # -> pass
@@ -781,13 +789,13 @@ EOT
781
789
  end
782
790
 
783
791
  ##
784
- # Passes if the block throws +expected_object+
792
+ # Passes if the block throws `expected_object`
785
793
  #
786
794
  # @example
787
795
  # assert_throw(:done) do
788
796
  # throw(:done)
789
797
  # end
790
- def assert_throw(expected_object, message="", &proc)
798
+ def assert_throw(expected_object, message=nil, &proc)
791
799
  _wrap_assertion do
792
800
  begin
793
801
  catch([]) {}
@@ -813,7 +821,7 @@ EOT
813
821
  tag = extractor.extract_tag
814
822
  raise if tag.nil?
815
823
  full_message = build_message(message,
816
- "<?> expected to be thrown but\n" +
824
+ "<?> was expected to be thrown but\n" +
817
825
  "<?> was thrown.",
818
826
  expected_object, tag)
819
827
  flunk(full_message)
@@ -833,7 +841,7 @@ EOT
833
841
  # assert_nothing_thrown do
834
842
  # [1, 2].uniq
835
843
  # end
836
- def assert_nothing_thrown(message="", &proc)
844
+ def assert_nothing_thrown(message=nil, &proc)
837
845
  _wrap_assertion do
838
846
  assert(block_given?, "Should have passed a block to assert_nothing_thrown")
839
847
  begin
@@ -852,8 +860,8 @@ EOT
852
860
  end
853
861
 
854
862
  ##
855
- # Passes if +expected_float+ and +actual_float+ are equal
856
- # within +delta+ tolerance.
863
+ # Passes if `expected_float` and `actual_float` are equal
864
+ # within `delta` tolerance.
857
865
  #
858
866
  # @example
859
867
  # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
@@ -873,8 +881,8 @@ EOT
873
881
  end
874
882
 
875
883
  ##
876
- # Passes if +expected_float+ and +actual_float+ are
877
- # not equal within +delta+ tolerance.
884
+ # Passes if `expected_float` and `actual_float` are
885
+ # not equal within `delta` tolerance.
878
886
  #
879
887
  # @example
880
888
  # assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
@@ -921,12 +929,12 @@ EOT
921
929
  message, options={})
922
930
  if options[:negative_assertion]
923
931
  format = <<-EOT
924
- <?> -/+ <?> expected to not include
932
+ <?> -/+ <?> was expected to not include
925
933
  <?>.
926
934
  EOT
927
935
  else
928
936
  format = <<-EOT
929
- <?> -/+ <?> expected to include
937
+ <?> -/+ <?> was expected to include
930
938
  <?>.
931
939
  EOT
932
940
  end
@@ -960,7 +968,7 @@ EOT
960
968
  end
961
969
 
962
970
  if relation_format
963
- format << <<-EOT
971
+ format += <<-EOT
964
972
 
965
973
  Relation:
966
974
  #{relation_format}
@@ -973,8 +981,8 @@ EOT
973
981
 
974
982
  public
975
983
  ##
976
- # Passes if +expected_float+ and +actual_float+ are equal
977
- # within +epsilon+ relative error of +expected_float+.
984
+ # Passes if `expected_float` and `actual_float` are equal
985
+ # within `epsilon` relative error of `expected_float`.
978
986
  #
979
987
  # @example
980
988
  # assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
@@ -1003,9 +1011,9 @@ EOT
1003
1011
  end
1004
1012
 
1005
1013
  ##
1006
- # Passes if +expected_float+ and +actual_float+ are
1007
- # not equal within +epsilon+ relative error of
1008
- # +expected_float+.
1014
+ # Passes if `expected_float` and `actual_float` are
1015
+ # not equal within `epsilon` relative error of
1016
+ # `expected_float`.
1009
1017
  #
1010
1018
  # @example
1011
1019
  # assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
@@ -1060,12 +1068,12 @@ EOT
1060
1068
 
1061
1069
  if options[:negative_assertion]
1062
1070
  format = <<-EOT
1063
- <?> -/+ (<?> * <?>)[?] expected to not include
1071
+ <?> -/+ (<?> * <?>)[?] was expected to not include
1064
1072
  <?>.
1065
1073
  EOT
1066
1074
  else
1067
1075
  format = <<-EOT
1068
- <?> -/+ (<?> * <?>)[?] expected to include
1076
+ <?> -/+ (<?> * <?>)[?] was expected to include
1069
1077
  <?>.
1070
1078
  EOT
1071
1079
  end
@@ -1098,7 +1106,7 @@ EOT
1098
1106
  end
1099
1107
 
1100
1108
  if relation_format
1101
- format << <<-EOT
1109
+ format += <<-EOT
1102
1110
 
1103
1111
  Relation:
1104
1112
  #{relation_format}
@@ -1113,7 +1121,7 @@ EOT
1113
1121
  ##
1114
1122
  # Passes if the method send returns a true value.
1115
1123
  #
1116
- # +send_array+ is composed of:
1124
+ # `send_array` is composed of:
1117
1125
  # * A receiver
1118
1126
  # * A method
1119
1127
  # * Arguments to the method
@@ -1130,7 +1138,7 @@ EOT
1130
1138
  "assert_send requires at least a receiver " +
1131
1139
  "and a message name")
1132
1140
  format = <<EOT
1133
- <?> expected to respond to
1141
+ <?> was expected to respond to
1134
1142
  <?(*?)> with a true value but was
1135
1143
  <?>.
1136
1144
  EOT
@@ -1153,7 +1161,7 @@ EOT
1153
1161
  ##
1154
1162
  # Passes if the method send doesn't return a true value.
1155
1163
  #
1156
- # +send_array+ is composed of:
1164
+ # `send_array` is composed of:
1157
1165
  # * A receiver
1158
1166
  # * A method
1159
1167
  # * Arguments to the method
@@ -1170,7 +1178,7 @@ EOT
1170
1178
  "assert_not_send requires at least a receiver " +
1171
1179
  "and a message name")
1172
1180
  format = <<EOT
1173
- <?> expected to respond to
1181
+ <?> was expected to respond to
1174
1182
  <?(*?)> with not a true value but was
1175
1183
  <?>.
1176
1184
  EOT
@@ -1191,7 +1199,7 @@ EOT
1191
1199
  end
1192
1200
 
1193
1201
  ##
1194
- # Passes if +actual+ is a boolean value.
1202
+ # Passes if `actual` is a boolean value.
1195
1203
  #
1196
1204
  # @example
1197
1205
  # assert_boolean(true) # -> pass
@@ -1207,7 +1215,7 @@ EOT
1207
1215
  end
1208
1216
 
1209
1217
  ##
1210
- # Passes if +actual+ is true.
1218
+ # Passes if `actual` is true.
1211
1219
  #
1212
1220
  # @example
1213
1221
  # assert_true(true) # -> pass
@@ -1223,7 +1231,7 @@ EOT
1223
1231
  end
1224
1232
 
1225
1233
  ##
1226
- # Passes if +actual+ is false.
1234
+ # Passes if `actual` is false.
1227
1235
  #
1228
1236
  # @example
1229
1237
  # assert_false(false) # -> pass
@@ -1239,8 +1247,8 @@ EOT
1239
1247
  end
1240
1248
 
1241
1249
  ##
1242
- # Passes if expression "+expected+ +operator+
1243
- # +actual+" is true.
1250
+ # Passes if expression "`expected` `operator`
1251
+ # `actual`" is true.
1244
1252
  #
1245
1253
  # @example
1246
1254
  # assert_compare(1, "<", 10) # -> pass
@@ -1260,7 +1268,7 @@ EOT
1260
1268
  end
1261
1269
  template = <<-EOT
1262
1270
  <?> #{operator} <?> should be true
1263
- <?> expected #{operator_description}
1271
+ <?> was expected to be #{operator_description}
1264
1272
  <?>.
1265
1273
  EOT
1266
1274
  full_message = build_message(message, template,
@@ -1295,7 +1303,7 @@ EOT
1295
1303
 
1296
1304
  ##
1297
1305
  # Passes if an exception is raised in block and its
1298
- # message is +expected+.
1306
+ # message is `expected`.
1299
1307
  #
1300
1308
  # @example
1301
1309
  # assert_raise_message("exception") {raise "exception"} # -> pass
@@ -1305,7 +1313,7 @@ EOT
1305
1313
  def assert_raise_message(expected, message=nil)
1306
1314
  _wrap_assertion do
1307
1315
  full_message = build_message(message,
1308
- "<?> exception message expected " +
1316
+ "<?> exception message was expected " +
1309
1317
  "but none was thrown.",
1310
1318
  expected)
1311
1319
  exception = nil
@@ -1335,7 +1343,7 @@ EOT
1335
1343
  end
1336
1344
 
1337
1345
  ##
1338
- # Passes if +object+.const_defined?(+constant_name+)
1346
+ # Passes if `object`.const_defined?(`constant_name`)
1339
1347
  #
1340
1348
  # @example
1341
1349
  # assert_const_defined(Test, :Unit) # -> pass
@@ -1352,7 +1360,7 @@ EOT
1352
1360
  end
1353
1361
 
1354
1362
  ##
1355
- # Passes if !+object+.const_defined?(+constant_name+)
1363
+ # Passes if !`object`.const_defined?(`constant_name`)
1356
1364
  #
1357
1365
  # @example
1358
1366
  # assert_not_const_defined(Object, :Nonexistent) # -> pass
@@ -1369,7 +1377,7 @@ EOT
1369
1377
  end
1370
1378
 
1371
1379
  ##
1372
- # Passes if +object+.+predicate+ is _true_.
1380
+ # Passes if `object`.`predicate` is _true_.
1373
1381
  #
1374
1382
  # @example
1375
1383
  # assert_predicate([], :empty?) # -> pass
@@ -1391,7 +1399,7 @@ EOT
1391
1399
  end
1392
1400
 
1393
1401
  ##
1394
- # Passes if +object+.+predicate+ is not _true_.
1402
+ # Passes if `object`.`predicate` is not _true_.
1395
1403
  #
1396
1404
  # @example
1397
1405
  # assert_not_predicate([1], :empty?) # -> pass
@@ -1418,8 +1426,8 @@ EOT
1418
1426
  alias_method :refute_predicate, :assert_not_predicate
1419
1427
 
1420
1428
  ##
1421
- # Passes if +object+#+alias_name+ is an alias method of
1422
- # +object+#+original_name+.
1429
+ # Passes if `object`#`alias_name` is an alias method of
1430
+ # `object`#`original_name`.
1423
1431
  #
1424
1432
  # @example
1425
1433
  # assert_alias_method([], :length, :size) # -> pass
@@ -1466,7 +1474,7 @@ EOT
1466
1474
  end
1467
1475
 
1468
1476
  ##
1469
- # Passes if +path+ exists.
1477
+ # Passes if `path` exists.
1470
1478
  #
1471
1479
  # @example
1472
1480
  # assert_path_exist("/tmp") # -> pass
@@ -1475,7 +1483,7 @@ EOT
1475
1483
  def assert_path_exist(path, message=nil)
1476
1484
  _wrap_assertion do
1477
1485
  failure_message = build_message(message,
1478
- "<?> expected to exist",
1486
+ "<?> was expected to exist",
1479
1487
  path)
1480
1488
  assert_block(failure_message) do
1481
1489
  File.exist?(path)
@@ -1484,7 +1492,7 @@ EOT
1484
1492
  end
1485
1493
 
1486
1494
  ##
1487
- # Passes if +path+ doesn't exist.
1495
+ # Passes if `path` doesn't exist.
1488
1496
  #
1489
1497
  # @example
1490
1498
  # assert_path_not_exist("/nonexistent") # -> pass
@@ -1493,7 +1501,7 @@ EOT
1493
1501
  def assert_path_not_exist(path, message=nil)
1494
1502
  _wrap_assertion do
1495
1503
  failure_message = build_message(message,
1496
- "<?> expected to not exist",
1504
+ "<?> was expected to not exist",
1497
1505
  path)
1498
1506
  assert_block(failure_message) do
1499
1507
  not File.exist?(path)
@@ -1502,7 +1510,7 @@ EOT
1502
1510
  end
1503
1511
 
1504
1512
  ##
1505
- # Passes if +collection+ includes +object+.
1513
+ # Passes if `collection` includes `object`.
1506
1514
  #
1507
1515
  # @example
1508
1516
  # assert_include([1, 10], 1) # -> pass
@@ -1514,7 +1522,7 @@ EOT
1514
1522
  assert_respond_to(collection, :include?,
1515
1523
  "The collection must respond to :include?.")
1516
1524
  full_message = build_message(message,
1517
- "<?> expected to include\n<?>.",
1525
+ "<?> was expected to include\n<?>.",
1518
1526
  collection,
1519
1527
  object)
1520
1528
  assert_block(full_message) do
@@ -1529,7 +1537,7 @@ EOT
1529
1537
  alias_method :assert_includes, :assert_include
1530
1538
 
1531
1539
  ##
1532
- # Passes if +collection+ doesn't include +object+.
1540
+ # Passes if `collection` doesn't include `object`.
1533
1541
  #
1534
1542
  # @example
1535
1543
  # assert_not_include([1, 10], 5) # -> pass
@@ -1541,7 +1549,7 @@ EOT
1541
1549
  assert_respond_to(collection, :include?,
1542
1550
  "The collection must respond to :include?.")
1543
1551
  full_message = build_message(message,
1544
- "<?> expected to not include\n<?>.",
1552
+ "<?> was expected to not include\n<?>.",
1545
1553
  collection,
1546
1554
  object)
1547
1555
  assert_block(full_message) do
@@ -1561,7 +1569,7 @@ EOT
1561
1569
  alias_method :refute_includes, :assert_not_include
1562
1570
 
1563
1571
  ##
1564
- # Passes if +object+ is empty.
1572
+ # Passes if `object` is empty.
1565
1573
  #
1566
1574
  # @example
1567
1575
  # assert_empty("") # -> pass
@@ -1575,7 +1583,7 @@ EOT
1575
1583
  assert_respond_to(object, :empty?,
1576
1584
  "The object must respond to :empty?.")
1577
1585
  full_message = build_message(message,
1578
- "<?> expected to be empty.",
1586
+ "<?> was expected to be empty.",
1579
1587
  object)
1580
1588
  assert_block(full_message) do
1581
1589
  object.empty?
@@ -1584,7 +1592,7 @@ EOT
1584
1592
  end
1585
1593
 
1586
1594
  ##
1587
- # Passes if +object+ is not empty.
1595
+ # Passes if `object` is not empty.
1588
1596
  #
1589
1597
  # @example
1590
1598
  # assert_not_empty(" ") # -> pass
@@ -1598,7 +1606,7 @@ EOT
1598
1606
  assert_respond_to(object, :empty?,
1599
1607
  "The object must respond to :empty?.")
1600
1608
  full_message = build_message(message,
1601
- "<?> expected to not be empty.",
1609
+ "<?> was expected to not be empty.",
1602
1610
  object)
1603
1611
  assert_block(full_message) do
1604
1612
  not object.empty?
@@ -1612,11 +1620,12 @@ EOT
1612
1620
  alias_method :refute_empty, :assert_not_empty
1613
1621
 
1614
1622
  ##
1615
- # Builds a failure message. +head+ is added before the +template+ and
1616
- # +arguments+ replaces the '?'s positionally in the template.
1617
- def build_message(head, template=nil, *arguments)
1623
+ # Builds a failure message. `user_message` is added before the
1624
+ # `template` and `arguments` replaces the '?'s positionally in
1625
+ # the template.
1626
+ def build_message(user_message, template=nil, *arguments)
1618
1627
  template &&= template.chomp
1619
- return AssertionMessage.new(head, template, arguments)
1628
+ return AssertionMessage.new(user_message, template, arguments)
1620
1629
  end
1621
1630
 
1622
1631
  private
@@ -1666,7 +1675,7 @@ EOT
1666
1675
  expected = assert_exception_helper.expected_exceptions
1667
1676
  actual_exception = nil
1668
1677
  full_message = build_message(message,
1669
- "<?> exception expected " +
1678
+ "<?> exception was expected " +
1670
1679
  "but none was thrown.",
1671
1680
  expected)
1672
1681
  assert_block(full_message) do
@@ -1683,12 +1692,11 @@ EOT
1683
1692
  end
1684
1693
  end
1685
1694
 
1686
- def _set_failed_information(failure, expected, actual, user_message)
1695
+ def _set_failed_information(failure, expected, actual)
1687
1696
  failure.expected = expected
1688
1697
  failure.actual = actual
1689
1698
  failure.inspected_expected = AssertionMessage.convert(expected)
1690
1699
  failure.inspected_actual = AssertionMessage.convert(actual)
1691
- failure.user_message = user_message
1692
1700
  end
1693
1701
 
1694
1702
  class AssertionMessage
@@ -1775,7 +1783,7 @@ EOT
1775
1783
 
1776
1784
  if Diff.need_fold?(diff)
1777
1785
  folded_diff = Diff.folded_readable(from, to)
1778
- diff << "\n\nfolded diff:\n#{folded_diff}"
1786
+ diff += "\n\nfolded diff:\n#{folded_diff}"
1779
1787
  end
1780
1788
 
1781
1789
  diff
@@ -1789,9 +1797,9 @@ EOT
1789
1797
  inspector = Inspector.new(object)
1790
1798
  if use_pp
1791
1799
  begin
1792
- require 'pp' unless defined?(PP)
1800
+ require "pp" unless defined?(PP)
1793
1801
  begin
1794
- return PP.pp(inspector, '').chomp
1802
+ return PP.pp(inspector, String.new).chomp
1795
1803
  rescue NameError
1796
1804
  end
1797
1805
  rescue LoadError
@@ -2040,34 +2048,34 @@ EOT
2040
2048
  expanded_template = ""
2041
2049
  @parts.each do |part|
2042
2050
  if part == '?'
2043
- encoding_safe_concat(expanded_template, params.shift)
2051
+ param = params.shift
2052
+ if Object.const_defined?(:Encoding)
2053
+ expanded_template += concatenatable(param,
2054
+ expanded_template.encoding)
2055
+ else
2056
+ expanded_template += param
2057
+ end
2044
2058
  else
2045
- expanded_template << part.gsub(/\\\?/m, '?')
2059
+ expanded_template += part.gsub(/\\\?/m, '?')
2046
2060
  end
2047
2061
  end
2048
2062
  expanded_template
2049
2063
  end
2050
2064
 
2051
2065
  private
2052
- if Object.const_defined?(:Encoding)
2053
- def encoding_safe_concat(buffer, parameter)
2054
- if Encoding.compatible?(buffer, parameter)
2055
- buffer << parameter
2056
- else
2057
- buffer << parameter.dup.force_encoding(buffer.encoding)
2058
- end
2059
- end
2060
- else
2061
- def encoding_safe_concat(buffer, parameter)
2062
- buffer << parameter
2066
+ def concatenatable(text, encoding)
2067
+ if Encoding.compatible?(text, encoding)
2068
+ text
2069
+ else
2070
+ text.dup.force_encoding(encoding)
2063
2071
  end
2064
2072
  end
2065
2073
  end
2066
2074
 
2067
2075
  include Util::BacktraceFilter
2068
2076
 
2069
- def initialize(head, template_string, parameters)
2070
- @head = head
2077
+ def initialize(user_message, template_string, parameters)
2078
+ @user_message = user_message
2071
2079
  @template_string = template_string
2072
2080
  @parameters = parameters
2073
2081
  end
@@ -2080,24 +2088,28 @@ EOT
2080
2088
  @template ||= Template.create(@template_string)
2081
2089
  end
2082
2090
 
2083
- def add_period(string)
2084
- (string =~ /\.\Z/ ? string : string + '.')
2091
+ def user_message
2092
+ return nil unless @user_message
2093
+ message = @user_message
2094
+ message = message.call if message.respond_to?(:call)
2095
+ message.to_s
2085
2096
  end
2086
2097
 
2087
2098
  def to_s
2088
2099
  message_parts = []
2089
- if (@head)
2090
- head = @head
2091
- head = head.call if head.respond_to?(:call)
2092
- head = head.to_s
2093
- unless(head.empty?)
2094
- message_parts << add_period(head)
2095
- end
2100
+ head = user_message
2101
+ if head and not head.empty?
2102
+ message_parts << add_period(head)
2096
2103
  end
2097
2104
  tail = template.result(@parameters.collect{|e| convert(e)})
2098
2105
  message_parts << tail unless(tail.empty?)
2099
2106
  message_parts.join("\n")
2100
2107
  end
2108
+
2109
+ private
2110
+ def add_period(string)
2111
+ (string =~ /\.\Z/ ? string : string + '.')
2112
+ end
2101
2113
  end
2102
2114
 
2103
2115
  class AssertExceptionHelper
@@ -2169,19 +2181,45 @@ EOT
2169
2181
  expected_exceptions.each do |exception_type|
2170
2182
  if exception_type.instance_of?(Module)
2171
2183
  exception_modules << exception_type
2172
- elsif exception_type.is_a?(Exception)
2184
+ elsif exception_object?(exception_type)
2173
2185
  exception_objects << exception_type
2174
- else
2175
- @test_case.__send__(:assert,
2176
- Exception >= exception_type,
2177
- "Should expect a class of exception, " +
2178
- "#{exception_type}")
2186
+ elsif exception_class?(exception_type)
2179
2187
  exception_classes << exception_type
2188
+ else
2189
+ full_message =
2190
+ @test_case.__send__(:build_message,
2191
+ nil,
2192
+ "<?> must be " +
2193
+ "a subclass of Exception, " +
2194
+ "an object of Exception subclasses " +
2195
+ "or a Module",
2196
+ exception_type)
2197
+ @test_case.flunk(full_message)
2180
2198
  end
2181
2199
  end
2182
2200
  [exception_classes, exception_modules, exception_objects]
2183
2201
  end
2184
2202
 
2203
+ def exception_object?(exception_type)
2204
+ return true if exception_type.is_a?(Exception)
2205
+
2206
+ if Object.const_defined?(:Java)
2207
+ return true if exception_type.is_a?(Java::JavaLang::Throwable)
2208
+ end
2209
+
2210
+ false
2211
+ end
2212
+
2213
+ def exception_class?(exception_type)
2214
+ return true if exception_type <= Exception
2215
+
2216
+ if Object.const_defined?(:Java)
2217
+ return true if exception_type <= Java::JavaLang::Throwable
2218
+ end
2219
+
2220
+ false
2221
+ end
2222
+
2185
2223
  def expected_class?(actual_exception, equality)
2186
2224
  @expected_classes.any? do |expected_class|
2187
2225
  actual_exception.__send__(equality, expected_class)