test-unit 3.2.9 → 3.5.5
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 +4 -4
- data/BSDL +24 -0
- data/COPYING +41 -44
- data/README.md +18 -11
- data/Rakefile +0 -23
- data/doc/text/getting-started.md +2 -2
- data/doc/text/news.md +329 -1
- data/lib/test/unit/assertion-failed-error.rb +35 -0
- data/lib/test/unit/assertions.rb +453 -161
- data/lib/test/unit/attribute.rb +3 -1
- data/lib/test/unit/autorunner.rb +78 -30
- 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 +3 -3
- 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 +26 -15
- data/lib/test/unit/data.rb +5 -5
- 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 -5
- data/lib/test/unit/runner/console.rb +0 -17
- data/lib/test/unit/testcase.rb +222 -146
- data/lib/test/unit/testsuite.rb +1 -1
- data/lib/test/unit/ui/console/testrunner.rb +58 -49
- 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/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 +14 -91
- 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 -419
- 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 -1279
- 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,29 +919,51 @@ 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
|
865
927
|
def assert_in_delta(expected_float, actual_float, delta=0.001, message="")
|
866
928
|
_wrap_assertion do
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
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
|
874
958
|
assert_block(full_message) do
|
875
|
-
|
959
|
+
pass
|
876
960
|
end
|
877
961
|
end
|
878
962
|
end
|
879
963
|
|
880
964
|
##
|
881
|
-
# Passes if
|
882
|
-
# not equal within
|
965
|
+
# Passes if `expected_float` and `actual_float` are
|
966
|
+
# not equal within `delta` tolerance.
|
883
967
|
#
|
884
968
|
# @example
|
885
969
|
# assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
|
@@ -889,13 +973,32 @@ EOT
|
|
889
973
|
_assert_in_delta_validate_arguments(expected_float,
|
890
974
|
actual_float,
|
891
975
|
delta)
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
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
|
897
1000
|
assert_block(full_message) do
|
898
|
-
|
1001
|
+
pass
|
899
1002
|
end
|
900
1003
|
end
|
901
1004
|
end
|
@@ -922,7 +1025,9 @@ EOT
|
|
922
1025
|
assert_operator(delta, :>=, 0.0, "The delta should not be negative")
|
923
1026
|
end
|
924
1027
|
|
925
|
-
def _assert_in_delta_message(expected_float,
|
1028
|
+
def _assert_in_delta_message(expected_float, normalized_expected,
|
1029
|
+
actual_float, normalized_actual,
|
1030
|
+
delta, normalized_delta,
|
926
1031
|
message, options={})
|
927
1032
|
if options[:negative_assertion]
|
928
1033
|
format = <<-EOT
|
@@ -936,9 +1041,6 @@ EOT
|
|
936
1041
|
EOT
|
937
1042
|
end
|
938
1043
|
arguments = [expected_float, delta, actual_float]
|
939
|
-
normalized_expected = expected_float.to_f
|
940
|
-
normalized_actual = actual_float.to_f
|
941
|
-
normalized_delta = delta.to_f
|
942
1044
|
relation_format = nil
|
943
1045
|
relation_arguments = nil
|
944
1046
|
if normalized_actual < normalized_expected - normalized_delta
|
@@ -978,8 +1080,8 @@ EOT
|
|
978
1080
|
|
979
1081
|
public
|
980
1082
|
##
|
981
|
-
# Passes if
|
982
|
-
# within
|
1083
|
+
# Passes if `expected_float` and `actual_float` are equal
|
1084
|
+
# within `epsilon` relative error of `expected_float`.
|
983
1085
|
#
|
984
1086
|
# @example
|
985
1087
|
# assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
|
@@ -987,30 +1089,60 @@ EOT
|
|
987
1089
|
def assert_in_epsilon(expected_float, actual_float, epsilon=0.001,
|
988
1090
|
message="")
|
989
1091
|
_wrap_assertion do
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
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
|
1001
1122
|
else
|
1002
|
-
delta =
|
1123
|
+
delta = normalized_expected * normalized_epsilon
|
1003
1124
|
end
|
1004
1125
|
delta = delta.abs
|
1005
|
-
|
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
|
1006
1138
|
end
|
1007
1139
|
end
|
1008
1140
|
end
|
1009
1141
|
|
1010
1142
|
##
|
1011
|
-
# Passes if
|
1012
|
-
# not equal within
|
1013
|
-
#
|
1143
|
+
# Passes if `expected_float` and `actual_float` are
|
1144
|
+
# not equal within `epsilon` relative error of
|
1145
|
+
# `expected_float`.
|
1014
1146
|
#
|
1015
1147
|
# @example
|
1016
1148
|
# assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
|
@@ -1018,18 +1150,43 @@ EOT
|
|
1018
1150
|
def assert_not_in_epsilon(expected_float, actual_float, epsilon=0.001,
|
1019
1151
|
message="")
|
1020
1152
|
_wrap_assertion do
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
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
|
1029
1188
|
assert_block(full_message) do
|
1030
|
-
|
1031
|
-
delta = normalized_expected_float * epsilon.to_f
|
1032
|
-
(normalized_expected_float - actual_float.to_f).abs > delta
|
1189
|
+
pass
|
1033
1190
|
end
|
1034
1191
|
end
|
1035
1192
|
end
|
@@ -1056,13 +1213,10 @@ EOT
|
|
1056
1213
|
assert_operator(epsilon, :>=, 0.0, "The epsilon should not be negative")
|
1057
1214
|
end
|
1058
1215
|
|
1059
|
-
def _assert_in_epsilon_message(expected_float,
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
normalized_epsilon = epsilon.to_f
|
1064
|
-
delta = normalized_expected * normalized_epsilon
|
1065
|
-
|
1216
|
+
def _assert_in_epsilon_message(expected_float, normalized_expected,
|
1217
|
+
actual_float, normalized_actual,
|
1218
|
+
epsilon, normalized_epsilon,
|
1219
|
+
delta, message, options={})
|
1066
1220
|
if options[:negative_assertion]
|
1067
1221
|
format = <<-EOT
|
1068
1222
|
<?> -/+ (<?> * <?>)[?] was expected to not include
|
@@ -1116,9 +1270,9 @@ EOT
|
|
1116
1270
|
|
1117
1271
|
public
|
1118
1272
|
##
|
1119
|
-
# Passes if the method
|
1273
|
+
# Passes if the method `__send__` returns not false nor nil.
|
1120
1274
|
#
|
1121
|
-
#
|
1275
|
+
# `send_array` is composed of:
|
1122
1276
|
# * A receiver
|
1123
1277
|
# * A method
|
1124
1278
|
# * Arguments to the method
|
@@ -1156,9 +1310,9 @@ EOT
|
|
1156
1310
|
end
|
1157
1311
|
|
1158
1312
|
##
|
1159
|
-
# Passes if the method
|
1313
|
+
# Passes if the method `__send__` returns false or nil.
|
1160
1314
|
#
|
1161
|
-
#
|
1315
|
+
# `send_array` is composed of:
|
1162
1316
|
# * A receiver
|
1163
1317
|
# * A method
|
1164
1318
|
# * Arguments to the method
|
@@ -1196,7 +1350,7 @@ EOT
|
|
1196
1350
|
end
|
1197
1351
|
|
1198
1352
|
##
|
1199
|
-
# Passes if
|
1353
|
+
# Passes if `actual` is a boolean value.
|
1200
1354
|
#
|
1201
1355
|
# @example
|
1202
1356
|
# assert_boolean(true) # -> pass
|
@@ -1206,13 +1360,13 @@ EOT
|
|
1206
1360
|
assert_block(build_message(message,
|
1207
1361
|
"<true> or <false> expected but was\n<?>",
|
1208
1362
|
actual)) do
|
1209
|
-
|
1363
|
+
true == actual || false == actual
|
1210
1364
|
end
|
1211
1365
|
end
|
1212
1366
|
end
|
1213
1367
|
|
1214
1368
|
##
|
1215
|
-
# Passes if
|
1369
|
+
# Passes if `actual` is true.
|
1216
1370
|
#
|
1217
1371
|
# @example
|
1218
1372
|
# assert_true(true) # -> pass
|
@@ -1222,13 +1376,13 @@ EOT
|
|
1222
1376
|
assert_block(build_message(message,
|
1223
1377
|
"<true> expected but was\n<?>",
|
1224
1378
|
actual)) do
|
1225
|
-
|
1379
|
+
true == actual
|
1226
1380
|
end
|
1227
1381
|
end
|
1228
1382
|
end
|
1229
1383
|
|
1230
1384
|
##
|
1231
|
-
# Passes if
|
1385
|
+
# Passes if `actual` is false.
|
1232
1386
|
#
|
1233
1387
|
# @example
|
1234
1388
|
# assert_false(false) # -> pass
|
@@ -1238,14 +1392,14 @@ EOT
|
|
1238
1392
|
assert_block(build_message(message,
|
1239
1393
|
"<false> expected but was\n<?>",
|
1240
1394
|
actual)) do
|
1241
|
-
|
1395
|
+
false == actual
|
1242
1396
|
end
|
1243
1397
|
end
|
1244
1398
|
end
|
1245
1399
|
|
1246
1400
|
##
|
1247
|
-
# Passes if expression "
|
1248
|
-
#
|
1401
|
+
# Passes if expression "`expected` `operator`
|
1402
|
+
# `actual`" is not false nor nil.
|
1249
1403
|
#
|
1250
1404
|
# @example
|
1251
1405
|
# assert_compare(1, "<", 10) # -> pass
|
@@ -1300,7 +1454,7 @@ EOT
|
|
1300
1454
|
|
1301
1455
|
##
|
1302
1456
|
# Passes if an exception is raised in block and its
|
1303
|
-
# message is
|
1457
|
+
# message is `expected`.
|
1304
1458
|
#
|
1305
1459
|
# @example
|
1306
1460
|
# assert_raise_message("exception") {raise "exception"} # -> pass
|
@@ -1340,7 +1494,7 @@ EOT
|
|
1340
1494
|
end
|
1341
1495
|
|
1342
1496
|
##
|
1343
|
-
# Passes if
|
1497
|
+
# Passes if `object`.const_defined?(`constant_name`)
|
1344
1498
|
#
|
1345
1499
|
# @example
|
1346
1500
|
# assert_const_defined(Test, :Unit) # -> pass
|
@@ -1357,7 +1511,7 @@ EOT
|
|
1357
1511
|
end
|
1358
1512
|
|
1359
1513
|
##
|
1360
|
-
# Passes if
|
1514
|
+
# Passes if !`object`.const_defined?(`constant_name`)
|
1361
1515
|
#
|
1362
1516
|
# @example
|
1363
1517
|
# assert_not_const_defined(Object, :Nonexistent) # -> pass
|
@@ -1374,7 +1528,7 @@ EOT
|
|
1374
1528
|
end
|
1375
1529
|
|
1376
1530
|
##
|
1377
|
-
# Passes if
|
1531
|
+
# Passes if `object`.`predicate` is not false nor nil.
|
1378
1532
|
#
|
1379
1533
|
# @example
|
1380
1534
|
# assert_predicate([], :empty?) # -> pass
|
@@ -1396,7 +1550,7 @@ EOT
|
|
1396
1550
|
end
|
1397
1551
|
|
1398
1552
|
##
|
1399
|
-
# Passes if
|
1553
|
+
# Passes if `object`.`predicate` is false or nil.
|
1400
1554
|
#
|
1401
1555
|
# @example
|
1402
1556
|
# assert_not_predicate([1], :empty?) # -> pass
|
@@ -1423,8 +1577,8 @@ EOT
|
|
1423
1577
|
alias_method :refute_predicate, :assert_not_predicate
|
1424
1578
|
|
1425
1579
|
##
|
1426
|
-
# Passes if
|
1427
|
-
#
|
1580
|
+
# Passes if `object`#`alias_name` is an alias method of
|
1581
|
+
# `object`#`original_name`.
|
1428
1582
|
#
|
1429
1583
|
# @example
|
1430
1584
|
# assert_alias_method([], :length, :size) # -> pass
|
@@ -1471,7 +1625,7 @@ EOT
|
|
1471
1625
|
end
|
1472
1626
|
|
1473
1627
|
##
|
1474
|
-
# Passes if
|
1628
|
+
# Passes if `path` exists.
|
1475
1629
|
#
|
1476
1630
|
# @example
|
1477
1631
|
# assert_path_exist("/tmp") # -> pass
|
@@ -1489,7 +1643,7 @@ EOT
|
|
1489
1643
|
end
|
1490
1644
|
|
1491
1645
|
##
|
1492
|
-
# Passes if
|
1646
|
+
# Passes if `path` doesn't exist.
|
1493
1647
|
#
|
1494
1648
|
# @example
|
1495
1649
|
# assert_path_not_exist("/nonexistent") # -> pass
|
@@ -1507,7 +1661,7 @@ EOT
|
|
1507
1661
|
end
|
1508
1662
|
|
1509
1663
|
##
|
1510
|
-
# Passes if
|
1664
|
+
# Passes if `collection` includes `object`.
|
1511
1665
|
#
|
1512
1666
|
# @example
|
1513
1667
|
# assert_include([1, 10], 1) # -> pass
|
@@ -1534,7 +1688,7 @@ EOT
|
|
1534
1688
|
alias_method :assert_includes, :assert_include
|
1535
1689
|
|
1536
1690
|
##
|
1537
|
-
# Passes if
|
1691
|
+
# Passes if `collection` doesn't include `object`.
|
1538
1692
|
#
|
1539
1693
|
# @example
|
1540
1694
|
# assert_not_include([1, 10], 5) # -> pass
|
@@ -1566,7 +1720,7 @@ EOT
|
|
1566
1720
|
alias_method :refute_includes, :assert_not_include
|
1567
1721
|
|
1568
1722
|
##
|
1569
|
-
# Passes if
|
1723
|
+
# Passes if `object` is empty.
|
1570
1724
|
#
|
1571
1725
|
# @example
|
1572
1726
|
# assert_empty("") # -> pass
|
@@ -1589,7 +1743,7 @@ EOT
|
|
1589
1743
|
end
|
1590
1744
|
|
1591
1745
|
##
|
1592
|
-
# Passes if
|
1746
|
+
# Passes if `object` is not empty.
|
1593
1747
|
#
|
1594
1748
|
# @example
|
1595
1749
|
# assert_not_empty(" ") # -> pass
|
@@ -1616,12 +1770,140 @@ EOT
|
|
1616
1770
|
# @since 3.0.0
|
1617
1771
|
alias_method :refute_empty, :assert_not_empty
|
1618
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
|
+
|
1619
1900
|
##
|
1620
|
-
# Builds a failure message.
|
1621
|
-
#
|
1622
|
-
|
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)
|
1623
1905
|
template &&= template.chomp
|
1624
|
-
return AssertionMessage.new(
|
1906
|
+
return AssertionMessage.new(user_message, template, arguments)
|
1625
1907
|
end
|
1626
1908
|
|
1627
1909
|
private
|
@@ -1688,12 +1970,11 @@ EOT
|
|
1688
1970
|
end
|
1689
1971
|
end
|
1690
1972
|
|
1691
|
-
def _set_failed_information(failure, expected, actual
|
1973
|
+
def _set_failed_information(failure, expected, actual)
|
1692
1974
|
failure.expected = expected
|
1693
1975
|
failure.actual = actual
|
1694
1976
|
failure.inspected_expected = AssertionMessage.convert(expected)
|
1695
1977
|
failure.inspected_actual = AssertionMessage.convert(actual)
|
1696
|
-
failure.user_message = user_message
|
1697
1978
|
end
|
1698
1979
|
|
1699
1980
|
class AssertionMessage
|
@@ -2071,8 +2352,8 @@ EOT
|
|
2071
2352
|
|
2072
2353
|
include Util::BacktraceFilter
|
2073
2354
|
|
2074
|
-
def initialize(
|
2075
|
-
@
|
2355
|
+
def initialize(user_message, template_string, parameters)
|
2356
|
+
@user_message = user_message
|
2076
2357
|
@template_string = template_string
|
2077
2358
|
@parameters = parameters
|
2078
2359
|
end
|
@@ -2085,24 +2366,28 @@ EOT
|
|
2085
2366
|
@template ||= Template.create(@template_string)
|
2086
2367
|
end
|
2087
2368
|
|
2088
|
-
def
|
2089
|
-
|
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
|
2090
2374
|
end
|
2091
2375
|
|
2092
2376
|
def to_s
|
2093
2377
|
message_parts = []
|
2094
|
-
|
2095
|
-
|
2096
|
-
|
2097
|
-
head = head.to_s
|
2098
|
-
unless(head.empty?)
|
2099
|
-
message_parts << add_period(head)
|
2100
|
-
end
|
2378
|
+
head = user_message
|
2379
|
+
if head and not head.empty?
|
2380
|
+
message_parts << add_period(head)
|
2101
2381
|
end
|
2102
2382
|
tail = template.result(@parameters.collect{|e| convert(e)})
|
2103
2383
|
message_parts << tail unless(tail.empty?)
|
2104
2384
|
message_parts.join("\n")
|
2105
2385
|
end
|
2386
|
+
|
2387
|
+
private
|
2388
|
+
def add_period(string)
|
2389
|
+
(string =~ /\.\Z/ ? string : string + '.')
|
2390
|
+
end
|
2106
2391
|
end
|
2107
2392
|
|
2108
2393
|
class AssertExceptionHelper
|
@@ -2114,7 +2399,14 @@ EOT
|
|
2114
2399
|
|
2115
2400
|
def inspect
|
2116
2401
|
if default_inspect?
|
2117
|
-
"#{@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
|
2118
2410
|
else
|
2119
2411
|
@exception.inspect
|
2120
2412
|
end
|