minitest 5.11.3 → 5.25.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1720 @@
1
+ require "minitest/autorun"
2
+ require_relative "metametameta"
3
+
4
+ e = Encoding.default_external
5
+ if e != Encoding::UTF_8 then
6
+ warn ""
7
+ warn ""
8
+ warn "NOTE: External encoding #{e} is not UTF-8. Tests WILL fail."
9
+ warn " Run tests with `RUBYOPT=-Eutf-8 rake` to avoid errors."
10
+ warn ""
11
+ warn ""
12
+ end
13
+
14
+ SomeError = Class.new Exception
15
+
16
+ unless defined? MyModule then
17
+ module MyModule; end
18
+ class AnError < StandardError; include MyModule; end
19
+ end
20
+
21
+ class TestMinitestAssertions < Minitest::Test
22
+ # do not call parallelize_me! - teardown accesses @tc._assertions
23
+ # which is not threadsafe. Nearly every method in here is an
24
+ # assertion test so it isn't worth splitting it out further.
25
+
26
+ # not included in JRuby
27
+ RE_LEVELS = /\(\d+ levels\) /
28
+
29
+ class DummyTest
30
+ include Minitest::Assertions
31
+
32
+ attr_accessor :assertions, :failure
33
+
34
+ def initialize
35
+ self.assertions = 0
36
+ self.failure = nil
37
+ end
38
+ end
39
+
40
+ def setup
41
+ super
42
+
43
+ Minitest::Test.reset
44
+
45
+ @tc = DummyTest.new
46
+ @zomg = "zomg ponies!" # TODO: const
47
+ @assertion_count = 1
48
+ end
49
+
50
+ def teardown
51
+ assert_equal(@assertion_count, @tc.assertions,
52
+ "expected #{@assertion_count} assertions to be fired during the test, not #{@tc.assertions}")
53
+ end
54
+
55
+ def assert_triggered expected, klass = Minitest::Assertion
56
+ e = assert_raises klass do
57
+ yield
58
+ end
59
+
60
+ msg = e.message.sub(/(---Backtrace---).*/m, '\1')
61
+ msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
62
+ msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
63
+
64
+ assert_msg = Regexp === expected ? :assert_match : :assert_equal
65
+ self.send assert_msg, expected, msg
66
+ end
67
+
68
+ def assert_unexpected expected
69
+ expected = Regexp.new expected if String === expected
70
+
71
+ assert_triggered expected, Minitest::UnexpectedError do
72
+ yield
73
+ end
74
+ end
75
+
76
+ def non_verbose
77
+ orig_verbose = $VERBOSE
78
+ $VERBOSE = false
79
+
80
+ yield
81
+ ensure
82
+ $VERBOSE = orig_verbose
83
+ end
84
+
85
+ def test_assert
86
+ @assertion_count = 2
87
+
88
+ @tc.assert_equal true, @tc.assert(true), "returns true on success"
89
+ end
90
+
91
+ def test_assert__triggered
92
+ assert_triggered "Expected false to be truthy." do
93
+ @tc.assert false
94
+ end
95
+ end
96
+
97
+ def test_assert__triggered_message
98
+ assert_triggered @zomg do
99
+ @tc.assert false, @zomg
100
+ end
101
+ end
102
+
103
+ def test_assert__triggered_lambda
104
+ assert_triggered "whoops" do
105
+ @tc.assert false, lambda { "whoops" }
106
+ end
107
+ end
108
+
109
+ def test_assert_empty
110
+ @assertion_count = 2
111
+
112
+ @tc.assert_empty []
113
+ end
114
+
115
+ def test_assert_empty_triggered
116
+ @assertion_count = 2
117
+
118
+ assert_triggered "Expected [1] to be empty." do
119
+ @tc.assert_empty [1]
120
+ end
121
+ end
122
+
123
+ def test_assert_equal
124
+ @tc.assert_equal 1, 1
125
+ end
126
+
127
+ def test_assert_equal_different_collection_array_hex_invisible
128
+ exp = Object.new
129
+ act = Object.new
130
+ msg = <<~EOM.chomp
131
+ No visible difference in the Array#inspect output.
132
+ You should look at the implementation of #== on Array or its members.
133
+ [#<Object:0xXXXXXX>]
134
+ EOM
135
+ assert_triggered msg do
136
+ @tc.assert_equal [exp], [act]
137
+ end
138
+ end
139
+
140
+ def test_assert_equal_different_collection_hash_hex_invisible
141
+ exp, act = {}, {}
142
+ exp[1] = Object.new
143
+ act[1] = Object.new
144
+ act_obj = act[1]
145
+ # TODO: switch to endless when 2.7 is dropped
146
+ act_obj.define_singleton_method(:inspect) { "#<Object:0xXXXXXX>" }
147
+ msg = <<~EOM.chomp % [act]
148
+ No visible difference in the Hash#inspect output.
149
+ You should look at the implementation of #== on Hash or its members.
150
+ %p
151
+ EOM
152
+
153
+ assert_triggered msg do
154
+ @tc.assert_equal exp, act
155
+ end
156
+ end
157
+
158
+ def test_assert_equal_different_diff_deactivated
159
+ without_diff do
160
+ assert_triggered util_msg("haha" * 10, "blah" * 10) do
161
+ exp = "haha" * 10
162
+ act = "blah" * 10
163
+
164
+ @tc.assert_equal exp, act
165
+ end
166
+ end
167
+ end
168
+
169
+ def test_assert_equal_different_message
170
+ assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
171
+ @tc.assert_equal 1, 2, message { "whoops" }
172
+ end
173
+ end
174
+
175
+ def test_assert_equal_different_lambda
176
+ assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
177
+ @tc.assert_equal 1, 2, lambda { "whoops" }
178
+ end
179
+ end
180
+
181
+ def test_assert_equal_different_hex
182
+ c = Class.new do
183
+ def initialize s; @name = s; end
184
+ end
185
+
186
+ exp = c.new "a"
187
+ act = c.new "b"
188
+ msg = <<~EOS
189
+ --- expected
190
+ +++ actual
191
+ @@ -1 +1 @@
192
+ -#<#<Class:0xXXXXXX>:0xXXXXXX @name="a">
193
+ +#<#<Class:0xXXXXXX>:0xXXXXXX @name="b">
194
+ EOS
195
+
196
+ assert_triggered msg do
197
+ @tc.assert_equal exp, act
198
+ end
199
+ end
200
+
201
+ def test_assert_equal_different_hex_invisible
202
+ exp = Object.new
203
+ act = Object.new
204
+
205
+ msg = <<~EOM.chomp
206
+ No visible difference in the Object#inspect output.
207
+ You should look at the implementation of #== on Object or its members.
208
+ #<Object:0xXXXXXX>
209
+ EOM
210
+
211
+ assert_triggered msg do
212
+ @tc.assert_equal exp, act
213
+ end
214
+ end
215
+
216
+ def test_assert_equal_different_long
217
+ msg = <<~EOM
218
+ --- expected
219
+ +++ actual
220
+ @@ -1 +1 @@
221
+ -"hahahahahahahahahahahahahahahahahahahaha"
222
+ +"blahblahblahblahblahblahblahblahblahblah"
223
+ EOM
224
+
225
+ assert_triggered msg do
226
+ exp = "haha" * 10
227
+ act = "blah" * 10
228
+
229
+ @tc.assert_equal exp, act
230
+ end
231
+ end
232
+
233
+ def test_assert_equal_different_long_invisible
234
+ msg = <<~EOM.chomp
235
+ No visible difference in the String#inspect output.
236
+ You should look at the implementation of #== on String or its members.
237
+ "blahblahblahblahblahblahblahblahblahblah"
238
+ EOM
239
+
240
+ assert_triggered msg do
241
+ exp = "blah" * 10
242
+ act = "blah" * 10
243
+ def exp.== _
244
+ false
245
+ end
246
+ @tc.assert_equal exp, act
247
+ end
248
+ end
249
+
250
+ def test_assert_equal_different_long_msg
251
+ msg = <<~EOM
252
+ message.
253
+ --- expected
254
+ +++ actual
255
+ @@ -1 +1 @@
256
+ -"hahahahahahahahahahahahahahahahahahahaha"
257
+ +"blahblahblahblahblahblahblahblahblahblah"
258
+ EOM
259
+
260
+ assert_triggered msg do
261
+ exp = "haha" * 10
262
+ act = "blah" * 10
263
+ @tc.assert_equal exp, act, "message"
264
+ end
265
+ end
266
+
267
+ def test_assert_equal_different_short
268
+ assert_triggered util_msg(1, 2) do
269
+ @tc.assert_equal 1, 2
270
+ end
271
+ end
272
+
273
+ def test_assert_equal_different_short_msg
274
+ assert_triggered util_msg(1, 2, "message") do
275
+ @tc.assert_equal 1, 2, "message"
276
+ end
277
+ end
278
+
279
+ def test_assert_equal_different_short_multiline
280
+ msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n"
281
+ assert_triggered msg do
282
+ @tc.assert_equal "a\nb", "a\nc"
283
+ end
284
+ end
285
+
286
+ def test_assert_equal_does_not_allow_lhs_nil
287
+ if Minitest::VERSION >= "6" then
288
+ warn "Time to strip the MT5 test"
289
+
290
+ @assertion_count += 1
291
+ assert_triggered(/Use assert_nil if expecting nil/) do
292
+ @tc.assert_equal nil, nil
293
+ end
294
+ else
295
+ err_re = /Use assert_nil if expecting nil from .*test_minitest_\w+.rb/
296
+ err_re = "" if $-w.nil?
297
+
298
+ assert_deprecation err_re do
299
+ @tc.assert_equal nil, nil
300
+ end
301
+ end
302
+ end
303
+
304
+ def test_assert_equal_does_not_allow_lhs_nil_triggered
305
+ assert_triggered "Expected: nil\n Actual: false" do
306
+ @tc.assert_equal nil, false
307
+ end
308
+ end
309
+
310
+ def test_assert_equal_string_bug791
311
+ exp = <<~EOM.chomp
312
+ Expected: "\\\\n"
313
+ Actual: "\\\\"
314
+ EOM
315
+ assert_triggered exp do
316
+ @tc.assert_equal "\\n", "\\"
317
+ end
318
+ end
319
+
320
+ def test_assert_equal_string_both_escaped_unescaped_newlines
321
+ msg = <<~EOM
322
+ --- expected
323
+ +++ actual
324
+ @@ -1,2 +1 @@
325
+ -"A\\n
326
+ -B"
327
+ +"A\\n\\\\nB"
328
+ EOM
329
+
330
+ assert_triggered msg do
331
+ exp = "A\\nB"
332
+ act = "A\n\\nB"
333
+
334
+ @tc.assert_equal exp, act
335
+ end
336
+ end
337
+
338
+ def test_assert_equal_string_encodings
339
+ msg = <<~EOM
340
+ --- expected
341
+ +++ actual
342
+ @@ -1,3 +1,3 @@
343
+ -# encoding: UTF-8
344
+ -# valid: false
345
+ +# encoding: #{Encoding::BINARY.name}
346
+ +# valid: true
347
+ "bad-utf8-\\xF1.txt"
348
+ EOM
349
+
350
+ assert_triggered msg do
351
+ exp = "bad-utf8-\xF1.txt"
352
+ act = exp.dup.b
353
+ @tc.assert_equal exp, act
354
+ end
355
+ end
356
+
357
+ def test_assert_equal_string_encodings_both_different
358
+ msg = <<~EOM
359
+ --- expected
360
+ +++ actual
361
+ @@ -1,3 +1,3 @@
362
+ -# encoding: US-ASCII
363
+ -# valid: false
364
+ +# encoding: #{Encoding::BINARY.name}
365
+ +# valid: true
366
+ "bad-utf8-\\xF1.txt"
367
+ EOM
368
+
369
+ assert_triggered msg do
370
+ exp = "bad-utf8-\xF1.txt".dup.force_encoding Encoding::ASCII
371
+ act = exp.dup.b
372
+ @tc.assert_equal exp, act
373
+ end
374
+ end
375
+
376
+ def test_assert_equal_unescape_newlines
377
+ msg = <<~'EOM' # NOTE single quotes on heredoc
378
+ --- expected
379
+ +++ actual
380
+ @@ -1,2 +1,2 @@
381
+ -"hello
382
+ +"hello\n
383
+ world"
384
+ EOM
385
+
386
+ assert_triggered msg do
387
+ exp = "hello\nworld"
388
+ act = 'hello\nworld' # notice single quotes
389
+
390
+ @tc.assert_equal exp, act
391
+ end
392
+ end
393
+
394
+ def test_assert_in_delta
395
+ @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
396
+ end
397
+
398
+ def test_assert_in_delta_triggered
399
+ x = "1.0e-06"
400
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do
401
+ @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001
402
+ end
403
+ end
404
+
405
+ def test_assert_in_epsilon
406
+ @assertion_count = 10
407
+
408
+ @tc.assert_in_epsilon 10_000, 9991
409
+ @tc.assert_in_epsilon 9991, 10_000
410
+ @tc.assert_in_epsilon 1.0, 1.001
411
+ @tc.assert_in_epsilon 1.001, 1.0
412
+
413
+ @tc.assert_in_epsilon 10_000, 9999.1, 0.0001
414
+ @tc.assert_in_epsilon 9999.1, 10_000, 0.0001
415
+ @tc.assert_in_epsilon 1.0, 1.0001, 0.0001
416
+ @tc.assert_in_epsilon 1.0001, 1.0, 0.0001
417
+
418
+ @tc.assert_in_epsilon(-1, -1)
419
+ @tc.assert_in_epsilon(-10_000, -9991)
420
+ end
421
+
422
+ def test_assert_in_epsilon_triggered
423
+ assert_triggered "Expected |10000 - 9990| (10) to be <= 9.99." do
424
+ @tc.assert_in_epsilon 10_000, 9990
425
+ end
426
+ end
427
+
428
+ def test_assert_in_epsilon_triggered_negative_case
429
+ x = "0.100000xxx"
430
+ y = "0.1"
431
+ assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do
432
+ @tc.assert_in_epsilon(-1.1, -1, 0.1)
433
+ end
434
+ end
435
+
436
+ def test_assert_includes
437
+ @assertion_count = 2
438
+
439
+ @tc.assert_includes [true], true
440
+ end
441
+
442
+ def test_assert_includes_triggered
443
+ @assertion_count = 3
444
+
445
+ e = @tc.assert_raises Minitest::Assertion do
446
+ @tc.assert_includes [true], false
447
+ end
448
+
449
+ expected = "Expected [true] to include false."
450
+ assert_equal expected, e.message
451
+ end
452
+
453
+ def test_assert_instance_of
454
+ @tc.assert_instance_of String, "blah"
455
+ end
456
+
457
+ def test_assert_instance_of_triggered
458
+ assert_triggered 'Expected "blah" to be an instance of Array, not String.' do
459
+ @tc.assert_instance_of Array, "blah"
460
+ end
461
+ end
462
+
463
+ def test_assert_kind_of
464
+ @tc.assert_kind_of String, "blah"
465
+ end
466
+
467
+ def test_assert_kind_of_triggered
468
+ assert_triggered 'Expected "blah" to be a kind of Array, not String.' do
469
+ @tc.assert_kind_of Array, "blah"
470
+ end
471
+ end
472
+
473
+ def test_assert_match
474
+ @assertion_count = 2
475
+ m = @tc.assert_match(/\w+/, "blah blah blah")
476
+
477
+ assert_kind_of MatchData, m
478
+ assert_equal "blah", m[0]
479
+ end
480
+
481
+ def test_assert_match_matchee_to_str
482
+ @assertion_count = 2
483
+
484
+ obj = Object.new
485
+ def obj.to_str; "blah" end
486
+
487
+ @tc.assert_match "blah", obj
488
+ end
489
+
490
+ def test_assert_match_matcher_object
491
+ @assertion_count = 2
492
+
493
+ pattern = Object.new
494
+ def pattern.=~ _; true end
495
+
496
+ @tc.assert_match pattern, 5
497
+ end
498
+
499
+ def test_assert_match_object_triggered
500
+ @assertion_count = 2
501
+
502
+ pattern = Object.new
503
+ def pattern.=~ _; false end
504
+ def pattern.inspect; "[Object]" end
505
+
506
+ assert_triggered "Expected [Object] to match 5." do
507
+ @tc.assert_match pattern, 5
508
+ end
509
+ end
510
+
511
+ def test_assert_match_triggered
512
+ @assertion_count = 2
513
+ assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
514
+ @tc.assert_match(/\d+/, "blah blah blah")
515
+ end
516
+ end
517
+
518
+ def test_assert_nil
519
+ @tc.assert_nil nil
520
+ end
521
+
522
+ def test_assert_nil_triggered
523
+ assert_triggered "Expected 42 to be nil." do
524
+ @tc.assert_nil 42
525
+ end
526
+ end
527
+
528
+ def test_assert_operator
529
+ @tc.assert_operator 2, :>, 1
530
+ end
531
+
532
+ def test_assert_operator_bad_object
533
+ bad = Object.new
534
+ def bad.== _; true end
535
+
536
+ @tc.assert_operator bad, :equal?, bad
537
+ end
538
+
539
+ def test_assert_operator_triggered
540
+ assert_triggered "Expected 2 to be < 1." do
541
+ @tc.assert_operator 2, :<, 1
542
+ end
543
+ end
544
+
545
+ def test_assert_output_both
546
+ @assertion_count = 2
547
+
548
+ @tc.assert_output "yay", "blah" do
549
+ print "yay"
550
+ $stderr.print "blah"
551
+ end
552
+ end
553
+
554
+ def test_assert_output_both_regexps
555
+ @assertion_count = 4
556
+
557
+ @tc.assert_output(/y.y/, /bl.h/) do
558
+ print "yay"
559
+ $stderr.print "blah"
560
+ end
561
+ end
562
+
563
+ def test_assert_output_err
564
+ @tc.assert_output nil, "blah" do
565
+ $stderr.print "blah"
566
+ end
567
+ end
568
+
569
+ def test_assert_output_neither
570
+ @assertion_count = 0
571
+
572
+ @tc.assert_output do
573
+ # do nothing
574
+ end
575
+ end
576
+
577
+ def test_assert_output_out
578
+ @tc.assert_output "blah" do
579
+ print "blah"
580
+ end
581
+ end
582
+
583
+ def test_assert_output_triggered_both
584
+ assert_triggered util_msg("blah", "blah blah", "In stderr") do
585
+ @tc.assert_output "yay", "blah" do
586
+ print "boo"
587
+ $stderr.print "blah blah"
588
+ end
589
+ end
590
+ end
591
+
592
+ def test_assert_output_triggered_err
593
+ assert_triggered util_msg("blah", "blah blah", "In stderr") do
594
+ @tc.assert_output nil, "blah" do
595
+ $stderr.print "blah blah"
596
+ end
597
+ end
598
+ end
599
+
600
+ def test_assert_output_triggered_out
601
+ assert_triggered util_msg("blah", "blah blah", "In stdout") do
602
+ @tc.assert_output "blah" do
603
+ print "blah blah"
604
+ end
605
+ end
606
+ end
607
+
608
+ def test_assert_output_no_block
609
+ assert_triggered "assert_output requires a block to capture output." do
610
+ @tc.assert_output "blah"
611
+ end
612
+ end
613
+
614
+ def test_assert_output_nested_assert_uncaught
615
+ @assertion_count = 1
616
+
617
+ assert_triggered "Epic Fail!" do
618
+ @tc.assert_output "blah\n" do
619
+ puts "blah"
620
+ @tc.flunk
621
+ end
622
+ end
623
+ end
624
+
625
+ def test_assert_output_nested_raise
626
+ @assertion_count = 2
627
+
628
+ @tc.assert_output "blah\n" do
629
+ @tc.assert_raises RuntimeError do
630
+ puts "blah"
631
+ raise "boom!"
632
+ end
633
+ end
634
+ end
635
+
636
+ def test_assert_output_nested_raise_bad
637
+ @assertion_count = 0
638
+
639
+ assert_unexpected "boom!" do
640
+ @tc.assert_raises do # 2) bypassed via UnexpectedError
641
+ @tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
642
+ puts "not_blah"
643
+ raise "boom!"
644
+ end
645
+ end
646
+ end
647
+ end
648
+
649
+ def test_assert_output_nested_raise_mismatch
650
+ # this test is redundant, but illustrative
651
+ @assertion_count = 0
652
+
653
+ assert_unexpected "boom!" do
654
+ @tc.assert_raises RuntimeError do # 2) bypassed via UnexpectedError
655
+ @tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
656
+ puts "not_blah"
657
+ raise ArgumentError, "boom!"
658
+ end
659
+ end
660
+ end
661
+ end
662
+
663
+ def test_assert_output_nested_throw_caught
664
+ @assertion_count = 2
665
+
666
+ @tc.assert_output "blah\n" do
667
+ @tc.assert_throws :boom! do
668
+ puts "blah"
669
+ throw :boom!
670
+ end
671
+ end
672
+ end
673
+
674
+ def test_assert_output_nested_throw_caught_bad
675
+ @assertion_count = 1 # want 0; can't prevent throw from escaping :(
676
+
677
+ @tc.assert_throws :boom! do # 2) captured via catch
678
+ @tc.assert_output "blah\n" do # 1) bypassed via throw
679
+ puts "not_blah"
680
+ throw :boom!
681
+ end
682
+ end
683
+ end
684
+
685
+ def test_assert_output_nested_throw_mismatch
686
+ @assertion_count = 0
687
+
688
+ assert_unexpected "uncaught throw :boom!" do
689
+ @tc.assert_throws :not_boom! do # 2) captured via assert_throws+rescue
690
+ @tc.assert_output "blah\n" do # 1) bypassed via throw
691
+ puts "not_blah"
692
+ throw :boom!
693
+ end
694
+ end
695
+ end
696
+ end
697
+
698
+ def test_assert_output_uncaught_raise
699
+ @assertion_count = 0
700
+
701
+ assert_unexpected "RuntimeError: boom!" do
702
+ @tc.assert_output "blah\n" do
703
+ puts "not_blah"
704
+ raise "boom!"
705
+ end
706
+ end
707
+ end
708
+
709
+ def test_assert_output_uncaught_throw
710
+ @assertion_count = 0
711
+
712
+ assert_unexpected "uncaught throw :boom!" do
713
+ @tc.assert_output "blah\n" do
714
+ puts "not_blah"
715
+ throw :boom!
716
+ end
717
+ end
718
+ end
719
+
720
+ def test_assert_predicate
721
+ @tc.assert_predicate "", :empty?
722
+ end
723
+
724
+ def test_assert_predicate_triggered
725
+ assert_triggered 'Expected "blah" to be empty?.' do
726
+ @tc.assert_predicate "blah", :empty?
727
+ end
728
+ end
729
+
730
+ def test_assert_raises
731
+ @tc.assert_raises RuntimeError do
732
+ raise "blah"
733
+ end
734
+ end
735
+
736
+ def test_assert_raises_default
737
+ @tc.assert_raises do
738
+ raise StandardError, "blah"
739
+ end
740
+ end
741
+
742
+ def test_assert_raises_default_triggered
743
+ e = assert_raises Minitest::Assertion do
744
+ @tc.assert_raises do
745
+ raise SomeError, "blah"
746
+ end
747
+ end
748
+
749
+ expected = <<~EOM.chomp
750
+ [StandardError] exception expected, not
751
+ Class: <SomeError>
752
+ Message: <"blah">
753
+ ---Backtrace---
754
+ FILE:LINE:in 'block in test_assert_raises_default_triggered'
755
+ ---------------
756
+ EOM
757
+
758
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
759
+ actual.gsub! RE_LEVELS, "" unless jruby?
760
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
761
+
762
+ assert_equal expected, actual
763
+ end
764
+
765
+ def test_assert_raises_exit
766
+ @tc.assert_raises SystemExit do
767
+ exit 1
768
+ end
769
+ end
770
+
771
+ def test_assert_raises_module
772
+ @tc.assert_raises MyModule do
773
+ raise AnError
774
+ end
775
+ end
776
+
777
+ def test_assert_raises_signals
778
+ @tc.assert_raises SignalException do
779
+ raise SignalException, :INT
780
+ end
781
+ end
782
+
783
+ def test_assert_raises_throw_nested_bad
784
+ @assertion_count = 0
785
+
786
+ assert_unexpected "RuntimeError: boom!" do
787
+ @tc.assert_raises do
788
+ @tc.assert_throws :blah do
789
+ raise "boom!"
790
+ throw :not_blah
791
+ end
792
+ end
793
+ end
794
+ end
795
+
796
+ ##
797
+ # *sigh* This is quite an odd scenario, but it is from real (albeit
798
+ # ugly) test code in ruby-core:
799
+
800
+ # https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
801
+
802
+ def test_assert_raises_skip
803
+ @assertion_count = 0
804
+
805
+ assert_triggered "skipped", Minitest::Skip do
806
+ @tc.assert_raises ArgumentError do
807
+ begin
808
+ raise "blah"
809
+ rescue
810
+ skip "skipped"
811
+ end
812
+ end
813
+ end
814
+ end
815
+
816
+ def test_assert_raises_subclass
817
+ @tc.assert_raises StandardError do
818
+ raise AnError
819
+ end
820
+ end
821
+
822
+ def test_assert_raises_subclass_triggered
823
+ e = assert_raises Minitest::Assertion do
824
+ @tc.assert_raises SomeError do
825
+ raise AnError, "some message"
826
+ end
827
+ end
828
+
829
+ expected = <<~EOM
830
+ [SomeError] exception expected, not
831
+ Class: <AnError>
832
+ Message: <"some message">
833
+ ---Backtrace---
834
+ FILE:LINE:in 'block in test_assert_raises_subclass_triggered'
835
+ ---------------
836
+ EOM
837
+
838
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
839
+ actual.gsub! RE_LEVELS, "" unless jruby?
840
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
841
+
842
+ assert_equal expected.chomp, actual
843
+ end
844
+
845
+ def test_assert_raises_triggered_different
846
+ e = assert_raises Minitest::Assertion do
847
+ @tc.assert_raises RuntimeError do
848
+ raise SyntaxError, "icky"
849
+ end
850
+ end
851
+
852
+ expected = <<~EOM.chomp
853
+ [RuntimeError] exception expected, not
854
+ Class: <SyntaxError>
855
+ Message: <"icky">
856
+ ---Backtrace---
857
+ FILE:LINE:in 'block in test_assert_raises_triggered_different'
858
+ ---------------
859
+ EOM
860
+
861
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
862
+ actual.gsub! RE_LEVELS, "" unless jruby?
863
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
864
+
865
+ assert_equal expected, actual
866
+ end
867
+
868
+ def test_assert_raises_triggered_different_msg
869
+ e = assert_raises Minitest::Assertion do
870
+ @tc.assert_raises RuntimeError, "XXX" do
871
+ raise SyntaxError, "icky"
872
+ end
873
+ end
874
+
875
+ expected = <<~EOM
876
+ XXX.
877
+ [RuntimeError] exception expected, not
878
+ Class: <SyntaxError>
879
+ Message: <"icky">
880
+ ---Backtrace---
881
+ FILE:LINE:in 'block in test_assert_raises_triggered_different_msg'
882
+ ---------------
883
+ EOM
884
+
885
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
886
+ actual.gsub! RE_LEVELS, "" unless jruby?
887
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
888
+
889
+ assert_equal expected.chomp, actual
890
+ end
891
+
892
+ def test_assert_raises_triggered_none
893
+ e = assert_raises Minitest::Assertion do
894
+ @tc.assert_raises Minitest::Assertion do
895
+ # do nothing
896
+ end
897
+ end
898
+
899
+ expected = "Minitest::Assertion expected but nothing was raised."
900
+
901
+ assert_equal expected, e.message
902
+ end
903
+
904
+ def test_assert_raises_triggered_none_msg
905
+ e = assert_raises Minitest::Assertion do
906
+ @tc.assert_raises Minitest::Assertion, "XXX" do
907
+ # do nothing
908
+ end
909
+ end
910
+
911
+ expected = "XXX.\nMinitest::Assertion expected but nothing was raised."
912
+
913
+ assert_equal expected, e.message
914
+ end
915
+
916
+ def test_assert_raises_without_block
917
+ assert_triggered "assert_raises requires a block to capture errors." do
918
+ @tc.assert_raises StandardError
919
+ end
920
+ end
921
+
922
+ def test_assert_respond_to
923
+ @tc.assert_respond_to "blah", :empty?
924
+ end
925
+
926
+ def test_assert_respond_to_triggered
927
+ assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
928
+ @tc.assert_respond_to "blah", :rawr!
929
+ end
930
+ end
931
+
932
+ def test_assert_respond_to__include_all
933
+ @tc.assert_respond_to @tc, :exit, include_all: true
934
+ end
935
+
936
+ def test_assert_respond_to__include_all_triggered
937
+ assert_triggered(/Expected .+::DummyTest. to respond to #exit\?/) do
938
+ @tc.assert_respond_to @tc, :exit?, include_all: true
939
+ end
940
+ end
941
+
942
+ def test_assert_same
943
+ @assertion_count = 3
944
+
945
+ o = "blah"
946
+ @tc.assert_same 1, 1
947
+ @tc.assert_same :blah, :blah
948
+ @tc.assert_same o, o
949
+ end
950
+
951
+ def test_assert_same_triggered
952
+ @assertion_count = 2
953
+
954
+ assert_triggered "Expected 2 (oid=N) to be the same as 1 (oid=N)." do
955
+ @tc.assert_same 1, 2
956
+ end
957
+
958
+ s1 = +"blah"
959
+ s2 = +"blah"
960
+
961
+ assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
962
+ @tc.assert_same s1, s2
963
+ end
964
+ end
965
+
966
+ def test_assert_send
967
+ @assertion_count = 0 if error_on_warn?
968
+ assert_deprecation(/DEPRECATED: assert_send/) do
969
+ @tc.assert_send [1, :<, 2]
970
+ end
971
+ end
972
+
973
+ def test_assert_send_bad
974
+ if error_on_warn? then
975
+ @assertion_count = 0
976
+ assert_deprecation(/DEPRECATED: assert_send/) do
977
+ @tc.assert_send [1, :>, 2]
978
+ end
979
+ else
980
+ assert_triggered "Expected 1.>(*[2]) to return true." do
981
+ assert_deprecation(/DEPRECATED: assert_send/) do
982
+ @tc.assert_send [1, :>, 2]
983
+ end
984
+ end
985
+ end
986
+ end
987
+
988
+ def test_assert_silent
989
+ @assertion_count = 2
990
+
991
+ @tc.assert_silent do
992
+ # do nothing
993
+ end
994
+ end
995
+
996
+ def test_assert_silent_triggered_err
997
+ assert_triggered util_msg("", "blah blah", "In stderr") do
998
+ @tc.assert_silent do
999
+ $stderr.print "blah blah"
1000
+ end
1001
+ end
1002
+ end
1003
+
1004
+ def test_assert_silent_triggered_out
1005
+ @assertion_count = 2
1006
+
1007
+ assert_triggered util_msg("", "blah blah", "In stdout") do
1008
+ @tc.assert_silent do
1009
+ print "blah blah"
1010
+ end
1011
+ end
1012
+ end
1013
+
1014
+ def test_assert_throws
1015
+ v = @tc.assert_throws :blah do
1016
+ throw :blah
1017
+ end
1018
+
1019
+ assert_nil v
1020
+ end
1021
+
1022
+ def test_assert_throws_value
1023
+ v = @tc.assert_throws :blah do
1024
+ throw :blah, 42
1025
+ end
1026
+
1027
+ assert_equal 42, v
1028
+ end
1029
+
1030
+ def test_assert_throws_argument_exception
1031
+ @assertion_count = 0
1032
+
1033
+ assert_unexpected "ArgumentError" do
1034
+ @tc.assert_throws :blah do
1035
+ raise ArgumentError
1036
+ end
1037
+ end
1038
+ end
1039
+
1040
+ def test_assert_throws_different
1041
+ assert_triggered "Expected :blah to have been thrown, not :not_blah." do
1042
+ @tc.assert_throws :blah do
1043
+ throw :not_blah
1044
+ end
1045
+ end
1046
+ end
1047
+
1048
+ def test_assert_throws_name_error
1049
+ @assertion_count = 0
1050
+
1051
+ assert_unexpected "NameError" do
1052
+ @tc.assert_throws :blah do
1053
+ raise NameError
1054
+ end
1055
+ end
1056
+ end
1057
+
1058
+ def test_assert_throws_unthrown
1059
+ assert_triggered "Expected :blah to have been thrown." do
1060
+ @tc.assert_throws :blah do
1061
+ # do nothing
1062
+ end
1063
+ end
1064
+ end
1065
+
1066
+ def test_assert_path_exists
1067
+ @tc.assert_path_exists __FILE__
1068
+ end
1069
+
1070
+ def test_assert_path_exists_triggered
1071
+ assert_triggered "Expected path 'blah' to exist." do
1072
+ @tc.assert_path_exists "blah"
1073
+ end
1074
+ end
1075
+
1076
+ def test_assert_pattern
1077
+ if RUBY_VERSION > "3" then
1078
+ @tc.assert_pattern do
1079
+ exp = if RUBY_VERSION.start_with? "3.0"
1080
+ "(eval):1: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!\n"
1081
+ else
1082
+ ""
1083
+ end
1084
+ assert_output nil, exp do
1085
+ eval "[1,2,3] => [Integer, Integer, Integer]" # eval to escape parser for ruby<3
1086
+ end
1087
+ end
1088
+ else
1089
+ @assertion_count = 0
1090
+
1091
+ assert_raises NotImplementedError do
1092
+ @tc.assert_pattern do
1093
+ # do nothing
1094
+ end
1095
+ end
1096
+ end
1097
+ end
1098
+
1099
+ def test_assert_pattern_traps_nomatchingpatternerror
1100
+ skip unless RUBY_VERSION > "3"
1101
+ exp = if RUBY_VERSION.start_with? "3.0" then
1102
+ "[1, 2, 3]" # terrible error message!
1103
+ else
1104
+ /length mismatch/
1105
+ end
1106
+
1107
+ assert_triggered exp do
1108
+ @tc.assert_pattern do
1109
+ capture_io do # 3.0 is noisy
1110
+ eval "[1,2,3] => [Integer, Integer]" # eval to escape parser for ruby<3
1111
+ end
1112
+ end
1113
+ end
1114
+ end
1115
+
1116
+ def test_assert_pattern_raises_other_exceptions
1117
+ skip unless RUBY_VERSION >= "3.0"
1118
+
1119
+ @assertion_count = 0
1120
+
1121
+ assert_raises RuntimeError do
1122
+ @tc.assert_pattern do
1123
+ raise "boom"
1124
+ end
1125
+ end
1126
+ end
1127
+
1128
+ def test_assert_pattern_with_no_block
1129
+ skip unless RUBY_VERSION >= "3.0"
1130
+
1131
+ assert_triggered "assert_pattern requires a block to capture errors." do
1132
+ @tc.assert_pattern
1133
+ end
1134
+ end
1135
+
1136
+ def test_capture_io
1137
+ @assertion_count = 0
1138
+
1139
+ non_verbose do
1140
+ out, err = capture_io do
1141
+ puts "hi"
1142
+ $stderr.puts "bye!"
1143
+ end
1144
+
1145
+ assert_equal "hi\n", out
1146
+ assert_equal "bye!\n", err
1147
+ end
1148
+ end
1149
+
1150
+ def test_capture_subprocess_io
1151
+ @assertion_count = 0
1152
+
1153
+ non_verbose do
1154
+ out, err = capture_subprocess_io do
1155
+ system "echo hi"
1156
+ system "echo bye! 1>&2"
1157
+ end
1158
+
1159
+ assert_equal "hi\n", out
1160
+ assert_equal "bye!", err.strip
1161
+ end
1162
+ end
1163
+
1164
+ def test_class_asserts_match_refutes
1165
+ @assertion_count = 0
1166
+
1167
+ methods = Minitest::Assertions.public_instance_methods.map(&:to_s)
1168
+
1169
+ # These don't have corresponding refutes _on purpose_. They're
1170
+ # useless and will never be added, so don't bother.
1171
+ ignores = %w[assert_output assert_raises assert_send
1172
+ assert_silent assert_throws assert_mock]
1173
+
1174
+ ignores += %w[assert_allocations] # for minitest-gcstats
1175
+
1176
+ asserts = methods.grep(/^assert/).sort - ignores
1177
+ refutes = methods.grep(/^refute/).sort - ignores
1178
+
1179
+ assert_empty refutes.map { |n| n.sub(/^refute/, "assert") } - asserts
1180
+ assert_empty asserts.map { |n| n.sub(/^assert/, "refute") } - refutes
1181
+ end
1182
+
1183
+ def test_delta_consistency
1184
+ @assertion_count = 2
1185
+
1186
+ @tc.assert_in_delta 0, 1, 1
1187
+
1188
+ assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
1189
+ @tc.refute_in_delta 0, 1, 1
1190
+ end
1191
+ end
1192
+
1193
+ def test_epsilon_consistency
1194
+ @assertion_count = 2
1195
+
1196
+ @tc.assert_in_epsilon 1.0, 1.001
1197
+
1198
+ msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
1199
+ assert_triggered msg do
1200
+ @tc.refute_in_epsilon 1.0, 1.001
1201
+ end
1202
+ end
1203
+
1204
+ def assert_fail_after t
1205
+ @tc.fail_after t.year, t.month, t.day, "remove the deprecations"
1206
+ end
1207
+
1208
+ def test_fail_after
1209
+ d0 = Time.now
1210
+ d1 = d0 + 86_400 # I am an idiot
1211
+
1212
+ assert_silent do
1213
+ assert_fail_after d1
1214
+ end
1215
+
1216
+ assert_triggered "remove the deprecations" do
1217
+ assert_fail_after d0
1218
+ end
1219
+ end
1220
+
1221
+ def test_flunk
1222
+ assert_triggered "Epic Fail!" do
1223
+ @tc.flunk
1224
+ end
1225
+ end
1226
+
1227
+ def test_flunk_message
1228
+ assert_triggered @zomg do
1229
+ @tc.flunk @zomg
1230
+ end
1231
+ end
1232
+
1233
+ def test_pass
1234
+ @tc.pass
1235
+ end
1236
+
1237
+ def test_refute
1238
+ @assertion_count = 2
1239
+
1240
+ @tc.assert_equal true, @tc.refute(false), "returns true on success"
1241
+ end
1242
+
1243
+ def test_refute_empty
1244
+ @assertion_count = 2
1245
+
1246
+ @tc.refute_empty [1]
1247
+ end
1248
+
1249
+ def test_refute_empty_triggered
1250
+ @assertion_count = 2
1251
+
1252
+ assert_triggered "Expected [] to not be empty." do
1253
+ @tc.refute_empty []
1254
+ end
1255
+ end
1256
+
1257
+ def test_refute_equal
1258
+ @tc.refute_equal "blah", "yay"
1259
+ end
1260
+
1261
+ def test_refute_equal_triggered
1262
+ assert_triggered 'Expected "blah" to not be equal to "blah".' do
1263
+ @tc.refute_equal "blah", "blah"
1264
+ end
1265
+ end
1266
+
1267
+ def test_refute_in_delta
1268
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
1269
+ end
1270
+
1271
+ def test_refute_in_delta_triggered
1272
+ x = "0.1"
1273
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
1274
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
1275
+ end
1276
+ end
1277
+
1278
+ def test_refute_in_epsilon
1279
+ @tc.refute_in_epsilon 10_000, 9990-1
1280
+ end
1281
+
1282
+ def test_refute_in_epsilon_triggered
1283
+ assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
1284
+ @tc.refute_in_epsilon 10_000, 9990
1285
+ flunk
1286
+ end
1287
+ end
1288
+
1289
+ def test_refute_includes
1290
+ @assertion_count = 2
1291
+
1292
+ @tc.refute_includes [true], false
1293
+ end
1294
+
1295
+ def test_refute_includes_triggered
1296
+ @assertion_count = 3
1297
+
1298
+ e = @tc.assert_raises Minitest::Assertion do
1299
+ @tc.refute_includes [true], true
1300
+ end
1301
+
1302
+ expected = "Expected [true] to not include true."
1303
+ assert_equal expected, e.message
1304
+ end
1305
+
1306
+ def test_refute_instance_of
1307
+ @tc.refute_instance_of Array, "blah"
1308
+ end
1309
+
1310
+ def test_refute_instance_of_triggered
1311
+ assert_triggered 'Expected "blah" to not be an instance of String.' do
1312
+ @tc.refute_instance_of String, "blah"
1313
+ end
1314
+ end
1315
+
1316
+ def test_refute_kind_of
1317
+ @tc.refute_kind_of Array, "blah"
1318
+ end
1319
+
1320
+ def test_refute_kind_of_triggered
1321
+ assert_triggered 'Expected "blah" to not be a kind of String.' do
1322
+ @tc.refute_kind_of String, "blah"
1323
+ end
1324
+ end
1325
+
1326
+ def test_refute_match
1327
+ @assertion_count = 2
1328
+ @tc.refute_match(/\d+/, "blah blah blah")
1329
+ end
1330
+
1331
+ def test_refute_match_matcher_object
1332
+ @assertion_count = 2
1333
+ pattern = Object.new
1334
+ def pattern.=~ _; false end
1335
+ @tc.refute_match pattern, 5
1336
+ end
1337
+
1338
+ def test_refute_match_object_triggered
1339
+ @assertion_count = 2
1340
+
1341
+ pattern = Object.new
1342
+ def pattern.=~ _; true end
1343
+ def pattern.inspect; "[Object]" end
1344
+
1345
+ assert_triggered "Expected [Object] to not match 5." do
1346
+ @tc.refute_match pattern, 5
1347
+ end
1348
+ end
1349
+
1350
+ def test_refute_match_triggered
1351
+ @assertion_count = 2
1352
+ assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
1353
+ @tc.refute_match(/\w+/, "blah blah blah")
1354
+ end
1355
+ end
1356
+
1357
+ def test_refute_nil
1358
+ @tc.refute_nil 42
1359
+ end
1360
+
1361
+ def test_refute_nil_triggered
1362
+ assert_triggered "Expected nil to not be nil." do
1363
+ @tc.refute_nil nil
1364
+ end
1365
+ end
1366
+
1367
+ def test_refute_operator
1368
+ @tc.refute_operator 2, :<, 1
1369
+ end
1370
+
1371
+ def test_refute_operator_bad_object
1372
+ bad = Object.new
1373
+ def bad.== _; true end
1374
+
1375
+ @tc.refute_operator true, :equal?, bad
1376
+ end
1377
+
1378
+ def test_refute_operator_triggered
1379
+ assert_triggered "Expected 2 to not be > 1." do
1380
+ @tc.refute_operator 2, :>, 1
1381
+ end
1382
+ end
1383
+
1384
+ def test_refute_pattern
1385
+ if RUBY_VERSION >= "3.0"
1386
+ @tc.refute_pattern do
1387
+ capture_io do # 3.0 is noisy
1388
+ eval "[1,2,3] => [Integer, Integer, String]"
1389
+ end
1390
+ end
1391
+ else
1392
+ @assertion_count = 0
1393
+
1394
+ assert_raises NotImplementedError do
1395
+ @tc.refute_pattern do
1396
+ eval "[1,2,3] => [Integer, Integer, String]"
1397
+ end
1398
+ end
1399
+ end
1400
+ end
1401
+
1402
+ def test_refute_pattern_expects_nomatchingpatternerror
1403
+ skip unless RUBY_VERSION > "3"
1404
+
1405
+ assert_triggered(/NoMatchingPatternError expected, but nothing was raised./) do
1406
+ @tc.refute_pattern do
1407
+ capture_io do # 3.0 is noisy
1408
+ eval "[1,2,3] => [Integer, Integer, Integer]"
1409
+ end
1410
+ end
1411
+ end
1412
+ end
1413
+
1414
+ def test_refute_pattern_raises_other_exceptions
1415
+ skip unless RUBY_VERSION >= "3.0"
1416
+
1417
+ @assertion_count = 0
1418
+
1419
+ assert_raises RuntimeError do
1420
+ @tc.refute_pattern do
1421
+ raise "boom"
1422
+ end
1423
+ end
1424
+ end
1425
+
1426
+ def test_refute_pattern_with_no_block
1427
+ skip unless RUBY_VERSION >= "3.0"
1428
+
1429
+ assert_triggered "refute_pattern requires a block to capture errors." do
1430
+ @tc.refute_pattern
1431
+ end
1432
+ end
1433
+
1434
+ def test_refute_predicate
1435
+ @tc.refute_predicate "42", :empty?
1436
+ end
1437
+
1438
+ def test_refute_predicate_triggered
1439
+ assert_triggered 'Expected "" to not be empty?.' do
1440
+ @tc.refute_predicate "", :empty?
1441
+ end
1442
+ end
1443
+
1444
+ def test_refute_respond_to
1445
+ @tc.refute_respond_to "blah", :rawr!
1446
+ end
1447
+
1448
+ def test_refute_respond_to_triggered
1449
+ assert_triggered 'Expected "blah" to not respond to empty?.' do
1450
+ @tc.refute_respond_to "blah", :empty?
1451
+ end
1452
+ end
1453
+
1454
+ def test_refute_respond_to__include_all
1455
+ @tc.refute_respond_to "blah", :missing, include_all: true
1456
+ end
1457
+
1458
+ def test_refute_respond_to__include_all_triggered
1459
+ assert_triggered(/Expected .*DummyTest.* to not respond to exit./) do
1460
+ @tc.refute_respond_to @tc, :exit, include_all: true
1461
+ end
1462
+ end
1463
+
1464
+ def test_refute_same
1465
+ @tc.refute_same 1, 2
1466
+ end
1467
+
1468
+ def test_refute_same_triggered
1469
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
1470
+ @tc.refute_same 1, 1
1471
+ end
1472
+ end
1473
+
1474
+ def test_refute_path_exists
1475
+ @tc.refute_path_exists "blah"
1476
+ end
1477
+
1478
+ def test_refute_path_exists_triggered
1479
+ assert_triggered "Expected path '#{__FILE__}' to not exist." do
1480
+ @tc.refute_path_exists __FILE__
1481
+ end
1482
+ end
1483
+
1484
+ def test_skip
1485
+ @assertion_count = 0
1486
+
1487
+ assert_triggered "haha!", Minitest::Skip do
1488
+ @tc.skip "haha!"
1489
+ end
1490
+ end
1491
+
1492
+ def assert_skip_until t, msg
1493
+ @tc.skip_until t.year, t.month, t.day, msg
1494
+ end
1495
+
1496
+ def test_skip_until
1497
+ @assertion_count = 0
1498
+
1499
+ d0 = Time.now
1500
+ d1 = d0 + 86_400 # I am an idiot
1501
+
1502
+ assert_deprecation(/Stale skip_until \"not yet\" at .*?:\d+$/) do
1503
+ assert_skip_until d0, "not yet"
1504
+ end
1505
+
1506
+ assert_triggered "not ready yet", Minitest::Skip do
1507
+ assert_skip_until d1, "not ready yet"
1508
+ end
1509
+ end
1510
+
1511
+ def util_msg exp, act, msg = nil
1512
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
1513
+ s = "#{msg}.\n#{s}" if msg
1514
+ s
1515
+ end
1516
+
1517
+ def without_diff
1518
+ old_diff = Minitest::Assertions.diff
1519
+ Minitest::Assertions.diff = nil
1520
+
1521
+ yield
1522
+ ensure
1523
+ Minitest::Assertions.diff = old_diff
1524
+ end
1525
+ end
1526
+
1527
+ class TestMinitestAssertionHelpers < Minitest::Test
1528
+ def assert_mu_pp exp, input, raw = false
1529
+ act = mu_pp input
1530
+
1531
+ if String === input && !raw then
1532
+ assert_equal "\"#{exp}\"", act
1533
+ else
1534
+ assert_equal exp, act
1535
+ end
1536
+ end
1537
+
1538
+ def assert_mu_pp_for_diff exp, input, raw = false
1539
+ act = mu_pp_for_diff input
1540
+
1541
+ if String === input && !raw then
1542
+ assert_equal "\"#{exp}\"", act
1543
+ else
1544
+ assert_equal exp, act
1545
+ end
1546
+ end
1547
+
1548
+ def test_diff_equal
1549
+ msg = <<~EOM.chomp
1550
+ No visible difference in the String#inspect output.
1551
+ You should look at the implementation of #== on String or its members.
1552
+ "blahblahblahblahblahblahblahblahblahblah"
1553
+ EOM
1554
+
1555
+ o1 = "blah" * 10
1556
+ o2 = "blah" * 10
1557
+ def o1.== _
1558
+ false
1559
+ end
1560
+
1561
+ assert_equal msg, diff(o1, o2)
1562
+ end
1563
+
1564
+ def test_diff_str_mixed
1565
+ msg = <<~'EOM' # NOTE single quotes on heredoc
1566
+ --- expected
1567
+ +++ actual
1568
+ @@ -1 +1 @@
1569
+ -"A\\n\nB"
1570
+ +"A\n\\nB"
1571
+ EOM
1572
+
1573
+ exp = "A\\n\nB"
1574
+ act = "A\n\\nB"
1575
+
1576
+ assert_equal msg, diff(exp, act)
1577
+ end
1578
+
1579
+ def test_diff_str_multiline
1580
+ msg = <<~EOM
1581
+ --- expected
1582
+ +++ actual
1583
+ @@ -1,2 +1,2 @@
1584
+ "A
1585
+ -B"
1586
+ +C"
1587
+ EOM
1588
+
1589
+ exp = "A\nB"
1590
+ act = "A\nC"
1591
+
1592
+ assert_equal msg, diff(exp, act)
1593
+ end
1594
+
1595
+ def test_diff_str_simple
1596
+ msg = <<~EOM.chomp
1597
+ Expected: "A"
1598
+ Actual: "B"
1599
+ EOM
1600
+
1601
+ exp = "A"
1602
+ act = "B"
1603
+
1604
+ assert_equal msg, diff(exp, act)
1605
+ end
1606
+
1607
+ def test_message
1608
+ assert_equal "blah2.", message { "blah2" }.call
1609
+ assert_equal "blah2.", message("") { "blah2" }.call
1610
+ assert_equal "blah1.\nblah2.", message(:blah1) { "blah2" }.call
1611
+ assert_equal "blah1.\nblah2.", message("blah1") { "blah2" }.call
1612
+
1613
+ message = proc { "blah1" }
1614
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1615
+
1616
+ message = message { "blah1" }
1617
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1618
+ end
1619
+
1620
+ def test_message_deferred
1621
+ var = nil
1622
+
1623
+ msg = message { var = "blah" }
1624
+
1625
+ assert_nil var
1626
+
1627
+ msg.call
1628
+
1629
+ assert_equal "blah", var
1630
+ end
1631
+
1632
+ def test_mu_pp
1633
+ assert_mu_pp 42.inspect, 42
1634
+ assert_mu_pp %w[a b c].inspect, %w[a b c]
1635
+ assert_mu_pp "A B", "A B"
1636
+ assert_mu_pp "A\\nB", "A\nB"
1637
+ assert_mu_pp "A\\\\nB", 'A\nB' # notice single quotes
1638
+ end
1639
+
1640
+ def test_mu_pp_for_diff
1641
+ assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
1642
+ assert_mu_pp_for_diff "A B", "A B"
1643
+ assert_mu_pp_for_diff [1, 2, 3].inspect, [1, 2, 3]
1644
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1645
+ end
1646
+
1647
+ def test_mu_pp_for_diff_str_bad_encoding
1648
+ str = "\666".dup.force_encoding Encoding::UTF_8
1649
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1650
+
1651
+ assert_mu_pp_for_diff exp, str, :raw
1652
+ end
1653
+
1654
+ def test_mu_pp_for_diff_str_bad_encoding_both
1655
+ str = "\666A\\n\nB".dup.force_encoding Encoding::UTF_8
1656
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""
1657
+
1658
+ assert_mu_pp_for_diff exp, str, :raw
1659
+ end
1660
+
1661
+ def test_mu_pp_for_diff_str_encoding
1662
+ str = "A\nB".b
1663
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\nB\""
1664
+
1665
+ assert_mu_pp_for_diff exp, str, :raw
1666
+ end
1667
+
1668
+ def test_mu_pp_for_diff_str_encoding_both
1669
+ str = "A\\n\nB".b
1670
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\\\n\\nB\""
1671
+
1672
+ assert_mu_pp_for_diff exp, str, :raw
1673
+ end
1674
+
1675
+ def test_mu_pp_for_diff_str_nerd
1676
+ assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
1677
+ assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
1678
+ assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
1679
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1680
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1681
+ assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
1682
+ assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
1683
+ assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
1684
+ assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
1685
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1686
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1687
+ end
1688
+
1689
+ def test_mu_pp_for_diff_str_normal
1690
+ assert_mu_pp_for_diff "", ""
1691
+ assert_mu_pp_for_diff "A\\n\n", "A\\n"
1692
+ assert_mu_pp_for_diff "A\\n\nB", "A\\nB"
1693
+ assert_mu_pp_for_diff "A\n", "A\n"
1694
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1695
+ assert_mu_pp_for_diff "\\n\n", "\\n"
1696
+ assert_mu_pp_for_diff "\n", "\n"
1697
+ assert_mu_pp_for_diff "\\n\nA", "\\nA"
1698
+ assert_mu_pp_for_diff "\nA", "\nA"
1699
+ end
1700
+
1701
+ def test_mu_pp_str_bad_encoding
1702
+ str = "\666".dup.force_encoding Encoding::UTF_8
1703
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1704
+
1705
+ assert_mu_pp exp, str, :raw
1706
+ end
1707
+
1708
+ def test_mu_pp_str_encoding
1709
+ str = "A\nB".b
1710
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\nB\""
1711
+
1712
+ assert_mu_pp exp, str, :raw
1713
+ end
1714
+
1715
+ def test_mu_pp_str_immutable
1716
+ printer = Class.new { extend Minitest::Assertions }
1717
+ str = "test".freeze
1718
+ assert_equal '"test"', printer.mu_pp(str)
1719
+ end
1720
+ end