spec 5.0.14

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