diff options
author | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-05-02 04:48:43 +0000 |
---|---|---|
committer | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-05-02 04:48:43 +0000 |
commit | f8e5c7c79e720d3b0af3cb96f27d421f08eb7744 (patch) | |
tree | 0d950c668cf9141d516152aa7ca117d62afa3676 /test | |
parent | ce3029cd19dc40f4ffe09b8c6633500bd182b324 (diff) | |
download | ruby-f8e5c7c79e720d3b0af3cb96f27d421f08eb7744.tar.gz |
Imported minitest 4.7.4 (r8483)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40553 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/minitest/test_minitest_benchmark.rb | 16 | ||||
-rw-r--r-- | test/minitest/test_minitest_mock.rb | 43 | ||||
-rw-r--r-- | test/minitest/test_minitest_spec.rb | 129 | ||||
-rw-r--r-- | test/minitest/test_minitest_unit.rb | 398 |
4 files changed, 435 insertions, 151 deletions
diff --git a/test/minitest/test_minitest_benchmark.rb b/test/minitest/test_minitest_benchmark.rb index 708589b3fa..8f0aab4904 100644 --- a/test/minitest/test_minitest_benchmark.rb +++ b/test/minitest/test_minitest_benchmark.rb @@ -51,6 +51,22 @@ class TestMiniTestBenchmark < MiniTest::Unit::TestCase assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820 end + def test_fit_logarithmic_clean + x = [1.0, 2.0, 3.0, 4.0, 5.0] + y = x.map { |n| 1.1 + 2.1 * Math.log(n) } + + assert_fit :logarithmic, x, y, 1.0, 1.1, 2.1 + end + + def test_fit_logarithmic_noisy + x = [1.0, 2.0, 3.0, 4.0, 5.0] + # Generated with + # y = x.map { |n| jitter = 0.999 + 0.002 * rand; (Math.log(n) ) * jitter } + y = [0.0, 0.6935, 1.0995, 1.3873, 1.6097] + + assert_fit :logarithmic, x, y, 0.95, 0, 1 + end + def test_fit_constant_clean x = (1..5).to_a y = [5.0, 5.0, 5.0, 5.0, 5.0] diff --git a/test/minitest/test_minitest_mock.rb b/test/minitest/test_minitest_mock.rb index 918ef4c012..7fabb79292 100644 --- a/test/minitest/test_minitest_mock.rb +++ b/test/minitest/test_minitest_mock.rb @@ -5,10 +5,7 @@ # File a patch instead and assign it to Ryan Davis. ###################################################################### -require 'minitest/mock' -require 'minitest/unit' - -MiniTest::Unit.autorun +require 'minitest/autorun' class TestMiniTestMock < MiniTest::Unit::TestCase parallelize_me! if ENV["PARALLEL"] @@ -74,6 +71,8 @@ class TestMiniTestMock < MiniTest::Unit::TestCase end def test_mock_args_does_not_raise + skip "non-opaque use of ==" if maglev? + arg = MiniTest::Mock.new mock = MiniTest::Mock.new mock.expect(:foo, nil, [arg]) @@ -276,7 +275,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase end end -require_relative "metametameta" +require "minitest/metametameta" class TestMiniTestStub < MiniTest::Unit::TestCase parallelize_me! if ENV["PARALLEL"] @@ -308,6 +307,40 @@ class TestMiniTestStub < MiniTest::Unit::TestCase end end + def test_stub_private_module_method + @assertion_count += 1 + + t0 = Time.now + + self.stub :sleep, nil do + @tc.assert_nil sleep(10) + end + + @tc.assert_operator Time.now - t0, :<=, 1 + end + + def test_stub_private_module_method_indirect + @assertion_count += 1 + + slow_clapper = Class.new do + def slow_clap + sleep 3 + :clap + end + end.new + + slow_clapper.stub :sleep, nil do |fast_clapper| + @tc.assert_equal :clap, fast_clapper.slow_clap # either form works + @tc.assert_equal :clap, slow_clapper.slow_clap # yay closures + end + end + + def test_stub_public_module_method + Math.stub(:log10, 42.0) do + @tc.assert_in_delta 42.0, Math.log10(1000) + end + end + def test_stub_value assert_stub 42 end diff --git a/test/minitest/test_minitest_spec.rb b/test/minitest/test_minitest_spec.rb index 85cb95f42b..96589ede0b 100644 --- a/test/minitest/test_minitest_spec.rb +++ b/test/minitest/test_minitest_spec.rb @@ -6,16 +6,20 @@ ###################################################################### # encoding: utf-8 -require 'minitest/autorun' -require 'stringio' +require "minitest/autorun" +require "stringio" class MiniSpecA < MiniTest::Spec; end -class MiniSpecB < MiniTest::Spec; end +class MiniSpecB < MiniTest::Unit::TestCase; extend MiniTest::Spec::DSL; end +class MiniSpecC < MiniSpecB; end +class NamedExampleA < MiniSpecA; end +class NamedExampleB < MiniSpecB; end +class NamedExampleC < MiniSpecC; end class ExampleA; end class ExampleB < ExampleA; end describe MiniTest::Spec do - # do not parallelize this suite... it just can't handle it. + # do not parallelize this suite... it just can"t handle it. def assert_triggered expected = "blah", klass = MiniTest::Assertion @assertion_count += 2 @@ -25,7 +29,7 @@ describe MiniTest::Spec do end msg = e.message.sub(/(---Backtrace---).*/m, '\1') - msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)') + msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)") assert_equal expected, msg end @@ -35,7 +39,7 @@ describe MiniTest::Spec do end after do - self._assertions.must_equal @assertion_count + self._assertions.must_equal @assertion_count if passed? and not skipped? end it "needs to be able to catch a MiniTest::Assertion exception" do @@ -84,10 +88,10 @@ describe MiniTest::Spec do it "needs to catch an unexpected exception" do @assertion_count -= 2 # no positive - msg = <<-EOM.gsub(/^ {6}/, '').chomp + msg = <<-EOM.gsub(/^ {6}/, "").chomp [RuntimeError] exception expected, not Class: <MiniTest::Assertion> - Message: <\"MiniTest::Assertion\"> + Message: <"MiniTest::Assertion"> ---Backtrace--- EOM @@ -140,7 +144,7 @@ describe MiniTest::Spec do bad = %w[not raise throw send output be_silent] - expected_wonts = expected_musts.map { |m| m.sub(/^must/, 'wont') } + expected_wonts = expected_musts.map { |m| m.sub(/^must/, "wont") } expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ } musts.must_equal expected_musts @@ -162,7 +166,7 @@ describe MiniTest::Spec do it "needs to verify binary messages" do 42.wont_be(:<, 24).must_equal false - assert_triggered 'Expected 24 to not be < 42.' do + assert_triggered "Expected 24 to not be < 42." do 24.wont_be :<, 42 end @@ -202,15 +206,16 @@ describe MiniTest::Spec do 24.wont_be_close_to(42).must_equal false - assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.001.' do + assert_triggered "Expected |42 - 42.0| (0.0) to not be <= 0.001." do (6 * 7.0).wont_be_close_to 42 end - assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 1.0e-05.' do + x = maglev? ? "1.0000000000000001e-05" : "1.0e-05" + assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do (6 * 7.0).wont_be_close_to 42, 0.00001 end - assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be < 1.0e-05." do + assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do (6 * 7.0).wont_be_close_to 42, 0.00001, "msg" end end @@ -220,15 +225,17 @@ describe MiniTest::Spec do 24.wont_be_within_epsilon(42).must_equal false - assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.042.' do + x = maglev? ? "0.042000000000000003" : "0.042" + assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do (6 * 7.0).wont_be_within_epsilon 42 end - assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.00042.' do + x = maglev? ? "0.00042000000000000002" : "0.00042" + assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do (6 * 7.0).wont_be_within_epsilon 42, 0.00001 end - assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be < 0.00042." do + assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do (6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg" end end @@ -238,15 +245,16 @@ describe MiniTest::Spec do (6.0 * 7).must_be_close_to(42.0).must_equal true - assert_triggered 'Expected |0.0 - 0.01| (0.01) to be < 0.001.' do + assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.001." do (1.0 / 100).must_be_close_to 0.0 end - assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 1.0e-06.' do + x = maglev? ? "9.9999999999999995e-07" : "1.0e-06" + assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do (1.0 / 1000).must_be_close_to 0.0, 0.000001 end - assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be < 1.0e-06." do + assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= #{x}." do (1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg" end end @@ -256,15 +264,15 @@ describe MiniTest::Spec do (6.0 * 7).must_be_within_epsilon(42.0).must_equal true - assert_triggered 'Expected |0.0 - 0.01| (0.01) to be < 0.0.' do + assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.0." do (1.0 / 100).must_be_within_epsilon 0.0 end - assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 0.0.' do + assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= 0.0." do (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001 end - assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be < 0.0." do + assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= 0.0." do (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg" end end @@ -296,7 +304,7 @@ describe MiniTest::Spec do it "needs to verify instances of a class" do 42.wont_be_instance_of(String).must_equal false - assert_triggered 'Expected 42 to not be an instance of Fixnum.' do + assert_triggered "Expected 42 to not be an instance of Fixnum." do 42.wont_be_instance_of Fixnum end @@ -308,7 +316,7 @@ describe MiniTest::Spec do it "needs to verify kinds of a class" do 42.wont_be_kind_of(String).must_equal false - assert_triggered 'Expected 42 to not be a kind of Integer.' do + assert_triggered "Expected 42 to not be a kind of Integer." do 42.wont_be_kind_of Integer end @@ -361,7 +369,7 @@ describe MiniTest::Spec do it "needs to verify non-emptyness" do @assertion_count += 3 # empty is 2 assertions - ['some item'].wont_be_empty.must_equal false + ["some item"].wont_be_empty.must_equal false assert_triggered "Expected [] to not be empty." do [].wont_be_empty @@ -399,7 +407,7 @@ describe MiniTest::Spec do it "needs to verify objects not responding to a message" do "".wont_respond_to(:woot!).must_equal false - assert_triggered 'Expected "" to not respond to to_s.' do + assert_triggered "Expected \"\" to not respond to to_s." do "".wont_respond_to :to_s end @@ -479,7 +487,7 @@ describe MiniTest::Spec do end it "needs to verify using any (negative) predicate" do - @assertion_count -= 1 # doesn't take a message + @assertion_count -= 1 # doesn"t take a message "blah".wont_be(:empty?).must_equal false @@ -653,6 +661,26 @@ class TestMeta < MiniTest::Unit::TestCase MiniTest::Spec::TYPES.replace original_types end + def test_name + spec_a = describe ExampleA do; end + spec_b = describe ExampleB, :random_method do; end + + assert_equal "ExampleA", spec_a.name + assert_equal "ExampleB::random_method", spec_b.name + end + + def test_name2 + assert_equal "NamedExampleA", NamedExampleA.name + assert_equal "NamedExampleB", NamedExampleB.name + assert_equal "NamedExampleC", NamedExampleC.name + + spec_a = describe ExampleA do; end + spec_b = describe ExampleB, :random_method do; end + + assert_equal "ExampleA", spec_a.name + assert_equal "ExampleB::random_method", spec_b.name + end + def test_structure x, y, z, * = util_structure @@ -700,7 +728,7 @@ class TestMeta < MiniTest::Unit::TestCase z = describe "second thingy" do end end - test_methods = ['test_0001_top level it', 'test_0002_не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world'].sort + test_methods = ["test_0001_top level it", "test_0002_не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world"].sort assert_equal test_methods, [x1, x2] assert_equal test_methods, @@ -736,3 +764,48 @@ class TestMeta < MiniTest::Unit::TestCase end end end + +require "minitest/metametameta" + +class TestSpecInTestCase < MetaMetaMetaTestCase + def setup + super + + @tc = MiniTest::Unit::TestCase.new "fake tc" + @assertion_count = 1 + end + + def util_assert_triggered expected, klass = MiniTest::Assertion # REFACTOR + e = assert_raises klass do + yield + end + + msg = e.message.sub(/(---Backtrace---).*/m, "\1") + msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)") + + assert_equal expected, msg + end + + def teardown # REFACTOR + assert_equal(@assertion_count, @tc._assertions, + "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc.passed? + end + + def test_expectation + @assertion_count = 2 + + @tc.assert_equal true, 1.must_equal(1) + end + + def test_expectation_triggered + util_assert_triggered "Expected: 2\n Actual: 1" do + 1.must_equal 2 + end + end + + def test_expectation_with_a_message + util_assert_triggered "Expected: 2\n Actual: 1" do + 1.must_equal 2, "" + end + end +end diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb index 65b912e986..199fa0347f 100644 --- a/test/minitest/test_minitest_unit.rb +++ b/test/minitest/test_minitest_unit.rb @@ -6,7 +6,7 @@ ###################################################################### require 'pathname' -require_relative 'metametameta' +require 'minitest/metametameta' module MyModule; end class AnError < StandardError; include MyModule; end @@ -24,30 +24,13 @@ 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"] assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception) assert_equal 1, @tu.failures assert_match(/^Failure.*Oh no!/m, @tu.report.first) - assert_match("method_name(SomeClass) [unhappy]", @tu.report.first) + assert_match("SomeClass#method_name [unhappy]", @tu.report.first) end def test_class_puke_with_assertion_failed_and_long_backtrace @@ -67,7 +50,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) assert_equal 1, @tu.failures assert_match(/^Failure.*Oh no!/m, @tu.report.first) - assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first) + assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first) end def test_class_puke_with_assertion_failed_and_user_defined_assertions @@ -90,7 +73,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) assert_equal 1, @tu.failures assert_match(/^Failure.*Oh no!/m, @tu.report.first) - assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first) + assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first) end def test_class_puke_with_failure_and_flunk_in_backtrace @@ -123,7 +106,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) assert_equal 1, @tu.failures assert_match(/^Failure.*Oh no!/m, @tu.report.first) - assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first) + assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first) end def test_class_puke_with_non_failure_exception @@ -179,42 +162,22 @@ class TestMiniTestUnit < MetaMetaMetaTestCase 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 + 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 - 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 + test = test_class.new :test_omg + test.run @tu + assert test.passed? end - def test_passed_eh_teardown_good + def test_passed_eh_teardown_skipped test_class = Class.new MiniTest::Unit::TestCase do def teardown; assert true; end - def test_omg; assert true; end + def test_omg; skip "bork"; end end test = test_class.new :test_omg @@ -242,6 +205,40 @@ class TestMiniTestUnit < MetaMetaMetaTestCase end end +class TestMiniTestUnitInherited < MetaMetaMetaTestCase + 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 +end + class TestMiniTestRunner < MetaMetaMetaTestCase # do not parallelize this suite... it just can't handle it. @@ -297,9 +294,9 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Error: - test_error(#<Class:0xXXX>): + #<Class:0xXXX>#test_error: RuntimeError: unhandled exception - FILE:LINE:in `test_error' + FILE:LINE:in \`test_error\' 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips EOM @@ -324,9 +321,9 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Error: - test_something(#<Class:0xXXX>): + #<Class:0xXXX>#test_something: RuntimeError: unhandled exception - FILE:LINE:in `teardown' + FILE:LINE:in \`teardown\' 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips EOM @@ -351,7 +348,7 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Failure: - test_failure(#<Class:0xXXX>) [FILE:LINE]: + #<Class:0xXXX>#test_failure [FILE:LINE]: Failed assertion, no message given. 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips @@ -382,6 +379,65 @@ class TestMiniTestRunner < MetaMetaMetaTestCase assert_report expected, %w[--name /some|thing/ --seed 42] end + def assert_filtering name, expected, a = false + args = %W[--name #{name} --seed 42] + + alpha = Class.new MiniTest::Unit::TestCase do + define_method :test_something do + assert a + end + end + Object.const_set(:Alpha, alpha) + + beta = Class.new MiniTest::Unit::TestCase do + define_method :test_something do + assert true + end + end + Object.const_set(:Beta, beta) + + assert_report expected, args + ensure + Object.send :remove_const, :Alpha + Object.send :remove_const, :Beta + end + + def test_run_filtered_including_suite_name + expected = clean <<-EOM + . + + Finished tests in 0.00 + + 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips + EOM + + assert_filtering "/Beta#test_something/", expected + end + + def test_run_filtered_including_suite_name_string + expected = clean <<-EOM + . + + Finished tests in 0.00 + + 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips + EOM + + assert_filtering "Beta#test_something", expected + end + + def test_run_filtered_string_method_only + expected = clean <<-EOM + .. + + Finished tests in 0.00 + + 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips + EOM + + assert_filtering "test_something", expected, :pass + end + def test_run_passing Class.new MiniTest::Unit::TestCase do def test_something @@ -441,7 +497,7 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Skipped: - test_skip(#<Class:0xXXX>) [FILE:LINE]: + #<Class:0xXXX>#test_skip [FILE:LINE]: not yet 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips @@ -508,7 +564,13 @@ class TestMiniTestRunner < MetaMetaMetaTestCase end end + def test_parallel_each_size + assert_equal 0, ParallelEach.new([]).size + end + def test_run_parallel + skip "I don't have ParallelEach debugged yet" if maglev? + test_count = 2 test_latch = Latch.new test_count main_latch = Latch.new @@ -637,12 +699,12 @@ class TestMiniTestUnitOrder < MetaMetaMetaTestCase def test_setup_and_teardown_survive_inheritance call_order = [] - parent = Class.new MiniTest::Spec do - before do + parent = Class.new MiniTest::Unit::TestCase do + define_method :setup do call_order << :setup_method end - after do + define_method :teardown do call_order << :teardown_method end @@ -665,7 +727,9 @@ class TestMiniTestUnitOrder < MetaMetaMetaTestCase end class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase - parallelize_me! if ENV["PARALLEL"] + # do not call parallelize_me! - teardown accesses @tc._assertions + # which is not threadsafe. Nearly every method in here is an + # assertion test so it isn't worth splitting it out further. RUBY18 = ! defined? Encoding @@ -681,7 +745,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase def teardown assert_equal(@assertion_count, @tc._assertions, - "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc._assertions + "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc.passed? end def non_verbose @@ -711,30 +775,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end end - def test_assert_block - exp = ["NOTE: MiniTest::Unit::TestCase#assert_block is deprecated,", - "use assert. It will be removed on 2013-01-01."].join " " - - out, err = capture_io do - @tc.assert_block do - true - end - end - - assert_equal "", out - assert_match exp, err - end - - def test_assert_block_triggered - assert_output do - util_assert_triggered "blah.\nExpected block to return true value." do - @tc.assert_block "blah" do - false - end - end - end - end - def test_assert_empty @assertion_count = 2 @@ -778,6 +818,8 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_equal_different_diff_deactivated + skip "https://github.com/MagLev/maglev/issues/209" if maglev? + without_diff do util_assert_triggered util_msg("haha" * 10, "blah" * 10) do o1 = "haha" * 10 @@ -890,8 +932,17 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1 end + def test_delta_consistency + @tc.assert_in_delta 0, 1, 1 + + util_assert_triggered "Expected |0 - 1| (1) to not be <= 1." do + @tc.refute_in_delta 0, 1, 1 + end + end + def test_assert_in_delta_triggered - util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 1.0e-06.' do + x = maglev? ? "9.999999xxxe-07" : "1.0e-06" + util_assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001 end end @@ -913,15 +964,25 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_in_epsilon(-10000, -9991) end + def test_epsilon_consistency + @tc.assert_in_epsilon 1.0, 1.001 + + msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001." + util_assert_triggered msg do + @tc.refute_in_epsilon 1.0, 1.001 + end + end + def test_assert_in_epsilon_triggered - util_assert_triggered 'Expected |10000 - 9990| (10) to be < 9.99.' do + util_assert_triggered 'Expected |10000 - 9990| (10) to be <= 9.99.' do @tc.assert_in_epsilon 10000, 9990 end end def test_assert_in_epsilon_triggered_negative_case - x = RUBY18 ? "0.1" : "0.10000000000000009" - util_assert_triggered "Expected |-1.1 - -1| (#{x}) to be < 0.1." do + x = (RUBY18 and not maglev?) ? "0.1" : "0.100000xxx" + y = maglev? ? "0.100000xxx" : "0.1" + util_assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do @tc.assert_in_epsilon(-1.1, -1, 0.1) end end @@ -1149,7 +1210,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase Class: <SyntaxError> Message: <\"icky\"> ---Backtrace--- - FILE:LINE:in `test_assert_raises_triggered_different' + FILE:LINE:in \`test_assert_raises_triggered_different\' --------------- EOM @@ -1172,7 +1233,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase Class: <SyntaxError> Message: <\"icky\"> ---Backtrace--- - FILE:LINE:in `test_assert_raises_triggered_different_msg' + FILE:LINE:in \`test_assert_raises_triggered_different_msg\' --------------- EOM @@ -1218,7 +1279,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase Class: <AnError> Message: <\"AnError\"> ---Backtrace--- - FILE:LINE:in `test_assert_raises_triggered_subclass' + FILE:LINE:in \`test_assert_raises_triggered_subclass\' --------------- EOM @@ -1326,7 +1387,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase non_verbose do out, err = capture_io do puts 'hi' - warn 'bye!' + $stderr.puts 'bye!' end assert_equal "hi\n", out @@ -1357,7 +1418,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase # These don't have corresponding refutes _on purpose_. They're # useless and will never be added, so don't bother. - ignores = %w[assert_block assert_output assert_raises assert_send + ignores = %w[assert_output assert_raises assert_send assert_silent assert_throws] # These are test/unit methods. I'm not actually sure why they're still here @@ -1372,24 +1433,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes end - def test_expectation - @assertion_count = 2 - - @tc.assert_equal true, 1.must_equal(1) - end - - def test_expectation_triggered - util_assert_triggered "Expected: 2\n Actual: 1" do - 1.must_equal 2 - end - end - - def test_expectation_with_a_message - util_assert_triggered "Expected: 2\n Actual: 1" do - 1.must_equal 2, '' - end - end - def test_flunk util_assert_triggered 'Epic Fail!' do @tc.flunk @@ -1409,6 +1452,36 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase assert_equal "blah2.", @tc.message("") { "blah2" }.call assert_equal "blah1.\nblah2.", @tc.message(:blah1) { "blah2" }.call assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call + + message = proc { "blah1" } + assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call + + message = @tc.message { "blah1" } + assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call + end + + def test_message_message + util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do + @tc.assert_equal 1, 2, message { "whoops" } + end + end + + def test_message_lambda + util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do + @tc.assert_equal 1, 2, lambda { "whoops" } + end + end + + def test_message_deferred + @assertion_count, var = 0, nil + + msg = message { var = "blah" } + + assert_nil var + + msg.call + + assert_equal "blah", var end def test_pass @@ -1455,18 +1528,19 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_refute_in_delta_triggered - util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to not be < 0.1.' do + x = maglev? ? "0.100000xxx" : "0.1" + util_assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1 end end def test_refute_in_epsilon - @tc.refute_in_epsilon 10000, 9990 + @tc.refute_in_epsilon 10000, 9990-1 end def test_refute_in_epsilon_triggered - util_assert_triggered 'Expected |10000 - 9991| (9) to not be < 10.0.' do - @tc.refute_in_epsilon 10000, 9991 + util_assert_triggered 'Expected |10000 - 9990| (10) to not be <= 10.0.' do + @tc.refute_in_epsilon 10000, 9990 fail end end @@ -1613,7 +1687,12 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end srand 42 - expected = %w(test_test2 test_test1 test_test3) + expected = case + when maglev? then + %w(test_test2 test_test3 test_test1) + else + %w(test_test2 test_test1 test_test3) + end assert_equal expected, sample_test_case.test_methods end @@ -1660,6 +1739,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase msg = e.message.sub(/(---Backtrace---).*/m, '\1') msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)') + msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform assert_equal expected, msg end @@ -1703,3 +1783,85 @@ class TestMiniTestGuard < MiniTest::Unit::TestCase assert self.windows? "mswin" end end + +class TestMiniTestUnitRecording < MetaMetaMetaTestCase + # do not parallelize this suite... it just can't handle it. + + def assert_run_record(*expected, &block) + def @tu.record suite, method, assertions, time, error + recording[method] << error + end + + def @tu.recording + @recording ||= Hash.new { |h,k| h[k] = [] } + end + + MiniTest::Unit.runner = @tu + + Class.new MiniTest::Unit::TestCase, &block + + with_output do + @tu.run + end + + recorded = @tu.recording.fetch("test_method").map(&:class) + + assert_equal expected, recorded + end + + def test_record_passing + assert_run_record NilClass do + def test_method + assert true + end + end + end + + def test_record_failing + assert_run_record MiniTest::Assertion do + def test_method + assert false + end + end + end + + def test_record_error + assert_run_record RuntimeError do + def test_method + raise "unhandled exception" + end + end + end + + def test_record_error_teardown + assert_run_record NilClass, RuntimeError do + def test_method + assert true + end + + def teardown + raise "unhandled exception" + end + end + end + + def test_record_error_in_test_and_teardown + assert_run_record AnError, RuntimeError do + def test_method + raise AnError + end + + def teardown + raise "unhandled exception" + end + end + end + + def test_record_skip + assert_run_record MiniTest::Skip do + def test_method + skip "not yet" + end + end + end +end |