minitest 5.11.3 → 5.17.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,1591 @@
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_capture_io
1066
+ @assertion_count = 0
1067
+
1068
+ non_verbose do
1069
+ out, err = capture_io do
1070
+ puts "hi"
1071
+ $stderr.puts "bye!"
1072
+ end
1073
+
1074
+ assert_equal "hi\n", out
1075
+ assert_equal "bye!\n", err
1076
+ end
1077
+ end
1078
+
1079
+ def test_capture_subprocess_io
1080
+ @assertion_count = 0
1081
+
1082
+ non_verbose do
1083
+ out, err = capture_subprocess_io do
1084
+ system("echo hi")
1085
+ system("echo bye! 1>&2")
1086
+ end
1087
+
1088
+ assert_equal "hi\n", out
1089
+ assert_equal "bye!", err.strip
1090
+ end
1091
+ end
1092
+
1093
+ def test_class_asserts_match_refutes
1094
+ @assertion_count = 0
1095
+
1096
+ methods = Minitest::Assertions.public_instance_methods
1097
+ methods.map!(&:to_s) if Symbol === methods.first
1098
+
1099
+ # These don't have corresponding refutes _on purpose_. They're
1100
+ # useless and will never be added, so don't bother.
1101
+ ignores = %w[assert_output assert_raises assert_send
1102
+ assert_silent assert_throws assert_mock]
1103
+
1104
+ # These are test/unit methods. I'm not actually sure why they're still here
1105
+ ignores += %w[assert_no_match assert_not_equal assert_not_nil
1106
+ assert_not_same assert_nothing_raised
1107
+ assert_nothing_thrown assert_raise]
1108
+
1109
+ asserts = methods.grep(/^assert/).sort - ignores
1110
+ refutes = methods.grep(/^refute/).sort - ignores
1111
+
1112
+ assert_empty refutes.map { |n| n.sub(/^refute/, "assert") } - asserts
1113
+ assert_empty asserts.map { |n| n.sub(/^assert/, "refute") } - refutes
1114
+ end
1115
+
1116
+ def test_delta_consistency
1117
+ @assertion_count = 2
1118
+
1119
+ @tc.assert_in_delta 0, 1, 1
1120
+
1121
+ assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
1122
+ @tc.refute_in_delta 0, 1, 1
1123
+ end
1124
+ end
1125
+
1126
+ def test_epsilon_consistency
1127
+ @assertion_count = 2
1128
+
1129
+ @tc.assert_in_epsilon 1.0, 1.001
1130
+
1131
+ msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
1132
+ assert_triggered msg do
1133
+ @tc.refute_in_epsilon 1.0, 1.001
1134
+ end
1135
+ end
1136
+
1137
+ def assert_fail_after t
1138
+ @tc.fail_after t.year, t.month, t.day, "remove the deprecations"
1139
+ end
1140
+
1141
+ def test_fail_after
1142
+ d0 = Time.now
1143
+ d1 = d0 + 86_400 # I am an idiot
1144
+
1145
+ assert_silent do
1146
+ assert_fail_after d1
1147
+ end
1148
+
1149
+ assert_triggered "remove the deprecations" do
1150
+ assert_fail_after d0
1151
+ end
1152
+ end
1153
+
1154
+ def test_flunk
1155
+ assert_triggered "Epic Fail!" do
1156
+ @tc.flunk
1157
+ end
1158
+ end
1159
+
1160
+ def test_flunk_message
1161
+ assert_triggered @zomg do
1162
+ @tc.flunk @zomg
1163
+ end
1164
+ end
1165
+
1166
+ def test_pass
1167
+ @tc.pass
1168
+ end
1169
+
1170
+ def test_refute
1171
+ @assertion_count = 2
1172
+
1173
+ @tc.assert_equal true, @tc.refute(false), "returns true on success"
1174
+ end
1175
+
1176
+ def test_refute_empty
1177
+ @assertion_count = 2
1178
+
1179
+ @tc.refute_empty [1]
1180
+ end
1181
+
1182
+ def test_refute_empty_triggered
1183
+ @assertion_count = 2
1184
+
1185
+ assert_triggered "Expected [] to not be empty." do
1186
+ @tc.refute_empty []
1187
+ end
1188
+ end
1189
+
1190
+ def test_refute_equal
1191
+ @tc.refute_equal "blah", "yay"
1192
+ end
1193
+
1194
+ def test_refute_equal_triggered
1195
+ assert_triggered 'Expected "blah" to not be equal to "blah".' do
1196
+ @tc.refute_equal "blah", "blah"
1197
+ end
1198
+ end
1199
+
1200
+ def test_refute_in_delta
1201
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
1202
+ end
1203
+
1204
+ def test_refute_in_delta_triggered
1205
+ x = "0.1"
1206
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
1207
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
1208
+ end
1209
+ end
1210
+
1211
+ def test_refute_in_epsilon
1212
+ @tc.refute_in_epsilon 10_000, 9990-1
1213
+ end
1214
+
1215
+ def test_refute_in_epsilon_triggered
1216
+ assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
1217
+ @tc.refute_in_epsilon 10_000, 9990
1218
+ flunk
1219
+ end
1220
+ end
1221
+
1222
+ def test_refute_includes
1223
+ @assertion_count = 2
1224
+
1225
+ @tc.refute_includes [true], false
1226
+ end
1227
+
1228
+ def test_refute_includes_triggered
1229
+ @assertion_count = 3
1230
+
1231
+ e = @tc.assert_raises Minitest::Assertion do
1232
+ @tc.refute_includes [true], true
1233
+ end
1234
+
1235
+ expected = "Expected [true] to not include true."
1236
+ assert_equal expected, e.message
1237
+ end
1238
+
1239
+ def test_refute_instance_of
1240
+ @tc.refute_instance_of Array, "blah"
1241
+ end
1242
+
1243
+ def test_refute_instance_of_triggered
1244
+ assert_triggered 'Expected "blah" to not be an instance of String.' do
1245
+ @tc.refute_instance_of String, "blah"
1246
+ end
1247
+ end
1248
+
1249
+ def test_refute_kind_of
1250
+ @tc.refute_kind_of Array, "blah"
1251
+ end
1252
+
1253
+ def test_refute_kind_of_triggered
1254
+ assert_triggered 'Expected "blah" to not be a kind of String.' do
1255
+ @tc.refute_kind_of String, "blah"
1256
+ end
1257
+ end
1258
+
1259
+ def test_refute_match
1260
+ @assertion_count = 2
1261
+ @tc.refute_match(/\d+/, "blah blah blah")
1262
+ end
1263
+
1264
+ def test_refute_match_matcher_object
1265
+ @assertion_count = 2
1266
+ pattern = Object.new
1267
+ def pattern.=~ _; false end
1268
+ @tc.refute_match pattern, 5
1269
+ end
1270
+
1271
+ def test_refute_match_object_triggered
1272
+ @assertion_count = 2
1273
+
1274
+ pattern = Object.new
1275
+ def pattern.=~ _; true end
1276
+ def pattern.inspect; "[Object]" end
1277
+
1278
+ assert_triggered "Expected [Object] to not match 5." do
1279
+ @tc.refute_match pattern, 5
1280
+ end
1281
+ end
1282
+
1283
+ def test_refute_match_triggered
1284
+ @assertion_count = 2
1285
+ assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
1286
+ @tc.refute_match(/\w+/, "blah blah blah")
1287
+ end
1288
+ end
1289
+
1290
+ def test_refute_nil
1291
+ @tc.refute_nil 42
1292
+ end
1293
+
1294
+ def test_refute_nil_triggered
1295
+ assert_triggered "Expected nil to not be nil." do
1296
+ @tc.refute_nil nil
1297
+ end
1298
+ end
1299
+
1300
+ def test_refute_operator
1301
+ @tc.refute_operator 2, :<, 1
1302
+ end
1303
+
1304
+ def test_refute_operator_bad_object
1305
+ bad = Object.new
1306
+ def bad.== _; true end
1307
+
1308
+ @tc.refute_operator true, :equal?, bad
1309
+ end
1310
+
1311
+ def test_refute_operator_triggered
1312
+ assert_triggered "Expected 2 to not be > 1." do
1313
+ @tc.refute_operator 2, :>, 1
1314
+ end
1315
+ end
1316
+
1317
+ def test_refute_predicate
1318
+ @tc.refute_predicate "42", :empty?
1319
+ end
1320
+
1321
+ def test_refute_predicate_triggered
1322
+ assert_triggered 'Expected "" to not be empty?.' do
1323
+ @tc.refute_predicate "", :empty?
1324
+ end
1325
+ end
1326
+
1327
+ def test_refute_respond_to
1328
+ @tc.refute_respond_to "blah", :rawr!
1329
+ end
1330
+
1331
+ def test_refute_respond_to_triggered
1332
+ assert_triggered 'Expected "blah" to not respond to empty?.' do
1333
+ @tc.refute_respond_to "blah", :empty?
1334
+ end
1335
+ end
1336
+
1337
+ def test_refute_same
1338
+ @tc.refute_same 1, 2
1339
+ end
1340
+
1341
+ def test_refute_same_triggered
1342
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
1343
+ @tc.refute_same 1, 1
1344
+ end
1345
+ end
1346
+
1347
+ def test_refute_path_exists
1348
+ @tc.refute_path_exists "blah"
1349
+ end
1350
+
1351
+ def test_refute_path_exists_triggered
1352
+ assert_triggered "Expected path '#{__FILE__}' to not exist." do
1353
+ @tc.refute_path_exists __FILE__
1354
+ end
1355
+ end
1356
+
1357
+ def test_skip
1358
+ @assertion_count = 0
1359
+
1360
+ assert_triggered "haha!", Minitest::Skip do
1361
+ @tc.skip "haha!"
1362
+ end
1363
+ end
1364
+
1365
+ def assert_skip_until t, msg
1366
+ @tc.skip_until t.year, t.month, t.day, msg
1367
+ end
1368
+
1369
+ def test_skip_until
1370
+ @assertion_count = 0
1371
+
1372
+ d0 = Time.now
1373
+ d1 = d0 + 86_400 # I am an idiot
1374
+
1375
+ assert_output "", /Stale skip_until \"not yet\" at .*?:\d+$/ do
1376
+ assert_skip_until d0, "not yet"
1377
+ end
1378
+
1379
+ assert_triggered "not ready yet", Minitest::Skip do
1380
+ assert_skip_until d1, "not ready yet"
1381
+ end
1382
+ end
1383
+
1384
+ def util_msg exp, act, msg = nil
1385
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
1386
+ s = "#{msg}.\n#{s}" if msg
1387
+ s
1388
+ end
1389
+
1390
+ def without_diff
1391
+ old_diff = Minitest::Assertions.diff
1392
+ Minitest::Assertions.diff = nil
1393
+
1394
+ yield
1395
+ ensure
1396
+ Minitest::Assertions.diff = old_diff
1397
+ end
1398
+ end
1399
+
1400
+ class TestMinitestAssertionHelpers < Minitest::Test
1401
+ def assert_mu_pp exp, input, raw = false
1402
+ act = mu_pp input
1403
+
1404
+ if String === input && !raw then
1405
+ assert_equal "\"#{exp}\"", act
1406
+ else
1407
+ assert_equal exp, act
1408
+ end
1409
+ end
1410
+
1411
+ def assert_mu_pp_for_diff exp, input, raw = false
1412
+ act = mu_pp_for_diff input
1413
+
1414
+ if String === input && !raw then
1415
+ assert_equal "\"#{exp}\"", act
1416
+ else
1417
+ assert_equal exp, act
1418
+ end
1419
+ end
1420
+
1421
+ def test_diff_equal
1422
+ msg = "No visible difference in the String#inspect output.
1423
+ You should look at the implementation of #== on String or its members.
1424
+ \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
1425
+
1426
+ o1 = "blah" * 10
1427
+ o2 = "blah" * 10
1428
+ def o1.== _
1429
+ false
1430
+ end
1431
+
1432
+ assert_equal msg, diff(o1, o2)
1433
+ end
1434
+
1435
+ def test_diff_str_mixed
1436
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1437
+ --- expected
1438
+ +++ actual
1439
+ @@ -1 +1 @@
1440
+ -"A\\n\nB"
1441
+ +"A\n\\nB"
1442
+ EOM
1443
+
1444
+ exp = "A\\n\nB"
1445
+ act = "A\n\\nB"
1446
+
1447
+ assert_equal msg, diff(exp, act)
1448
+ end
1449
+
1450
+ def test_diff_str_multiline
1451
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1452
+ --- expected
1453
+ +++ actual
1454
+ @@ -1,2 +1,2 @@
1455
+ "A
1456
+ -B"
1457
+ +C"
1458
+ EOM
1459
+
1460
+ exp = "A\nB"
1461
+ act = "A\nC"
1462
+
1463
+ assert_equal msg, diff(exp, act)
1464
+ end
1465
+
1466
+ def test_diff_str_simple
1467
+ msg = <<-'EOM'.gsub(/^ {10}/, "").chomp # NOTE single quotes on heredoc
1468
+ Expected: "A"
1469
+ Actual: "B"
1470
+ EOM
1471
+
1472
+ exp = "A"
1473
+ act = "B"
1474
+
1475
+ assert_equal msg, diff(exp, act)
1476
+ end
1477
+
1478
+ def test_message
1479
+ assert_equal "blah2.", message { "blah2" }.call
1480
+ assert_equal "blah2.", message("") { "blah2" }.call
1481
+ assert_equal "blah1.\nblah2.", message(:blah1) { "blah2" }.call
1482
+ assert_equal "blah1.\nblah2.", message("blah1") { "blah2" }.call
1483
+
1484
+ message = proc { "blah1" }
1485
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1486
+
1487
+ message = message { "blah1" }
1488
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1489
+ end
1490
+
1491
+ def test_message_deferred
1492
+ var = nil
1493
+
1494
+ msg = message { var = "blah" }
1495
+
1496
+ assert_nil var
1497
+
1498
+ msg.call
1499
+
1500
+ assert_equal "blah", var
1501
+ end
1502
+
1503
+ def test_mu_pp
1504
+ assert_mu_pp 42.inspect, 42
1505
+ assert_mu_pp %w[a b c].inspect, %w[a b c]
1506
+ assert_mu_pp "A B", "A B"
1507
+ assert_mu_pp "A\\nB", "A\nB"
1508
+ assert_mu_pp "A\\\\nB", 'A\nB' # notice single quotes
1509
+ end
1510
+
1511
+ def test_mu_pp_for_diff
1512
+ assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
1513
+ assert_mu_pp_for_diff "A B", "A B"
1514
+ assert_mu_pp_for_diff [1, 2, 3].inspect, [1, 2, 3]
1515
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1516
+ end
1517
+
1518
+ def test_mu_pp_for_diff_str_bad_encoding
1519
+ str = "\666".force_encoding Encoding::UTF_8
1520
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1521
+
1522
+ assert_mu_pp_for_diff exp, str, :raw
1523
+ end
1524
+
1525
+ def test_mu_pp_for_diff_str_bad_encoding_both
1526
+ str = "\666A\\n\nB".force_encoding Encoding::UTF_8
1527
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""
1528
+
1529
+ assert_mu_pp_for_diff exp, str, :raw
1530
+ end
1531
+
1532
+ def test_mu_pp_for_diff_str_encoding
1533
+ str = "A\nB".b
1534
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\nB\""
1535
+
1536
+ assert_mu_pp_for_diff exp, str, :raw
1537
+ end
1538
+
1539
+ def test_mu_pp_for_diff_str_encoding_both
1540
+ str = "A\\n\nB".b
1541
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\\\n\\nB\""
1542
+
1543
+ assert_mu_pp_for_diff exp, str, :raw
1544
+ end
1545
+
1546
+ def test_mu_pp_for_diff_str_nerd
1547
+ assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
1548
+ assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
1549
+ assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
1550
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1551
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1552
+ assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
1553
+ assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
1554
+ assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
1555
+ assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
1556
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1557
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1558
+ end
1559
+
1560
+ def test_mu_pp_for_diff_str_normal
1561
+ assert_mu_pp_for_diff "", ""
1562
+ assert_mu_pp_for_diff "A\\n\n", "A\\n"
1563
+ assert_mu_pp_for_diff "A\\n\nB", "A\\nB"
1564
+ assert_mu_pp_for_diff "A\n", "A\n"
1565
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1566
+ assert_mu_pp_for_diff "\\n\n", "\\n"
1567
+ assert_mu_pp_for_diff "\n", "\n"
1568
+ assert_mu_pp_for_diff "\\n\nA", "\\nA"
1569
+ assert_mu_pp_for_diff "\nA", "\nA"
1570
+ end
1571
+
1572
+ def test_mu_pp_str_bad_encoding
1573
+ str = "\666".force_encoding Encoding::UTF_8
1574
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1575
+
1576
+ assert_mu_pp exp, str, :raw
1577
+ end
1578
+
1579
+ def test_mu_pp_str_encoding
1580
+ str = "A\nB".b
1581
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\nB\""
1582
+
1583
+ assert_mu_pp exp, str, :raw
1584
+ end
1585
+
1586
+ def test_mu_pp_str_immutable
1587
+ printer = Class.new { extend Minitest::Assertions }
1588
+ str = "test".freeze
1589
+ assert_equal '"test"', printer.mu_pp(str)
1590
+ end
1591
+ end