minitest 5.11.3 → 5.22.3

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,1721 @@
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
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
772
+
773
+ assert_equal expected, actual
774
+ end
775
+
776
+ def test_assert_raises_exit
777
+ @tc.assert_raises SystemExit do
778
+ exit 1
779
+ end
780
+ end
781
+
782
+ def test_assert_raises_module
783
+ @tc.assert_raises MyModule do
784
+ raise AnError
785
+ end
786
+ end
787
+
788
+ def test_assert_raises_signals
789
+ @tc.assert_raises SignalException do
790
+ raise SignalException, :INT
791
+ end
792
+ end
793
+
794
+ def test_assert_raises_throw_nested_bad
795
+ @assertion_count = 0
796
+
797
+ assert_unexpected "RuntimeError: boom!" do
798
+ @tc.assert_raises do
799
+ @tc.assert_throws :blah do
800
+ raise "boom!"
801
+ throw :not_blah
802
+ end
803
+ end
804
+ end
805
+ end
806
+
807
+ ##
808
+ # *sigh* This is quite an odd scenario, but it is from real (albeit
809
+ # ugly) test code in ruby-core:
810
+
811
+ # https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
812
+
813
+ def test_assert_raises_skip
814
+ @assertion_count = 0
815
+
816
+ assert_triggered "skipped", Minitest::Skip do
817
+ @tc.assert_raises ArgumentError do
818
+ begin
819
+ raise "blah"
820
+ rescue
821
+ skip "skipped"
822
+ end
823
+ end
824
+ end
825
+ end
826
+
827
+ def test_assert_raises_subclass
828
+ @tc.assert_raises StandardError do
829
+ raise AnError
830
+ end
831
+ end
832
+
833
+ def test_assert_raises_subclass_triggered
834
+ e = assert_raises Minitest::Assertion do
835
+ @tc.assert_raises SomeError do
836
+ raise AnError, "some message"
837
+ end
838
+ end
839
+
840
+ expected = clean <<-EOM
841
+ [SomeError] exception expected, not
842
+ Class: <AnError>
843
+ Message: <\"some message\">
844
+ ---Backtrace---
845
+ FILE:LINE:in \'block in test_assert_raises_subclass_triggered\'
846
+ ---------------
847
+ EOM
848
+
849
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
850
+ actual.gsub!(RE_LEVELS, "") unless jruby?
851
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
852
+
853
+ assert_equal expected.chomp, actual
854
+ end
855
+
856
+ def test_assert_raises_triggered_different
857
+ e = assert_raises Minitest::Assertion do
858
+ @tc.assert_raises RuntimeError do
859
+ raise SyntaxError, "icky"
860
+ end
861
+ end
862
+
863
+ expected = clean <<-EOM.chomp
864
+ [RuntimeError] exception expected, not
865
+ Class: <SyntaxError>
866
+ Message: <\"icky\">
867
+ ---Backtrace---
868
+ FILE:LINE:in \'block in test_assert_raises_triggered_different\'
869
+ ---------------
870
+ EOM
871
+
872
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
873
+ actual.gsub!(RE_LEVELS, "") unless jruby?
874
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
875
+
876
+ assert_equal expected, actual
877
+ end
878
+
879
+ def test_assert_raises_triggered_different_msg
880
+ e = assert_raises Minitest::Assertion do
881
+ @tc.assert_raises RuntimeError, "XXX" do
882
+ raise SyntaxError, "icky"
883
+ end
884
+ end
885
+
886
+ expected = clean <<-EOM
887
+ XXX.
888
+ [RuntimeError] exception expected, not
889
+ Class: <SyntaxError>
890
+ Message: <\"icky\">
891
+ ---Backtrace---
892
+ FILE:LINE:in \'block in test_assert_raises_triggered_different_msg\'
893
+ ---------------
894
+ EOM
895
+
896
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
897
+ actual.gsub!(RE_LEVELS, "") unless jruby?
898
+ actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ")
899
+
900
+ assert_equal expected.chomp, actual
901
+ end
902
+
903
+ def test_assert_raises_triggered_none
904
+ e = assert_raises Minitest::Assertion do
905
+ @tc.assert_raises Minitest::Assertion do
906
+ # do nothing
907
+ end
908
+ end
909
+
910
+ expected = "Minitest::Assertion expected but nothing was raised."
911
+
912
+ assert_equal expected, e.message
913
+ end
914
+
915
+ def test_assert_raises_triggered_none_msg
916
+ e = assert_raises Minitest::Assertion do
917
+ @tc.assert_raises Minitest::Assertion, "XXX" do
918
+ # do nothing
919
+ end
920
+ end
921
+
922
+ expected = "XXX.\nMinitest::Assertion expected but nothing was raised."
923
+
924
+ assert_equal expected, e.message
925
+ end
926
+
927
+ def test_assert_raises_without_block
928
+ assert_triggered "assert_raises requires a block to capture errors." do
929
+ @tc.assert_raises StandardError
930
+ end
931
+ end
932
+
933
+ def test_assert_respond_to
934
+ @tc.assert_respond_to "blah", :empty?
935
+ end
936
+
937
+ def test_assert_respond_to_triggered
938
+ assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
939
+ @tc.assert_respond_to "blah", :rawr!
940
+ end
941
+ end
942
+
943
+ def test_assert_respond_to__include_all
944
+ @tc.assert_respond_to @tc, :exit, include_all: true
945
+ end
946
+
947
+ def test_assert_respond_to__include_all_triggered
948
+ assert_triggered(/Expected .+::DummyTest. to respond to #exit\?/) do
949
+ @tc.assert_respond_to @tc, :exit?, include_all: true
950
+ end
951
+ end
952
+
953
+ def test_assert_same
954
+ @assertion_count = 3
955
+
956
+ o = "blah"
957
+ @tc.assert_same 1, 1
958
+ @tc.assert_same :blah, :blah
959
+ @tc.assert_same o, o
960
+ end
961
+
962
+ def test_assert_same_triggered
963
+ @assertion_count = 2
964
+
965
+ assert_triggered "Expected 2 (oid=N) to be the same as 1 (oid=N)." do
966
+ @tc.assert_same 1, 2
967
+ end
968
+
969
+ s1 = "blah"
970
+ s2 = "blah"
971
+
972
+ assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
973
+ @tc.assert_same s1, s2
974
+ end
975
+ end
976
+
977
+ def test_assert_send
978
+ assert_deprecated :assert_send do
979
+ @tc.assert_send [1, :<, 2]
980
+ end
981
+ end
982
+
983
+ def test_assert_send_bad
984
+ assert_deprecated :assert_send do
985
+ assert_triggered "Expected 1.>(*[2]) to return true." do
986
+ @tc.assert_send [1, :>, 2]
987
+ end
988
+ end
989
+ end
990
+
991
+ def test_assert_silent
992
+ @assertion_count = 2
993
+
994
+ @tc.assert_silent do
995
+ # do nothing
996
+ end
997
+ end
998
+
999
+ def test_assert_silent_triggered_err
1000
+ assert_triggered util_msg("", "blah blah", "In stderr") do
1001
+ @tc.assert_silent do
1002
+ $stderr.print "blah blah"
1003
+ end
1004
+ end
1005
+ end
1006
+
1007
+ def test_assert_silent_triggered_out
1008
+ @assertion_count = 2
1009
+
1010
+ assert_triggered util_msg("", "blah blah", "In stdout") do
1011
+ @tc.assert_silent do
1012
+ print "blah blah"
1013
+ end
1014
+ end
1015
+ end
1016
+
1017
+ def test_assert_throws
1018
+ v = @tc.assert_throws :blah do
1019
+ throw :blah
1020
+ end
1021
+
1022
+ assert_nil v
1023
+ end
1024
+
1025
+ def test_assert_throws_value
1026
+ v = @tc.assert_throws :blah do
1027
+ throw :blah, 42
1028
+ end
1029
+
1030
+ assert_equal 42, v
1031
+ end
1032
+
1033
+ def test_assert_throws_argument_exception
1034
+ @assertion_count = 0
1035
+
1036
+ assert_unexpected "ArgumentError" do
1037
+ @tc.assert_throws :blah do
1038
+ raise ArgumentError
1039
+ end
1040
+ end
1041
+ end
1042
+
1043
+ def test_assert_throws_different
1044
+ assert_triggered "Expected :blah to have been thrown, not :not_blah." do
1045
+ @tc.assert_throws :blah do
1046
+ throw :not_blah
1047
+ end
1048
+ end
1049
+ end
1050
+
1051
+ def test_assert_throws_name_error
1052
+ @assertion_count = 0
1053
+
1054
+ assert_unexpected "NameError" do
1055
+ @tc.assert_throws :blah do
1056
+ raise NameError
1057
+ end
1058
+ end
1059
+ end
1060
+
1061
+ def test_assert_throws_unthrown
1062
+ assert_triggered "Expected :blah to have been thrown." do
1063
+ @tc.assert_throws :blah do
1064
+ # do nothing
1065
+ end
1066
+ end
1067
+ end
1068
+
1069
+ def test_assert_path_exists
1070
+ @tc.assert_path_exists __FILE__
1071
+ end
1072
+
1073
+ def test_assert_path_exists_triggered
1074
+ assert_triggered "Expected path 'blah' to exist." do
1075
+ @tc.assert_path_exists "blah"
1076
+ end
1077
+ end
1078
+
1079
+ def test_assert_pattern
1080
+ if RUBY_VERSION > "3" then
1081
+ @tc.assert_pattern do
1082
+ exp = if RUBY_VERSION.start_with? "3.0"
1083
+ "(eval):1: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!\n"
1084
+ else
1085
+ ""
1086
+ end
1087
+ assert_output nil, exp do
1088
+ eval "[1,2,3] => [Integer, Integer, Integer]" # eval to escape parser for ruby<3
1089
+ end
1090
+ end
1091
+ else
1092
+ @assertion_count = 0
1093
+
1094
+ assert_raises NotImplementedError do
1095
+ @tc.assert_pattern do
1096
+ # do nothing
1097
+ end
1098
+ end
1099
+ end
1100
+ end
1101
+
1102
+ def test_assert_pattern_traps_nomatchingpatternerror
1103
+ skip unless RUBY_VERSION > "3"
1104
+ exp = if RUBY_VERSION.start_with? "3.0" then
1105
+ "[1, 2, 3]" # terrible error message!
1106
+ else
1107
+ /length mismatch/
1108
+ end
1109
+
1110
+ assert_triggered exp do
1111
+ @tc.assert_pattern do
1112
+ capture_io do # 3.0 is noisy
1113
+ eval "[1,2,3] => [Integer, Integer]" # eval to escape parser for ruby<3
1114
+ end
1115
+ end
1116
+ end
1117
+ end
1118
+
1119
+ def test_assert_pattern_raises_other_exceptions
1120
+ skip unless RUBY_VERSION >= "3.0"
1121
+
1122
+ @assertion_count = 0
1123
+
1124
+ assert_raises RuntimeError do
1125
+ @tc.assert_pattern do
1126
+ raise "boom"
1127
+ end
1128
+ end
1129
+ end
1130
+
1131
+ def test_assert_pattern_with_no_block
1132
+ skip unless RUBY_VERSION >= "3.0"
1133
+
1134
+ assert_triggered "assert_pattern requires a block to capture errors." do
1135
+ @tc.assert_pattern
1136
+ end
1137
+ end
1138
+
1139
+ def test_capture_io
1140
+ @assertion_count = 0
1141
+
1142
+ non_verbose do
1143
+ out, err = capture_io do
1144
+ puts "hi"
1145
+ $stderr.puts "bye!"
1146
+ end
1147
+
1148
+ assert_equal "hi\n", out
1149
+ assert_equal "bye!\n", err
1150
+ end
1151
+ end
1152
+
1153
+ def test_capture_subprocess_io
1154
+ @assertion_count = 0
1155
+
1156
+ non_verbose do
1157
+ out, err = capture_subprocess_io do
1158
+ system("echo hi")
1159
+ system("echo bye! 1>&2")
1160
+ end
1161
+
1162
+ assert_equal "hi\n", out
1163
+ assert_equal "bye!", err.strip
1164
+ end
1165
+ end
1166
+
1167
+ def test_class_asserts_match_refutes
1168
+ @assertion_count = 0
1169
+
1170
+ methods = Minitest::Assertions.public_instance_methods.map(&:to_s)
1171
+
1172
+ # These don't have corresponding refutes _on purpose_. They're
1173
+ # useless and will never be added, so don't bother.
1174
+ ignores = %w[assert_output assert_raises assert_send
1175
+ assert_silent assert_throws assert_mock]
1176
+
1177
+ ignores += %w[assert_allocations] # for minitest-gcstats
1178
+
1179
+ asserts = methods.grep(/^assert/).sort - ignores
1180
+ refutes = methods.grep(/^refute/).sort - ignores
1181
+
1182
+ assert_empty refutes.map { |n| n.sub(/^refute/, "assert") } - asserts
1183
+ assert_empty asserts.map { |n| n.sub(/^assert/, "refute") } - refutes
1184
+ end
1185
+
1186
+ def test_delta_consistency
1187
+ @assertion_count = 2
1188
+
1189
+ @tc.assert_in_delta 0, 1, 1
1190
+
1191
+ assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
1192
+ @tc.refute_in_delta 0, 1, 1
1193
+ end
1194
+ end
1195
+
1196
+ def test_epsilon_consistency
1197
+ @assertion_count = 2
1198
+
1199
+ @tc.assert_in_epsilon 1.0, 1.001
1200
+
1201
+ msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
1202
+ assert_triggered msg do
1203
+ @tc.refute_in_epsilon 1.0, 1.001
1204
+ end
1205
+ end
1206
+
1207
+ def assert_fail_after t
1208
+ @tc.fail_after t.year, t.month, t.day, "remove the deprecations"
1209
+ end
1210
+
1211
+ def test_fail_after
1212
+ d0 = Time.now
1213
+ d1 = d0 + 86_400 # I am an idiot
1214
+
1215
+ assert_silent do
1216
+ assert_fail_after d1
1217
+ end
1218
+
1219
+ assert_triggered "remove the deprecations" do
1220
+ assert_fail_after d0
1221
+ end
1222
+ end
1223
+
1224
+ def test_flunk
1225
+ assert_triggered "Epic Fail!" do
1226
+ @tc.flunk
1227
+ end
1228
+ end
1229
+
1230
+ def test_flunk_message
1231
+ assert_triggered @zomg do
1232
+ @tc.flunk @zomg
1233
+ end
1234
+ end
1235
+
1236
+ def test_pass
1237
+ @tc.pass
1238
+ end
1239
+
1240
+ def test_refute
1241
+ @assertion_count = 2
1242
+
1243
+ @tc.assert_equal true, @tc.refute(false), "returns true on success"
1244
+ end
1245
+
1246
+ def test_refute_empty
1247
+ @assertion_count = 2
1248
+
1249
+ @tc.refute_empty [1]
1250
+ end
1251
+
1252
+ def test_refute_empty_triggered
1253
+ @assertion_count = 2
1254
+
1255
+ assert_triggered "Expected [] to not be empty." do
1256
+ @tc.refute_empty []
1257
+ end
1258
+ end
1259
+
1260
+ def test_refute_equal
1261
+ @tc.refute_equal "blah", "yay"
1262
+ end
1263
+
1264
+ def test_refute_equal_triggered
1265
+ assert_triggered 'Expected "blah" to not be equal to "blah".' do
1266
+ @tc.refute_equal "blah", "blah"
1267
+ end
1268
+ end
1269
+
1270
+ def test_refute_in_delta
1271
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
1272
+ end
1273
+
1274
+ def test_refute_in_delta_triggered
1275
+ x = "0.1"
1276
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
1277
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
1278
+ end
1279
+ end
1280
+
1281
+ def test_refute_in_epsilon
1282
+ @tc.refute_in_epsilon 10_000, 9990-1
1283
+ end
1284
+
1285
+ def test_refute_in_epsilon_triggered
1286
+ assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
1287
+ @tc.refute_in_epsilon 10_000, 9990
1288
+ flunk
1289
+ end
1290
+ end
1291
+
1292
+ def test_refute_includes
1293
+ @assertion_count = 2
1294
+
1295
+ @tc.refute_includes [true], false
1296
+ end
1297
+
1298
+ def test_refute_includes_triggered
1299
+ @assertion_count = 3
1300
+
1301
+ e = @tc.assert_raises Minitest::Assertion do
1302
+ @tc.refute_includes [true], true
1303
+ end
1304
+
1305
+ expected = "Expected [true] to not include true."
1306
+ assert_equal expected, e.message
1307
+ end
1308
+
1309
+ def test_refute_instance_of
1310
+ @tc.refute_instance_of Array, "blah"
1311
+ end
1312
+
1313
+ def test_refute_instance_of_triggered
1314
+ assert_triggered 'Expected "blah" to not be an instance of String.' do
1315
+ @tc.refute_instance_of String, "blah"
1316
+ end
1317
+ end
1318
+
1319
+ def test_refute_kind_of
1320
+ @tc.refute_kind_of Array, "blah"
1321
+ end
1322
+
1323
+ def test_refute_kind_of_triggered
1324
+ assert_triggered 'Expected "blah" to not be a kind of String.' do
1325
+ @tc.refute_kind_of String, "blah"
1326
+ end
1327
+ end
1328
+
1329
+ def test_refute_match
1330
+ @assertion_count = 2
1331
+ @tc.refute_match(/\d+/, "blah blah blah")
1332
+ end
1333
+
1334
+ def test_refute_match_matcher_object
1335
+ @assertion_count = 2
1336
+ pattern = Object.new
1337
+ def pattern.=~ _; false end
1338
+ @tc.refute_match pattern, 5
1339
+ end
1340
+
1341
+ def test_refute_match_object_triggered
1342
+ @assertion_count = 2
1343
+
1344
+ pattern = Object.new
1345
+ def pattern.=~ _; true end
1346
+ def pattern.inspect; "[Object]" end
1347
+
1348
+ assert_triggered "Expected [Object] to not match 5." do
1349
+ @tc.refute_match pattern, 5
1350
+ end
1351
+ end
1352
+
1353
+ def test_refute_match_triggered
1354
+ @assertion_count = 2
1355
+ assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
1356
+ @tc.refute_match(/\w+/, "blah blah blah")
1357
+ end
1358
+ end
1359
+
1360
+ def test_refute_nil
1361
+ @tc.refute_nil 42
1362
+ end
1363
+
1364
+ def test_refute_nil_triggered
1365
+ assert_triggered "Expected nil to not be nil." do
1366
+ @tc.refute_nil nil
1367
+ end
1368
+ end
1369
+
1370
+ def test_refute_operator
1371
+ @tc.refute_operator 2, :<, 1
1372
+ end
1373
+
1374
+ def test_refute_operator_bad_object
1375
+ bad = Object.new
1376
+ def bad.== _; true end
1377
+
1378
+ @tc.refute_operator true, :equal?, bad
1379
+ end
1380
+
1381
+ def test_refute_operator_triggered
1382
+ assert_triggered "Expected 2 to not be > 1." do
1383
+ @tc.refute_operator 2, :>, 1
1384
+ end
1385
+ end
1386
+
1387
+ def test_refute_pattern
1388
+ if RUBY_VERSION >= "3.0"
1389
+ @tc.refute_pattern do
1390
+ capture_io do # 3.0 is noisy
1391
+ eval "[1,2,3] => [Integer, Integer, String]"
1392
+ end
1393
+ end
1394
+ else
1395
+ @assertion_count = 0
1396
+
1397
+ assert_raises NotImplementedError do
1398
+ @tc.refute_pattern do
1399
+ eval "[1,2,3] => [Integer, Integer, String]"
1400
+ end
1401
+ end
1402
+ end
1403
+ end
1404
+
1405
+ def test_refute_pattern_expects_nomatchingpatternerror
1406
+ skip unless RUBY_VERSION > "3"
1407
+
1408
+ assert_triggered(/NoMatchingPatternError expected, but nothing was raised./) do
1409
+ @tc.refute_pattern do
1410
+ capture_io do # 3.0 is noisy
1411
+ eval "[1,2,3] => [Integer, Integer, Integer]"
1412
+ end
1413
+ end
1414
+ end
1415
+ end
1416
+
1417
+ def test_refute_pattern_raises_other_exceptions
1418
+ skip unless RUBY_VERSION >= "3.0"
1419
+
1420
+ @assertion_count = 0
1421
+
1422
+ assert_raises RuntimeError do
1423
+ @tc.refute_pattern do
1424
+ raise "boom"
1425
+ end
1426
+ end
1427
+ end
1428
+
1429
+ def test_refute_pattern_with_no_block
1430
+ skip unless RUBY_VERSION >= "3.0"
1431
+
1432
+ assert_triggered "refute_pattern requires a block to capture errors." do
1433
+ @tc.refute_pattern
1434
+ end
1435
+ end
1436
+
1437
+ def test_refute_predicate
1438
+ @tc.refute_predicate "42", :empty?
1439
+ end
1440
+
1441
+ def test_refute_predicate_triggered
1442
+ assert_triggered 'Expected "" to not be empty?.' do
1443
+ @tc.refute_predicate "", :empty?
1444
+ end
1445
+ end
1446
+
1447
+ def test_refute_respond_to
1448
+ @tc.refute_respond_to "blah", :rawr!
1449
+ end
1450
+
1451
+ def test_refute_respond_to_triggered
1452
+ assert_triggered 'Expected "blah" to not respond to empty?.' do
1453
+ @tc.refute_respond_to "blah", :empty?
1454
+ end
1455
+ end
1456
+
1457
+ def test_refute_respond_to__include_all
1458
+ @tc.refute_respond_to "blah", :missing, include_all: true
1459
+ end
1460
+
1461
+ def test_refute_respond_to__include_all_triggered
1462
+ assert_triggered(/Expected .*DummyTest.* to not respond to exit./) do
1463
+ @tc.refute_respond_to @tc, :exit, include_all: true
1464
+ end
1465
+ end
1466
+
1467
+ def test_refute_same
1468
+ @tc.refute_same 1, 2
1469
+ end
1470
+
1471
+ def test_refute_same_triggered
1472
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
1473
+ @tc.refute_same 1, 1
1474
+ end
1475
+ end
1476
+
1477
+ def test_refute_path_exists
1478
+ @tc.refute_path_exists "blah"
1479
+ end
1480
+
1481
+ def test_refute_path_exists_triggered
1482
+ assert_triggered "Expected path '#{__FILE__}' to not exist." do
1483
+ @tc.refute_path_exists __FILE__
1484
+ end
1485
+ end
1486
+
1487
+ def test_skip
1488
+ @assertion_count = 0
1489
+
1490
+ assert_triggered "haha!", Minitest::Skip do
1491
+ @tc.skip "haha!"
1492
+ end
1493
+ end
1494
+
1495
+ def assert_skip_until t, msg
1496
+ @tc.skip_until t.year, t.month, t.day, msg
1497
+ end
1498
+
1499
+ def test_skip_until
1500
+ @assertion_count = 0
1501
+
1502
+ d0 = Time.now
1503
+ d1 = d0 + 86_400 # I am an idiot
1504
+
1505
+ assert_output "", /Stale skip_until \"not yet\" at .*?:\d+$/ do
1506
+ assert_skip_until d0, "not yet"
1507
+ end
1508
+
1509
+ assert_triggered "not ready yet", Minitest::Skip do
1510
+ assert_skip_until d1, "not ready yet"
1511
+ end
1512
+ end
1513
+
1514
+ def util_msg exp, act, msg = nil
1515
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
1516
+ s = "#{msg}.\n#{s}" if msg
1517
+ s
1518
+ end
1519
+
1520
+ def without_diff
1521
+ old_diff = Minitest::Assertions.diff
1522
+ Minitest::Assertions.diff = nil
1523
+
1524
+ yield
1525
+ ensure
1526
+ Minitest::Assertions.diff = old_diff
1527
+ end
1528
+ end
1529
+
1530
+ class TestMinitestAssertionHelpers < Minitest::Test
1531
+ def assert_mu_pp exp, input, raw = false
1532
+ act = mu_pp input
1533
+
1534
+ if String === input && !raw then
1535
+ assert_equal "\"#{exp}\"", act
1536
+ else
1537
+ assert_equal exp, act
1538
+ end
1539
+ end
1540
+
1541
+ def assert_mu_pp_for_diff exp, input, raw = false
1542
+ act = mu_pp_for_diff input
1543
+
1544
+ if String === input && !raw then
1545
+ assert_equal "\"#{exp}\"", act
1546
+ else
1547
+ assert_equal exp, act
1548
+ end
1549
+ end
1550
+
1551
+ def test_diff_equal
1552
+ msg = "No visible difference in the String#inspect output.
1553
+ You should look at the implementation of #== on String or its members.
1554
+ \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
1555
+
1556
+ o1 = "blah" * 10
1557
+ o2 = "blah" * 10
1558
+ def o1.== _
1559
+ false
1560
+ end
1561
+
1562
+ assert_equal msg, diff(o1, o2)
1563
+ end
1564
+
1565
+ def test_diff_str_mixed
1566
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1567
+ --- expected
1568
+ +++ actual
1569
+ @@ -1 +1 @@
1570
+ -"A\\n\nB"
1571
+ +"A\n\\nB"
1572
+ EOM
1573
+
1574
+ exp = "A\\n\nB"
1575
+ act = "A\n\\nB"
1576
+
1577
+ assert_equal msg, diff(exp, act)
1578
+ end
1579
+
1580
+ def test_diff_str_multiline
1581
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1582
+ --- expected
1583
+ +++ actual
1584
+ @@ -1,2 +1,2 @@
1585
+ "A
1586
+ -B"
1587
+ +C"
1588
+ EOM
1589
+
1590
+ exp = "A\nB"
1591
+ act = "A\nC"
1592
+
1593
+ assert_equal msg, diff(exp, act)
1594
+ end
1595
+
1596
+ def test_diff_str_simple
1597
+ msg = <<-'EOM'.gsub(/^ {10}/, "").chomp # NOTE single quotes on heredoc
1598
+ Expected: "A"
1599
+ Actual: "B"
1600
+ EOM
1601
+
1602
+ exp = "A"
1603
+ act = "B"
1604
+
1605
+ assert_equal msg, diff(exp, act)
1606
+ end
1607
+
1608
+ def test_message
1609
+ assert_equal "blah2.", message { "blah2" }.call
1610
+ assert_equal "blah2.", message("") { "blah2" }.call
1611
+ assert_equal "blah1.\nblah2.", message(:blah1) { "blah2" }.call
1612
+ assert_equal "blah1.\nblah2.", message("blah1") { "blah2" }.call
1613
+
1614
+ message = proc { "blah1" }
1615
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1616
+
1617
+ message = message { "blah1" }
1618
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1619
+ end
1620
+
1621
+ def test_message_deferred
1622
+ var = nil
1623
+
1624
+ msg = message { var = "blah" }
1625
+
1626
+ assert_nil var
1627
+
1628
+ msg.call
1629
+
1630
+ assert_equal "blah", var
1631
+ end
1632
+
1633
+ def test_mu_pp
1634
+ assert_mu_pp 42.inspect, 42
1635
+ assert_mu_pp %w[a b c].inspect, %w[a b c]
1636
+ assert_mu_pp "A B", "A B"
1637
+ assert_mu_pp "A\\nB", "A\nB"
1638
+ assert_mu_pp "A\\\\nB", 'A\nB' # notice single quotes
1639
+ end
1640
+
1641
+ def test_mu_pp_for_diff
1642
+ assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
1643
+ assert_mu_pp_for_diff "A B", "A B"
1644
+ assert_mu_pp_for_diff [1, 2, 3].inspect, [1, 2, 3]
1645
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1646
+ end
1647
+
1648
+ def test_mu_pp_for_diff_str_bad_encoding
1649
+ str = "\666".force_encoding Encoding::UTF_8
1650
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1651
+
1652
+ assert_mu_pp_for_diff exp, str, :raw
1653
+ end
1654
+
1655
+ def test_mu_pp_for_diff_str_bad_encoding_both
1656
+ str = "\666A\\n\nB".force_encoding Encoding::UTF_8
1657
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""
1658
+
1659
+ assert_mu_pp_for_diff exp, str, :raw
1660
+ end
1661
+
1662
+ def test_mu_pp_for_diff_str_encoding
1663
+ str = "A\nB".b
1664
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\nB\""
1665
+
1666
+ assert_mu_pp_for_diff exp, str, :raw
1667
+ end
1668
+
1669
+ def test_mu_pp_for_diff_str_encoding_both
1670
+ str = "A\\n\nB".b
1671
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\\\n\\nB\""
1672
+
1673
+ assert_mu_pp_for_diff exp, str, :raw
1674
+ end
1675
+
1676
+ def test_mu_pp_for_diff_str_nerd
1677
+ assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
1678
+ assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
1679
+ assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
1680
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1681
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1682
+ assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
1683
+ assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
1684
+ assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
1685
+ assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
1686
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1687
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1688
+ end
1689
+
1690
+ def test_mu_pp_for_diff_str_normal
1691
+ assert_mu_pp_for_diff "", ""
1692
+ assert_mu_pp_for_diff "A\\n\n", "A\\n"
1693
+ assert_mu_pp_for_diff "A\\n\nB", "A\\nB"
1694
+ assert_mu_pp_for_diff "A\n", "A\n"
1695
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1696
+ assert_mu_pp_for_diff "\\n\n", "\\n"
1697
+ assert_mu_pp_for_diff "\n", "\n"
1698
+ assert_mu_pp_for_diff "\\n\nA", "\\nA"
1699
+ assert_mu_pp_for_diff "\nA", "\nA"
1700
+ end
1701
+
1702
+ def test_mu_pp_str_bad_encoding
1703
+ str = "\666".force_encoding Encoding::UTF_8
1704
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1705
+
1706
+ assert_mu_pp exp, str, :raw
1707
+ end
1708
+
1709
+ def test_mu_pp_str_encoding
1710
+ str = "A\nB".b
1711
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\nB\""
1712
+
1713
+ assert_mu_pp exp, str, :raw
1714
+ end
1715
+
1716
+ def test_mu_pp_str_immutable
1717
+ printer = Class.new { extend Minitest::Assertions }
1718
+ str = "test".freeze
1719
+ assert_equal '"test"', printer.mu_pp(str)
1720
+ end
1721
+ end