diff options
Diffstat (limited to 'test/minitest/test_minitest_unit.rb')
-rw-r--r-- | test/minitest/test_minitest_unit.rb | 292 |
1 files changed, 220 insertions, 72 deletions
diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb index eadf59ee70..45bd96fcd4 100644 --- a/test/minitest/test_minitest_unit.rb +++ b/test/minitest/test_minitest_unit.rb @@ -13,6 +13,8 @@ class AnError < StandardError; include MyModule; end class ImmutableString < String; def inspect; super.freeze; end; end class TestMiniTestUnit < MetaMetaMetaTestCase + parallelize_me! if ENV["PARALLEL"] + pwd = Pathname.new File.expand_path Dir.pwd basedir = Pathname.new(File.expand_path "lib/minitest") + 'mini' basedir = basedir.relative_path_from(pwd).to_s @@ -22,6 +24,23 @@ class TestMiniTestUnit < MetaMetaMetaTestCase "#{MINITEST_BASE_DIR}/test.rb:139:in `run'", "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"] + def test_wtf + $hook_value = nil + + capture_io do # don't care about deprecation + MiniTest::Unit::TestCase.add_setup_hook do + $hook_value = 42 + end + end + + run_setup_hooks + + assert_equal 42, $hook_value + assert_equal [Proc], MiniTest::Unit::TestCase.setup_hooks.map(&:class) + MiniTest::Unit::TestCase.reset_setup_teardown_hooks + assert_equal [], MiniTest::Unit::TestCase.setup_hooks.map(&:class) + end + def test_class_puke_with_assertion_failed exception = MiniTest::Assertion.new "Oh no!" exception.set_backtrace ["unhappy"] @@ -156,6 +175,85 @@ class TestMiniTestUnit < MetaMetaMetaTestCase assert_equal ex, fu end + def test_default_runner_is_minitest_unit + assert_instance_of MiniTest::Unit, MiniTest::Unit.runner + end + + def with_overridden_include + Class.class_eval do + def inherited_with_hacks klass + throw :inherited_hook + end + + alias inherited_without_hacks inherited + alias inherited inherited_with_hacks + alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros + end + + yield + ensure + Class.class_eval do + alias inherited inherited_without_hacks + + undef_method :inherited_with_hacks + undef_method :inherited_without_hacks + end + + refute_respond_to Class, :inherited_with_hacks + refute_respond_to Class, :inherited_without_hacks + end + + def test_inherited_hook_plays_nice_with_others + with_overridden_include do + assert_throws :inherited_hook do + Class.new MiniTest::Unit::TestCase + end + end + end + + def test_passed_eh_teardown_good + test_class = Class.new MiniTest::Unit::TestCase do + def teardown; assert true; end + def test_omg; assert true; end + end + + test = test_class.new :test_omg + test.run @tu + assert test.passed? + end + + def test_passed_eh_teardown_flunked + test_class = Class.new MiniTest::Unit::TestCase do + def teardown; flunk; end + def test_omg; assert true; end + end + + test = test_class.new :test_omg + test.run @tu + refute test.passed? + end + + def util_expand_bt bt + if RUBY_VERSION >= '1.9.0' then + bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f } + else + bt + end + end +end + +class TestMiniTestRunner < MetaMetaMetaTestCase + # do not parallelize this suite... it just can't handle it. + + def test_class_test_suites + @assertion_count = 0 + + tc = Class.new(MiniTest::Unit::TestCase) + + assert_equal 1, MiniTest::Unit::TestCase.test_suites.size + assert_equal [tc], MiniTest::Unit::TestCase.test_suites + end + def test_run_test Class.new MiniTest::Unit::TestCase do attr_reader :foo @@ -352,10 +450,6 @@ class TestMiniTestUnit < MetaMetaMetaTestCase assert_report expected, %w[--seed 42 --verbose] end - def test_default_runner_is_minitest_unit - assert_instance_of MiniTest::Unit, MiniTest::Unit.runner - end - def test_run_with_other_runner MiniTest::Unit.runner = Class.new MiniTest::Unit do def _run_suite suite, type @@ -393,37 +487,74 @@ class TestMiniTestUnit < MetaMetaMetaTestCase assert_report expected end - def with_overridden_include - Class.class_eval do - def inherited_with_hacks klass - throw :inherited_hook - end + require 'monitor' - alias inherited_without_hacks inherited - alias inherited inherited_with_hacks - alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros + class Latch + def initialize count = 1 + @count = count + @lock = Monitor.new + @cv = @lock.new_cond end - yield - ensure - Class.class_eval do - alias inherited inherited_without_hacks - - undef_method :inherited_with_hacks - undef_method :inherited_without_hacks + def release + @lock.synchronize do + @count -= 1 if @count > 0 + @cv.broadcast if @count == 0 + end end - refute_respond_to Class, :inherited_with_hacks - refute_respond_to Class, :inherited_without_hacks + def await + @lock.synchronize { @cv.wait_while { @count > 0 } } + end end - def test_inherited_hook_plays_nice_with_others - with_overridden_include do - assert_throws :inherited_hook do - Class.new MiniTest::Unit::TestCase + def test_run_parallel + test_count = 2 + test_latch = Latch.new test_count + main_latch = Latch.new + + thread = Thread.new { + Thread.current.abort_on_exception = true + + # This latch waits until both test latches have been released. Both + # latches can't be released unless done in separate threads because + # `main_latch` keeps the test method from finishing. + test_latch.await + main_latch.release + } + + Class.new MiniTest::Unit::TestCase do + parallelize_me! + + test_count.times do |i| + define_method :"test_wait_on_main_thread_#{i}" do + test_latch.release + + # This latch blocks until the "main thread" releases it. The main + # thread can't release this latch until both test latches have + # been released. This forces the latches to be released in separate + # threads. + main_latch.await + assert true + end end end + + expected = clean <<-EOM + .. + + Finished tests in 0.00 + + 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips + EOM + + assert_report expected + assert thread.join end +end + +class TestMiniTestUnitOrder < MetaMetaMetaTestCase + # do not parallelize this suite... it just can't handle it. def test_before_setup call_order = [] @@ -440,34 +571,14 @@ class TestMiniTestUnit < MetaMetaMetaTestCase def test_omg; assert true; end end - @tu.run %w[--seed 42] + with_output do + @tu.run %w[--seed 42] + end expected = [:before_setup, :setup] assert_equal expected, call_order end - def test_passed_eh_teardown_good - test_class = Class.new MiniTest::Unit::TestCase do - def teardown; assert true; end - def test_omg; assert true; end - end - - test = test_class.new :test_omg - test.run @tu - assert test.passed? - end - - def test_passed_eh_teardown_flunked - test_class = Class.new MiniTest::Unit::TestCase do - def teardown; flunk; end - def test_omg; assert true; end - end - - test = test_class.new :test_omg - test.run @tu - refute test.passed? - end - def test_after_teardown call_order = [] Class.new MiniTest::Unit::TestCase do @@ -483,7 +594,9 @@ class TestMiniTestUnit < MetaMetaMetaTestCase def test_omg; assert true; end end - @tu.run %w[--seed 42] + with_output do + @tu.run %w[--seed 42] + end expected = [:teardown, :after_teardown] assert_equal expected, call_order @@ -513,7 +626,9 @@ class TestMiniTestUnit < MetaMetaMetaTestCase def test_omg; assert true; end end - @tu.run %w[--seed 42] + with_output do + @tu.run %w[--seed 42] + end expected = [:before_teardown, :teardown, :after_teardown] assert_equal expected, call_order @@ -538,24 +653,20 @@ class TestMiniTestUnit < MetaMetaMetaTestCase _ = Class.new parent - @tu.run %w[--seed 42] + with_output do + @tu.run %w[--seed 42] + end # Once for the parent class, once for the child expected = [:setup_method, :test, :teardown_method] * 2 assert_equal expected, call_order end - - def util_expand_bt bt - if RUBY_VERSION >= '1.9.0' then - bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f } - else - bt - end - end end class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase + parallelize_me! if ENV["PARALLEL"] + RUBY18 = ! defined? Encoding def setup @@ -633,6 +744,30 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_equal 1, 1 end + def test_assert_equal_different_collection_array_hex_invisible + object1 = Object.new + object2 = Object.new + msg = "No visible difference in the Array#inspect output. + You should look at the implementation of #== on Array or its members. + [#<Object:0xXXXXXX>]".gsub(/^ +/, "") + util_assert_triggered msg do + @tc.assert_equal [object1], [object2] + end + end + + def test_assert_equal_different_collection_hash_hex_invisible + h1, h2 = {}, {} + h1[1] = Object.new + h2[1] = Object.new + msg = "No visible difference in the Hash#inspect output. + You should look at the implementation of #== on Hash or its members. + {1=>#<Object:0xXXXXXX>}".gsub(/^ +/, "") + + util_assert_triggered msg do + @tc.assert_equal h1, h2 + end + end + def test_assert_equal_different_diff_deactivated without_diff do util_assert_triggered util_msg("haha" * 10, "blah" * 10) do @@ -667,8 +802,8 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase o1 = Object.new o2 = Object.new - msg = "No visible difference. - You should look at your implementation of Object#==. + msg = "No visible difference in the Object#inspect output. + You should look at the implementation of #== on Object or its members. #<Object:0xXXXXXX>".gsub(/^ +/, "") util_assert_triggered msg do @@ -693,8 +828,8 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_equal_different_long_invisible - msg = "No visible difference. - You should look at your implementation of String#==. + msg = "No visible difference in the String#inspect output. + You should look at the implementation of #== on String or its members. \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "") util_assert_triggered msg do @@ -1181,6 +1316,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase orig_verbose = $VERBOSE $VERBOSE = false + out, err = capture_io do puts 'hi' warn 'bye!' @@ -1192,6 +1328,24 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase $VERBOSE = orig_verbose end + def test_capture_subprocess_io + @assertion_count = 0 + skip "Dunno why but the parallel run of this fails" + + orig_verbose = $VERBOSE + $VERBOSE = false + + out, err = capture_subprocess_io do + system("echo 'hi'") + system("echo 'bye!' 1>&2") + end + + assert_equal "hi\n", out + assert_equal "bye!\n", err + ensure + $VERBOSE = orig_verbose + end + def test_class_asserts_match_refutes @assertion_count = 0 @@ -1215,15 +1369,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes end - def test_class_test_suites - @assertion_count = 0 - - tc = Class.new(MiniTest::Unit::TestCase) - - assert_equal 1, MiniTest::Unit::TestCase.test_suites.size - assert_equal [tc], MiniTest::Unit::TestCase.test_suites - end - def test_expectation @assertion_count = 2 @@ -1458,6 +1603,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @assertion_count = 0 sample_test_case = Class.new MiniTest::Unit::TestCase do + def self.test_order; :random; end def test_test1; assert "does not matter" end def test_test2; assert "does not matter" end def test_test3; assert "does not matter" end @@ -1532,6 +1678,8 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end class TestMiniTestGuard < MiniTest::Unit::TestCase + parallelize_me! if ENV["PARALLEL"] + def test_mri_eh assert self.class.mri? "ruby blah" assert self.mri? "ruby blah" |