minitest 5.10.3 → 5.18.0

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