minitest 5.12.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1055 @@
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
+ class Minitest::Runnable
19
+ def whatever # faked for testing
20
+ assert true
21
+ end
22
+ end
23
+
24
+ class TestMinitestUnit < MetaMetaMetaTestCase
25
+ parallelize_me!
26
+
27
+ pwd = Pathname.new File.expand_path Dir.pwd
28
+ basedir = Pathname.new(File.expand_path "lib/minitest") + "mini"
29
+ basedir = basedir.relative_path_from(pwd).to_s
30
+ MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
31
+ BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
32
+ "#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
33
+ "#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
34
+ "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
35
+
36
+ def test_filter_backtrace
37
+ # this is a semi-lame mix of relative paths.
38
+ # I cheated by making the autotest parts not have ./
39
+ bt = (["lib/autotest.rb:571:in `add_exception'",
40
+ "test/test_autotest.rb:62:in `test_add_exception'",
41
+ "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
42
+ BT_MIDDLE +
43
+ ["#{MINITEST_BASE_DIR}/test.rb:29",
44
+ "test/test_autotest.rb:422"])
45
+ bt = util_expand_bt bt
46
+
47
+ ex = ["lib/autotest.rb:571:in `add_exception'",
48
+ "test/test_autotest.rb:62:in `test_add_exception'"]
49
+ ex = util_expand_bt ex
50
+
51
+ fu = Minitest.filter_backtrace(bt)
52
+
53
+ assert_equal ex, fu
54
+ end
55
+
56
+ def test_filter_backtrace_all_unit
57
+ bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
58
+ BT_MIDDLE +
59
+ ["#{MINITEST_BASE_DIR}/test.rb:29"])
60
+ ex = bt.clone
61
+ fu = Minitest.filter_backtrace(bt)
62
+ assert_equal ex, fu
63
+ end
64
+
65
+ def test_filter_backtrace_unit_starts
66
+ bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
67
+ BT_MIDDLE +
68
+ ["#{MINITEST_BASE_DIR}/mini/test.rb:29",
69
+ "-e:1"])
70
+
71
+ bt = util_expand_bt bt
72
+
73
+ ex = ["-e:1"]
74
+ fu = Minitest.filter_backtrace bt
75
+ assert_equal ex, fu
76
+ end
77
+
78
+ # def test_default_runner_is_minitest_unit
79
+ # assert_instance_of Minitest::Unit, Minitest::Unit.runner
80
+ # end
81
+
82
+ def test_infectious_binary_encoding
83
+ @tu = Class.new FakeNamedTest do
84
+ def test_this_is_not_ascii_assertion
85
+ assert_equal "ЁЁЁ", "ёёё"
86
+ end
87
+
88
+ def test_this_is_non_ascii_failure_message
89
+ fail 'ЁЁЁ'.force_encoding('ASCII-8BIT')
90
+ end
91
+ end
92
+
93
+ expected = clean <<-EOM
94
+ EF
95
+
96
+ Finished in 0.00
97
+
98
+ 1) Error:
99
+ FakeNamedTestXX#test_this_is_non_ascii_failure_message:
100
+ RuntimeError: ЁЁЁ
101
+ FILE:LINE:in `test_this_is_non_ascii_failure_message'
102
+
103
+ 2) Failure:
104
+ FakeNamedTestXX#test_this_is_not_ascii_assertion [FILE:LINE]:
105
+ Expected: \"ЁЁЁ\"
106
+ Actual: \"ёёё\"
107
+
108
+ 2 runs, 1 assertions, 1 failures, 1 errors, 0 skips
109
+ EOM
110
+
111
+ assert_report expected
112
+ end
113
+
114
+ def test_passed_eh_teardown_good
115
+ test_class = Class.new FakeNamedTest do
116
+ def teardown; assert true; end
117
+ def test_omg; assert true; end
118
+ end
119
+
120
+ test = test_class.new :test_omg
121
+ test.run
122
+
123
+ refute_predicate test, :error?
124
+ assert_predicate test, :passed?
125
+ refute_predicate test, :skipped?
126
+ end
127
+
128
+ def test_passed_eh_teardown_skipped
129
+ test_class = Class.new FakeNamedTest do
130
+ def teardown; assert true; end
131
+ def test_omg; skip "bork"; end
132
+ end
133
+
134
+ test = test_class.new :test_omg
135
+ test.run
136
+
137
+ refute_predicate test, :error?
138
+ refute_predicate test, :passed?
139
+ assert_predicate test, :skipped?
140
+ end
141
+
142
+ def test_passed_eh_teardown_flunked
143
+ test_class = Class.new FakeNamedTest do
144
+ def teardown; flunk; end
145
+ def test_omg; assert true; end
146
+ end
147
+
148
+ test = test_class.new :test_omg
149
+ test.run
150
+
151
+ refute_predicate test, :error?
152
+ refute_predicate test, :passed?
153
+ refute_predicate test, :skipped?
154
+ end
155
+
156
+ def util_expand_bt bt
157
+ if RUBY_VERSION >= "1.9.0" then
158
+ bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
159
+ else
160
+ bt
161
+ end
162
+ end
163
+ end
164
+
165
+ class TestMinitestUnitInherited < MetaMetaMetaTestCase
166
+ def with_overridden_include
167
+ Class.class_eval do
168
+ def inherited_with_hacks _klass
169
+ throw :inherited_hook
170
+ end
171
+
172
+ alias inherited_without_hacks inherited
173
+ alias inherited inherited_with_hacks
174
+ alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros
175
+ end
176
+
177
+ yield
178
+ ensure
179
+ Class.class_eval do
180
+ alias inherited inherited_without_hacks
181
+
182
+ undef_method :inherited_with_hacks
183
+ undef_method :inherited_without_hacks
184
+ end
185
+
186
+ refute_respond_to Class, :inherited_with_hacks
187
+ refute_respond_to Class, :inherited_without_hacks
188
+ end
189
+
190
+ def test_inherited_hook_plays_nice_with_others
191
+ with_overridden_include do
192
+ assert_throws :inherited_hook do
193
+ Class.new FakeNamedTest
194
+ end
195
+ end
196
+ end
197
+ end
198
+
199
+ class TestMinitestRunner < MetaMetaMetaTestCase
200
+ # do not parallelize this suite... it just can't handle it.
201
+
202
+ def test_class_runnables
203
+ @assertion_count = 0
204
+
205
+ tc = Class.new(Minitest::Test)
206
+
207
+ assert_equal 1, Minitest::Test.runnables.size
208
+ assert_equal [tc], Minitest::Test.runnables
209
+ end
210
+
211
+ def test_run_test
212
+ @tu =
213
+ Class.new FakeNamedTest do
214
+ attr_reader :foo
215
+
216
+ def run
217
+ @foo = "hi mom!"
218
+ r = super
219
+ @foo = "okay"
220
+
221
+ r
222
+ end
223
+
224
+ def test_something
225
+ assert_equal "hi mom!", foo
226
+ end
227
+ end
228
+
229
+ expected = clean <<-EOM
230
+ .
231
+
232
+ Finished in 0.00
233
+
234
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
235
+ EOM
236
+
237
+ assert_report expected
238
+ end
239
+
240
+ def test_run_error
241
+ @tu =
242
+ Class.new FakeNamedTest do
243
+ def test_something
244
+ assert true
245
+ end
246
+
247
+ def test_error
248
+ raise "unhandled exception"
249
+ end
250
+ end
251
+
252
+ expected = clean <<-EOM
253
+ E.
254
+
255
+ Finished in 0.00
256
+
257
+ 1) Error:
258
+ FakeNamedTestXX#test_error:
259
+ RuntimeError: unhandled exception
260
+ FILE:LINE:in \`test_error\'
261
+
262
+ 2 runs, 1 assertions, 0 failures, 1 errors, 0 skips
263
+ EOM
264
+
265
+ assert_report expected
266
+ end
267
+
268
+ def test_run_error_teardown
269
+ @tu =
270
+ Class.new FakeNamedTest do
271
+ def test_something
272
+ assert true
273
+ end
274
+
275
+ def teardown
276
+ raise "unhandled exception"
277
+ end
278
+ end
279
+
280
+ expected = clean <<-EOM
281
+ E
282
+
283
+ Finished in 0.00
284
+
285
+ 1) Error:
286
+ FakeNamedTestXX#test_something:
287
+ RuntimeError: unhandled exception
288
+ FILE:LINE:in \`teardown\'
289
+
290
+ 1 runs, 1 assertions, 0 failures, 1 errors, 0 skips
291
+ EOM
292
+
293
+ assert_report expected
294
+ end
295
+
296
+ def test_run_failing
297
+ setup_basic_tu
298
+
299
+ expected = clean <<-EOM
300
+ F.
301
+
302
+ Finished in 0.00
303
+
304
+ 1) Failure:
305
+ FakeNamedTestXX#test_failure [FILE:LINE]:
306
+ Expected false to be truthy.
307
+
308
+ 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
309
+ EOM
310
+
311
+ assert_report expected
312
+ end
313
+
314
+ def setup_basic_tu
315
+ @tu =
316
+ Class.new FakeNamedTest do
317
+ def test_something
318
+ assert true
319
+ end
320
+
321
+ def test_failure
322
+ assert false
323
+ end
324
+ end
325
+ end
326
+
327
+ def test_run_failing_filtered
328
+ setup_basic_tu
329
+
330
+ expected = clean <<-EOM
331
+ .
332
+
333
+ Finished in 0.00
334
+
335
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
336
+ EOM
337
+
338
+ assert_report expected, %w[--name /some|thing/ --seed 42]
339
+ end
340
+
341
+ def assert_filtering filter, name, expected, a = false
342
+ args = %W[--#{filter} #{name} --seed 42]
343
+
344
+ alpha = Class.new FakeNamedTest do
345
+ define_method :test_something do
346
+ assert a
347
+ end
348
+ end
349
+ Object.const_set(:Alpha, alpha)
350
+
351
+ beta = Class.new FakeNamedTest do
352
+ define_method :test_something do
353
+ assert true
354
+ end
355
+ end
356
+ Object.const_set(:Beta, beta)
357
+
358
+ @tus = [alpha, beta]
359
+
360
+ assert_report expected, args
361
+ ensure
362
+ Object.send :remove_const, :Alpha
363
+ Object.send :remove_const, :Beta
364
+ end
365
+
366
+ def test_run_filtered_including_suite_name
367
+ expected = clean <<-EOM
368
+ .
369
+
370
+ Finished in 0.00
371
+
372
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
373
+ EOM
374
+
375
+ assert_filtering "name", "/Beta#test_something/", expected
376
+ end
377
+
378
+ def test_run_filtered_including_suite_name_string
379
+ expected = clean <<-EOM
380
+ .
381
+
382
+ Finished in 0.00
383
+
384
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
385
+ EOM
386
+
387
+ assert_filtering "name", "Beta#test_something", expected
388
+ end
389
+
390
+ def test_run_filtered_string_method_only
391
+ expected = clean <<-EOM
392
+ ..
393
+
394
+ Finished in 0.00
395
+
396
+ 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
397
+ EOM
398
+
399
+ assert_filtering "name", "test_something", expected, :pass
400
+ end
401
+
402
+ def test_run_failing_excluded
403
+ setup_basic_tu
404
+
405
+ expected = clean <<-EOM
406
+ .
407
+
408
+ Finished in 0.00
409
+
410
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
411
+ EOM
412
+
413
+ assert_report expected, %w[--exclude /failure/ --seed 42]
414
+ end
415
+
416
+ def test_run_filtered_excluding_suite_name
417
+ expected = clean <<-EOM
418
+ .
419
+
420
+ Finished in 0.00
421
+
422
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
423
+ EOM
424
+
425
+ assert_filtering "exclude", "/Alpha#test_something/", expected
426
+ end
427
+
428
+ def test_run_filtered_excluding_suite_name_string
429
+ expected = clean <<-EOM
430
+ .
431
+
432
+ Finished in 0.00
433
+
434
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
435
+ EOM
436
+
437
+ assert_filtering "exclude", "Alpha#test_something", expected
438
+ end
439
+
440
+ def test_run_filtered_excluding_string_method_only
441
+ expected = clean <<-EOM
442
+
443
+
444
+ Finished in 0.00
445
+
446
+ 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
447
+ EOM
448
+
449
+ assert_filtering "exclude", "test_something", expected, :pass
450
+ end
451
+
452
+ def test_run_passing
453
+ @tu =
454
+ Class.new FakeNamedTest do
455
+ def test_something
456
+ assert true
457
+ end
458
+ end
459
+
460
+ expected = clean <<-EOM
461
+ .
462
+
463
+ Finished in 0.00
464
+
465
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
466
+ EOM
467
+
468
+ assert_report expected
469
+ end
470
+
471
+ def test_run_skip
472
+ @tu =
473
+ Class.new FakeNamedTest do
474
+ def test_something
475
+ assert true
476
+ end
477
+
478
+ def test_skip
479
+ skip "not yet"
480
+ end
481
+ end
482
+
483
+ expected = clean <<-EOM
484
+ S.
485
+
486
+ Finished in 0.00
487
+
488
+ 2 runs, 1 assertions, 0 failures, 0 errors, 1 skips
489
+
490
+ You have skipped tests. Run with --verbose for details.
491
+ EOM
492
+
493
+ restore_env do
494
+ assert_report expected
495
+ end
496
+ end
497
+
498
+ def test_run_skip_verbose
499
+ @tu =
500
+ Class.new FakeNamedTest do
501
+ def test_something
502
+ assert true
503
+ end
504
+
505
+ def test_skip
506
+ skip "not yet"
507
+ end
508
+ end
509
+
510
+ expected = clean <<-EOM
511
+ FakeNamedTestXX#test_skip = 0.00 s = S
512
+ FakeNamedTestXX#test_something = 0.00 s = .
513
+
514
+ Finished in 0.00
515
+
516
+ 1) Skipped:
517
+ FakeNamedTestXX#test_skip [FILE:LINE]:
518
+ not yet
519
+
520
+ 2 runs, 1 assertions, 0 failures, 0 errors, 1 skips
521
+ EOM
522
+
523
+ assert_report expected, %w[--seed 42 --verbose]
524
+ end
525
+
526
+ def test_run_with_other_runner
527
+ @tu =
528
+ Class.new FakeNamedTest do
529
+ def self.run reporter, options = {}
530
+ @reporter = reporter
531
+ before_my_suite
532
+ super
533
+ end
534
+
535
+ def self.name; "wacky!" end
536
+
537
+ def self.before_my_suite
538
+ @reporter.io.puts "Running #{self.name} tests"
539
+ @@foo = 1
540
+ end
541
+
542
+ def test_something
543
+ assert_equal 1, @@foo
544
+ end
545
+
546
+ def test_something_else
547
+ assert_equal 1, @@foo
548
+ end
549
+ end
550
+
551
+ expected = clean <<-EOM
552
+ Running wacky! tests
553
+ ..
554
+
555
+ Finished in 0.00
556
+
557
+ 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
558
+ EOM
559
+
560
+ assert_report expected
561
+ end
562
+
563
+ require "monitor"
564
+
565
+ class Latch
566
+ def initialize count = 1
567
+ @count = count
568
+ @lock = Monitor.new
569
+ @cv = @lock.new_cond
570
+ end
571
+
572
+ def release
573
+ @lock.synchronize do
574
+ @count -= 1 if @count > 0
575
+ @cv.broadcast if @count == 0
576
+ end
577
+ end
578
+
579
+ def await
580
+ @lock.synchronize { @cv.wait_while { @count > 0 } }
581
+ end
582
+ end
583
+
584
+ def test_run_parallel
585
+ skip "I don't have ParallelEach debugged yet" if maglev?
586
+
587
+ test_count = 2
588
+ test_latch = Latch.new test_count
589
+ wait_latch = Latch.new test_count
590
+ main_latch = Latch.new
591
+
592
+ thread = Thread.new {
593
+ Thread.current.abort_on_exception = true
594
+
595
+ # This latch waits until both test latches have been released. Both
596
+ # latches can't be released unless done in separate threads because
597
+ # `main_latch` keeps the test method from finishing.
598
+ test_latch.await
599
+ main_latch.release
600
+ }
601
+
602
+ @tu =
603
+ Class.new FakeNamedTest do
604
+ parallelize_me!
605
+
606
+ test_count.times do |i|
607
+ define_method :"test_wait_on_main_thread_#{i}" do
608
+ test_latch.release
609
+
610
+ # This latch blocks until the "main thread" releases it. The main
611
+ # thread can't release this latch until both test latches have
612
+ # been released. This forces the latches to be released in separate
613
+ # threads.
614
+ main_latch.await
615
+ assert true
616
+ end
617
+ end
618
+ end
619
+
620
+ expected = clean <<-EOM
621
+ ..
622
+
623
+ Finished in 0.00
624
+
625
+ 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
626
+ EOM
627
+
628
+ assert_report(expected) do |reporter|
629
+ reporter.extend(Module.new {
630
+ define_method("record") do |result|
631
+ super(result)
632
+ wait_latch.release
633
+ end
634
+
635
+ define_method("report") do
636
+ wait_latch.await
637
+ super()
638
+ end
639
+ })
640
+ end
641
+ assert thread.join
642
+ end
643
+ end
644
+
645
+ class TestMinitestUnitOrder < MetaMetaMetaTestCase
646
+ # do not parallelize this suite... it just can't handle it.
647
+
648
+ def test_before_setup
649
+ call_order = []
650
+ @tu =
651
+ Class.new FakeNamedTest do
652
+ define_method :setup do
653
+ super()
654
+ call_order << :setup
655
+ end
656
+
657
+ define_method :before_setup do
658
+ call_order << :before_setup
659
+ end
660
+
661
+ def test_omg; assert true; end
662
+ end
663
+
664
+ run_tu_with_fresh_reporter
665
+
666
+ expected = [:before_setup, :setup]
667
+ assert_equal expected, call_order
668
+ end
669
+
670
+ def test_after_teardown
671
+ call_order = []
672
+ @tu =
673
+ Class.new FakeNamedTest do
674
+ define_method :teardown do
675
+ super()
676
+ call_order << :teardown
677
+ end
678
+
679
+ define_method :after_teardown do
680
+ call_order << :after_teardown
681
+ end
682
+
683
+ def test_omg; assert true; end
684
+ end
685
+
686
+ run_tu_with_fresh_reporter
687
+
688
+ expected = [:teardown, :after_teardown]
689
+ assert_equal expected, call_order
690
+ end
691
+
692
+ def test_all_teardowns_are_guaranteed_to_run
693
+ call_order = []
694
+ @tu =
695
+ Class.new FakeNamedTest do
696
+ define_method :after_teardown do
697
+ super()
698
+ call_order << :after_teardown
699
+ raise
700
+ end
701
+
702
+ define_method :teardown do
703
+ super()
704
+ call_order << :teardown
705
+ raise
706
+ end
707
+
708
+ define_method :before_teardown do
709
+ super()
710
+ call_order << :before_teardown
711
+ raise
712
+ end
713
+
714
+ def test_omg; assert true; end
715
+ end
716
+
717
+ run_tu_with_fresh_reporter
718
+
719
+ expected = [:before_teardown, :teardown, :after_teardown]
720
+ assert_equal expected, call_order
721
+ end
722
+
723
+ def test_setup_and_teardown_survive_inheritance
724
+ call_order = []
725
+
726
+ @tu = Class.new FakeNamedTest do
727
+ define_method :setup do
728
+ call_order << :setup_method
729
+ end
730
+
731
+ define_method :teardown do
732
+ call_order << :teardown_method
733
+ end
734
+
735
+ define_method :test_something do
736
+ call_order << :test
737
+ end
738
+ end
739
+
740
+ run_tu_with_fresh_reporter
741
+
742
+ @tu = Class.new @tu
743
+ run_tu_with_fresh_reporter
744
+
745
+ # Once for the parent class, once for the child
746
+ expected = [:setup_method, :test, :teardown_method] * 2
747
+
748
+ assert_equal expected, call_order
749
+ end
750
+ end
751
+
752
+ class TestMinitestRunnable < Minitest::Test
753
+ def setup_marshal klass
754
+ tc = klass.new "whatever"
755
+ tc.assertions = 42
756
+ tc.failures << "a failure"
757
+
758
+ yield tc if block_given?
759
+
760
+ def tc.setup
761
+ @blah = "blah"
762
+ end
763
+ tc.setup
764
+
765
+ @tc = Minitest::Result.from tc
766
+ end
767
+
768
+ def assert_marshal expected_ivars
769
+ new_tc = Marshal.load Marshal.dump @tc
770
+
771
+ ivars = new_tc.instance_variables.map(&:to_s).sort
772
+ assert_equal expected_ivars, ivars
773
+ assert_equal "whatever", new_tc.name
774
+ assert_equal 42, new_tc.assertions
775
+ assert_equal ["a failure"], new_tc.failures
776
+
777
+ yield new_tc if block_given?
778
+ end
779
+
780
+ def test_marshal
781
+ setup_marshal Minitest::Runnable
782
+
783
+ assert_marshal %w[@NAME @assertions @failures @klass @source_location @time]
784
+ end
785
+
786
+ def test_spec_marshal
787
+ klass = describe("whatever") { it("passes") { assert true } }
788
+ rm = klass.runnable_methods.first
789
+
790
+ # Run the test
791
+ @tc = klass.new(rm).run
792
+
793
+ assert_kind_of Minitest::Result, @tc
794
+
795
+ # Pass it over the wire
796
+ over_the_wire = Marshal.load Marshal.dump @tc
797
+
798
+ assert_equal @tc.time, over_the_wire.time
799
+ assert_equal @tc.name, over_the_wire.name
800
+ assert_equal @tc.assertions, over_the_wire.assertions
801
+ assert_equal @tc.failures, over_the_wire.failures
802
+ assert_equal @tc.klass, over_the_wire.klass
803
+ end
804
+ end
805
+
806
+ class TestMinitestTest < TestMinitestRunnable
807
+ def test_dup
808
+ setup_marshal Minitest::Test do |tc|
809
+ tc.time = 3.14
810
+ end
811
+
812
+ assert_marshal %w[@NAME @assertions @failures @klass @source_location @time] do |new_tc|
813
+ assert_in_epsilon 3.14, new_tc.time
814
+ end
815
+ end
816
+ end
817
+
818
+ class TestMinitestUnitTestCase < Minitest::Test
819
+ # do not call parallelize_me! - teardown accesses @tc._assertions
820
+ # which is not threadsafe. Nearly every method in here is an
821
+ # assertion test so it isn't worth splitting it out further.
822
+
823
+ RUBY18 = !defined? Encoding
824
+
825
+ def setup
826
+ super
827
+
828
+ Minitest::Test.reset
829
+
830
+ @tc = Minitest::Test.new "fake tc"
831
+ @zomg = "zomg ponies!"
832
+ @assertion_count = 1
833
+ end
834
+
835
+ def teardown
836
+ assert_equal(@assertion_count, @tc.assertions,
837
+ "expected #{@assertion_count} assertions to be fired during the test, not #{@tc.assertions}") if @tc.passed?
838
+ end
839
+
840
+ def non_verbose
841
+ orig_verbose = $VERBOSE
842
+ $VERBOSE = false
843
+
844
+ yield
845
+ ensure
846
+ $VERBOSE = orig_verbose
847
+ end
848
+
849
+ def test_runnable_methods_random
850
+ @assertion_count = 0
851
+
852
+ sample_test_case = Class.new FakeNamedTest do
853
+ def self.test_order; :random; end
854
+ def test_test1; assert "does not matter" end
855
+ def test_test2; assert "does not matter" end
856
+ def test_test3; assert "does not matter" end
857
+ end
858
+
859
+ srand 42
860
+ expected = case
861
+ when maglev? then
862
+ %w[test_test2 test_test3 test_test1]
863
+ else
864
+ %w[test_test2 test_test1 test_test3]
865
+ end
866
+ assert_equal expected, sample_test_case.runnable_methods
867
+ end
868
+
869
+ def test_runnable_methods_sorted
870
+ @assertion_count = 0
871
+
872
+ sample_test_case = Class.new FakeNamedTest do
873
+ def self.test_order; :sorted end
874
+ def test_test3; assert "does not matter" end
875
+ def test_test2; assert "does not matter" end
876
+ def test_test1; assert "does not matter" end
877
+ end
878
+
879
+ expected = %w[test_test1 test_test2 test_test3]
880
+ assert_equal expected, sample_test_case.runnable_methods
881
+ end
882
+
883
+ def test_i_suck_and_my_tests_are_order_dependent_bang_sets_test_order_alpha
884
+ @assertion_count = 0
885
+
886
+ shitty_test_case = Class.new FakeNamedTest
887
+
888
+ shitty_test_case.i_suck_and_my_tests_are_order_dependent!
889
+
890
+ assert_equal :alpha, shitty_test_case.test_order
891
+ end
892
+
893
+ def test_i_suck_and_my_tests_are_order_dependent_bang_does_not_warn
894
+ @assertion_count = 0
895
+
896
+ shitty_test_case = Class.new FakeNamedTest
897
+
898
+ def shitty_test_case.test_order; :lol end
899
+
900
+ assert_silent do
901
+ shitty_test_case.i_suck_and_my_tests_are_order_dependent!
902
+ end
903
+ end
904
+ end
905
+
906
+ class TestMinitestGuard < Minitest::Test
907
+ parallelize_me!
908
+
909
+ def test_mri_eh
910
+ assert self.class.mri? "ruby blah"
911
+ assert self.mri? "ruby blah"
912
+ end
913
+
914
+ def test_jruby_eh
915
+ assert self.class.jruby? "java"
916
+ assert self.jruby? "java"
917
+ end
918
+
919
+ def test_rubinius_eh
920
+ assert self.class.rubinius? "rbx"
921
+ assert self.rubinius? "rbx"
922
+ end
923
+
924
+ def test_windows_eh
925
+ assert self.class.windows? "mswin"
926
+ assert self.windows? "mswin"
927
+ end
928
+ end
929
+
930
+ class TestMinitestUnitRecording < MetaMetaMetaTestCase
931
+ # do not parallelize this suite... it just can't handle it.
932
+
933
+ def assert_run_record *expected, &block
934
+ @tu = Class.new FakeNamedTest, &block
935
+
936
+ run_tu_with_fresh_reporter
937
+
938
+ recorded = first_reporter.results.map(&:failures).flatten.map { |f| f.error.class }
939
+
940
+ assert_equal expected, recorded
941
+ end
942
+
943
+ def test_run_with_bogus_reporter
944
+ # https://github.com/seattlerb/minitest/issues/659
945
+ # TODO: remove test for minitest 6
946
+ @tu = Class.new FakeNamedTest do
947
+ def test_method
948
+ assert true
949
+ end
950
+ end
951
+
952
+ bogus_reporter = Class.new do # doesn't subclass AbstractReporter
953
+ def start; @success = false; end
954
+ # def prerecord klass, name; end # doesn't define full API
955
+ def record result; @success = true; end
956
+ def report; end
957
+ def passed?; end
958
+ def results; end
959
+ def success?; @success; end
960
+ end.new
961
+
962
+ self.reporter = Minitest::CompositeReporter.new
963
+ reporter << bogus_reporter
964
+
965
+ Minitest::Runnable.runnables.delete @tu
966
+
967
+ @tu.run reporter, {}
968
+
969
+ assert_predicate bogus_reporter, :success?
970
+ end
971
+
972
+ def test_record_passing
973
+ assert_run_record do
974
+ def test_method
975
+ assert true
976
+ end
977
+ end
978
+ end
979
+
980
+ def test_record_failing
981
+ assert_run_record Minitest::Assertion do
982
+ def test_method
983
+ assert false
984
+ end
985
+ end
986
+ end
987
+
988
+ def test_record_error
989
+ assert_run_record RuntimeError do
990
+ def test_method
991
+ raise "unhandled exception"
992
+ end
993
+ end
994
+ end
995
+
996
+ def test_record_error_teardown
997
+ assert_run_record RuntimeError do
998
+ def test_method
999
+ assert true
1000
+ end
1001
+
1002
+ def teardown
1003
+ raise "unhandled exception"
1004
+ end
1005
+ end
1006
+ end
1007
+
1008
+ def test_record_error_in_test_and_teardown
1009
+ assert_run_record AnError, RuntimeError do
1010
+ def test_method
1011
+ raise AnError
1012
+ end
1013
+
1014
+ def teardown
1015
+ raise "unhandled exception"
1016
+ end
1017
+ end
1018
+ end
1019
+
1020
+ def test_to_s_error_in_test_and_teardown
1021
+ @tu = Class.new FakeNamedTest do
1022
+ def test_method
1023
+ raise AnError
1024
+ end
1025
+
1026
+ def teardown
1027
+ raise "unhandled exception"
1028
+ end
1029
+ end
1030
+
1031
+ run_tu_with_fresh_reporter
1032
+
1033
+ exp = clean "
1034
+ Error:
1035
+ FakeNamedTestXX#test_method:
1036
+ AnError: AnError
1037
+ FILE:LINE:in `test_method'
1038
+
1039
+ Error:
1040
+ FakeNamedTestXX#test_method:
1041
+ RuntimeError: unhandled exception
1042
+ FILE:LINE:in `teardown'
1043
+ "
1044
+
1045
+ assert_equal exp.strip, normalize_output(first_reporter.results.first.to_s).strip
1046
+ end
1047
+
1048
+ def test_record_skip
1049
+ assert_run_record Minitest::Skip do
1050
+ def test_method
1051
+ skip "not yet"
1052
+ end
1053
+ end
1054
+ end
1055
+ end