summaryrefslogtreecommitdiff
path: root/test/minitest/test_minitest_unit.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/minitest/test_minitest_unit.rb')
-rw-r--r--test/minitest/test_minitest_unit.rb224
1 files changed, 214 insertions, 10 deletions
diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb
index eaf1aa475e..bc4ffac7b7 100644
--- a/test/minitest/test_minitest_unit.rb
+++ b/test/minitest/test_minitest_unit.rb
@@ -51,6 +51,7 @@ Finished tests in 0.00
def teardown
MiniTest::Unit.output = $stdout
+ # HACK for ruby-trunk: MiniTest::Unit.runner = nil
Object.send :remove_const, :ATestCase if defined? ATestCase
end
@@ -254,7 +255,7 @@ RuntimeError: unhandled exception
assert_report expected
end
- def test_run_failing # TODO: add error test
+ def test_run_failing
tc = Class.new(MiniTest::Unit::TestCase) do
def test_something
assert true
@@ -351,6 +352,36 @@ S.
Finished tests in 0.00
+2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
+"
+ assert_report expected
+ end
+
+ def test_run_skip_verbose
+ tc = Class.new(MiniTest::Unit::TestCase) do
+ def test_something
+ assert true
+ end
+
+ def test_skip
+ skip "not yet"
+ end
+ end
+
+ Object.const_set(:ATestCase, tc)
+
+ @tu.run %w[--seed 42 --verbose]
+
+ expected = "Run options: --seed 42 --verbose
+
+# Running tests:
+
+ATestCase#test_skip = 0.00 s = S
+ATestCase#test_something = 0.00 s = .
+
+
+Finished tests in 0.00
+
1) Skipped:
test_skip(ATestCase) [FILE:LINE]:
not yet
@@ -360,6 +391,60 @@ not yet
assert_report expected
end
+ def test_default_runner_is_minitest_unit
+ skip "ruby-trunk won't run with runner code :("
+
+ assert_instance_of MiniTest::Unit, MiniTest::Unit.runner
+ end
+
+ def test_run_with_other_runner
+ skip "ruby-trunk won't run with runner code :("
+
+ runner = Class.new(MiniTest::Unit) do
+ # Run once before each suite
+ def _run_suite(suite, type)
+ begin
+ suite.before_suite
+ super(suite, type)
+ end
+ end
+ end
+
+ tc = Class.new(MiniTest::Unit::TestCase) do
+
+ def self.before_suite
+ MiniTest::Unit.output.puts "Running #{self.name} tests"
+ @@foo = 1
+ end
+
+ def test_something
+ assert_equal 1, @@foo
+ end
+
+ def test_something_else
+ assert_equal 1, @@foo
+ end
+ end
+
+ Object.const_set(:ATestCase, tc)
+ MiniTest::Unit.runner = runner.new
+ @tu.run %w[--seed 42]
+
+ # We should only see 'running ATestCase tests' once
+ expected = "Run options: --seed 42
+
+# Running tests:
+
+Running ATestCase tests
+..
+
+Finished tests in 0.00
+
+2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
+"
+ assert_report expected
+ end
+
def util_expand_bt bt
if RUBY_VERSION =~ /^1\.9/ then
bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
@@ -434,12 +519,115 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
@tc.assert_equal 1, 1
end
- def test_assert_equal_different
- util_assert_triggered "Expected 1, not 2." do
+ def test_assert_equal_different_diff_deactivated
+ without_diff do
+ util_assert_triggered util_msg("haha" * 10, "blah" * 10) do
+ o1 = "haha" * 10
+ o2 = "blah" * 10
+
+ @tc.assert_equal o1, o2
+ end
+ end
+ end
+
+ def test_assert_equal_different_hex
+ c = Class.new do
+ def initialize s; @name = s; end
+ end
+
+ o1 = c.new "a"
+ o2 = c.new "b"
+ msg = "--- expected
+ +++ actual
+ @@ -1 +1 @@
+ -#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">
+ +#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">
+ .".gsub(/^ +/, "")
+
+ util_assert_triggered msg do
+ @tc.assert_equal o1, o2
+ end
+ end
+
+ def test_assert_equal_different_hex_invisible
+ o1 = Object.new
+ o2 = Object.new
+
+ msg = "No visible difference.
+ You should look at your implementation of Object#==.
+ #<Object:0xXXXXXX>.".gsub(/^ +/, "")
+
+ util_assert_triggered msg do
+ @tc.assert_equal o1, o2
+ end
+ end
+
+ def test_assert_equal_different_long
+ msg = "--- expected
+ +++ actual
+ @@ -1 +1 @@
+ -\"hahahahahahahahahahahahahahahahahahahaha\"
+ +\"blahblahblahblahblahblahblahblahblahblah\"
+ .".gsub(/^ +/, "")
+
+ util_assert_triggered msg do
+ o1 = "haha" * 10
+ o2 = "blah" * 10
+
+ @tc.assert_equal o1, o2
+ end
+ end
+
+ def test_assert_equal_different_long_invisible
+ msg = "No visible difference.
+ You should look at your implementation of String#==.
+ \"blahblahblahblahblahblahblahblahblahblah\".".gsub(/^ +/, "")
+
+ util_assert_triggered msg do
+ o1 = "blah" * 10
+ o2 = "blah" * 10
+ def o1.== o
+ false
+ end
+ @tc.assert_equal o1, o2
+ end
+ end
+
+ def test_assert_equal_different_long_msg
+ msg = "message.
+ --- expected
+ +++ actual
+ @@ -1 +1 @@
+ -\"hahahahahahahahahahahahahahahahahahahaha\"
+ +\"blahblahblahblahblahblahblahblahblahblah\"
+ .".gsub(/^ +/, "")
+
+ util_assert_triggered msg do
+ o1 = "haha" * 10
+ o2 = "blah" * 10
+ @tc.assert_equal o1, o2, "message"
+ end
+ end
+
+ def test_assert_equal_different_short
+ util_assert_triggered util_msg(1, 2) do
@tc.assert_equal 1, 2
end
end
+ def test_assert_equal_different_short_msg
+ util_assert_triggered util_msg(1, 2, "message") do
+ @tc.assert_equal 1, 2, "message"
+ end
+ end
+
+ def test_assert_equal_different_short_multiline
+ msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n."
+ util_assert_triggered msg do
+ @tc.assert_equal "a\nb", "a\nc"
+ end
+ end
+
def test_assert_in_delta
@tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
end
@@ -590,7 +778,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
end
def test_assert_output_triggered_both
- util_assert_triggered "In stdout.\nExpected \"yay\", not \"boo\"." do
+ util_assert_triggered util_msg("yay", "boo", "In stdout") do
@tc.assert_output "yay", "blah" do
print "boo"
$stderr.print "blah blah"
@@ -599,7 +787,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
end
def test_assert_output_triggered_err
- util_assert_triggered "In stderr.\nExpected \"blah\", not \"blah blah\"." do
+ util_assert_triggered util_msg("blah", "blah blah", "In stderr") do
@tc.assert_output nil, "blah" do
$stderr.print "blah blah"
end
@@ -607,7 +795,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
end
def test_assert_output_triggered_out
- util_assert_triggered "In stdout.\nExpected \"blah\", not \"blah blah\"." do
+ util_assert_triggered util_msg("blah", "blah blah", "In stdout") do
@tc.assert_output "blah" do
print "blah blah"
end
@@ -786,7 +974,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
def test_assert_silent_triggered_err
@assertion_count = 2
- util_assert_triggered "In stderr.\nExpected \"\", not \"blah blah\"." do
+ util_assert_triggered util_msg("", "blah blah", "In stderr") do
@tc.assert_silent do
$stderr.print "blah blah"
end
@@ -794,7 +982,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
end
def test_assert_silent_triggered_out
- util_assert_triggered "In stdout.\nExpected \"\", not \"blah blah\"." do
+ util_assert_triggered util_msg("", "blah blah", "In stdout") do
@tc.assert_silent do
print "blah blah"
end
@@ -889,8 +1077,9 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
def test_message
@assertion_count = 0
- assert_equal "blah2.", @tc.message { "blah2" }.call
- assert_equal "blah2.", @tc.message("") { "blah2" }.call
+ assert_equal "blah2.", @tc.message { "blah2" }.call
+ 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
end
@@ -1101,4 +1290,19 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
assert_equal expected, msg
end
+
+ def util_msg exp, act, msg = nil
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}."
+ s = "#{msg}.\n#{s}" if msg
+ s
+ end
+
+ def without_diff
+ old_diff = MiniTest::Assertions.diff
+ MiniTest::Assertions.diff = nil
+
+ yield
+ ensure
+ MiniTest::Assertions.diff = old_diff
+ end
end