minitest 5.10.3 → 5.15.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,1588 @@
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: ASCII-8BIT
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: ASCII-8BIT
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
+ @tc.assert_match(/\w+/, "blah blah blah")
488
+ end
489
+
490
+ def test_assert_match_matchee_to_str
491
+ @assertion_count = 2
492
+
493
+ obj = Object.new
494
+ def obj.to_str; "blah" end
495
+
496
+ @tc.assert_match "blah", obj
497
+ end
498
+
499
+ def test_assert_match_matcher_object
500
+ @assertion_count = 2
501
+
502
+ pattern = Object.new
503
+ def pattern.=~ _; true end
504
+
505
+ @tc.assert_match pattern, 5
506
+ end
507
+
508
+ def test_assert_match_object_triggered
509
+ @assertion_count = 2
510
+
511
+ pattern = Object.new
512
+ def pattern.=~ _; false end
513
+ def pattern.inspect; "[Object]" end
514
+
515
+ assert_triggered "Expected [Object] to match 5." do
516
+ @tc.assert_match pattern, 5
517
+ end
518
+ end
519
+
520
+ def test_assert_match_triggered
521
+ @assertion_count = 2
522
+ assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
523
+ @tc.assert_match(/\d+/, "blah blah blah")
524
+ end
525
+ end
526
+
527
+ def test_assert_nil
528
+ @tc.assert_nil nil
529
+ end
530
+
531
+ def test_assert_nil_triggered
532
+ assert_triggered "Expected 42 to be nil." do
533
+ @tc.assert_nil 42
534
+ end
535
+ end
536
+
537
+ def test_assert_operator
538
+ @tc.assert_operator 2, :>, 1
539
+ end
540
+
541
+ def test_assert_operator_bad_object
542
+ bad = Object.new
543
+ def bad.== _; true end
544
+
545
+ @tc.assert_operator bad, :equal?, bad
546
+ end
547
+
548
+ def test_assert_operator_triggered
549
+ assert_triggered "Expected 2 to be < 1." do
550
+ @tc.assert_operator 2, :<, 1
551
+ end
552
+ end
553
+
554
+ def test_assert_output_both
555
+ @assertion_count = 2
556
+
557
+ @tc.assert_output "yay", "blah" do
558
+ print "yay"
559
+ $stderr.print "blah"
560
+ end
561
+ end
562
+
563
+ def test_assert_output_both_regexps
564
+ @assertion_count = 4
565
+
566
+ @tc.assert_output(/y.y/, /bl.h/) do
567
+ print "yay"
568
+ $stderr.print "blah"
569
+ end
570
+ end
571
+
572
+ def test_assert_output_err
573
+ @tc.assert_output nil, "blah" do
574
+ $stderr.print "blah"
575
+ end
576
+ end
577
+
578
+ def test_assert_output_neither
579
+ @assertion_count = 0
580
+
581
+ @tc.assert_output do
582
+ # do nothing
583
+ end
584
+ end
585
+
586
+ def test_assert_output_out
587
+ @tc.assert_output "blah" do
588
+ print "blah"
589
+ end
590
+ end
591
+
592
+ def test_assert_output_triggered_both
593
+ assert_triggered util_msg("blah", "blah blah", "In stderr") do
594
+ @tc.assert_output "yay", "blah" do
595
+ print "boo"
596
+ $stderr.print "blah blah"
597
+ end
598
+ end
599
+ end
600
+
601
+ def test_assert_output_triggered_err
602
+ assert_triggered util_msg("blah", "blah blah", "In stderr") do
603
+ @tc.assert_output nil, "blah" do
604
+ $stderr.print "blah blah"
605
+ end
606
+ end
607
+ end
608
+
609
+ def test_assert_output_triggered_out
610
+ assert_triggered util_msg("blah", "blah blah", "In stdout") do
611
+ @tc.assert_output "blah" do
612
+ print "blah blah"
613
+ end
614
+ end
615
+ end
616
+
617
+ def test_assert_output_no_block
618
+ assert_triggered "assert_output requires a block to capture output." do
619
+ @tc.assert_output "blah"
620
+ end
621
+ end
622
+
623
+ def test_assert_output_nested_assert_uncaught
624
+ @assertion_count = 1
625
+
626
+ assert_triggered "Epic Fail!" do
627
+ @tc.assert_output "blah\n" do
628
+ puts "blah"
629
+ @tc.flunk
630
+ end
631
+ end
632
+ end
633
+
634
+ def test_assert_output_nested_raise
635
+ @assertion_count = 2
636
+
637
+ @tc.assert_output "blah\n" do
638
+ @tc.assert_raises RuntimeError do
639
+ puts "blah"
640
+ raise "boom!"
641
+ end
642
+ end
643
+ end
644
+
645
+ def test_assert_output_nested_raise_bad
646
+ @assertion_count = 0
647
+
648
+ assert_unexpected "boom!" do
649
+ @tc.assert_raises do # 2) bypassed via UnexpectedError
650
+ @tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
651
+ puts "not_blah"
652
+ raise "boom!"
653
+ end
654
+ end
655
+ end
656
+ end
657
+
658
+ def test_assert_output_nested_raise_mismatch
659
+ # this test is redundant, but illustrative
660
+ @assertion_count = 0
661
+
662
+ assert_unexpected "boom!" do
663
+ @tc.assert_raises RuntimeError do # 2) bypassed via UnexpectedError
664
+ @tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
665
+ puts "not_blah"
666
+ raise ArgumentError, "boom!"
667
+ end
668
+ end
669
+ end
670
+ end
671
+
672
+ def test_assert_output_nested_throw_caught
673
+ @assertion_count = 2
674
+
675
+ @tc.assert_output "blah\n" do
676
+ @tc.assert_throws :boom! do
677
+ puts "blah"
678
+ throw :boom!
679
+ end
680
+ end
681
+ end
682
+
683
+ def test_assert_output_nested_throw_caught_bad
684
+ @assertion_count = 1 # want 0; can't prevent throw from escaping :(
685
+
686
+ @tc.assert_throws :boom! do # 2) captured via catch
687
+ @tc.assert_output "blah\n" do # 1) bypassed via throw
688
+ puts "not_blah"
689
+ throw :boom!
690
+ end
691
+ end
692
+ end
693
+
694
+ def test_assert_output_nested_throw_mismatch
695
+ @assertion_count = 0
696
+
697
+ assert_unexpected "uncaught throw :boom!" do
698
+ @tc.assert_throws :not_boom! do # 2) captured via assert_throws+rescue
699
+ @tc.assert_output "blah\n" do # 1) bypassed via throw
700
+ puts "not_blah"
701
+ throw :boom!
702
+ end
703
+ end
704
+ end
705
+ end
706
+
707
+ def test_assert_output_uncaught_raise
708
+ @assertion_count = 0
709
+
710
+ assert_unexpected "RuntimeError: boom!" do
711
+ @tc.assert_output "blah\n" do
712
+ puts "not_blah"
713
+ raise "boom!"
714
+ end
715
+ end
716
+ end
717
+
718
+ def test_assert_output_uncaught_throw
719
+ @assertion_count = 0
720
+
721
+ assert_unexpected "uncaught throw :boom!" do
722
+ @tc.assert_output "blah\n" do
723
+ puts "not_blah"
724
+ throw :boom!
725
+ end
726
+ end
727
+ end
728
+ def test_assert_predicate
729
+ @tc.assert_predicate "", :empty?
730
+ end
731
+
732
+ def test_assert_predicate_triggered
733
+ assert_triggered 'Expected "blah" to be empty?.' do
734
+ @tc.assert_predicate "blah", :empty?
735
+ end
736
+ end
737
+
738
+ def test_assert_raises
739
+ @tc.assert_raises RuntimeError do
740
+ raise "blah"
741
+ end
742
+ end
743
+
744
+ def test_assert_raises_default
745
+ @tc.assert_raises do
746
+ raise StandardError, "blah"
747
+ end
748
+ end
749
+
750
+ def test_assert_raises_default_triggered
751
+ e = assert_raises Minitest::Assertion do
752
+ @tc.assert_raises do
753
+ raise SomeError, "blah"
754
+ end
755
+ end
756
+
757
+ expected = clean <<-EOM.chomp
758
+ [StandardError] exception expected, not
759
+ Class: <SomeError>
760
+ Message: <\"blah\">
761
+ ---Backtrace---
762
+ FILE:LINE:in \`block in test_assert_raises_default_triggered\'
763
+ ---------------
764
+ EOM
765
+
766
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
767
+ actual.gsub!(RE_LEVELS, "") unless jruby?
768
+
769
+ assert_equal expected, actual
770
+ end
771
+
772
+ def test_assert_raises_exit
773
+ @tc.assert_raises SystemExit do
774
+ exit 1
775
+ end
776
+ end
777
+
778
+ def test_assert_raises_module
779
+ @tc.assert_raises MyModule do
780
+ raise AnError
781
+ end
782
+ end
783
+
784
+ def test_assert_raises_signals
785
+ @tc.assert_raises SignalException do
786
+ raise SignalException, :INT
787
+ end
788
+ end
789
+
790
+ def test_assert_raises_throw_nested_bad
791
+ @assertion_count = 0
792
+
793
+ assert_unexpected "RuntimeError: boom!" do
794
+ @tc.assert_raises do
795
+ @tc.assert_throws :blah do
796
+ raise "boom!"
797
+ throw :not_blah
798
+ end
799
+ end
800
+ end
801
+ end
802
+
803
+ ##
804
+ # *sigh* This is quite an odd scenario, but it is from real (albeit
805
+ # ugly) test code in ruby-core:
806
+
807
+ # http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
808
+
809
+ def test_assert_raises_skip
810
+ @assertion_count = 0
811
+
812
+ assert_triggered "skipped", Minitest::Skip do
813
+ @tc.assert_raises ArgumentError do
814
+ begin
815
+ raise "blah"
816
+ rescue
817
+ skip "skipped"
818
+ end
819
+ end
820
+ end
821
+ end
822
+
823
+ def test_assert_raises_subclass
824
+ @tc.assert_raises StandardError do
825
+ raise AnError
826
+ end
827
+ end
828
+
829
+ def test_assert_raises_subclass_triggered
830
+ e = assert_raises Minitest::Assertion do
831
+ @tc.assert_raises SomeError do
832
+ raise AnError, "some message"
833
+ end
834
+ end
835
+
836
+ expected = clean <<-EOM
837
+ [SomeError] exception expected, not
838
+ Class: <AnError>
839
+ Message: <\"some message\">
840
+ ---Backtrace---
841
+ FILE:LINE:in \`block in test_assert_raises_subclass_triggered\'
842
+ ---------------
843
+ EOM
844
+
845
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
846
+ actual.gsub!(RE_LEVELS, "") unless jruby?
847
+
848
+ assert_equal expected.chomp, actual
849
+ end
850
+
851
+ def test_assert_raises_triggered_different
852
+ e = assert_raises Minitest::Assertion do
853
+ @tc.assert_raises RuntimeError do
854
+ raise SyntaxError, "icky"
855
+ end
856
+ end
857
+
858
+ expected = clean <<-EOM.chomp
859
+ [RuntimeError] exception expected, not
860
+ Class: <SyntaxError>
861
+ Message: <\"icky\">
862
+ ---Backtrace---
863
+ FILE:LINE:in \`block in test_assert_raises_triggered_different\'
864
+ ---------------
865
+ EOM
866
+
867
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
868
+ actual.gsub!(RE_LEVELS, "") unless jruby?
869
+
870
+ assert_equal expected, actual
871
+ end
872
+
873
+ def test_assert_raises_triggered_different_msg
874
+ e = assert_raises Minitest::Assertion do
875
+ @tc.assert_raises RuntimeError, "XXX" do
876
+ raise SyntaxError, "icky"
877
+ end
878
+ end
879
+
880
+ expected = clean <<-EOM
881
+ XXX.
882
+ [RuntimeError] exception expected, not
883
+ Class: <SyntaxError>
884
+ Message: <\"icky\">
885
+ ---Backtrace---
886
+ FILE:LINE:in \`block in test_assert_raises_triggered_different_msg\'
887
+ ---------------
888
+ EOM
889
+
890
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
891
+ actual.gsub!(RE_LEVELS, "") unless jruby?
892
+
893
+ assert_equal expected.chomp, actual
894
+ end
895
+
896
+ def test_assert_raises_triggered_none
897
+ e = assert_raises Minitest::Assertion do
898
+ @tc.assert_raises Minitest::Assertion do
899
+ # do nothing
900
+ end
901
+ end
902
+
903
+ expected = "Minitest::Assertion expected but nothing was raised."
904
+
905
+ assert_equal expected, e.message
906
+ end
907
+
908
+ def test_assert_raises_triggered_none_msg
909
+ e = assert_raises Minitest::Assertion do
910
+ @tc.assert_raises Minitest::Assertion, "XXX" do
911
+ # do nothing
912
+ end
913
+ end
914
+
915
+ expected = "XXX.\nMinitest::Assertion expected but nothing was raised."
916
+
917
+ assert_equal expected, e.message
918
+ end
919
+
920
+ def test_assert_raises_without_block
921
+ assert_triggered "assert_raises requires a block to capture errors." do
922
+ @tc.assert_raises StandardError
923
+ end
924
+ end
925
+
926
+ def test_assert_respond_to
927
+ @tc.assert_respond_to "blah", :empty?
928
+ end
929
+
930
+ def test_assert_respond_to_triggered
931
+ assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
932
+ @tc.assert_respond_to "blah", :rawr!
933
+ end
934
+ end
935
+
936
+ def test_assert_same
937
+ @assertion_count = 3
938
+
939
+ o = "blah"
940
+ @tc.assert_same 1, 1
941
+ @tc.assert_same :blah, :blah
942
+ @tc.assert_same o, o
943
+ end
944
+
945
+ def test_assert_same_triggered
946
+ @assertion_count = 2
947
+
948
+ assert_triggered "Expected 2 (oid=N) to be the same as 1 (oid=N)." do
949
+ @tc.assert_same 1, 2
950
+ end
951
+
952
+ s1 = "blah"
953
+ s2 = "blah"
954
+
955
+ assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
956
+ @tc.assert_same s1, s2
957
+ end
958
+ end
959
+
960
+ def test_assert_send
961
+ assert_deprecated :assert_send do
962
+ @tc.assert_send [1, :<, 2]
963
+ end
964
+ end
965
+
966
+ def test_assert_send_bad
967
+ assert_deprecated :assert_send do
968
+ assert_triggered "Expected 1.>(*[2]) to return true." do
969
+ @tc.assert_send [1, :>, 2]
970
+ end
971
+ end
972
+ end
973
+
974
+ def test_assert_silent
975
+ @assertion_count = 2
976
+
977
+ @tc.assert_silent do
978
+ # do nothing
979
+ end
980
+ end
981
+
982
+ def test_assert_silent_triggered_err
983
+ assert_triggered util_msg("", "blah blah", "In stderr") do
984
+ @tc.assert_silent do
985
+ $stderr.print "blah blah"
986
+ end
987
+ end
988
+ end
989
+
990
+ def test_assert_silent_triggered_out
991
+ @assertion_count = 2
992
+
993
+ assert_triggered util_msg("", "blah blah", "In stdout") do
994
+ @tc.assert_silent do
995
+ print "blah blah"
996
+ end
997
+ end
998
+ end
999
+
1000
+ def test_assert_throws
1001
+ v = @tc.assert_throws :blah do
1002
+ throw :blah
1003
+ end
1004
+
1005
+ assert_nil v
1006
+ end
1007
+
1008
+ def test_assert_throws_value
1009
+ v = @tc.assert_throws :blah do
1010
+ throw :blah, 42
1011
+ end
1012
+
1013
+ assert_equal 42, v
1014
+ end
1015
+
1016
+ def test_assert_throws_argument_exception
1017
+ @assertion_count = 0
1018
+
1019
+ assert_unexpected "ArgumentError" do
1020
+ @tc.assert_throws :blah do
1021
+ raise ArgumentError
1022
+ end
1023
+ end
1024
+ end
1025
+
1026
+ def test_assert_throws_different
1027
+ assert_triggered "Expected :blah to have been thrown, not :not_blah." do
1028
+ @tc.assert_throws :blah do
1029
+ throw :not_blah
1030
+ end
1031
+ end
1032
+ end
1033
+
1034
+ def test_assert_throws_name_error
1035
+ @assertion_count = 0
1036
+
1037
+ assert_unexpected "NameError" do
1038
+ @tc.assert_throws :blah do
1039
+ raise NameError
1040
+ end
1041
+ end
1042
+ end
1043
+
1044
+ def test_assert_throws_unthrown
1045
+ assert_triggered "Expected :blah to have been thrown." do
1046
+ @tc.assert_throws :blah do
1047
+ # do nothing
1048
+ end
1049
+ end
1050
+ end
1051
+
1052
+ def test_assert_path_exists
1053
+ @tc.assert_path_exists __FILE__
1054
+ end
1055
+
1056
+ def test_assert_path_exists_triggered
1057
+ assert_triggered "Expected path 'blah' to exist." do
1058
+ @tc.assert_path_exists "blah"
1059
+ end
1060
+ end
1061
+
1062
+ def test_capture_io
1063
+ @assertion_count = 0
1064
+
1065
+ non_verbose do
1066
+ out, err = capture_io do
1067
+ puts "hi"
1068
+ $stderr.puts "bye!"
1069
+ end
1070
+
1071
+ assert_equal "hi\n", out
1072
+ assert_equal "bye!\n", err
1073
+ end
1074
+ end
1075
+
1076
+ def test_capture_subprocess_io
1077
+ @assertion_count = 0
1078
+
1079
+ non_verbose do
1080
+ out, err = capture_subprocess_io do
1081
+ system("echo hi")
1082
+ system("echo bye! 1>&2")
1083
+ end
1084
+
1085
+ assert_equal "hi\n", out
1086
+ assert_equal "bye!", err.strip
1087
+ end
1088
+ end
1089
+
1090
+ def test_class_asserts_match_refutes
1091
+ @assertion_count = 0
1092
+
1093
+ methods = Minitest::Assertions.public_instance_methods
1094
+ methods.map!(&:to_s) if Symbol === methods.first
1095
+
1096
+ # These don't have corresponding refutes _on purpose_. They're
1097
+ # useless and will never be added, so don't bother.
1098
+ ignores = %w[assert_output assert_raises assert_send
1099
+ assert_silent assert_throws assert_mock]
1100
+
1101
+ # These are test/unit methods. I'm not actually sure why they're still here
1102
+ ignores += %w[assert_no_match assert_not_equal assert_not_nil
1103
+ assert_not_same assert_nothing_raised
1104
+ assert_nothing_thrown assert_raise]
1105
+
1106
+ asserts = methods.grep(/^assert/).sort - ignores
1107
+ refutes = methods.grep(/^refute/).sort - ignores
1108
+
1109
+ assert_empty refutes.map { |n| n.sub(/^refute/, "assert") } - asserts
1110
+ assert_empty asserts.map { |n| n.sub(/^assert/, "refute") } - refutes
1111
+ end
1112
+
1113
+ def test_delta_consistency
1114
+ @assertion_count = 2
1115
+
1116
+ @tc.assert_in_delta 0, 1, 1
1117
+
1118
+ assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
1119
+ @tc.refute_in_delta 0, 1, 1
1120
+ end
1121
+ end
1122
+
1123
+ def test_epsilon_consistency
1124
+ @assertion_count = 2
1125
+
1126
+ @tc.assert_in_epsilon 1.0, 1.001
1127
+
1128
+ msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
1129
+ assert_triggered msg do
1130
+ @tc.refute_in_epsilon 1.0, 1.001
1131
+ end
1132
+ end
1133
+
1134
+ def assert_fail_after t
1135
+ @tc.fail_after t.year, t.month, t.day, "remove the deprecations"
1136
+ end
1137
+
1138
+ def test_fail_after
1139
+ d0 = Time.now
1140
+ d1 = d0 + 86_400 # I am an idiot
1141
+
1142
+ assert_silent do
1143
+ assert_fail_after d1
1144
+ end
1145
+
1146
+ assert_triggered "remove the deprecations" do
1147
+ assert_fail_after d0
1148
+ end
1149
+ end
1150
+
1151
+ def test_flunk
1152
+ assert_triggered "Epic Fail!" do
1153
+ @tc.flunk
1154
+ end
1155
+ end
1156
+
1157
+ def test_flunk_message
1158
+ assert_triggered @zomg do
1159
+ @tc.flunk @zomg
1160
+ end
1161
+ end
1162
+
1163
+ def test_pass
1164
+ @tc.pass
1165
+ end
1166
+
1167
+ def test_refute
1168
+ @assertion_count = 2
1169
+
1170
+ @tc.assert_equal true, @tc.refute(false), "returns true on success"
1171
+ end
1172
+
1173
+ def test_refute_empty
1174
+ @assertion_count = 2
1175
+
1176
+ @tc.refute_empty [1]
1177
+ end
1178
+
1179
+ def test_refute_empty_triggered
1180
+ @assertion_count = 2
1181
+
1182
+ assert_triggered "Expected [] to not be empty." do
1183
+ @tc.refute_empty []
1184
+ end
1185
+ end
1186
+
1187
+ def test_refute_equal
1188
+ @tc.refute_equal "blah", "yay"
1189
+ end
1190
+
1191
+ def test_refute_equal_triggered
1192
+ assert_triggered 'Expected "blah" to not be equal to "blah".' do
1193
+ @tc.refute_equal "blah", "blah"
1194
+ end
1195
+ end
1196
+
1197
+ def test_refute_in_delta
1198
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
1199
+ end
1200
+
1201
+ def test_refute_in_delta_triggered
1202
+ x = "0.1"
1203
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
1204
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
1205
+ end
1206
+ end
1207
+
1208
+ def test_refute_in_epsilon
1209
+ @tc.refute_in_epsilon 10_000, 9990-1
1210
+ end
1211
+
1212
+ def test_refute_in_epsilon_triggered
1213
+ assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
1214
+ @tc.refute_in_epsilon 10_000, 9990
1215
+ flunk
1216
+ end
1217
+ end
1218
+
1219
+ def test_refute_includes
1220
+ @assertion_count = 2
1221
+
1222
+ @tc.refute_includes [true], false
1223
+ end
1224
+
1225
+ def test_refute_includes_triggered
1226
+ @assertion_count = 3
1227
+
1228
+ e = @tc.assert_raises Minitest::Assertion do
1229
+ @tc.refute_includes [true], true
1230
+ end
1231
+
1232
+ expected = "Expected [true] to not include true."
1233
+ assert_equal expected, e.message
1234
+ end
1235
+
1236
+ def test_refute_instance_of
1237
+ @tc.refute_instance_of Array, "blah"
1238
+ end
1239
+
1240
+ def test_refute_instance_of_triggered
1241
+ assert_triggered 'Expected "blah" to not be an instance of String.' do
1242
+ @tc.refute_instance_of String, "blah"
1243
+ end
1244
+ end
1245
+
1246
+ def test_refute_kind_of
1247
+ @tc.refute_kind_of Array, "blah"
1248
+ end
1249
+
1250
+ def test_refute_kind_of_triggered
1251
+ assert_triggered 'Expected "blah" to not be a kind of String.' do
1252
+ @tc.refute_kind_of String, "blah"
1253
+ end
1254
+ end
1255
+
1256
+ def test_refute_match
1257
+ @assertion_count = 2
1258
+ @tc.refute_match(/\d+/, "blah blah blah")
1259
+ end
1260
+
1261
+ def test_refute_match_matcher_object
1262
+ @assertion_count = 2
1263
+ pattern = Object.new
1264
+ def pattern.=~ _; false end
1265
+ @tc.refute_match pattern, 5
1266
+ end
1267
+
1268
+ def test_refute_match_object_triggered
1269
+ @assertion_count = 2
1270
+
1271
+ pattern = Object.new
1272
+ def pattern.=~ _; true end
1273
+ def pattern.inspect; "[Object]" end
1274
+
1275
+ assert_triggered "Expected [Object] to not match 5." do
1276
+ @tc.refute_match pattern, 5
1277
+ end
1278
+ end
1279
+
1280
+ def test_refute_match_triggered
1281
+ @assertion_count = 2
1282
+ assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
1283
+ @tc.refute_match(/\w+/, "blah blah blah")
1284
+ end
1285
+ end
1286
+
1287
+ def test_refute_nil
1288
+ @tc.refute_nil 42
1289
+ end
1290
+
1291
+ def test_refute_nil_triggered
1292
+ assert_triggered "Expected nil to not be nil." do
1293
+ @tc.refute_nil nil
1294
+ end
1295
+ end
1296
+
1297
+ def test_refute_operator
1298
+ @tc.refute_operator 2, :<, 1
1299
+ end
1300
+
1301
+ def test_refute_operator_bad_object
1302
+ bad = Object.new
1303
+ def bad.== _; true end
1304
+
1305
+ @tc.refute_operator true, :equal?, bad
1306
+ end
1307
+
1308
+ def test_refute_operator_triggered
1309
+ assert_triggered "Expected 2 to not be > 1." do
1310
+ @tc.refute_operator 2, :>, 1
1311
+ end
1312
+ end
1313
+
1314
+ def test_refute_predicate
1315
+ @tc.refute_predicate "42", :empty?
1316
+ end
1317
+
1318
+ def test_refute_predicate_triggered
1319
+ assert_triggered 'Expected "" to not be empty?.' do
1320
+ @tc.refute_predicate "", :empty?
1321
+ end
1322
+ end
1323
+
1324
+ def test_refute_respond_to
1325
+ @tc.refute_respond_to "blah", :rawr!
1326
+ end
1327
+
1328
+ def test_refute_respond_to_triggered
1329
+ assert_triggered 'Expected "blah" to not respond to empty?.' do
1330
+ @tc.refute_respond_to "blah", :empty?
1331
+ end
1332
+ end
1333
+
1334
+ def test_refute_same
1335
+ @tc.refute_same 1, 2
1336
+ end
1337
+
1338
+ def test_refute_same_triggered
1339
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
1340
+ @tc.refute_same 1, 1
1341
+ end
1342
+ end
1343
+
1344
+ def test_refute_path_exists
1345
+ @tc.refute_path_exists "blah"
1346
+ end
1347
+
1348
+ def test_refute_path_exists_triggered
1349
+ assert_triggered "Expected path '#{__FILE__}' to not exist." do
1350
+ @tc.refute_path_exists __FILE__
1351
+ end
1352
+ end
1353
+
1354
+ def test_skip
1355
+ @assertion_count = 0
1356
+
1357
+ assert_triggered "haha!", Minitest::Skip do
1358
+ @tc.skip "haha!"
1359
+ end
1360
+ end
1361
+
1362
+ def assert_skip_until t, msg
1363
+ @tc.skip_until t.year, t.month, t.day, msg
1364
+ end
1365
+
1366
+ def test_skip_until
1367
+ @assertion_count = 0
1368
+
1369
+ d0 = Time.now
1370
+ d1 = d0 + 86_400 # I am an idiot
1371
+
1372
+ assert_output "", /Stale skip_until \"not yet\" at .*?:\d+$/ do
1373
+ assert_skip_until d0, "not yet"
1374
+ end
1375
+
1376
+ assert_triggered "not ready yet", Minitest::Skip do
1377
+ assert_skip_until d1, "not ready yet"
1378
+ end
1379
+ end
1380
+
1381
+ def util_msg exp, act, msg = nil
1382
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
1383
+ s = "#{msg}.\n#{s}" if msg
1384
+ s
1385
+ end
1386
+
1387
+ def without_diff
1388
+ old_diff = Minitest::Assertions.diff
1389
+ Minitest::Assertions.diff = nil
1390
+
1391
+ yield
1392
+ ensure
1393
+ Minitest::Assertions.diff = old_diff
1394
+ end
1395
+ end
1396
+
1397
+ class TestMinitestAssertionHelpers < Minitest::Test
1398
+ def assert_mu_pp exp, input, raw = false
1399
+ act = mu_pp input
1400
+
1401
+ if String === input && !raw then
1402
+ assert_equal "\"#{exp}\"", act
1403
+ else
1404
+ assert_equal exp, act
1405
+ end
1406
+ end
1407
+
1408
+ def assert_mu_pp_for_diff exp, input, raw = false
1409
+ act = mu_pp_for_diff input
1410
+
1411
+ if String === input && !raw then
1412
+ assert_equal "\"#{exp}\"", act
1413
+ else
1414
+ assert_equal exp, act
1415
+ end
1416
+ end
1417
+
1418
+ def test_diff_equal
1419
+ msg = "No visible difference in the String#inspect output.
1420
+ You should look at the implementation of #== on String or its members.
1421
+ \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
1422
+
1423
+ o1 = "blah" * 10
1424
+ o2 = "blah" * 10
1425
+ def o1.== _
1426
+ false
1427
+ end
1428
+
1429
+ assert_equal msg, diff(o1, o2)
1430
+ end
1431
+
1432
+ def test_diff_str_mixed
1433
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1434
+ --- expected
1435
+ +++ actual
1436
+ @@ -1 +1 @@
1437
+ -"A\\n\nB"
1438
+ +"A\n\\nB"
1439
+ EOM
1440
+
1441
+ exp = "A\\n\nB"
1442
+ act = "A\n\\nB"
1443
+
1444
+ assert_equal msg, diff(exp, act)
1445
+ end
1446
+
1447
+ def test_diff_str_multiline
1448
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1449
+ --- expected
1450
+ +++ actual
1451
+ @@ -1,2 +1,2 @@
1452
+ "A
1453
+ -B"
1454
+ +C"
1455
+ EOM
1456
+
1457
+ exp = "A\nB"
1458
+ act = "A\nC"
1459
+
1460
+ assert_equal msg, diff(exp, act)
1461
+ end
1462
+
1463
+ def test_diff_str_simple
1464
+ msg = <<-'EOM'.gsub(/^ {10}/, "").chomp # NOTE single quotes on heredoc
1465
+ Expected: "A"
1466
+ Actual: "B"
1467
+ EOM
1468
+
1469
+ exp = "A"
1470
+ act = "B"
1471
+
1472
+ assert_equal msg, diff(exp, act)
1473
+ end
1474
+
1475
+ def test_message
1476
+ assert_equal "blah2.", message { "blah2" }.call
1477
+ assert_equal "blah2.", message("") { "blah2" }.call
1478
+ assert_equal "blah1.\nblah2.", message(:blah1) { "blah2" }.call
1479
+ assert_equal "blah1.\nblah2.", message("blah1") { "blah2" }.call
1480
+
1481
+ message = proc { "blah1" }
1482
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1483
+
1484
+ message = message { "blah1" }
1485
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1486
+ end
1487
+
1488
+ def test_message_deferred
1489
+ var = nil
1490
+
1491
+ msg = message { var = "blah" }
1492
+
1493
+ assert_nil var
1494
+
1495
+ msg.call
1496
+
1497
+ assert_equal "blah", var
1498
+ end
1499
+
1500
+ def test_mu_pp
1501
+ assert_mu_pp 42.inspect, 42
1502
+ assert_mu_pp %w[a b c].inspect, %w[a b c]
1503
+ assert_mu_pp "A B", "A B"
1504
+ assert_mu_pp "A\\nB", "A\nB"
1505
+ assert_mu_pp "A\\\\nB", 'A\nB' # notice single quotes
1506
+ end
1507
+
1508
+ def test_mu_pp_for_diff
1509
+ assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
1510
+ assert_mu_pp_for_diff "A B", "A B"
1511
+ assert_mu_pp_for_diff [1, 2, 3].inspect, [1, 2, 3]
1512
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1513
+ end
1514
+
1515
+ def test_mu_pp_for_diff_str_bad_encoding
1516
+ str = "\666".force_encoding Encoding::UTF_8
1517
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1518
+
1519
+ assert_mu_pp_for_diff exp, str, :raw
1520
+ end
1521
+
1522
+ def test_mu_pp_for_diff_str_bad_encoding_both
1523
+ str = "\666A\\n\nB".force_encoding Encoding::UTF_8
1524
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""
1525
+
1526
+ assert_mu_pp_for_diff exp, str, :raw
1527
+ end
1528
+
1529
+ def test_mu_pp_for_diff_str_encoding
1530
+ str = "A\nB".b
1531
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\nB\""
1532
+
1533
+ assert_mu_pp_for_diff exp, str, :raw
1534
+ end
1535
+
1536
+ def test_mu_pp_for_diff_str_encoding_both
1537
+ str = "A\\n\nB".b
1538
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\\\n\\nB\""
1539
+
1540
+ assert_mu_pp_for_diff exp, str, :raw
1541
+ end
1542
+
1543
+ def test_mu_pp_for_diff_str_nerd
1544
+ assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
1545
+ assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
1546
+ assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
1547
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1548
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1549
+ assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
1550
+ assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
1551
+ assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
1552
+ assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
1553
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1554
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1555
+ end
1556
+
1557
+ def test_mu_pp_for_diff_str_normal
1558
+ assert_mu_pp_for_diff "", ""
1559
+ assert_mu_pp_for_diff "A\\n\n", "A\\n"
1560
+ assert_mu_pp_for_diff "A\\n\nB", "A\\nB"
1561
+ assert_mu_pp_for_diff "A\n", "A\n"
1562
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1563
+ assert_mu_pp_for_diff "\\n\n", "\\n"
1564
+ assert_mu_pp_for_diff "\n", "\n"
1565
+ assert_mu_pp_for_diff "\\n\nA", "\\nA"
1566
+ assert_mu_pp_for_diff "\nA", "\nA"
1567
+ end
1568
+
1569
+ def test_mu_pp_str_bad_encoding
1570
+ str = "\666".force_encoding Encoding::UTF_8
1571
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1572
+
1573
+ assert_mu_pp exp, str, :raw
1574
+ end
1575
+
1576
+ def test_mu_pp_str_encoding
1577
+ str = "A\nB".b
1578
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\nB\""
1579
+
1580
+ assert_mu_pp exp, str, :raw
1581
+ end
1582
+
1583
+ def test_mu_pp_str_immutable
1584
+ printer = Class.new { extend Minitest::Assertions }
1585
+ str = "test".freeze
1586
+ assert_equal '"test"', printer.mu_pp(str)
1587
+ end
1588
+ end