diff options
author | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-04 21:46:01 +0000 |
---|---|---|
committer | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-04 21:46:01 +0000 |
commit | 6af843b9cb99fb844bf866e71e9ee52be126080f (patch) | |
tree | afee639e216cf32357c2d6e76de49f2549c6d5af /test | |
parent | 95d4b3ba4974fdb9f08900267964949e30b1c821 (diff) |
Imported minitest 2.12.1 (r7323)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/minitest/metametameta.rb | 49 | ||||
-rw-r--r-- | test/minitest/test_minitest_benchmark.rb | 1 | ||||
-rw-r--r-- | test/minitest/test_minitest_mock.rb | 74 | ||||
-rw-r--r-- | test/minitest/test_minitest_spec.rb | 495 | ||||
-rw-r--r-- | test/minitest/test_minitest_unit.rb | 311 |
5 files changed, 740 insertions, 190 deletions
diff --git a/test/minitest/metametameta.rb b/test/minitest/metametameta.rb new file mode 100644 index 0000000000..4352cfee82 --- /dev/null +++ b/test/minitest/metametameta.rb @@ -0,0 +1,49 @@ +# encoding: utf-8 +###################################################################### +# This file is imported from the minitest project. +# DO NOT make modifications in this repo. They _will_ be reverted! +# File a patch instead and assign it to Ryan Davis. +###################################################################### + +require 'tempfile' +require 'stringio' +require 'minitest/autorun' + +class MetaMetaMetaTestCase < MiniTest::Unit::TestCase + def assert_report expected = nil + expected ||= <<-EOM.gsub(/^ {6}/, '') + Run options: --seed 42 + + # Running tests: + + . + + Finished tests in 0.00 + + 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips + EOM + + output = @output.string.dup + output.sub!(/Finished tests in .*/, "Finished tests in 0.00") + output.sub!(/Loaded suite .*/, 'Loaded suite blah') + output.gsub!(/\[[^\]:]+:\d+\]/, '[FILE:LINE]') + output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in') + assert_equal(expected, output) + end + + def setup + super + srand 42 + MiniTest::Unit::TestCase.reset + @tu = MiniTest::Unit.new + @output = StringIO.new("") + MiniTest::Unit.runner = nil # protect the outer runner from the inner tests + MiniTest::Unit.output = @output + end + + def teardown + super + MiniTest::Unit.output = $stdout + Object.send :remove_const, :ATestCase if defined? ATestCase + end +end diff --git a/test/minitest/test_minitest_benchmark.rb b/test/minitest/test_minitest_benchmark.rb index cdd7c3c640..fc1301e63b 100644 --- a/test/minitest/test_minitest_benchmark.rb +++ b/test/minitest/test_minitest_benchmark.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! diff --git a/test/minitest/test_minitest_mock.rb b/test/minitest/test_minitest_mock.rb index e8954621ec..02321e0f97 100644 --- a/test/minitest/test_minitest_mock.rb +++ b/test/minitest/test_minitest_mock.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! @@ -26,7 +27,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase def test_blow_up_if_not_called @mock.foo - util_verify_bad + util_verify_bad "expected meaning_of_life() => 42, got []" end def test_not_blow_up_if_everything_called @@ -46,7 +47,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase @mock.meaning_of_life @mock.expect(:bar, true) - util_verify_bad + util_verify_bad "expected bar() => true, got []" end def test_blow_up_on_wrong_number_of_arguments @@ -84,9 +85,20 @@ class TestMiniTestMock < MiniTest::Unit::TestCase @mock.meaning_of_life @mock.expect(:sum, 3, [1, 2]) - @mock.sum(2, 4) + e = assert_raises MockExpectationError do + @mock.sum(2, 4) + end + + exp = "mocked method :sum called with unexpected arguments [2, 4]" + assert_equal exp, e.message + end + + def test_expect_with_non_array_args + e = assert_raises ArgumentError do + @mock.expect :blah, 3, false + end - util_verify_bad + assert_equal "args must be an array", e.message end def test_respond_appropriately @@ -142,29 +154,59 @@ class TestMiniTestMock < MiniTest::Unit::TestCase def test_verify_raises_with_strict_args mock = MiniTest::Mock.new mock.expect :strict_expectation, true, [2] - mock.strict_expectation 1 - util_verify_bad + e = assert_raises MockExpectationError do + mock.strict_expectation 1 + end + + exp = "mocked method :strict_expectation called with unexpected arguments [1]" + assert_equal exp, e.message end - def test_verify_shows_the_actual_arguments_in_the_message + def test_method_missing_empty mock = MiniTest::Mock.new - mock.expect :capitalized, true, ["a"] - mock.capitalized "b" + + mock.expect :a, nil + + mock.a + e = assert_raises MockExpectationError do - mock.verify + mock.a end - a = {:retval=>true, :args=>["a"]} - b = {:retval=>true, :args=>["b"]} + assert_equal "No more expects available for :a: []", e.message + end + + def test_same_method_expects_are_verified_when_all_called + mock = MiniTest::Mock.new + mock.expect :foo, nil, [:bar] + mock.expect :foo, nil, [:baz] + + mock.foo :bar + mock.foo :baz - expected = "expected capitalized, #{a.inspect}, got [#{b.inspect}]" - assert_equal expected, e.message + assert mock.verify + end + + def test_same_method_expects_blow_up_when_not_all_called + mock = MiniTest::Mock.new + mock.expect :foo, nil, [:bar] + mock.expect :foo, nil, [:baz] + + mock.foo :bar + + e = assert_raises(MockExpectationError) { mock.verify } + + exp = "expected foo(:baz) => nil, got [foo(:bar) => nil]" + + assert_equal exp, e.message end - def util_verify_bad - assert_raises MockExpectationError do + def util_verify_bad exp + e = assert_raises MockExpectationError do @mock.verify end + + assert_equal exp, e.message end end diff --git a/test/minitest/test_minitest_spec.rb b/test/minitest/test_minitest_spec.rb index 27e3dc7628..68c4dec003 100644 --- a/test/minitest/test_minitest_spec.rb +++ b/test/minitest/test_minitest_spec.rb @@ -1,9 +1,11 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! # File a patch instead and assign it to Ryan Davis. ###################################################################### +# encoding: utf-8 require 'minitest/autorun' require 'stringio' @@ -13,6 +15,19 @@ class ExampleA; end class ExampleB < ExampleA; end describe MiniTest::Spec do + def assert_triggered expected = "blah", klass = MiniTest::Assertion + @assertion_count += 2 + + 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 + before do @assertion_count = 4 end @@ -21,8 +36,78 @@ describe MiniTest::Spec do self._assertions.must_equal @assertion_count end - # TODO: figure out how the hell to write a test for this - # it "will skip if there is no block" + it "needs to be able to catch a MiniTest::Assertion exception" do + @assertion_count = 1 + + assert_triggered "Expected 1 to not be equal to 1." do + 1.wont_equal 1 + end + end + + it "needs to be sensible about must_include order" do + @assertion_count += 3 # must_include is 2 assertions + + [1, 2, 3].must_include(2).must_equal true + + assert_triggered "Expected [1, 2, 3] to include 5." do + [1, 2, 3].must_include 5 + end + + assert_triggered "msg.\nExpected [1, 2, 3] to include 5." do + [1, 2, 3].must_include 5, "msg" + end + end + + it "needs to be sensible about wont_include order" do + @assertion_count += 3 # wont_include is 2 assertions + + [1, 2, 3].wont_include(5).must_equal false + + assert_triggered "Expected [1, 2, 3] to not include 2." do + [1, 2, 3].wont_include 2 + end + + assert_triggered "msg.\nExpected [1, 2, 3] to not include 2." do + [1, 2, 3].wont_include 2, "msg" + end + end + + it "needs to catch an expected exception" do + @assertion_count = 2 + + proc { raise "blah" }.must_raise RuntimeError + proc { raise MiniTest::Assertion }.must_raise MiniTest::Assertion + end + + it "needs to catch an unexpected exception" do + @assertion_count -= 2 # no positive + + msg = <<-EOM.gsub(/^ {6}/, '').chomp + [RuntimeError] exception expected, not + Class: <MiniTest::Assertion> + Message: <\"MiniTest::Assertion\"> + ---Backtrace--- + EOM + + assert_triggered msg do + proc { raise MiniTest::Assertion }.must_raise RuntimeError + end + + assert_triggered "msg.\n#{msg}" do + proc { raise MiniTest::Assertion }.must_raise RuntimeError, "msg" + end + end + + it "needs to ensure silence" do + @assertion_count -= 1 # no msg + @assertion_count += 2 # assert_output is 2 assertions + + proc { }.must_be_silent.must_equal true + + assert_triggered "In stdout.\nExpected: \"\"\n Actual: \"xxx\"" do + proc { print "xxx" }.must_be_silent + end + end it "needs to have all methods named well" do @assertion_count = 2 @@ -60,159 +145,379 @@ describe MiniTest::Spec do wonts.must_equal expected_wonts end + it "needs to raise if an expected exception is not raised" do + @assertion_count -= 2 # no positive test + + assert_triggered "RuntimeError expected but nothing was raised." do + proc { 42 }.must_raise RuntimeError + end + + assert_triggered "msg.\nRuntimeError expected but nothing was raised." do + proc { 42 }.must_raise RuntimeError, "msg" + end + end + + it "needs to verify binary messages" do + 42.wont_be(:<, 24).must_equal false + + assert_triggered 'Expected 24 to not be < 42.' do + 24.wont_be :<, 42 + end + + assert_triggered "msg.\nExpected 24 to not be < 42." do + 24.wont_be :<, 42, "msg" + end + end + + it "needs to verify emptyness" do + @assertion_count += 3 # empty is 2 assertions + + [].must_be_empty.must_equal true + + assert_triggered "Expected [42] to be empty." do + [42].must_be_empty + end + + assert_triggered "msg.\nExpected [42] to be empty." do + [42].must_be_empty "msg" + end + end + it "needs to verify equality" do (6 * 7).must_equal(42).must_equal true - proc { (6 * 9).must_equal(42) }.must_raise MiniTest::Assertion + + assert_triggered "Expected: 42\n Actual: 54" do + (6 * 9).must_equal 42 + end + + assert_triggered "msg.\nExpected: 42\n Actual: 54" do + (6 * 9).must_equal 42, "msg" + end + end + + it "needs to verify floats outside a delta" do + @assertion_count += 1 # extra test + + 24.wont_be_close_to(42).must_equal false + + 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 + (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 + (6 * 7.0).wont_be_close_to 42, 0.00001, "msg" + end + end + + it "needs to verify floats outside an epsilon" do + @assertion_count += 1 # extra test + + 24.wont_be_within_epsilon(42).must_equal false + + assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.042.' do + (6 * 7.0).wont_be_within_epsilon 42 + end + + assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.00042.' 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 + (6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg" + end end it "needs to verify floats within a delta" do + @assertion_count += 1 # extra test + (6.0 * 7).must_be_close_to(42.0).must_equal true - proc { 42.002.must_be_close_to 42.0 }.must_raise MiniTest::Assertion + + 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 + (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 + (1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg" + end end - it "needs to verify types of objects" do - (6 * 7).must_be_instance_of(Fixnum).must_equal true - proc { (6 * 7).must_be_instance_of String }.must_raise MiniTest::Assertion + it "needs to verify floats within an epsilon" do + @assertion_count += 1 # extra test + + (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 + (1.0 / 100).must_be_within_epsilon 0.0 + end + + 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 + (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg" + end + end + + it "needs to verify identity" do + 1.must_be_same_as(1).must_equal true + + assert_triggered "Expected 1 (oid=N) to be the same as 2 (oid=N)." do + 1.must_be_same_as 2 + end + + assert_triggered "msg.\nExpected 1 (oid=N) to be the same as 2 (oid=N)." do + 1.must_be_same_as 2, "msg" + end + end + + it "needs to verify inequality" do + 42.wont_equal(6 * 9).must_equal false + + assert_triggered "Expected 1 to not be equal to 1." do + 1.wont_equal 1 + end + + assert_triggered "msg.\nExpected 1 to not be equal to 1." do + 1.wont_equal 1, "msg" + end + end + + 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 + 42.wont_be_instance_of Fixnum + end + + assert_triggered "msg.\nExpected 42 to not be an instance of Fixnum." do + 42.wont_be_instance_of Fixnum, "msg" + end + end + + 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 + 42.wont_be_kind_of Integer + end + + assert_triggered "msg.\nExpected 42 to not be a kind of Integer." do + 42.wont_be_kind_of Integer, "msg" + end end it "needs to verify kinds of objects" do - @assertion_count = 6 + @assertion_count += 2 # extra test (6 * 7).must_be_kind_of(Fixnum).must_equal true (6 * 7).must_be_kind_of(Numeric).must_equal true - proc { (6 * 7).must_be_kind_of String }.must_raise MiniTest::Assertion + + assert_triggered "Expected 42 to be a kind of String, not Fixnum." do + (6 * 7).must_be_kind_of String + end + + assert_triggered "msg.\nExpected 42 to be a kind of String, not Fixnum." do + (6 * 7).must_be_kind_of String, "msg" + end end - it "needs to verify regexp matches" do - @assertion_count = 6 + it "needs to verify mismatch" do + @assertion_count += 3 # match is 2 - "blah".must_match(/\w+/).must_equal true - proc { "blah".must_match(/\d+/) }.must_raise MiniTest::Assertion + "blah".wont_match(/\d+/).must_equal false + + assert_triggered "Expected /\\w+/ to not match \"blah\"." do + "blah".wont_match(/\w+/) + end + + assert_triggered "msg.\nExpected /\\w+/ to not match \"blah\"." do + "blah".wont_match(/\w+/, "msg") + end end it "needs to verify nil" do nil.must_be_nil.must_equal true - proc { 42.must_be_nil }.must_raise MiniTest::Assertion - end - it "needs to verify using any binary operator" do - 41.must_be(:<, 42).must_equal true - proc { 42.must_be(:<, 41) }.must_raise MiniTest::Assertion + assert_triggered "Expected 42 to be nil." do + 42.must_be_nil + end + + assert_triggered "msg.\nExpected 42 to be nil." do + 42.must_be_nil "msg" + end end - it "needs to verify using any predicate" do - "".must_be(:empty?).must_equal true - proc { "blah".must_be(:empty?) }.must_raise MiniTest::Assertion + it "needs to verify non-emptyness" do + @assertion_count += 3 # empty is 2 assertions + + ['some item'].wont_be_empty.must_equal false + + assert_triggered "Expected [] to not be empty." do + [].wont_be_empty + end + + assert_triggered "msg.\nExpected [] to not be empty." do + [].wont_be_empty "msg" + end end - it "needs to catch an expected exception" do - @assertion_count = 2 + it "needs to verify non-identity" do + 1.wont_be_same_as(2).must_equal false - proc { raise "blah" }.must_raise RuntimeError - proc { raise MiniTest::Assertion }.must_raise MiniTest::Assertion + assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do + 1.wont_be_same_as 1 + end + + assert_triggered "msg.\nExpected 1 (oid=N) to not be the same as 1 (oid=N)." do + 1.wont_be_same_as 1, "msg" + end end - it "needs to catch an unexpected exception" do - @assertion_count = 2 + it "needs to verify non-nil" do + 42.wont_be_nil.must_equal false + + assert_triggered "Expected nil to not be nil." do + nil.wont_be_nil + end - proc { - proc { raise MiniTest::Assertion }.must_raise(RuntimeError) - }.must_raise MiniTest::Assertion + assert_triggered "msg.\nExpected nil to not be nil." do + nil.wont_be_nil "msg" + end end - it "needs raise if an expected exception is not raised" do - @assertion_count = 2 + 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 + "".wont_respond_to :to_s + end - proc { proc { 42 }.must_raise(RuntimeError) }.must_raise MiniTest::Assertion + assert_triggered "msg.\nExpected \"\" to not respond to to_s." do + "".wont_respond_to :to_s, "msg" + end end - it "needs to be able to catch a MiniTest::Assertion exception" do - @assertion_count = 2 + it "needs to verify output in stderr" do + @assertion_count -= 1 # no msg + + proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true - proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion + assert_triggered "In stderr.\nExpected: \"blah\"\n Actual: \"xxx\"" do + proc { $stderr.print "xxx" }.must_output(nil, "blah") + end end - it "needs to verify using respond_to" do - 42.must_respond_to(:+).must_equal true - proc { 42.must_respond_to(:clear) }.must_raise MiniTest::Assertion + it "needs to verify output in stdout" do + @assertion_count -= 1 # no msg + + proc { print "blah" }.must_output("blah").must_equal true + + assert_triggered "In stdout.\nExpected: \"blah\"\n Actual: \"xxx\"" do + proc { print "xxx" }.must_output("blah") + end end - it "needs to verify identity" do - 1.must_be_same_as(1).must_equal true - proc { 1.must_be_same_as 2 }.must_raise MiniTest::Assertion + it "needs to verify regexp matches" do + @assertion_count += 3 # must_match is 2 assertions + + "blah".must_match(/\w+/).must_equal true + + assert_triggered "Expected /\\d+/ to match \"blah\"." do + "blah".must_match(/\d+/) + end + + assert_triggered "msg.\nExpected /\\d+/ to match \"blah\"." do + "blah".must_match(/\d+/, "msg") + end end it "needs to verify throw" do - @assertion_count = 6 + @assertion_count += 2 # 2 extra tests proc { throw :blah }.must_throw(:blah).must_equal true - proc { proc { }.must_throw(:blah) }.must_raise MiniTest::Assertion - proc { proc { throw :xxx }.must_throw(:blah) }.must_raise MiniTest::Assertion - end - it "needs to verify inequality" do - 42.wont_equal(6 * 9).must_equal false - proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion + assert_triggered "Expected :blah to have been thrown." do + proc { }.must_throw :blah + end + + assert_triggered "Expected :blah to have been thrown, not :xxx." do + proc { throw :xxx }.must_throw :blah + end + + assert_triggered "msg.\nExpected :blah to have been thrown." do + proc { }.must_throw :blah, "msg" + end + + assert_triggered "msg.\nExpected :blah to have been thrown, not :xxx." do + proc { throw :xxx }.must_throw :blah, "msg" + end end - it "needs to verify mismatch" do - @assertion_count = 6 - "blah".wont_match(/\d+/).must_equal false - proc { "blah".wont_match(/\w+/) }.must_raise MiniTest::Assertion + it "needs to verify types of objects" do + (6 * 7).must_be_instance_of(Fixnum).must_equal true + + exp = "Expected 42 to be an instance of String, not Fixnum." + + assert_triggered exp do + (6 * 7).must_be_instance_of String + end + + assert_triggered "msg.\n#{exp}" do + (6 * 7).must_be_instance_of String, "msg" + end end it "needs to verify using any (negative) predicate" do + @assertion_count -= 1 # doesn't take a message + "blah".wont_be(:empty?).must_equal false - proc { "".wont_be(:empty?) }.must_raise MiniTest::Assertion - end - it "needs to verify non-nil" do - 42.wont_be_nil.must_equal false - proc { nil.wont_be_nil }.must_raise MiniTest::Assertion + assert_triggered "Expected \"\" to not be empty?." do + "".wont_be :empty? + end end - it "needs to verify non-identity" do - 1.wont_be_same_as(2).must_equal false - proc { 1.wont_be_same_as 1 }.must_raise MiniTest::Assertion - end + it "needs to verify using any binary operator" do + @assertion_count -= 1 # no msg - it "needs to verify output in stdout" do - proc { print "blah" }.must_output("blah").must_equal true + 41.must_be(:<, 42).must_equal true - proc { - proc { print "xxx" }.must_output("blah") - }.must_raise MiniTest::Assertion + assert_triggered "Expected 42 to be < 41." do + 42.must_be(:<, 41) + end end - it "needs to verify output in stderr" do - proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true + it "needs to verify using any predicate" do + @assertion_count -= 1 # no msg - proc { - proc { $stderr.print "xxx" }.must_output(nil, "blah") - }.must_raise MiniTest::Assertion - end + "".must_be(:empty?).must_equal true - it "needs to ensure silence" do - @assertion_count = 5 + assert_triggered "Expected \"blah\" to be empty?." do + "blah".must_be :empty? + end + end - proc { }.must_be_silent.must_equal true + it "needs to verify using respond_to" do + 42.must_respond_to(:+).must_equal true - proc { - proc { print "xxx" }.must_be_silent - }.must_raise MiniTest::Assertion - end + assert_triggered "Expected 42 (Fixnum) to respond to #clear." do + 42.must_respond_to :clear + end - it "needs to be sensible about must_include order" do - @assertion_count = 6 - [1, 2, 3].must_include(2).must_equal true - proc { [1, 2, 3].must_include 5 }.must_raise MiniTest::Assertion + assert_triggered "msg.\nExpected 42 (Fixnum) to respond to #clear." do + 42.must_respond_to :clear, "msg" + end end - it "needs to be sensible about wont_include order" do - @assertion_count = 6 - [1, 2, 3].wont_include(5).must_equal false - proc { [1, 2, 3].wont_include 2 }.must_raise MiniTest::Assertion - end end describe MiniTest::Spec, :let do @@ -381,17 +686,21 @@ class TestMeta < MiniTest::Unit::TestCase end def test_describe_first_structure - x = y = z = nil + x = x1 = x2 = y = z = nil x = describe "top-level thingy" do y = describe "first thingy" do end - it "top-level-it" do end + x1 = it "top-level-it" do end + x2 = it "не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world" do end z = describe "second thingy" do end end - assert_equal ['test_0001_top_level_it'], - x.instance_methods.grep(/^test/).map {|o| o.to_s} + test_methods = ['test_0001_top_level_it', 'test_0002_не_латинские_буквы_и_спецсимволы_いった_α_β_γ_δ_ε_hello_world'].sort + + assert_equal test_methods, [x1, x2] + assert_equal test_methods, + x.instance_methods.grep(/^test/).map {|o| o.to_s}.sort assert_equal [], y.instance_methods.grep(/^test/) assert_equal [], z.instance_methods.grep(/^test/) end diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb index 7db7b3ce30..935b0e274a 100644 --- a/test/minitest/test_minitest_unit.rb +++ b/test/minitest/test_minitest_unit.rb @@ -1,18 +1,18 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! # File a patch instead and assign it to Ryan Davis. ###################################################################### -require 'stringio' require 'pathname' -require 'minitest/autorun' +require 'test/minitest/metametameta' module MyModule; end class AnError < StandardError; include MyModule; end class ImmutableString < String; def inspect; super.freeze; end; end -class TestMiniTestUnit < MiniTest::Unit::TestCase +class TestMiniTestUnit < MetaMetaMetaTestCase 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,38 +22,6 @@ class TestMiniTestUnit < MiniTest::Unit::TestCase "#{MINITEST_BASE_DIR}/test.rb:139:in `run'", "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"] - def assert_report expected = nil - expected ||= "Run options: --seed 42 - -# Running tests: - -. - -Finished tests in 0.00 - -1 tests, 1 assertions, 0 failures, 0 errors, 0 skips -" - output = @output.string.sub(/Finished tests in .*/, "Finished tests in 0.00") - output.sub!(/Loaded suite .*/, 'Loaded suite blah') - output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:') - output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]') - assert_equal(expected, output) - end - - def setup - srand 42 - MiniTest::Unit::TestCase.reset - @tu = MiniTest::Unit.new - @output = StringIO.new("") - MiniTest::Unit.runner = nil # protect the outer runner from the inner tests - MiniTest::Unit.output = @output - end - - def teardown - MiniTest::Unit.output = $stdout - Object.send :remove_const, :ATestCase if defined? ATestCase - end - def test_class_puke_with_assertion_failed exception = MiniTest::Assertion.new "Oh no!" exception.set_backtrace ["unhappy"] @@ -235,21 +203,23 @@ Finished tests in 0.00 @tu.run %w[--seed 42] - expected = "Run options: --seed 42 + expected = <<-EOM.gsub(/^ {6}/, '') + Run options: --seed 42 -# Running tests: + # Running tests: -E. + E. -Finished tests in 0.00 + Finished tests in 0.00 - 1) Error: -test_error(ATestCase): -RuntimeError: unhandled exception - FILE:LINE:in `test_error' + 1) Error: + test_error(ATestCase): + RuntimeError: unhandled exception + FILE:LINE:in `test_error' + + 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips + EOM -2 tests, 1 assertions, 0 failures, 1 errors, 0 skips -" assert_report expected end @@ -506,10 +476,82 @@ Finished tests in 0.00 end end + def test_before_setup + call_order = [] + Class.new(MiniTest::Unit::TestCase) do + define_method :setup do + super() + call_order << :setup + end + + define_method :before_setup do + call_order << :before_setup + end + + def test_omg; assert true; end + end + + @tu.run %w[--seed 42] + + expected = [:before_setup, :setup] + assert_equal expected, call_order + end + + def test_after_teardown + call_order = [] + Class.new(MiniTest::Unit::TestCase) do + define_method :teardown do + super() + call_order << :teardown + end + + define_method :after_teardown do + call_order << :after_teardown + end + + def test_omg; assert true; end + end + + @tu.run %w[--seed 42] + + expected = [:teardown, :after_teardown] + assert_equal expected, call_order + end + + def test_all_teardowns_are_guaranteed_to_run + call_order = [] + Class.new(MiniTest::Unit::TestCase) do + define_method :after_teardown do + super() + call_order << :after_teardown + raise + end + + define_method :teardown do + super() + call_order << :teardown + raise + end + + define_method :before_teardown do + super() + call_order << :before_teardown + raise + end + + def test_omg; assert true; end + end + + @tu.run %w[--seed 42] + + expected = [:before_teardown, :teardown, :after_teardown] + assert_equal expected, call_order + end + def test_setup_hooks call_order = [] - tc = Class.new(MiniTest::Unit::TestCase) do + tc = Class.new(MiniTest::Spec) do define_method :setup do super() call_order << :method @@ -546,7 +588,7 @@ Finished tests in 0.00 def test_teardown_hooks call_order = [] - tc = Class.new(MiniTest::Unit::TestCase) do + tc = Class.new(MiniTest::Spec) do define_method :teardown do super() call_order << :method @@ -583,7 +625,7 @@ Finished tests in 0.00 def test_setup_and_teardown_hooks_survive_inheritance call_order = [] - parent = Class.new(MiniTest::Unit::TestCase) do + parent = Class.new(MiniTest::Spec) do define_method :setup do super() call_order << :setup_method @@ -626,7 +668,11 @@ Finished tests in 0.00 end class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase + RUBY18 = ! defined? Encoding + def setup + super + MiniTest::Unit::TestCase.reset @tc = MiniTest::Unit::TestCase.new 'fake tc' @@ -659,15 +705,22 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_block - @tc.assert_block do - true + exp = ["NOTE: MiniTest::Unit::TestCase#assert_block is deprecated,", + "use assert. It will be removed on or after 2012-06-01.\n"].join " " + + assert_output "", exp do + @tc.assert_block do + true + end end end def test_assert_block_triggered - util_assert_triggered "blah.\nExpected block to return true value." do - @tc.assert_block "blah" do - false + assert_output do + util_assert_triggered "blah.\nExpected block to return true value." do + @tc.assert_block "blah" do + false + end end end end @@ -804,13 +857,13 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_in_delta_triggered - util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to be < 1.0e-06.' do + util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 1.0e-06.' do @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001 end end def test_assert_in_epsilon - @assertion_count = 8 + @assertion_count = 10 @tc.assert_in_epsilon 10000, 9991 @tc.assert_in_epsilon 9991, 10000 @@ -821,14 +874,24 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_in_epsilon 9999.1, 10000, 0.0001 @tc.assert_in_epsilon 1.0, 1.0001, 0.0001 @tc.assert_in_epsilon 1.0001, 1.0, 0.0001 + + @tc.assert_in_epsilon(-1, -1) + @tc.assert_in_epsilon(-10000, -9991) 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 + @tc.assert_in_epsilon(-1.1, -1, 0.1) + end + end + def test_assert_includes @assertion_count = 2 @@ -871,7 +934,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_match(/\w+/, "blah blah blah") end - def test_assert_match_object + def test_assert_match_matcher_object @assertion_count = 2 pattern = Object.new @@ -880,6 +943,15 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_match pattern, 5 end + def test_assert_match_matchee_to_str + @assertion_count = 2 + + obj = Object.new + def obj.to_str; "blah" end + + @tc.assert_match "blah", obj + end + def test_assert_match_object_triggered @assertion_count = 2 @@ -956,7 +1028,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_output_triggered_both - util_assert_triggered util_msg("yay", "boo", "In stdout") do + util_assert_triggered util_msg("blah", "blah blah", "In stderr") do @tc.assert_output "yay", "blah" do print "boo" $stderr.print "blah blah" @@ -980,12 +1052,28 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end end + def test_assert_predicate + @tc.assert_predicate "", :empty? + end + + def test_assert_predicate_triggered + util_assert_triggered 'Expected "blah" to be empty?.' do + @tc.assert_predicate "blah", :empty? + end + end + def test_assert_raises @tc.assert_raises RuntimeError do raise "blah" end end + def test_assert_raises_module + @tc.assert_raises MyModule do + raise AnError + end + end + ## # *sigh* This is quite an odd scenario, but it is from real (albeit # ugly) test code in ruby-core: @@ -1006,12 +1094,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end end - def test_assert_raises_module - @tc.assert_raises MyModule do - raise AnError - end - end - def test_assert_raises_triggered_different e = assert_raises MiniTest::Assertion do @tc.assert_raises RuntimeError do @@ -1039,13 +1121,15 @@ FILE:LINE:in `test_assert_raises_triggered_different' end end - expected = "XXX -[RuntimeError] exception expected, not -Class: <SyntaxError> -Message: <\"icky\"> ----Backtrace--- -FILE:LINE:in `test_assert_raises_triggered_different_msg' ----------------" + expected = <<-EOM.gsub(/^ {6}/, '').chomp + XXX. + [RuntimeError] exception expected, not + Class: <SyntaxError> + Message: <\"icky\"> + ---Backtrace--- + FILE:LINE:in `test_assert_raises_triggered_different_msg' + --------------- + EOM actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE') actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0' @@ -1072,7 +1156,7 @@ FILE:LINE:in `test_assert_raises_triggered_different_msg' end end - expected = "XXX\nMiniTest::Assertion expected but nothing was raised." + expected = "XXX.\nMiniTest::Assertion expected but nothing was raised." assert_equal expected, e.message end @@ -1150,8 +1234,6 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_assert_silent_triggered_err - @assertion_count = 2 - util_assert_triggered util_msg("", "blah blah", "In stderr") do @tc.assert_silent do $stderr.print "blah blah" @@ -1160,6 +1242,8 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_assert_silent_triggered_out + @assertion_count = 2 + util_assert_triggered util_msg("", "blah blah", "In stdout") do @tc.assert_silent do print "blah blah" @@ -1211,10 +1295,15 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' methods = MiniTest::Assertions.public_instance_methods methods.map! { |m| m.to_s } if Symbol === methods.first - ignores = %w(assert_block assert_no_match assert_not_equal - assert_not_nil assert_not_same assert_nothing_raised - assert_nothing_thrown assert_output assert_raise - assert_raises assert_send assert_silent assert_throws) + # 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 + assert_silent assert_throws] + + # These are test/unit methods. I'm not actually sure why they're still here + ignores += %w[assert_no_match assert_not_equal assert_not_nil + assert_not_same assert_nothing_raised + assert_nothing_thrown assert_raise] asserts = methods.grep(/^assert/).sort - ignores refutes = methods.grep(/^refute/).sort - ignores @@ -1252,6 +1341,12 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' 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 @@ -1317,7 +1412,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_refute_in_delta_triggered - util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to not be < 0.1.' do + util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to not be < 0.1.' do @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1 end end @@ -1327,7 +1422,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_refute_in_epsilon_triggered - util_assert_triggered 'Expected 10000 - 9991 (9) to not be < 10.0.' do + util_assert_triggered 'Expected |10000 - 9991| (9) to not be < 10.0.' do @tc.refute_in_epsilon 10000, 9991 fail end @@ -1375,7 +1470,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' @tc.refute_match(/\d+/, "blah blah blah") end - def test_refute_match_object + def test_refute_match_matcher_object @assertion_count = 2 @tc.refute_match Object.new, 5 # default #=~ returns false end @@ -1409,6 +1504,16 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end end + def test_refute_predicate + @tc.refute_predicate "42", :empty? + end + + def test_refute_predicate_triggered + util_assert_triggered 'Expected "" to not be empty?.' do + @tc.refute_predicate "", :empty? + end + end + def test_refute_operator @tc.refute_operator 2, :<, 1 end @@ -1482,6 +1587,28 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' assert_equal expected, sample_test_case.test_methods end + def test_i_suck_and_my_tests_are_order_dependent_bang_sets_test_order_alpha + @assertion_count = 0 + + shitty_test_case = Class.new MiniTest::Unit::TestCase + + shitty_test_case.i_suck_and_my_tests_are_order_dependent! + + assert_equal :alpha, shitty_test_case.test_order + end + + def test_i_suck_and_my_tests_are_order_dependent_bang_does_not_warn + @assertion_count = 0 + + shitty_test_case = Class.new MiniTest::Unit::TestCase + + def shitty_test_case.test_order ; :lol end + + assert_silent do + shitty_test_case.i_suck_and_my_tests_are_order_dependent! + end + end + def util_assert_triggered expected, klass = MiniTest::Assertion e = assert_raises(klass) do yield @@ -1508,3 +1635,25 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' MiniTest::Assertions.diff = old_diff end end + +class TestMiniTestGuard < MiniTest::Unit::TestCase + def test_mri_eh + assert self.class.mri? "ruby blah" + assert self.mri? "ruby blah" + end + + def test_jruby_eh + assert self.class.jruby? "java" + assert self.jruby? "java" + end + + def test_rubinius_eh + assert self.class.rubinius? "rbx" + assert self.rubinius? "rbx" + end + + def test_windows_eh + assert self.class.windows? "mswin" + assert self.windows? "mswin" + end +end |