minitest 5.11.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2135 @@
1
+ # encoding: UTF-8
2
+
3
+ require "pathname"
4
+ require "minitest/metametameta"
5
+
6
+ if defined? Encoding then
7
+ e = Encoding.default_external
8
+ if e != Encoding::UTF_8 then
9
+ warn ""
10
+ warn ""
11
+ warn "NOTE: External encoding #{e} is not UTF-8. Tests WILL fail."
12
+ warn " Run tests with `RUBYOPT=-Eutf-8 rake` to avoid errors."
13
+ warn ""
14
+ warn ""
15
+ end
16
+ end
17
+
18
+ module MyModule; end
19
+ class AnError < StandardError; include MyModule; end
20
+ class ImmutableString < String; def inspect; super.freeze; end; end
21
+ SomeError = Class.new Exception
22
+
23
+ class Minitest::Runnable
24
+ def whatever # faked for testing
25
+ assert true
26
+ end
27
+ end
28
+
29
+ class TestMinitestUnit < MetaMetaMetaTestCase
30
+ parallelize_me!
31
+
32
+ pwd = Pathname.new File.expand_path Dir.pwd
33
+ basedir = Pathname.new(File.expand_path "lib/minitest") + "mini"
34
+ basedir = basedir.relative_path_from(pwd).to_s
35
+ MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
36
+ BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
37
+ "#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
38
+ "#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
39
+ "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
40
+
41
+ def test_filter_backtrace
42
+ # this is a semi-lame mix of relative paths.
43
+ # I cheated by making the autotest parts not have ./
44
+ bt = (["lib/autotest.rb:571:in `add_exception'",
45
+ "test/test_autotest.rb:62:in `test_add_exception'",
46
+ "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
47
+ BT_MIDDLE +
48
+ ["#{MINITEST_BASE_DIR}/test.rb:29",
49
+ "test/test_autotest.rb:422"])
50
+ bt = util_expand_bt bt
51
+
52
+ ex = ["lib/autotest.rb:571:in `add_exception'",
53
+ "test/test_autotest.rb:62:in `test_add_exception'"]
54
+ ex = util_expand_bt ex
55
+
56
+ fu = Minitest.filter_backtrace(bt)
57
+
58
+ assert_equal ex, fu
59
+ end
60
+
61
+ def test_filter_backtrace_all_unit
62
+ bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
63
+ BT_MIDDLE +
64
+ ["#{MINITEST_BASE_DIR}/test.rb:29"])
65
+ ex = bt.clone
66
+ fu = Minitest.filter_backtrace(bt)
67
+ assert_equal ex, fu
68
+ end
69
+
70
+ def test_filter_backtrace_unit_starts
71
+ bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
72
+ BT_MIDDLE +
73
+ ["#{MINITEST_BASE_DIR}/mini/test.rb:29",
74
+ "-e:1"])
75
+
76
+ bt = util_expand_bt bt
77
+
78
+ ex = ["-e:1"]
79
+ fu = Minitest.filter_backtrace bt
80
+ assert_equal ex, fu
81
+ end
82
+
83
+ # def test_default_runner_is_minitest_unit
84
+ # assert_instance_of Minitest::Unit, Minitest::Unit.runner
85
+ # end
86
+
87
+ def test_infectious_binary_encoding
88
+ @tu = Class.new FakeNamedTest do
89
+ def test_this_is_not_ascii_assertion
90
+ assert_equal "ЁЁЁ", "ёёё"
91
+ end
92
+
93
+ def test_this_is_non_ascii_failure_message
94
+ fail 'ЁЁЁ'.force_encoding('ASCII-8BIT')
95
+ end
96
+ end
97
+
98
+ expected = clean <<-EOM
99
+ EF
100
+
101
+ Finished in 0.00
102
+
103
+ 1) Error:
104
+ FakeNamedTestXX#test_this_is_non_ascii_failure_message:
105
+ RuntimeError: ЁЁЁ
106
+ FILE:LINE:in `test_this_is_non_ascii_failure_message'
107
+
108
+ 2) Failure:
109
+ FakeNamedTestXX#test_this_is_not_ascii_assertion [FILE:LINE]:
110
+ Expected: \"ЁЁЁ\"
111
+ Actual: \"ёёё\"
112
+
113
+ 2 runs, 1 assertions, 1 failures, 1 errors, 0 skips
114
+ EOM
115
+
116
+ assert_report expected
117
+ end
118
+
119
+ def test_passed_eh_teardown_good
120
+ test_class = Class.new FakeNamedTest do
121
+ def teardown; assert true; end
122
+ def test_omg; assert true; end
123
+ end
124
+
125
+ test = test_class.new :test_omg
126
+ test.run
127
+ assert test.passed?
128
+ end
129
+
130
+ def test_passed_eh_teardown_skipped
131
+ test_class = Class.new FakeNamedTest do
132
+ def teardown; assert true; end
133
+ def test_omg; skip "bork"; end
134
+ end
135
+
136
+ test = test_class.new :test_omg
137
+ test.run
138
+
139
+ assert test.skipped?
140
+ refute test.passed?
141
+ end
142
+
143
+ def test_passed_eh_teardown_flunked
144
+ test_class = Class.new FakeNamedTest do
145
+ def teardown; flunk; end
146
+ def test_omg; assert true; end
147
+ end
148
+
149
+ test = test_class.new :test_omg
150
+ test.run
151
+ refute test.passed?
152
+ end
153
+
154
+ def util_expand_bt bt
155
+ if RUBY_VERSION >= "1.9.0" then
156
+ bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
157
+ else
158
+ bt
159
+ end
160
+ end
161
+ end
162
+
163
+ class TestMinitestUnitInherited < MetaMetaMetaTestCase
164
+ def with_overridden_include
165
+ Class.class_eval do
166
+ def inherited_with_hacks _klass
167
+ throw :inherited_hook
168
+ end
169
+
170
+ alias inherited_without_hacks inherited
171
+ alias inherited inherited_with_hacks
172
+ alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros
173
+ end
174
+
175
+ yield
176
+ ensure
177
+ Class.class_eval do
178
+ alias inherited inherited_without_hacks
179
+
180
+ undef_method :inherited_with_hacks
181
+ undef_method :inherited_without_hacks
182
+ end
183
+
184
+ refute_respond_to Class, :inherited_with_hacks
185
+ refute_respond_to Class, :inherited_without_hacks
186
+ end
187
+
188
+ def test_inherited_hook_plays_nice_with_others
189
+ with_overridden_include do
190
+ assert_throws :inherited_hook do
191
+ Class.new FakeNamedTest
192
+ end
193
+ end
194
+ end
195
+ end
196
+
197
+ class TestMinitestRunner < MetaMetaMetaTestCase
198
+ # do not parallelize this suite... it just can't handle it.
199
+
200
+ def test_class_runnables
201
+ @assertion_count = 0
202
+
203
+ tc = Class.new(Minitest::Test)
204
+
205
+ assert_equal 1, Minitest::Test.runnables.size
206
+ assert_equal [tc], Minitest::Test.runnables
207
+ end
208
+
209
+ def test_run_test
210
+ @tu =
211
+ Class.new FakeNamedTest do
212
+ attr_reader :foo
213
+
214
+ def run
215
+ @foo = "hi mom!"
216
+ r = super
217
+ @foo = "okay"
218
+
219
+ r
220
+ end
221
+
222
+ def test_something
223
+ assert_equal "hi mom!", foo
224
+ end
225
+ end
226
+
227
+ expected = clean <<-EOM
228
+ .
229
+
230
+ Finished in 0.00
231
+
232
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
233
+ EOM
234
+
235
+ assert_report expected
236
+ end
237
+
238
+ def test_run_error
239
+ @tu =
240
+ Class.new FakeNamedTest do
241
+ def test_something
242
+ assert true
243
+ end
244
+
245
+ def test_error
246
+ raise "unhandled exception"
247
+ end
248
+ end
249
+
250
+ expected = clean <<-EOM
251
+ E.
252
+
253
+ Finished in 0.00
254
+
255
+ 1) Error:
256
+ FakeNamedTestXX#test_error:
257
+ RuntimeError: unhandled exception
258
+ FILE:LINE:in \`test_error\'
259
+
260
+ 2 runs, 1 assertions, 0 failures, 1 errors, 0 skips
261
+ EOM
262
+
263
+ assert_report expected
264
+ end
265
+
266
+ def test_run_error_teardown
267
+ @tu =
268
+ Class.new FakeNamedTest do
269
+ def test_something
270
+ assert true
271
+ end
272
+
273
+ def teardown
274
+ raise "unhandled exception"
275
+ end
276
+ end
277
+
278
+ expected = clean <<-EOM
279
+ E
280
+
281
+ Finished in 0.00
282
+
283
+ 1) Error:
284
+ FakeNamedTestXX#test_something:
285
+ RuntimeError: unhandled exception
286
+ FILE:LINE:in \`teardown\'
287
+
288
+ 1 runs, 1 assertions, 0 failures, 1 errors, 0 skips
289
+ EOM
290
+
291
+ assert_report expected
292
+ end
293
+
294
+ def test_run_failing
295
+ setup_basic_tu
296
+
297
+ expected = clean <<-EOM
298
+ F.
299
+
300
+ Finished in 0.00
301
+
302
+ 1) Failure:
303
+ FakeNamedTestXX#test_failure [FILE:LINE]:
304
+ Expected false to be truthy.
305
+
306
+ 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
307
+ EOM
308
+
309
+ assert_report expected
310
+ end
311
+
312
+ def setup_basic_tu
313
+ @tu =
314
+ Class.new FakeNamedTest do
315
+ def test_something
316
+ assert true
317
+ end
318
+
319
+ def test_failure
320
+ assert false
321
+ end
322
+ end
323
+ end
324
+
325
+ def test_run_failing_filtered
326
+ setup_basic_tu
327
+
328
+ expected = clean <<-EOM
329
+ .
330
+
331
+ Finished in 0.00
332
+
333
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
334
+ EOM
335
+
336
+ assert_report expected, %w[--name /some|thing/ --seed 42]
337
+ end
338
+
339
+ def assert_filtering filter, name, expected, a = false
340
+ args = %W[--#{filter} #{name} --seed 42]
341
+
342
+ alpha = Class.new FakeNamedTest do
343
+ define_method :test_something do
344
+ assert a
345
+ end
346
+ end
347
+ Object.const_set(:Alpha, alpha)
348
+
349
+ beta = Class.new FakeNamedTest do
350
+ define_method :test_something do
351
+ assert true
352
+ end
353
+ end
354
+ Object.const_set(:Beta, beta)
355
+
356
+ @tus = [alpha, beta]
357
+
358
+ assert_report expected, args
359
+ ensure
360
+ Object.send :remove_const, :Alpha
361
+ Object.send :remove_const, :Beta
362
+ end
363
+
364
+ def test_run_filtered_including_suite_name
365
+ expected = clean <<-EOM
366
+ .
367
+
368
+ Finished in 0.00
369
+
370
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
371
+ EOM
372
+
373
+ assert_filtering "name", "/Beta#test_something/", expected
374
+ end
375
+
376
+ def test_run_filtered_including_suite_name_string
377
+ expected = clean <<-EOM
378
+ .
379
+
380
+ Finished in 0.00
381
+
382
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
383
+ EOM
384
+
385
+ assert_filtering "name", "Beta#test_something", expected
386
+ end
387
+
388
+ def test_run_filtered_string_method_only
389
+ expected = clean <<-EOM
390
+ ..
391
+
392
+ Finished in 0.00
393
+
394
+ 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
395
+ EOM
396
+
397
+ assert_filtering "name", "test_something", expected, :pass
398
+ end
399
+
400
+ def test_run_failing_excluded
401
+ setup_basic_tu
402
+
403
+ expected = clean <<-EOM
404
+ .
405
+
406
+ Finished in 0.00
407
+
408
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
409
+ EOM
410
+
411
+ assert_report expected, %w[--exclude /failure/ --seed 42]
412
+ end
413
+
414
+ def test_run_filtered_excluding_suite_name
415
+ expected = clean <<-EOM
416
+ .
417
+
418
+ Finished in 0.00
419
+
420
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
421
+ EOM
422
+
423
+ assert_filtering "exclude", "/Alpha#test_something/", expected
424
+ end
425
+
426
+ def test_run_filtered_excluding_suite_name_string
427
+ expected = clean <<-EOM
428
+ .
429
+
430
+ Finished in 0.00
431
+
432
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
433
+ EOM
434
+
435
+ assert_filtering "exclude", "Alpha#test_something", expected
436
+ end
437
+
438
+ def test_run_filtered_excluding_string_method_only
439
+ expected = clean <<-EOM
440
+
441
+
442
+ Finished in 0.00
443
+
444
+ 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
445
+ EOM
446
+
447
+ assert_filtering "exclude", "test_something", expected, :pass
448
+ end
449
+
450
+ def test_run_passing
451
+ @tu =
452
+ Class.new FakeNamedTest do
453
+ def test_something
454
+ assert true
455
+ end
456
+ end
457
+
458
+ expected = clean <<-EOM
459
+ .
460
+
461
+ Finished in 0.00
462
+
463
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
464
+ EOM
465
+
466
+ assert_report expected
467
+ end
468
+
469
+ def test_run_skip
470
+ @tu =
471
+ Class.new FakeNamedTest do
472
+ def test_something
473
+ assert true
474
+ end
475
+
476
+ def test_skip
477
+ skip "not yet"
478
+ end
479
+ end
480
+
481
+ expected = clean <<-EOM
482
+ S.
483
+
484
+ Finished in 0.00
485
+
486
+ 2 runs, 1 assertions, 0 failures, 0 errors, 1 skips
487
+
488
+ You have skipped tests. Run with --verbose for details.
489
+ EOM
490
+
491
+ restore_env do
492
+ assert_report expected
493
+ end
494
+ end
495
+
496
+ def test_run_skip_verbose
497
+ @tu =
498
+ Class.new FakeNamedTest do
499
+ def test_something
500
+ assert true
501
+ end
502
+
503
+ def test_skip
504
+ skip "not yet"
505
+ end
506
+ end
507
+
508
+ expected = clean <<-EOM
509
+ FakeNamedTestXX#test_skip = 0.00 s = S
510
+ FakeNamedTestXX#test_something = 0.00 s = .
511
+
512
+ Finished in 0.00
513
+
514
+ 1) Skipped:
515
+ FakeNamedTestXX#test_skip [FILE:LINE]:
516
+ not yet
517
+
518
+ 2 runs, 1 assertions, 0 failures, 0 errors, 1 skips
519
+ EOM
520
+
521
+ assert_report expected, %w[--seed 42 --verbose]
522
+ end
523
+
524
+ def test_run_with_other_runner
525
+ @tu =
526
+ Class.new FakeNamedTest do
527
+ def self.run reporter, options = {}
528
+ @reporter = reporter
529
+ before_my_suite
530
+ super
531
+ end
532
+
533
+ def self.name; "wacky!" end
534
+
535
+ def self.before_my_suite
536
+ @reporter.io.puts "Running #{self.name} tests"
537
+ @@foo = 1
538
+ end
539
+
540
+ def test_something
541
+ assert_equal 1, @@foo
542
+ end
543
+
544
+ def test_something_else
545
+ assert_equal 1, @@foo
546
+ end
547
+ end
548
+
549
+ expected = clean <<-EOM
550
+ Running wacky! tests
551
+ ..
552
+
553
+ Finished in 0.00
554
+
555
+ 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
556
+ EOM
557
+
558
+ assert_report expected
559
+ end
560
+
561
+ require "monitor"
562
+
563
+ class Latch
564
+ def initialize count = 1
565
+ @count = count
566
+ @lock = Monitor.new
567
+ @cv = @lock.new_cond
568
+ end
569
+
570
+ def release
571
+ @lock.synchronize do
572
+ @count -= 1 if @count > 0
573
+ @cv.broadcast if @count == 0
574
+ end
575
+ end
576
+
577
+ def await
578
+ @lock.synchronize { @cv.wait_while { @count > 0 } }
579
+ end
580
+ end
581
+
582
+ def test_run_parallel
583
+ skip "I don't have ParallelEach debugged yet" if maglev?
584
+
585
+ test_count = 2
586
+ test_latch = Latch.new test_count
587
+ wait_latch = Latch.new test_count
588
+ main_latch = Latch.new
589
+
590
+ thread = Thread.new {
591
+ Thread.current.abort_on_exception = true
592
+
593
+ # This latch waits until both test latches have been released. Both
594
+ # latches can't be released unless done in separate threads because
595
+ # `main_latch` keeps the test method from finishing.
596
+ test_latch.await
597
+ main_latch.release
598
+ }
599
+
600
+ @tu =
601
+ Class.new FakeNamedTest do
602
+ parallelize_me!
603
+
604
+ test_count.times do |i|
605
+ define_method :"test_wait_on_main_thread_#{i}" do
606
+ test_latch.release
607
+
608
+ # This latch blocks until the "main thread" releases it. The main
609
+ # thread can't release this latch until both test latches have
610
+ # been released. This forces the latches to be released in separate
611
+ # threads.
612
+ main_latch.await
613
+ assert true
614
+ end
615
+ end
616
+ end
617
+
618
+ expected = clean <<-EOM
619
+ ..
620
+
621
+ Finished in 0.00
622
+
623
+ 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
624
+ EOM
625
+
626
+ assert_report(expected) do |reporter|
627
+ reporter.extend(Module.new {
628
+ define_method("record") do |result|
629
+ super(result)
630
+ wait_latch.release
631
+ end
632
+
633
+ define_method("report") do
634
+ wait_latch.await
635
+ super()
636
+ end
637
+ })
638
+ end
639
+ assert thread.join
640
+ end
641
+ end
642
+
643
+ class TestMinitestUnitOrder < MetaMetaMetaTestCase
644
+ # do not parallelize this suite... it just can't handle it.
645
+
646
+ def test_before_setup
647
+ call_order = []
648
+ @tu =
649
+ Class.new FakeNamedTest do
650
+ define_method :setup do
651
+ super()
652
+ call_order << :setup
653
+ end
654
+
655
+ define_method :before_setup do
656
+ call_order << :before_setup
657
+ end
658
+
659
+ def test_omg; assert true; end
660
+ end
661
+
662
+ run_tu_with_fresh_reporter
663
+
664
+ expected = [:before_setup, :setup]
665
+ assert_equal expected, call_order
666
+ end
667
+
668
+ def test_after_teardown
669
+ call_order = []
670
+ @tu =
671
+ Class.new FakeNamedTest do
672
+ define_method :teardown do
673
+ super()
674
+ call_order << :teardown
675
+ end
676
+
677
+ define_method :after_teardown do
678
+ call_order << :after_teardown
679
+ end
680
+
681
+ def test_omg; assert true; end
682
+ end
683
+
684
+ run_tu_with_fresh_reporter
685
+
686
+ expected = [:teardown, :after_teardown]
687
+ assert_equal expected, call_order
688
+ end
689
+
690
+ def test_all_teardowns_are_guaranteed_to_run
691
+ call_order = []
692
+ @tu =
693
+ Class.new FakeNamedTest do
694
+ define_method :after_teardown do
695
+ super()
696
+ call_order << :after_teardown
697
+ raise
698
+ end
699
+
700
+ define_method :teardown do
701
+ super()
702
+ call_order << :teardown
703
+ raise
704
+ end
705
+
706
+ define_method :before_teardown do
707
+ super()
708
+ call_order << :before_teardown
709
+ raise
710
+ end
711
+
712
+ def test_omg; assert true; end
713
+ end
714
+
715
+ run_tu_with_fresh_reporter
716
+
717
+ expected = [:before_teardown, :teardown, :after_teardown]
718
+ assert_equal expected, call_order
719
+ end
720
+
721
+ def test_setup_and_teardown_survive_inheritance
722
+ call_order = []
723
+
724
+ @tu = Class.new FakeNamedTest do
725
+ define_method :setup do
726
+ call_order << :setup_method
727
+ end
728
+
729
+ define_method :teardown do
730
+ call_order << :teardown_method
731
+ end
732
+
733
+ define_method :test_something do
734
+ call_order << :test
735
+ end
736
+ end
737
+
738
+ run_tu_with_fresh_reporter
739
+
740
+ @tu = Class.new @tu
741
+ run_tu_with_fresh_reporter
742
+
743
+ # Once for the parent class, once for the child
744
+ expected = [:setup_method, :test, :teardown_method] * 2
745
+
746
+ assert_equal expected, call_order
747
+ end
748
+ end
749
+
750
+ class TestMinitestRunnable < Minitest::Test
751
+ def setup_marshal klass
752
+ tc = klass.new "whatever"
753
+ tc.assertions = 42
754
+ tc.failures << "a failure"
755
+
756
+ yield tc if block_given?
757
+
758
+ def tc.setup
759
+ @blah = "blah"
760
+ end
761
+ tc.setup
762
+
763
+ @tc = Minitest::Result.from tc
764
+ end
765
+
766
+ def assert_marshal expected_ivars
767
+ new_tc = Marshal.load Marshal.dump @tc
768
+
769
+ ivars = new_tc.instance_variables.map(&:to_s).sort
770
+ assert_equal expected_ivars, ivars
771
+ assert_equal "whatever", new_tc.name
772
+ assert_equal 42, new_tc.assertions
773
+ assert_equal ["a failure"], new_tc.failures
774
+
775
+ yield new_tc if block_given?
776
+ end
777
+
778
+ def test_marshal
779
+ setup_marshal Minitest::Runnable
780
+
781
+ assert_marshal %w[@NAME @assertions @failures @klass @source_location @time]
782
+ end
783
+
784
+ def test_spec_marshal
785
+ klass = describe("whatever") { it("passes") { assert true } }
786
+ rm = klass.runnable_methods.first
787
+
788
+ # Run the test
789
+ @tc = klass.new(rm).run
790
+
791
+ assert_kind_of Minitest::Result, @tc
792
+
793
+ # Pass it over the wire
794
+ over_the_wire = Marshal.load Marshal.dump @tc
795
+
796
+ assert_equal @tc.time, over_the_wire.time
797
+ assert_equal @tc.name, over_the_wire.name
798
+ assert_equal @tc.assertions, over_the_wire.assertions
799
+ assert_equal @tc.failures, over_the_wire.failures
800
+ assert_equal @tc.klass, over_the_wire.klass
801
+ end
802
+ end
803
+
804
+ class TestMinitestTest < TestMinitestRunnable
805
+ def test_dup
806
+ setup_marshal Minitest::Test do |tc|
807
+ tc.time = 3.14
808
+ end
809
+
810
+ assert_marshal %w[@NAME @assertions @failures @klass @source_location @time] do |new_tc|
811
+ assert_in_epsilon 3.14, new_tc.time
812
+ end
813
+ end
814
+ end
815
+
816
+ class TestMinitestUnitTestCase < Minitest::Test
817
+ # do not call parallelize_me! - teardown accesses @tc._assertions
818
+ # which is not threadsafe. Nearly every method in here is an
819
+ # assertion test so it isn't worth splitting it out further.
820
+
821
+ RUBY18 = !defined? Encoding
822
+
823
+ def setup
824
+ super
825
+
826
+ Minitest::Test.reset
827
+
828
+ @tc = Minitest::Test.new "fake tc"
829
+ @zomg = "zomg ponies!"
830
+ @assertion_count = 1
831
+ end
832
+
833
+ def teardown
834
+ assert_equal(@assertion_count, @tc.assertions,
835
+ "expected #{@assertion_count} assertions to be fired during the test, not #{@tc.assertions}") if @tc.passed?
836
+ end
837
+
838
+ def non_verbose
839
+ orig_verbose = $VERBOSE
840
+ $VERBOSE = false
841
+
842
+ yield
843
+ ensure
844
+ $VERBOSE = orig_verbose
845
+ end
846
+
847
+ def test_assert
848
+ @assertion_count = 2
849
+
850
+ @tc.assert_equal true, @tc.assert(true), "returns true on success"
851
+ end
852
+
853
+ def test_assert__triggered
854
+ assert_triggered "Expected false to be truthy." do
855
+ @tc.assert false
856
+ end
857
+ end
858
+
859
+ def test_assert__triggered_message
860
+ assert_triggered @zomg do
861
+ @tc.assert false, @zomg
862
+ end
863
+ end
864
+
865
+ def test_assert_empty
866
+ @assertion_count = 2
867
+
868
+ @tc.assert_empty []
869
+ end
870
+
871
+ def test_assert_empty_triggered
872
+ @assertion_count = 2
873
+
874
+ assert_triggered "Expected [1] to be empty." do
875
+ @tc.assert_empty [1]
876
+ end
877
+ end
878
+
879
+ def test_assert_equal
880
+ @tc.assert_equal 1, 1
881
+ end
882
+
883
+ def test_assert_equal_different_collection_array_hex_invisible
884
+ object1 = Object.new
885
+ object2 = Object.new
886
+ msg = "No visible difference in the Array#inspect output.
887
+ You should look at the implementation of #== on Array or its members.
888
+ [#<Object:0xXXXXXX>]".gsub(/^ +/, "")
889
+ assert_triggered msg do
890
+ @tc.assert_equal [object1], [object2]
891
+ end
892
+ end
893
+
894
+ def test_assert_equal_different_collection_hash_hex_invisible
895
+ h1, h2 = {}, {}
896
+ h1[1] = Object.new
897
+ h2[1] = Object.new
898
+ msg = "No visible difference in the Hash#inspect output.
899
+ You should look at the implementation of #== on Hash or its members.
900
+ {1=>#<Object:0xXXXXXX>}".gsub(/^ +/, "")
901
+
902
+ assert_triggered msg do
903
+ @tc.assert_equal h1, h2
904
+ end
905
+ end
906
+
907
+ def test_assert_equal_string_encodings
908
+ msg = <<-EOM.gsub(/^ {10}/, "")
909
+ --- expected
910
+ +++ actual
911
+ @@ -1 +1,2 @@
912
+ +# encoding: ASCII-8BIT
913
+ "bad-utf8-\\xF1.txt"
914
+ EOM
915
+
916
+ assert_triggered msg do
917
+ x = "bad-utf8-\xF1.txt"
918
+ y = x.dup.force_encoding "binary" # TODO: switch to .b when 1.9 dropped
919
+ @tc.assert_equal x, y
920
+ end
921
+ end unless RUBY18
922
+
923
+ def test_assert_equal_string_encodings_both_different
924
+ msg = <<-EOM.gsub(/^ {10}/, "")
925
+ --- expected
926
+ +++ actual
927
+ @@ -1,2 +1,2 @@
928
+ -# encoding: US-ASCII
929
+ +# encoding: ASCII-8BIT
930
+ "bad-utf8-\\xF1.txt"
931
+ EOM
932
+
933
+ assert_triggered msg do
934
+ x = "bad-utf8-\xF1.txt".force_encoding "ASCII"
935
+ y = x.dup.force_encoding "binary" # TODO: switch to .b when 1.9 dropped
936
+ @tc.assert_equal x, y
937
+ end
938
+ end unless RUBY18
939
+
940
+ def test_assert_equal_different_diff_deactivated
941
+ skip "https://github.com/MagLev/maglev/issues/209" if maglev?
942
+
943
+ without_diff do
944
+ assert_triggered util_msg("haha" * 10, "blah" * 10) do
945
+ o1 = "haha" * 10
946
+ o2 = "blah" * 10
947
+
948
+ @tc.assert_equal o1, o2
949
+ end
950
+ end
951
+ end
952
+
953
+ def test_assert_equal_different_hex
954
+ c = Class.new do
955
+ def initialize s; @name = s; end
956
+ end
957
+
958
+ o1 = c.new "a"
959
+ o2 = c.new "b"
960
+ msg = "--- expected
961
+ +++ actual
962
+ @@ -1 +1 @@
963
+ -#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">
964
+ +#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">
965
+ ".gsub(/^ +/, "")
966
+
967
+ assert_triggered msg do
968
+ @tc.assert_equal o1, o2
969
+ end
970
+ end
971
+
972
+ def test_assert_equal_different_hex_invisible
973
+ o1 = Object.new
974
+ o2 = Object.new
975
+
976
+ msg = "No visible difference in the Object#inspect output.
977
+ You should look at the implementation of #== on Object or its members.
978
+ #<Object:0xXXXXXX>".gsub(/^ +/, "")
979
+
980
+ assert_triggered msg do
981
+ @tc.assert_equal o1, o2
982
+ end
983
+ end
984
+
985
+ def test_assert_equal_different_long
986
+ msg = "--- expected
987
+ +++ actual
988
+ @@ -1 +1 @@
989
+ -\"hahahahahahahahahahahahahahahahahahahaha\"
990
+ +\"blahblahblahblahblahblahblahblahblahblah\"
991
+ ".gsub(/^ +/, "")
992
+
993
+ assert_triggered msg do
994
+ o1 = "haha" * 10
995
+ o2 = "blah" * 10
996
+
997
+ @tc.assert_equal o1, o2
998
+ end
999
+ end
1000
+
1001
+ def test_assert_equal_different_long_invisible
1002
+ msg = "No visible difference in the String#inspect output.
1003
+ You should look at the implementation of #== on String or its members.
1004
+ \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
1005
+
1006
+ assert_triggered msg do
1007
+ o1 = "blah" * 10
1008
+ o2 = "blah" * 10
1009
+ def o1.== _
1010
+ false
1011
+ end
1012
+ @tc.assert_equal o1, o2
1013
+ end
1014
+ end
1015
+
1016
+ def test_assert_equal_different_long_msg
1017
+ msg = "message.
1018
+ --- expected
1019
+ +++ actual
1020
+ @@ -1 +1 @@
1021
+ -\"hahahahahahahahahahahahahahahahahahahaha\"
1022
+ +\"blahblahblahblahblahblahblahblahblahblah\"
1023
+ ".gsub(/^ +/, "")
1024
+
1025
+ assert_triggered msg do
1026
+ o1 = "haha" * 10
1027
+ o2 = "blah" * 10
1028
+ @tc.assert_equal o1, o2, "message"
1029
+ end
1030
+ end
1031
+
1032
+ def test_assert_equal_different_short
1033
+ assert_triggered util_msg(1, 2) do
1034
+ @tc.assert_equal 1, 2
1035
+ end
1036
+ end
1037
+
1038
+ def test_assert_equal_different_short_msg
1039
+ assert_triggered util_msg(1, 2, "message") do
1040
+ @tc.assert_equal 1, 2, "message"
1041
+ end
1042
+ end
1043
+
1044
+ def test_assert_equal_different_short_multiline
1045
+ msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n"
1046
+ assert_triggered msg do
1047
+ @tc.assert_equal "a\nb", "a\nc"
1048
+ end
1049
+ end
1050
+
1051
+ def test_assert_equal_does_not_allow_lhs_nil
1052
+ if Minitest::VERSION =~ /^6/ then
1053
+ warn "Time to strip the MT5 test"
1054
+
1055
+ @assertion_count += 1
1056
+ assert_triggered(/Use assert_nil if expecting nil/) do
1057
+ @tc.assert_equal nil, nil
1058
+ end
1059
+ else
1060
+ err_re = /Use assert_nil if expecting nil from .*test_minitest_test.rb/
1061
+ err_re = "" if $-w.nil?
1062
+
1063
+ assert_output "", err_re do
1064
+ @tc.assert_equal nil, nil
1065
+ end
1066
+ end
1067
+ end
1068
+
1069
+ def test_assert_equal_does_not_allow_lhs_nil_triggered
1070
+ assert_triggered "Expected: nil\n Actual: false" do
1071
+ @tc.assert_equal nil, false
1072
+ end
1073
+ end
1074
+
1075
+ def test_assert_in_delta
1076
+ @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
1077
+ end
1078
+
1079
+ def test_delta_consistency
1080
+ @assertion_count = 2
1081
+
1082
+ @tc.assert_in_delta 0, 1, 1
1083
+
1084
+ assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
1085
+ @tc.refute_in_delta 0, 1, 1
1086
+ end
1087
+ end
1088
+
1089
+ def test_assert_in_delta_triggered
1090
+ x = maglev? ? "9.999999xxxe-07" : "1.0e-06"
1091
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do
1092
+ @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001
1093
+ end
1094
+ end
1095
+
1096
+ def test_assert_in_epsilon
1097
+ @assertion_count = 10
1098
+
1099
+ @tc.assert_in_epsilon 10_000, 9991
1100
+ @tc.assert_in_epsilon 9991, 10_000
1101
+ @tc.assert_in_epsilon 1.0, 1.001
1102
+ @tc.assert_in_epsilon 1.001, 1.0
1103
+
1104
+ @tc.assert_in_epsilon 10_000, 9999.1, 0.0001
1105
+ @tc.assert_in_epsilon 9999.1, 10_000, 0.0001
1106
+ @tc.assert_in_epsilon 1.0, 1.0001, 0.0001
1107
+ @tc.assert_in_epsilon 1.0001, 1.0, 0.0001
1108
+
1109
+ @tc.assert_in_epsilon(-1, -1)
1110
+ @tc.assert_in_epsilon(-10_000, -9991)
1111
+ end
1112
+
1113
+ def test_epsilon_consistency
1114
+ @assertion_count = 2
1115
+
1116
+ @tc.assert_in_epsilon 1.0, 1.001
1117
+
1118
+ msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
1119
+ assert_triggered msg do
1120
+ @tc.refute_in_epsilon 1.0, 1.001
1121
+ end
1122
+ end
1123
+
1124
+ def test_assert_in_epsilon_triggered
1125
+ assert_triggered "Expected |10000 - 9990| (10) to be <= 9.99." do
1126
+ @tc.assert_in_epsilon 10_000, 9990
1127
+ end
1128
+ end
1129
+
1130
+ def test_assert_in_epsilon_triggered_negative_case
1131
+ x = (RUBY18 and not maglev?) ? "0.1" : "0.100000xxx"
1132
+ y = maglev? ? "0.100000xxx" : "0.1"
1133
+ assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do
1134
+ @tc.assert_in_epsilon(-1.1, -1, 0.1)
1135
+ end
1136
+ end
1137
+
1138
+ def test_assert_includes
1139
+ @assertion_count = 2
1140
+
1141
+ @tc.assert_includes [true], true
1142
+ end
1143
+
1144
+ def test_assert_includes_triggered
1145
+ @assertion_count = 3
1146
+
1147
+ e = @tc.assert_raises Minitest::Assertion do
1148
+ @tc.assert_includes [true], false
1149
+ end
1150
+
1151
+ expected = "Expected [true] to include false."
1152
+ assert_equal expected, e.message
1153
+ end
1154
+
1155
+ def test_assert_instance_of
1156
+ @tc.assert_instance_of String, "blah"
1157
+ end
1158
+
1159
+ def test_assert_instance_of_triggered
1160
+ assert_triggered 'Expected "blah" to be an instance of Array, not String.' do
1161
+ @tc.assert_instance_of Array, "blah"
1162
+ end
1163
+ end
1164
+
1165
+ def test_assert_kind_of
1166
+ @tc.assert_kind_of String, "blah"
1167
+ end
1168
+
1169
+ def test_assert_kind_of_triggered
1170
+ assert_triggered 'Expected "blah" to be a kind of Array, not String.' do
1171
+ @tc.assert_kind_of Array, "blah"
1172
+ end
1173
+ end
1174
+
1175
+ def test_assert_match
1176
+ @assertion_count = 2
1177
+ @tc.assert_match(/\w+/, "blah blah blah")
1178
+ end
1179
+
1180
+ def test_assert_match_matcher_object
1181
+ @assertion_count = 2
1182
+
1183
+ pattern = Object.new
1184
+ def pattern.=~ _; true end
1185
+
1186
+ @tc.assert_match pattern, 5
1187
+ end
1188
+
1189
+ def test_assert_match_matchee_to_str
1190
+ @assertion_count = 2
1191
+
1192
+ obj = Object.new
1193
+ def obj.to_str; "blah" end
1194
+
1195
+ @tc.assert_match "blah", obj
1196
+ end
1197
+
1198
+ def test_assert_match_object_triggered
1199
+ @assertion_count = 2
1200
+
1201
+ pattern = Object.new
1202
+ def pattern.=~ _; false end
1203
+ def pattern.inspect; "[Object]" end
1204
+
1205
+ assert_triggered "Expected [Object] to match 5." do
1206
+ @tc.assert_match pattern, 5
1207
+ end
1208
+ end
1209
+
1210
+ def test_assert_match_triggered
1211
+ @assertion_count = 2
1212
+ assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
1213
+ @tc.assert_match(/\d+/, "blah blah blah")
1214
+ end
1215
+ end
1216
+
1217
+ def test_assert_nil
1218
+ @tc.assert_nil nil
1219
+ end
1220
+
1221
+ def test_assert_nil_triggered
1222
+ assert_triggered "Expected 42 to be nil." do
1223
+ @tc.assert_nil 42
1224
+ end
1225
+ end
1226
+
1227
+ def test_assert_operator
1228
+ @tc.assert_operator 2, :>, 1
1229
+ end
1230
+
1231
+ def test_assert_operator_bad_object
1232
+ bad = Object.new
1233
+ def bad.== _; true end
1234
+
1235
+ @tc.assert_operator bad, :equal?, bad
1236
+ end
1237
+
1238
+ def test_assert_operator_triggered
1239
+ assert_triggered "Expected 2 to be < 1." do
1240
+ @tc.assert_operator 2, :<, 1
1241
+ end
1242
+ end
1243
+
1244
+ def test_assert_output_both
1245
+ @assertion_count = 2
1246
+
1247
+ @tc.assert_output "yay", "blah" do
1248
+ print "yay"
1249
+ $stderr.print "blah"
1250
+ end
1251
+ end
1252
+
1253
+ def test_assert_output_both_regexps
1254
+ @assertion_count = 4
1255
+
1256
+ @tc.assert_output(/y.y/, /bl.h/) do
1257
+ print "yay"
1258
+ $stderr.print "blah"
1259
+ end
1260
+ end
1261
+
1262
+ def test_assert_output_err
1263
+ @tc.assert_output nil, "blah" do
1264
+ $stderr.print "blah"
1265
+ end
1266
+ end
1267
+
1268
+ def test_assert_output_neither
1269
+ @assertion_count = 0
1270
+
1271
+ @tc.assert_output do
1272
+ # do nothing
1273
+ end
1274
+ end
1275
+
1276
+ def test_assert_output_out
1277
+ @tc.assert_output "blah" do
1278
+ print "blah"
1279
+ end
1280
+ end
1281
+
1282
+ def test_assert_output_triggered_both
1283
+ assert_triggered util_msg("blah", "blah blah", "In stderr") do
1284
+ @tc.assert_output "yay", "blah" do
1285
+ print "boo"
1286
+ $stderr.print "blah blah"
1287
+ end
1288
+ end
1289
+ end
1290
+
1291
+ def test_assert_output_triggered_err
1292
+ assert_triggered util_msg("blah", "blah blah", "In stderr") do
1293
+ @tc.assert_output nil, "blah" do
1294
+ $stderr.print "blah blah"
1295
+ end
1296
+ end
1297
+ end
1298
+
1299
+ def test_assert_output_triggered_out
1300
+ assert_triggered util_msg("blah", "blah blah", "In stdout") do
1301
+ @tc.assert_output "blah" do
1302
+ print "blah blah"
1303
+ end
1304
+ end
1305
+ end
1306
+
1307
+ def test_assert_predicate
1308
+ @tc.assert_predicate "", :empty?
1309
+ end
1310
+
1311
+ def test_assert_predicate_triggered
1312
+ assert_triggered 'Expected "blah" to be empty?.' do
1313
+ @tc.assert_predicate "blah", :empty?
1314
+ end
1315
+ end
1316
+
1317
+ def test_assert_raises
1318
+ @tc.assert_raises RuntimeError do
1319
+ raise "blah"
1320
+ end
1321
+ end
1322
+
1323
+ def test_assert_raises_default
1324
+ @tc.assert_raises do
1325
+ raise StandardError, "blah"
1326
+ end
1327
+ end
1328
+
1329
+ def test_assert_raises_default_triggered
1330
+ e = assert_raises Minitest::Assertion do
1331
+ @tc.assert_raises do
1332
+ raise SomeError, "blah"
1333
+ end
1334
+ end
1335
+
1336
+ expected = clean <<-EOM.chomp
1337
+ [StandardError] exception expected, not
1338
+ Class: <SomeError>
1339
+ Message: <\"blah\">
1340
+ ---Backtrace---
1341
+ FILE:LINE:in \`test_assert_raises_default_triggered\'
1342
+ ---------------
1343
+ EOM
1344
+
1345
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
1346
+ actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
1347
+
1348
+ assert_equal expected, actual
1349
+ end
1350
+
1351
+ def test_assert_raises_module
1352
+ @tc.assert_raises MyModule do
1353
+ raise AnError
1354
+ end
1355
+ end
1356
+
1357
+ ##
1358
+ # *sigh* This is quite an odd scenario, but it is from real (albeit
1359
+ # ugly) test code in ruby-core:
1360
+ #
1361
+ # http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
1362
+
1363
+ def test_assert_raises_skip
1364
+ @assertion_count = 0
1365
+
1366
+ assert_triggered "skipped", Minitest::Skip do
1367
+ @tc.assert_raises ArgumentError do
1368
+ begin
1369
+ raise "blah"
1370
+ rescue
1371
+ skip "skipped"
1372
+ end
1373
+ end
1374
+ end
1375
+ end
1376
+
1377
+ def test_assert_raises_triggered_different
1378
+ e = assert_raises Minitest::Assertion do
1379
+ @tc.assert_raises RuntimeError do
1380
+ raise SyntaxError, "icky"
1381
+ end
1382
+ end
1383
+
1384
+ expected = clean <<-EOM.chomp
1385
+ [RuntimeError] exception expected, not
1386
+ Class: <SyntaxError>
1387
+ Message: <\"icky\">
1388
+ ---Backtrace---
1389
+ FILE:LINE:in \`test_assert_raises_triggered_different\'
1390
+ ---------------
1391
+ EOM
1392
+
1393
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
1394
+ actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
1395
+
1396
+ assert_equal expected, actual
1397
+ end
1398
+
1399
+ def test_assert_raises_triggered_different_msg
1400
+ e = assert_raises Minitest::Assertion do
1401
+ @tc.assert_raises RuntimeError, "XXX" do
1402
+ raise SyntaxError, "icky"
1403
+ end
1404
+ end
1405
+
1406
+ expected = clean <<-EOM
1407
+ XXX.
1408
+ [RuntimeError] exception expected, not
1409
+ Class: <SyntaxError>
1410
+ Message: <\"icky\">
1411
+ ---Backtrace---
1412
+ FILE:LINE:in \`test_assert_raises_triggered_different_msg\'
1413
+ ---------------
1414
+ EOM
1415
+
1416
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
1417
+ actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
1418
+
1419
+ assert_equal expected.chomp, actual
1420
+ end
1421
+
1422
+ def test_assert_raises_triggered_none
1423
+ e = assert_raises Minitest::Assertion do
1424
+ @tc.assert_raises Minitest::Assertion do
1425
+ # do nothing
1426
+ end
1427
+ end
1428
+
1429
+ expected = "Minitest::Assertion expected but nothing was raised."
1430
+
1431
+ assert_equal expected, e.message
1432
+ end
1433
+
1434
+ def test_assert_raises_triggered_none_msg
1435
+ e = assert_raises Minitest::Assertion do
1436
+ @tc.assert_raises Minitest::Assertion, "XXX" do
1437
+ # do nothing
1438
+ end
1439
+ end
1440
+
1441
+ expected = "XXX.\nMinitest::Assertion expected but nothing was raised."
1442
+
1443
+ assert_equal expected, e.message
1444
+ end
1445
+
1446
+ def test_assert_raises_subclass
1447
+ @tc.assert_raises StandardError do
1448
+ raise AnError
1449
+ end
1450
+ end
1451
+
1452
+ def test_assert_raises_subclass_triggered
1453
+ e = assert_raises Minitest::Assertion do
1454
+ @tc.assert_raises SomeError do
1455
+ raise AnError, "some message"
1456
+ end
1457
+ end
1458
+
1459
+ expected = clean <<-EOM
1460
+ [SomeError] exception expected, not
1461
+ Class: <AnError>
1462
+ Message: <\"some message\">
1463
+ ---Backtrace---
1464
+ FILE:LINE:in \`test_assert_raises_subclass_triggered\'
1465
+ ---------------
1466
+ EOM
1467
+
1468
+ actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
1469
+ actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
1470
+
1471
+ assert_equal expected.chomp, actual
1472
+ end
1473
+
1474
+ def test_assert_raises_exit
1475
+ @tc.assert_raises SystemExit do
1476
+ exit 1
1477
+ end
1478
+ end
1479
+
1480
+ def test_assert_raises_signals
1481
+ @tc.assert_raises SignalException do
1482
+ raise SignalException, :INT
1483
+ end
1484
+ end
1485
+
1486
+ def test_assert_respond_to
1487
+ @tc.assert_respond_to "blah", :empty?
1488
+ end
1489
+
1490
+ def test_assert_respond_to_triggered
1491
+ assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
1492
+ @tc.assert_respond_to "blah", :rawr!
1493
+ end
1494
+ end
1495
+
1496
+ def test_assert_same
1497
+ @assertion_count = 3
1498
+
1499
+ o = "blah"
1500
+ @tc.assert_same 1, 1
1501
+ @tc.assert_same :blah, :blah
1502
+ @tc.assert_same o, o
1503
+ end
1504
+
1505
+ def test_assert_same_triggered
1506
+ @assertion_count = 2
1507
+
1508
+ assert_triggered "Expected 2 (oid=N) to be the same as 1 (oid=N)." do
1509
+ @tc.assert_same 1, 2
1510
+ end
1511
+
1512
+ s1 = "blah"
1513
+ s2 = "blah"
1514
+
1515
+ assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
1516
+ @tc.assert_same s1, s2
1517
+ end
1518
+ end
1519
+
1520
+ def assert_deprecated name
1521
+ dep = /DEPRECATED: #{name}. From #{__FILE__}:\d+(?::.*)?/
1522
+ dep = "" if $-w.nil?
1523
+
1524
+ assert_output nil, dep do
1525
+ yield
1526
+ end
1527
+ end
1528
+
1529
+ def test_assert_send
1530
+ assert_deprecated :assert_send do
1531
+ @tc.assert_send [1, :<, 2]
1532
+ end
1533
+ end
1534
+
1535
+ def test_assert_send_bad
1536
+ assert_deprecated :assert_send do
1537
+ assert_triggered "Expected 1.>(*[2]) to return true." do
1538
+ @tc.assert_send [1, :>, 2]
1539
+ end
1540
+ end
1541
+ end
1542
+
1543
+ def test_assert_silent
1544
+ @assertion_count = 2
1545
+
1546
+ @tc.assert_silent do
1547
+ # do nothing
1548
+ end
1549
+ end
1550
+
1551
+ def test_assert_silent_triggered_err
1552
+ assert_triggered util_msg("", "blah blah", "In stderr") do
1553
+ @tc.assert_silent do
1554
+ $stderr.print "blah blah"
1555
+ end
1556
+ end
1557
+ end
1558
+
1559
+ def test_assert_silent_triggered_out
1560
+ @assertion_count = 2
1561
+
1562
+ assert_triggered util_msg("", "blah blah", "In stdout") do
1563
+ @tc.assert_silent do
1564
+ print "blah blah"
1565
+ end
1566
+ end
1567
+ end
1568
+
1569
+ def test_assert_throws
1570
+ @tc.assert_throws :blah do
1571
+ throw :blah
1572
+ end
1573
+ end
1574
+
1575
+ def test_assert_throws_name_error
1576
+ @tc.assert_raises NameError do
1577
+ @tc.assert_throws :blah do
1578
+ raise NameError
1579
+ end
1580
+ end
1581
+ end
1582
+
1583
+ def test_assert_throws_argument_exception
1584
+ @tc.assert_raises ArgumentError do
1585
+ @tc.assert_throws :blah do
1586
+ raise ArgumentError
1587
+ end
1588
+ end
1589
+ end
1590
+
1591
+ def test_assert_throws_different
1592
+ assert_triggered "Expected :blah to have been thrown, not :not_blah." do
1593
+ @tc.assert_throws :blah do
1594
+ throw :not_blah
1595
+ end
1596
+ end
1597
+ end
1598
+
1599
+ def test_assert_throws_unthrown
1600
+ assert_triggered "Expected :blah to have been thrown." do
1601
+ @tc.assert_throws :blah do
1602
+ # do nothing
1603
+ end
1604
+ end
1605
+ end
1606
+
1607
+ def test_capture_io
1608
+ @assertion_count = 0
1609
+
1610
+ non_verbose do
1611
+ out, err = capture_io do
1612
+ puts "hi"
1613
+ $stderr.puts "bye!"
1614
+ end
1615
+
1616
+ assert_equal "hi\n", out
1617
+ assert_equal "bye!\n", err
1618
+ end
1619
+ end
1620
+
1621
+ def test_capture_subprocess_io
1622
+ @assertion_count = 0
1623
+
1624
+ non_verbose do
1625
+ out, err = capture_subprocess_io do
1626
+ system("echo hi")
1627
+ system("echo bye! 1>&2")
1628
+ end
1629
+
1630
+ assert_equal "hi\n", out
1631
+ assert_equal "bye!", err.strip
1632
+ end
1633
+ end
1634
+
1635
+ def test_class_asserts_match_refutes
1636
+ @assertion_count = 0
1637
+
1638
+ methods = Minitest::Assertions.public_instance_methods
1639
+ methods.map!(&:to_s) if Symbol === methods.first
1640
+
1641
+ # These don't have corresponding refutes _on purpose_. They're
1642
+ # useless and will never be added, so don't bother.
1643
+ ignores = %w[assert_output assert_raises assert_send
1644
+ assert_silent assert_throws assert_mock]
1645
+
1646
+ # These are test/unit methods. I'm not actually sure why they're still here
1647
+ ignores += %w[assert_no_match assert_not_equal assert_not_nil
1648
+ assert_not_same assert_nothing_raised
1649
+ assert_nothing_thrown assert_raise]
1650
+
1651
+ asserts = methods.grep(/^assert/).sort - ignores
1652
+ refutes = methods.grep(/^refute/).sort - ignores
1653
+
1654
+ assert_empty refutes.map { |n| n.sub(/^refute/, "assert") } - asserts
1655
+ assert_empty asserts.map { |n| n.sub(/^assert/, "refute") } - refutes
1656
+ end
1657
+
1658
+ def test_flunk
1659
+ assert_triggered "Epic Fail!" do
1660
+ @tc.flunk
1661
+ end
1662
+ end
1663
+
1664
+ def test_flunk_message
1665
+ assert_triggered @zomg do
1666
+ @tc.flunk @zomg
1667
+ end
1668
+ end
1669
+
1670
+ def test_message
1671
+ @assertion_count = 0
1672
+
1673
+ assert_equal "blah2.", @tc.message { "blah2" }.call
1674
+ assert_equal "blah2.", @tc.message("") { "blah2" }.call
1675
+ assert_equal "blah1.\nblah2.", @tc.message(:blah1) { "blah2" }.call
1676
+ assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call
1677
+
1678
+ message = proc { "blah1" }
1679
+ assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call
1680
+
1681
+ message = @tc.message { "blah1" }
1682
+ assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call
1683
+ end
1684
+
1685
+ def test_message_message
1686
+ assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
1687
+ @tc.assert_equal 1, 2, message { "whoops" }
1688
+ end
1689
+ end
1690
+
1691
+ def test_message_lambda
1692
+ assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
1693
+ @tc.assert_equal 1, 2, lambda { "whoops" }
1694
+ end
1695
+ end
1696
+
1697
+ def test_message_deferred
1698
+ @assertion_count, var = 0, nil
1699
+
1700
+ msg = message { var = "blah" }
1701
+
1702
+ assert_nil var
1703
+
1704
+ msg.call
1705
+
1706
+ assert_equal "blah", var
1707
+ end
1708
+
1709
+ def test_pass
1710
+ @tc.pass
1711
+ end
1712
+
1713
+ def test_prints
1714
+ printer = Class.new { extend Minitest::Assertions }
1715
+ @tc.assert_equal '"test"', printer.mu_pp(ImmutableString.new "test")
1716
+ end
1717
+
1718
+ def test_refute
1719
+ @assertion_count = 2
1720
+
1721
+ @tc.assert_equal false, @tc.refute(false), "returns false on success"
1722
+ end
1723
+
1724
+ def test_refute_empty
1725
+ @assertion_count = 2
1726
+
1727
+ @tc.refute_empty [1]
1728
+ end
1729
+
1730
+ def test_refute_empty_triggered
1731
+ @assertion_count = 2
1732
+
1733
+ assert_triggered "Expected [] to not be empty." do
1734
+ @tc.refute_empty []
1735
+ end
1736
+ end
1737
+
1738
+ def test_refute_equal
1739
+ @tc.refute_equal "blah", "yay"
1740
+ end
1741
+
1742
+ def test_refute_equal_triggered
1743
+ assert_triggered 'Expected "blah" to not be equal to "blah".' do
1744
+ @tc.refute_equal "blah", "blah"
1745
+ end
1746
+ end
1747
+
1748
+ def test_refute_in_delta
1749
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
1750
+ end
1751
+
1752
+ def test_refute_in_delta_triggered
1753
+ x = maglev? ? "0.100000xxx" : "0.1"
1754
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
1755
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
1756
+ end
1757
+ end
1758
+
1759
+ def test_refute_in_epsilon
1760
+ @tc.refute_in_epsilon 10_000, 9990-1
1761
+ end
1762
+
1763
+ def test_refute_in_epsilon_triggered
1764
+ assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
1765
+ @tc.refute_in_epsilon 10_000, 9990
1766
+ flunk
1767
+ end
1768
+ end
1769
+
1770
+ def test_refute_includes
1771
+ @assertion_count = 2
1772
+
1773
+ @tc.refute_includes [true], false
1774
+ end
1775
+
1776
+ def test_refute_includes_triggered
1777
+ @assertion_count = 3
1778
+
1779
+ e = @tc.assert_raises Minitest::Assertion do
1780
+ @tc.refute_includes [true], true
1781
+ end
1782
+
1783
+ expected = "Expected [true] to not include true."
1784
+ assert_equal expected, e.message
1785
+ end
1786
+
1787
+ def test_refute_instance_of
1788
+ @tc.refute_instance_of Array, "blah"
1789
+ end
1790
+
1791
+ def test_refute_instance_of_triggered
1792
+ assert_triggered 'Expected "blah" to not be an instance of String.' do
1793
+ @tc.refute_instance_of String, "blah"
1794
+ end
1795
+ end
1796
+
1797
+ def test_refute_kind_of
1798
+ @tc.refute_kind_of Array, "blah"
1799
+ end
1800
+
1801
+ def test_refute_kind_of_triggered
1802
+ assert_triggered 'Expected "blah" to not be a kind of String.' do
1803
+ @tc.refute_kind_of String, "blah"
1804
+ end
1805
+ end
1806
+
1807
+ def test_refute_match
1808
+ @assertion_count = 2
1809
+ @tc.refute_match(/\d+/, "blah blah blah")
1810
+ end
1811
+
1812
+ def test_refute_match_matcher_object
1813
+ @assertion_count = 2
1814
+ @tc.refute_match Object.new, 5 # default #=~ returns false
1815
+ end
1816
+
1817
+ def test_refute_match_object_triggered
1818
+ @assertion_count = 2
1819
+
1820
+ pattern = Object.new
1821
+ def pattern.=~ _; true end
1822
+ def pattern.inspect; "[Object]" end
1823
+
1824
+ assert_triggered "Expected [Object] to not match 5." do
1825
+ @tc.refute_match pattern, 5
1826
+ end
1827
+ end
1828
+
1829
+ def test_refute_match_triggered
1830
+ @assertion_count = 2
1831
+ assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
1832
+ @tc.refute_match(/\w+/, "blah blah blah")
1833
+ end
1834
+ end
1835
+
1836
+ def test_refute_nil
1837
+ @tc.refute_nil 42
1838
+ end
1839
+
1840
+ def test_refute_nil_triggered
1841
+ assert_triggered "Expected nil to not be nil." do
1842
+ @tc.refute_nil nil
1843
+ end
1844
+ end
1845
+
1846
+ def test_refute_predicate
1847
+ @tc.refute_predicate "42", :empty?
1848
+ end
1849
+
1850
+ def test_refute_predicate_triggered
1851
+ assert_triggered 'Expected "" to not be empty?.' do
1852
+ @tc.refute_predicate "", :empty?
1853
+ end
1854
+ end
1855
+
1856
+ def test_refute_operator
1857
+ @tc.refute_operator 2, :<, 1
1858
+ end
1859
+
1860
+ def test_refute_operator_bad_object
1861
+ bad = Object.new
1862
+ def bad.== _; true end
1863
+
1864
+ @tc.refute_operator true, :equal?, bad
1865
+ end
1866
+
1867
+ def test_refute_operator_triggered
1868
+ assert_triggered "Expected 2 to not be > 1." do
1869
+ @tc.refute_operator 2, :>, 1
1870
+ end
1871
+ end
1872
+
1873
+ def test_refute_respond_to
1874
+ @tc.refute_respond_to "blah", :rawr!
1875
+ end
1876
+
1877
+ def test_refute_respond_to_triggered
1878
+ assert_triggered 'Expected "blah" to not respond to empty?.' do
1879
+ @tc.refute_respond_to "blah", :empty?
1880
+ end
1881
+ end
1882
+
1883
+ def test_refute_same
1884
+ @tc.refute_same 1, 2
1885
+ end
1886
+
1887
+ def test_refute_same_triggered
1888
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
1889
+ @tc.refute_same 1, 1
1890
+ end
1891
+ end
1892
+
1893
+ def test_skip
1894
+ @assertion_count = 0
1895
+
1896
+ assert_triggered "haha!", Minitest::Skip do
1897
+ @tc.skip "haha!"
1898
+ end
1899
+ end
1900
+
1901
+ def test_runnable_methods_random
1902
+ @assertion_count = 0
1903
+
1904
+ sample_test_case = Class.new FakeNamedTest do
1905
+ def self.test_order; :random; end
1906
+ def test_test1; assert "does not matter" end
1907
+ def test_test2; assert "does not matter" end
1908
+ def test_test3; assert "does not matter" end
1909
+ end
1910
+
1911
+ srand 42
1912
+ expected = case
1913
+ when maglev? then
1914
+ %w[test_test2 test_test3 test_test1]
1915
+ else
1916
+ %w[test_test2 test_test1 test_test3]
1917
+ end
1918
+ assert_equal expected, sample_test_case.runnable_methods
1919
+ end
1920
+
1921
+ def test_runnable_methods_sorted
1922
+ @assertion_count = 0
1923
+
1924
+ sample_test_case = Class.new FakeNamedTest do
1925
+ def self.test_order; :sorted end
1926
+ def test_test3; assert "does not matter" end
1927
+ def test_test2; assert "does not matter" end
1928
+ def test_test1; assert "does not matter" end
1929
+ end
1930
+
1931
+ expected = %w[test_test1 test_test2 test_test3]
1932
+ assert_equal expected, sample_test_case.runnable_methods
1933
+ end
1934
+
1935
+ def test_i_suck_and_my_tests_are_order_dependent_bang_sets_test_order_alpha
1936
+ @assertion_count = 0
1937
+
1938
+ shitty_test_case = Class.new FakeNamedTest
1939
+
1940
+ shitty_test_case.i_suck_and_my_tests_are_order_dependent!
1941
+
1942
+ assert_equal :alpha, shitty_test_case.test_order
1943
+ end
1944
+
1945
+ def test_i_suck_and_my_tests_are_order_dependent_bang_does_not_warn
1946
+ @assertion_count = 0
1947
+
1948
+ shitty_test_case = Class.new FakeNamedTest
1949
+
1950
+ def shitty_test_case.test_order; :lol end
1951
+
1952
+ assert_silent do
1953
+ shitty_test_case.i_suck_and_my_tests_are_order_dependent!
1954
+ end
1955
+ end
1956
+
1957
+ def assert_triggered expected, klass = Minitest::Assertion
1958
+ e = assert_raises klass do
1959
+ yield
1960
+ end
1961
+
1962
+ msg = e.message.sub(/(---Backtrace---).*/m, '\1')
1963
+ msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
1964
+ msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
1965
+
1966
+ assert_msg = Regexp === expected ? :assert_match : :assert_equal
1967
+ self.send assert_msg, expected, msg
1968
+ end
1969
+
1970
+ def util_msg exp, act, msg = nil
1971
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
1972
+ s = "#{msg}.\n#{s}" if msg
1973
+ s
1974
+ end
1975
+
1976
+ def without_diff
1977
+ old_diff = Minitest::Assertions.diff
1978
+ Minitest::Assertions.diff = nil
1979
+
1980
+ yield
1981
+ ensure
1982
+ Minitest::Assertions.diff = old_diff
1983
+ end
1984
+ end
1985
+
1986
+ class TestMinitestGuard < Minitest::Test
1987
+ parallelize_me!
1988
+
1989
+ def test_mri_eh
1990
+ assert self.class.mri? "ruby blah"
1991
+ assert self.mri? "ruby blah"
1992
+ end
1993
+
1994
+ def test_jruby_eh
1995
+ assert self.class.jruby? "java"
1996
+ assert self.jruby? "java"
1997
+ end
1998
+
1999
+ def test_rubinius_eh
2000
+ assert self.class.rubinius? "rbx"
2001
+ assert self.rubinius? "rbx"
2002
+ end
2003
+
2004
+ def test_windows_eh
2005
+ assert self.class.windows? "mswin"
2006
+ assert self.windows? "mswin"
2007
+ end
2008
+ end
2009
+
2010
+ class TestMinitestUnitRecording < MetaMetaMetaTestCase
2011
+ # do not parallelize this suite... it just can't handle it.
2012
+
2013
+ def assert_run_record *expected, &block
2014
+ @tu = Class.new FakeNamedTest, &block
2015
+
2016
+ run_tu_with_fresh_reporter
2017
+
2018
+ recorded = first_reporter.results.map(&:failures).flatten.map { |f| f.error.class }
2019
+
2020
+ assert_equal expected, recorded
2021
+ end
2022
+
2023
+ def test_run_with_bogus_reporter
2024
+ # https://github.com/seattlerb/minitest/issues/659
2025
+ # TODO: remove test for minitest 6
2026
+ @tu = Class.new FakeNamedTest do
2027
+ def test_method
2028
+ assert true
2029
+ end
2030
+ end
2031
+
2032
+ bogus_reporter = Class.new do # doesn't subclass AbstractReporter
2033
+ def start; @success = false; end
2034
+ # def prerecord klass, name; end # doesn't define full API
2035
+ def record result; @success = true; end
2036
+ def report; end
2037
+ def passed?; end
2038
+ def results; end
2039
+ def success?; @success; end
2040
+ end.new
2041
+
2042
+ self.reporter = Minitest::CompositeReporter.new
2043
+ reporter << bogus_reporter
2044
+
2045
+ Minitest::Runnable.runnables.delete @tu
2046
+
2047
+ @tu.run reporter, {}
2048
+
2049
+ assert_predicate bogus_reporter, :success?
2050
+ end
2051
+
2052
+ def test_record_passing
2053
+ assert_run_record do
2054
+ def test_method
2055
+ assert true
2056
+ end
2057
+ end
2058
+ end
2059
+
2060
+ def test_record_failing
2061
+ assert_run_record Minitest::Assertion do
2062
+ def test_method
2063
+ assert false
2064
+ end
2065
+ end
2066
+ end
2067
+
2068
+ def test_record_error
2069
+ assert_run_record RuntimeError do
2070
+ def test_method
2071
+ raise "unhandled exception"
2072
+ end
2073
+ end
2074
+ end
2075
+
2076
+ def test_record_error_teardown
2077
+ assert_run_record RuntimeError do
2078
+ def test_method
2079
+ assert true
2080
+ end
2081
+
2082
+ def teardown
2083
+ raise "unhandled exception"
2084
+ end
2085
+ end
2086
+ end
2087
+
2088
+ def test_record_error_in_test_and_teardown
2089
+ assert_run_record AnError, RuntimeError do
2090
+ def test_method
2091
+ raise AnError
2092
+ end
2093
+
2094
+ def teardown
2095
+ raise "unhandled exception"
2096
+ end
2097
+ end
2098
+ end
2099
+
2100
+ def test_to_s_error_in_test_and_teardown
2101
+ @tu = Class.new FakeNamedTest do
2102
+ def test_method
2103
+ raise AnError
2104
+ end
2105
+
2106
+ def teardown
2107
+ raise "unhandled exception"
2108
+ end
2109
+ end
2110
+
2111
+ run_tu_with_fresh_reporter
2112
+
2113
+ exp = clean "
2114
+ Error:
2115
+ FakeNamedTestXX#test_method:
2116
+ AnError: AnError
2117
+ FILE:LINE:in `test_method'
2118
+
2119
+ Error:
2120
+ FakeNamedTestXX#test_method:
2121
+ RuntimeError: unhandled exception
2122
+ FILE:LINE:in `teardown'
2123
+ "
2124
+
2125
+ assert_equal exp.strip, normalize_output(first_reporter.results.first.to_s).strip
2126
+ end
2127
+
2128
+ def test_record_skip
2129
+ assert_run_record Minitest::Skip do
2130
+ def test_method
2131
+ skip "not yet"
2132
+ end
2133
+ end
2134
+ end
2135
+ end