test-unit 3.2.5 → 3.4.7
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.
- checksums.yaml +5 -5
- data/BSDL +24 -0
- data/COPYING +41 -44
- data/README.md +8 -11
- data/Rakefile +0 -23
- data/doc/text/getting-started.md +1 -1
- data/doc/text/news.md +366 -0
- data/lib/test/unit/assertion-failed-error.rb +35 -0
- data/lib/test/unit/assertions.rb +312 -112
- data/lib/test/unit/attribute.rb +7 -2
- data/lib/test/unit/autorunner.rb +79 -31
- data/lib/test/unit/code-snippet-fetcher.rb +7 -7
- data/lib/test/unit/collector/descendant.rb +1 -0
- data/lib/test/unit/collector/dir.rb +4 -2
- data/lib/test/unit/collector/load.rb +10 -13
- data/lib/test/unit/collector/objectspace.rb +1 -0
- data/lib/test/unit/collector.rb +31 -0
- data/lib/test/unit/color-scheme.rb +20 -2
- data/lib/test/unit/data-sets.rb +116 -0
- data/lib/test/unit/data.rb +121 -12
- data/lib/test/unit/diff.rb +2 -3
- data/lib/test/unit/fixture.rb +6 -0
- data/lib/test/unit/notification.rb +9 -7
- data/lib/test/unit/omission.rb +34 -31
- data/lib/test/unit/pending.rb +12 -11
- data/lib/test/unit/priority.rb +7 -3
- data/lib/test/unit/runner/console.rb +8 -0
- data/lib/test/unit/test-suite-creator.rb +22 -8
- data/lib/test/unit/testcase.rb +216 -146
- data/lib/test/unit/testsuite.rb +1 -1
- data/lib/test/unit/ui/console/testrunner.rb +92 -32
- data/lib/test/unit/util/memory-usage.rb +47 -0
- data/lib/test/unit/util/observable.rb +2 -2
- data/lib/test/unit/util/output.rb +5 -4
- data/lib/test/unit/util/procwrapper.rb +4 -4
- data/lib/test/unit/version.rb +1 -1
- data/lib/test/unit/warning.rb +3 -0
- data/lib/test/unit.rb +177 -161
- data/lib/test-unit.rb +2 -17
- metadata +13 -88
- data/GPL +0 -339
- data/LGPL +0 -502
- data/test/collector/test-descendant.rb +0 -182
- data/test/collector/test-load.rb +0 -442
- data/test/collector/test_dir.rb +0 -407
- data/test/collector/test_objectspace.rb +0 -102
- data/test/fixtures/header-label.csv +0 -3
- data/test/fixtures/header-label.tsv +0 -3
- data/test/fixtures/header.csv +0 -3
- data/test/fixtures/header.tsv +0 -3
- data/test/fixtures/no-header.csv +0 -2
- data/test/fixtures/no-header.tsv +0 -2
- data/test/fixtures/plus.csv +0 -3
- data/test/run-test.rb +0 -22
- data/test/test-assertions.rb +0 -2180
- data/test/test-attribute-matcher.rb +0 -38
- data/test/test-attribute.rb +0 -123
- data/test/test-code-snippet.rb +0 -37
- data/test/test-color-scheme.rb +0 -82
- data/test/test-color.rb +0 -47
- data/test/test-data.rb +0 -303
- data/test/test-diff.rb +0 -518
- data/test/test-emacs-runner.rb +0 -60
- data/test/test-error.rb +0 -26
- data/test/test-failure.rb +0 -33
- data/test/test-fault-location-detector.rb +0 -163
- data/test/test-fixture.rb +0 -713
- data/test/test-notification.rb +0 -33
- data/test/test-omission.rb +0 -81
- data/test/test-pending.rb +0 -70
- data/test/test-priority.rb +0 -173
- data/test/test-test-case.rb +0 -1278
- data/test/test-test-result.rb +0 -113
- data/test/test-test-suite-creator.rb +0 -97
- data/test/test-test-suite.rb +0 -151
- data/test/testunit-test-util.rb +0 -31
- data/test/ui/test_testrunmediator.rb +0 -20
- data/test/util/test-method-owner-finder.rb +0 -38
- data/test/util/test-output.rb +0 -11
- data/test/util/test_backtracefilter.rb +0 -52
- data/test/util/test_observable.rb +0 -102
- data/test/util/test_procwrapper.rb +0 -36
data/lib/test/unit/assertions.rb
CHANGED
@@ -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
|
36
|
-
# message = build_message
|
37
|
-
# assert_block
|
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
|
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
|
-
|
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
|
@@ -167,7 +172,7 @@ module Test
|
|
167
172
|
end
|
168
173
|
end
|
169
174
|
|
170
|
-
# Asserts that
|
175
|
+
# Asserts that `object` is false or nil.
|
171
176
|
#
|
172
177
|
# @note Just for minitest compatibility. :<
|
173
178
|
#
|
@@ -209,7 +214,7 @@ module Test
|
|
209
214
|
end
|
210
215
|
|
211
216
|
##
|
212
|
-
# Passes if
|
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
|
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
|
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
|
@@ -345,8 +414,8 @@ EOT
|
|
345
414
|
end
|
346
415
|
|
347
416
|
##
|
348
|
-
# Passes if
|
349
|
-
# When
|
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,7 +424,7 @@ 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
|
@@ -386,11 +455,11 @@ EOT
|
|
386
455
|
alias_method :refute_instance_of, :assert_not_instance_of
|
387
456
|
|
388
457
|
##
|
389
|
-
# Passes if
|
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
464
|
<?> was expected to be nil.
|
396
465
|
EOT
|
@@ -398,7 +467,7 @@ EOT
|
|
398
467
|
end
|
399
468
|
|
400
469
|
##
|
401
|
-
# Passes if
|
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
|
@@ -434,8 +503,8 @@ EOT
|
|
434
503
|
end
|
435
504
|
|
436
505
|
##
|
437
|
-
# Passes if
|
438
|
-
# When
|
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
|
@@ -475,11 +544,11 @@ EOT
|
|
475
544
|
alias_method :refute_kind_of, :assert_not_kind_of
|
476
545
|
|
477
546
|
##
|
478
|
-
# Passes if
|
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
|
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,18 +594,13 @@ EOT
|
|
525
594
|
alias_method :refute_respond_to, :assert_not_respond_to
|
526
595
|
|
527
596
|
##
|
528
|
-
# Passes if
|
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 =
|
535
|
-
when String
|
536
|
-
Regexp.new(Regexp.escape(pattern))
|
537
|
-
else
|
538
|
-
pattern
|
539
|
-
end
|
603
|
+
pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a?(String)
|
540
604
|
full_message = build_message(message,
|
541
605
|
"<?> was expected to be =~\n<?>.",
|
542
606
|
pattern, string)
|
@@ -545,13 +609,13 @@ EOT
|
|
545
609
|
end
|
546
610
|
|
547
611
|
##
|
548
|
-
# Passes if
|
612
|
+
# Passes if `actual` .equal? `expected` (i.e. they are the same
|
549
613
|
# instance).
|
550
614
|
#
|
551
615
|
# @example
|
552
616
|
# o = Object.new
|
553
617
|
# assert_same o, o
|
554
|
-
def assert_same(expected, actual, message=
|
618
|
+
def assert_same(expected, actual, message=nil)
|
555
619
|
full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
|
556
620
|
<?>
|
557
621
|
with id <?> was expected to be equal\\? to
|
@@ -562,13 +626,13 @@ EOT
|
|
562
626
|
end
|
563
627
|
|
564
628
|
##
|
565
|
-
# Compares the
|
629
|
+
# Compares the `object1` with `object2` using `operator`.
|
566
630
|
#
|
567
|
-
# Passes if object1.__send__(operator, object2) is
|
631
|
+
# Passes if object1.__send__(operator, object2) is not false nor nil.
|
568
632
|
#
|
569
633
|
# @example
|
570
634
|
# assert_operator 5, :>=, 4
|
571
|
-
def assert_operator(object1, operator, object2, message=
|
635
|
+
def assert_operator(object1, operator, object2, message=nil)
|
572
636
|
_wrap_assertion do
|
573
637
|
full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
|
574
638
|
assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
|
@@ -582,16 +646,16 @@ EOT
|
|
582
646
|
end
|
583
647
|
|
584
648
|
##
|
585
|
-
# Compares the
|
649
|
+
# Compares the `object1` with `object2` using `operator`.
|
586
650
|
#
|
587
|
-
# Passes if object1.__send__(operator, object2) is
|
651
|
+
# Passes if object1.__send__(operator, object2) is false or nil.
|
588
652
|
#
|
589
653
|
# @example
|
590
654
|
# assert_not_operator(5, :<, 4) # => pass
|
591
655
|
# assert_not_operator(5, :>, 4) # => fail
|
592
656
|
#
|
593
657
|
# @since 3.0.0
|
594
|
-
def assert_not_operator(object1, operator, object2, message=
|
658
|
+
def assert_not_operator(object1, operator, object2, message=nil)
|
595
659
|
_wrap_assertion do
|
596
660
|
full_message = build_message(nil, "<?>\ngiven as the operator for #assert_not_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
|
597
661
|
assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
|
@@ -649,11 +713,11 @@ EOT
|
|
649
713
|
end
|
650
714
|
|
651
715
|
##
|
652
|
-
# Passes if !
|
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
723
|
with id <?> was expected to not be equal\\? to
|
@@ -669,11 +733,11 @@ EOT
|
|
669
733
|
alias_method :refute_same, :assert_not_same
|
670
734
|
|
671
735
|
##
|
672
|
-
# Passes if
|
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=
|
740
|
+
def assert_not_equal(expected, actual, message=nil)
|
677
741
|
full_message = build_message(message,
|
678
742
|
"<?> was expected to be != to\n<?>.",
|
679
743
|
expected, actual)
|
@@ -686,11 +750,11 @@ EOT
|
|
686
750
|
alias_method :refute_equal, :assert_not_equal
|
687
751
|
|
688
752
|
##
|
689
|
-
# Passes if !
|
753
|
+
# Passes if ! `object` .nil?
|
690
754
|
#
|
691
755
|
# @example
|
692
756
|
# assert_not_nil '1 two 3'.sub!(/two/, '2')
|
693
|
-
def assert_not_nil(object, message=
|
757
|
+
def assert_not_nil(object, message=nil)
|
694
758
|
full_message = build_message(message,
|
695
759
|
"<?> was expected to not be nil.",
|
696
760
|
object)
|
@@ -703,20 +767,18 @@ EOT
|
|
703
767
|
alias_method :refute_nil, :assert_not_nil
|
704
768
|
|
705
769
|
##
|
706
|
-
# Passes if
|
770
|
+
# Passes if `regexp` !~ `string`
|
707
771
|
#
|
708
772
|
# @example
|
709
773
|
# assert_not_match(/two/, 'one 2 three') # -> pass
|
710
774
|
# assert_not_match(/three/, 'one 2 three') # -> fail
|
711
|
-
def assert_not_match(
|
775
|
+
def assert_not_match(pattern, string, message=nil)
|
712
776
|
_wrap_assertion do
|
713
|
-
|
714
|
-
"<REGEXP> in assert_not_match(<REGEXP>, ...) " +
|
715
|
-
"should be a Regexp.")
|
777
|
+
pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a?(String)
|
716
778
|
full_message = build_message(message,
|
717
779
|
"<?> was expected to not match\n<?>.",
|
718
|
-
|
719
|
-
assert_block(full_message) {
|
780
|
+
pattern, string)
|
781
|
+
assert_block(full_message) { pattern !~ string }
|
720
782
|
end
|
721
783
|
end
|
722
784
|
|
@@ -726,9 +788,9 @@ EOT
|
|
726
788
|
alias_method :refute_match, :assert_not_match
|
727
789
|
|
728
790
|
##
|
729
|
-
#
|
791
|
+
# @deprecated Use {#assert_not_match} instead.
|
730
792
|
#
|
731
|
-
# Passes if
|
793
|
+
# Passes if `regexp` !~ `string`
|
732
794
|
#
|
733
795
|
# @example
|
734
796
|
# assert_no_match(/two/, 'one 2 three') # -> pass
|
@@ -786,13 +848,13 @@ EOT
|
|
786
848
|
end
|
787
849
|
|
788
850
|
##
|
789
|
-
# Passes if the block throws
|
851
|
+
# Passes if the block throws `expected_object`
|
790
852
|
#
|
791
853
|
# @example
|
792
854
|
# assert_throw(:done) do
|
793
855
|
# throw(:done)
|
794
856
|
# end
|
795
|
-
def assert_throw(expected_object, message=
|
857
|
+
def assert_throw(expected_object, message=nil, &proc)
|
796
858
|
_wrap_assertion do
|
797
859
|
begin
|
798
860
|
catch([]) {}
|
@@ -838,7 +900,7 @@ EOT
|
|
838
900
|
# assert_nothing_thrown do
|
839
901
|
# [1, 2].uniq
|
840
902
|
# end
|
841
|
-
def assert_nothing_thrown(message=
|
903
|
+
def assert_nothing_thrown(message=nil, &proc)
|
842
904
|
_wrap_assertion do
|
843
905
|
assert(block_given?, "Should have passed a block to assert_nothing_thrown")
|
844
906
|
begin
|
@@ -857,8 +919,8 @@ EOT
|
|
857
919
|
end
|
858
920
|
|
859
921
|
##
|
860
|
-
# Passes if
|
861
|
-
# within
|
922
|
+
# Passes if `expected_float` and `actual_float` are equal
|
923
|
+
# within `delta` tolerance.
|
862
924
|
#
|
863
925
|
# @example
|
864
926
|
# assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
|
@@ -878,8 +940,8 @@ EOT
|
|
878
940
|
end
|
879
941
|
|
880
942
|
##
|
881
|
-
# Passes if
|
882
|
-
# not equal within
|
943
|
+
# Passes if `expected_float` and `actual_float` are
|
944
|
+
# not equal within `delta` tolerance.
|
883
945
|
#
|
884
946
|
# @example
|
885
947
|
# assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
|
@@ -978,8 +1040,8 @@ EOT
|
|
978
1040
|
|
979
1041
|
public
|
980
1042
|
##
|
981
|
-
# Passes if
|
982
|
-
# within
|
1043
|
+
# Passes if `expected_float` and `actual_float` are equal
|
1044
|
+
# within `epsilon` relative error of `expected_float`.
|
983
1045
|
#
|
984
1046
|
# @example
|
985
1047
|
# assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
|
@@ -1008,9 +1070,9 @@ EOT
|
|
1008
1070
|
end
|
1009
1071
|
|
1010
1072
|
##
|
1011
|
-
# Passes if
|
1012
|
-
# not equal within
|
1013
|
-
#
|
1073
|
+
# Passes if `expected_float` and `actual_float` are
|
1074
|
+
# not equal within `epsilon` relative error of
|
1075
|
+
# `expected_float`.
|
1014
1076
|
#
|
1015
1077
|
# @example
|
1016
1078
|
# assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
|
@@ -1116,9 +1178,9 @@ EOT
|
|
1116
1178
|
|
1117
1179
|
public
|
1118
1180
|
##
|
1119
|
-
# Passes if the method
|
1181
|
+
# Passes if the method `__send__` returns not false nor nil.
|
1120
1182
|
#
|
1121
|
-
#
|
1183
|
+
# `send_array` is composed of:
|
1122
1184
|
# * A receiver
|
1123
1185
|
# * A method
|
1124
1186
|
# * Arguments to the method
|
@@ -1156,9 +1218,9 @@ EOT
|
|
1156
1218
|
end
|
1157
1219
|
|
1158
1220
|
##
|
1159
|
-
# Passes if the method
|
1221
|
+
# Passes if the method `__send__` returns false or nil.
|
1160
1222
|
#
|
1161
|
-
#
|
1223
|
+
# `send_array` is composed of:
|
1162
1224
|
# * A receiver
|
1163
1225
|
# * A method
|
1164
1226
|
# * Arguments to the method
|
@@ -1196,7 +1258,7 @@ EOT
|
|
1196
1258
|
end
|
1197
1259
|
|
1198
1260
|
##
|
1199
|
-
# Passes if
|
1261
|
+
# Passes if `actual` is a boolean value.
|
1200
1262
|
#
|
1201
1263
|
# @example
|
1202
1264
|
# assert_boolean(true) # -> pass
|
@@ -1206,13 +1268,13 @@ EOT
|
|
1206
1268
|
assert_block(build_message(message,
|
1207
1269
|
"<true> or <false> expected but was\n<?>",
|
1208
1270
|
actual)) do
|
1209
|
-
|
1271
|
+
true == actual || false == actual
|
1210
1272
|
end
|
1211
1273
|
end
|
1212
1274
|
end
|
1213
1275
|
|
1214
1276
|
##
|
1215
|
-
# Passes if
|
1277
|
+
# Passes if `actual` is true.
|
1216
1278
|
#
|
1217
1279
|
# @example
|
1218
1280
|
# assert_true(true) # -> pass
|
@@ -1222,13 +1284,13 @@ EOT
|
|
1222
1284
|
assert_block(build_message(message,
|
1223
1285
|
"<true> expected but was\n<?>",
|
1224
1286
|
actual)) do
|
1225
|
-
|
1287
|
+
true == actual
|
1226
1288
|
end
|
1227
1289
|
end
|
1228
1290
|
end
|
1229
1291
|
|
1230
1292
|
##
|
1231
|
-
# Passes if
|
1293
|
+
# Passes if `actual` is false.
|
1232
1294
|
#
|
1233
1295
|
# @example
|
1234
1296
|
# assert_false(false) # -> pass
|
@@ -1238,14 +1300,14 @@ EOT
|
|
1238
1300
|
assert_block(build_message(message,
|
1239
1301
|
"<false> expected but was\n<?>",
|
1240
1302
|
actual)) do
|
1241
|
-
|
1303
|
+
false == actual
|
1242
1304
|
end
|
1243
1305
|
end
|
1244
1306
|
end
|
1245
1307
|
|
1246
1308
|
##
|
1247
|
-
# Passes if expression "
|
1248
|
-
#
|
1309
|
+
# Passes if expression "`expected` `operator`
|
1310
|
+
# `actual`" is not false nor nil.
|
1249
1311
|
#
|
1250
1312
|
# @example
|
1251
1313
|
# assert_compare(1, "<", 10) # -> pass
|
@@ -1300,7 +1362,7 @@ EOT
|
|
1300
1362
|
|
1301
1363
|
##
|
1302
1364
|
# Passes if an exception is raised in block and its
|
1303
|
-
# message is
|
1365
|
+
# message is `expected`.
|
1304
1366
|
#
|
1305
1367
|
# @example
|
1306
1368
|
# assert_raise_message("exception") {raise "exception"} # -> pass
|
@@ -1340,7 +1402,7 @@ EOT
|
|
1340
1402
|
end
|
1341
1403
|
|
1342
1404
|
##
|
1343
|
-
# Passes if
|
1405
|
+
# Passes if `object`.const_defined?(`constant_name`)
|
1344
1406
|
#
|
1345
1407
|
# @example
|
1346
1408
|
# assert_const_defined(Test, :Unit) # -> pass
|
@@ -1357,7 +1419,7 @@ EOT
|
|
1357
1419
|
end
|
1358
1420
|
|
1359
1421
|
##
|
1360
|
-
# Passes if
|
1422
|
+
# Passes if !`object`.const_defined?(`constant_name`)
|
1361
1423
|
#
|
1362
1424
|
# @example
|
1363
1425
|
# assert_not_const_defined(Object, :Nonexistent) # -> pass
|
@@ -1374,7 +1436,7 @@ EOT
|
|
1374
1436
|
end
|
1375
1437
|
|
1376
1438
|
##
|
1377
|
-
# Passes if
|
1439
|
+
# Passes if `object`.`predicate` is not false nor nil.
|
1378
1440
|
#
|
1379
1441
|
# @example
|
1380
1442
|
# assert_predicate([], :empty?) # -> pass
|
@@ -1396,7 +1458,7 @@ EOT
|
|
1396
1458
|
end
|
1397
1459
|
|
1398
1460
|
##
|
1399
|
-
# Passes if
|
1461
|
+
# Passes if `object`.`predicate` is false or nil.
|
1400
1462
|
#
|
1401
1463
|
# @example
|
1402
1464
|
# assert_not_predicate([1], :empty?) # -> pass
|
@@ -1423,8 +1485,8 @@ EOT
|
|
1423
1485
|
alias_method :refute_predicate, :assert_not_predicate
|
1424
1486
|
|
1425
1487
|
##
|
1426
|
-
# Passes if
|
1427
|
-
#
|
1488
|
+
# Passes if `object`#`alias_name` is an alias method of
|
1489
|
+
# `object`#`original_name`.
|
1428
1490
|
#
|
1429
1491
|
# @example
|
1430
1492
|
# assert_alias_method([], :length, :size) # -> pass
|
@@ -1471,7 +1533,7 @@ EOT
|
|
1471
1533
|
end
|
1472
1534
|
|
1473
1535
|
##
|
1474
|
-
# Passes if
|
1536
|
+
# Passes if `path` exists.
|
1475
1537
|
#
|
1476
1538
|
# @example
|
1477
1539
|
# assert_path_exist("/tmp") # -> pass
|
@@ -1489,7 +1551,7 @@ EOT
|
|
1489
1551
|
end
|
1490
1552
|
|
1491
1553
|
##
|
1492
|
-
# Passes if
|
1554
|
+
# Passes if `path` doesn't exist.
|
1493
1555
|
#
|
1494
1556
|
# @example
|
1495
1557
|
# assert_path_not_exist("/nonexistent") # -> pass
|
@@ -1507,7 +1569,7 @@ EOT
|
|
1507
1569
|
end
|
1508
1570
|
|
1509
1571
|
##
|
1510
|
-
# Passes if
|
1572
|
+
# Passes if `collection` includes `object`.
|
1511
1573
|
#
|
1512
1574
|
# @example
|
1513
1575
|
# assert_include([1, 10], 1) # -> pass
|
@@ -1534,7 +1596,7 @@ EOT
|
|
1534
1596
|
alias_method :assert_includes, :assert_include
|
1535
1597
|
|
1536
1598
|
##
|
1537
|
-
# Passes if
|
1599
|
+
# Passes if `collection` doesn't include `object`.
|
1538
1600
|
#
|
1539
1601
|
# @example
|
1540
1602
|
# assert_not_include([1, 10], 5) # -> pass
|
@@ -1566,7 +1628,7 @@ EOT
|
|
1566
1628
|
alias_method :refute_includes, :assert_not_include
|
1567
1629
|
|
1568
1630
|
##
|
1569
|
-
# Passes if
|
1631
|
+
# Passes if `object` is empty.
|
1570
1632
|
#
|
1571
1633
|
# @example
|
1572
1634
|
# assert_empty("") # -> pass
|
@@ -1589,7 +1651,7 @@ EOT
|
|
1589
1651
|
end
|
1590
1652
|
|
1591
1653
|
##
|
1592
|
-
# Passes if
|
1654
|
+
# Passes if `object` is not empty.
|
1593
1655
|
#
|
1594
1656
|
# @example
|
1595
1657
|
# assert_not_empty(" ") # -> pass
|
@@ -1616,12 +1678,140 @@ EOT
|
|
1616
1678
|
# @since 3.0.0
|
1617
1679
|
alias_method :refute_empty, :assert_not_empty
|
1618
1680
|
|
1681
|
+
# @overload assert_all(collection, message=nil, &block)
|
1682
|
+
#
|
1683
|
+
# Asserts that all `block.call(item)` where `item` is each
|
1684
|
+
# item in `collection` are not false nor nil.
|
1685
|
+
#
|
1686
|
+
# If `collection` is empty, this assertion is always passed
|
1687
|
+
# with any `block`.
|
1688
|
+
#
|
1689
|
+
# @example Pass patterns
|
1690
|
+
# assert_all([1, 2, 3]) {|item| item > 0} # => pass
|
1691
|
+
# assert_all([1, 2, 3], &:positive?) # => pass
|
1692
|
+
# assert_all([]) {|item| false} # => pass
|
1693
|
+
#
|
1694
|
+
# @example Failure pattern
|
1695
|
+
# assert_all([0, 1, 2], &:zero?) # => failure
|
1696
|
+
#
|
1697
|
+
# @param [#each] collection The check target.
|
1698
|
+
# @param [String] message The additional user message. It is
|
1699
|
+
# showed when the assertion is failed.
|
1700
|
+
# @yield [Object] Give each item in `collection` to the block.
|
1701
|
+
# @yieldreturn [Object] The checked object.
|
1702
|
+
# @return [void]
|
1703
|
+
#
|
1704
|
+
# @since 3.4.4
|
1705
|
+
def assert_all(collection, message=nil)
|
1706
|
+
_wrap_assertion do
|
1707
|
+
failed = false
|
1708
|
+
result = {}
|
1709
|
+
collection.each do |item|
|
1710
|
+
element_result = yield(item)
|
1711
|
+
failed = true unless element_result
|
1712
|
+
result[item] = element_result
|
1713
|
+
end
|
1714
|
+
format = <<-FORMAT
|
1715
|
+
<?> was expected to be all true values with the given block but was
|
1716
|
+
<?>
|
1717
|
+
FORMAT
|
1718
|
+
full_message = build_message(message,
|
1719
|
+
format,
|
1720
|
+
collection,
|
1721
|
+
result)
|
1722
|
+
assert_block(full_message) do
|
1723
|
+
not failed
|
1724
|
+
end
|
1725
|
+
end
|
1726
|
+
end
|
1727
|
+
|
1728
|
+
# Just for Ruby's tool/lib/test/unit compatibility.
|
1729
|
+
#
|
1730
|
+
# @since 3.4.3
|
1731
|
+
alias_method :assert_all?, :assert_all
|
1732
|
+
|
1733
|
+
# @overload assert_nothing_leaked_memory(max_increasable_size, target=:physical, message=nil, &block)
|
1734
|
+
#
|
1735
|
+
# Asserts that increased memory usage by `block.call` is less
|
1736
|
+
# than `max_increasable_size`. `GC.start` is called before and
|
1737
|
+
# after `block.call`.
|
1738
|
+
#
|
1739
|
+
# This assertion may be fragile. Because memory usage is
|
1740
|
+
# depends on the current Ruby process's memory
|
1741
|
+
# usage. Launching a new Ruby process for this will produce
|
1742
|
+
# more stable result but we need to specify target code as
|
1743
|
+
# `String` instead of block for the approach. We choose easy
|
1744
|
+
# to write API approach rather than more stable result
|
1745
|
+
# approach for this case.
|
1746
|
+
#
|
1747
|
+
# @example Pass pattern
|
1748
|
+
# require "objspace"
|
1749
|
+
# size_per_object = ObjectSpace.memsize_of("Hello")
|
1750
|
+
# # If memory isn't leaked, physical memory of almost created objects
|
1751
|
+
# # (1000 - 10 objects) must be freed.
|
1752
|
+
# assert_nothing_leaked_memory(size_per_object * 10) do
|
1753
|
+
# 1_000.times do
|
1754
|
+
# "Hello".dup
|
1755
|
+
# end
|
1756
|
+
# end # => pass
|
1757
|
+
#
|
1758
|
+
# @example Failure pattern
|
1759
|
+
# require "objspace"
|
1760
|
+
# size_per_object = ObjectSpace.memsize_of("Hello")
|
1761
|
+
# strings = []
|
1762
|
+
# assert_nothing_leaked_memory(size_per_object * 10) do
|
1763
|
+
# 10_000.times do
|
1764
|
+
# # Created objects aren't GC-ed because they are referred.
|
1765
|
+
# strings << "Hello".dup
|
1766
|
+
# end
|
1767
|
+
# end # => failure
|
1768
|
+
#
|
1769
|
+
# @param target [:physical, :virtual] which memory usage is
|
1770
|
+
# used for comparing. `:physical` means physical memory usage
|
1771
|
+
# also known as Resident Set Size (RSS). `:virtual` means
|
1772
|
+
# virtual memory usage.
|
1773
|
+
# @yield [] do anything you want to measure memory usage
|
1774
|
+
# in the block.
|
1775
|
+
# @yieldreturn [void]
|
1776
|
+
# @return [void]
|
1777
|
+
#
|
1778
|
+
# @since 3.4.5
|
1779
|
+
def assert_nothing_leaked_memory(max_increasable_size,
|
1780
|
+
target=:physical,
|
1781
|
+
message=nil)
|
1782
|
+
_wrap_assertion do
|
1783
|
+
GC.start
|
1784
|
+
before = Util::MemoryUsage.new
|
1785
|
+
unless before.collected?
|
1786
|
+
omit("memory usage collection isn't supported on this platform")
|
1787
|
+
end
|
1788
|
+
yield
|
1789
|
+
GC.start
|
1790
|
+
after = Util::MemoryUsage.new
|
1791
|
+
before_value = before.__send__(target)
|
1792
|
+
after_value = after.__send__(target)
|
1793
|
+
actual_increased_size = after_value - before_value
|
1794
|
+
template = <<-TEMPLATE
|
1795
|
+
<?> was expected to be less than
|
1796
|
+
<?>.
|
1797
|
+
TEMPLATE
|
1798
|
+
full_message = build_message(message,
|
1799
|
+
template,
|
1800
|
+
actual_increased_size,
|
1801
|
+
max_increasable_size)
|
1802
|
+
assert_block(full_message) do
|
1803
|
+
actual_increased_size < max_increasable_size
|
1804
|
+
end
|
1805
|
+
end
|
1806
|
+
end
|
1807
|
+
|
1619
1808
|
##
|
1620
|
-
# Builds a failure message.
|
1621
|
-
#
|
1622
|
-
|
1809
|
+
# Builds a failure message. `user_message` is added before the
|
1810
|
+
# `template` and `arguments` replaces the '?'s positionally in
|
1811
|
+
# the template.
|
1812
|
+
def build_message(user_message, template=nil, *arguments)
|
1623
1813
|
template &&= template.chomp
|
1624
|
-
return AssertionMessage.new(
|
1814
|
+
return AssertionMessage.new(user_message, template, arguments)
|
1625
1815
|
end
|
1626
1816
|
|
1627
1817
|
private
|
@@ -1688,12 +1878,11 @@ EOT
|
|
1688
1878
|
end
|
1689
1879
|
end
|
1690
1880
|
|
1691
|
-
def _set_failed_information(failure, expected, actual
|
1881
|
+
def _set_failed_information(failure, expected, actual)
|
1692
1882
|
failure.expected = expected
|
1693
1883
|
failure.actual = actual
|
1694
1884
|
failure.inspected_expected = AssertionMessage.convert(expected)
|
1695
1885
|
failure.inspected_actual = AssertionMessage.convert(actual)
|
1696
|
-
failure.user_message = user_message
|
1697
1886
|
end
|
1698
1887
|
|
1699
1888
|
class AssertionMessage
|
@@ -2071,8 +2260,8 @@ EOT
|
|
2071
2260
|
|
2072
2261
|
include Util::BacktraceFilter
|
2073
2262
|
|
2074
|
-
def initialize(
|
2075
|
-
@
|
2263
|
+
def initialize(user_message, template_string, parameters)
|
2264
|
+
@user_message = user_message
|
2076
2265
|
@template_string = template_string
|
2077
2266
|
@parameters = parameters
|
2078
2267
|
end
|
@@ -2085,24 +2274,28 @@ EOT
|
|
2085
2274
|
@template ||= Template.create(@template_string)
|
2086
2275
|
end
|
2087
2276
|
|
2088
|
-
def
|
2089
|
-
|
2277
|
+
def user_message
|
2278
|
+
return nil unless @user_message
|
2279
|
+
message = @user_message
|
2280
|
+
message = message.call if message.respond_to?(:call)
|
2281
|
+
message.to_s
|
2090
2282
|
end
|
2091
2283
|
|
2092
2284
|
def to_s
|
2093
2285
|
message_parts = []
|
2094
|
-
|
2095
|
-
|
2096
|
-
|
2097
|
-
head = head.to_s
|
2098
|
-
unless(head.empty?)
|
2099
|
-
message_parts << add_period(head)
|
2100
|
-
end
|
2286
|
+
head = user_message
|
2287
|
+
if head and not head.empty?
|
2288
|
+
message_parts << add_period(head)
|
2101
2289
|
end
|
2102
2290
|
tail = template.result(@parameters.collect{|e| convert(e)})
|
2103
2291
|
message_parts << tail unless(tail.empty?)
|
2104
2292
|
message_parts.join("\n")
|
2105
2293
|
end
|
2294
|
+
|
2295
|
+
private
|
2296
|
+
def add_period(string)
|
2297
|
+
(string =~ /\.\Z/ ? string : string + '.')
|
2298
|
+
end
|
2106
2299
|
end
|
2107
2300
|
|
2108
2301
|
class AssertExceptionHelper
|
@@ -2114,7 +2307,14 @@ EOT
|
|
2114
2307
|
|
2115
2308
|
def inspect
|
2116
2309
|
if default_inspect?
|
2117
|
-
"#{@exception.class.inspect}(<#{@exception.message}>)"
|
2310
|
+
inspected = "#{@exception.class.inspect}(<#{@exception.message}>)"
|
2311
|
+
unless (@exception.backtrace || []).empty?
|
2312
|
+
inspected += "\n"
|
2313
|
+
@exception.backtrace.each do |trace|
|
2314
|
+
inspected << "#{trace}\n"
|
2315
|
+
end
|
2316
|
+
end
|
2317
|
+
inspected
|
2118
2318
|
else
|
2119
2319
|
@exception.inspect
|
2120
2320
|
end
|