diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/testunit/test_assertions.rb | 8 | ||||
-rw-r--r-- | test/testunit/test_error.rb | 32 | ||||
-rw-r--r-- | test/testunit/test_failure.rb | 33 | ||||
-rw-r--r-- | test/testunit/test_testcase.rb | 42 | ||||
-rw-r--r-- | test/testunit/util/test_backtracefilter.rb | 38 |
5 files changed, 128 insertions, 25 deletions
diff --git a/test/testunit/test_assertions.rb b/test/testunit/test_assertions.rb index ec4b2c0c13..b09986b16f 100644 --- a/test/testunit/test_assertions.rb +++ b/test/testunit/test_assertions.rb @@ -2,7 +2,7 @@ # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. # License:: Ruby license. -require 'test/unit/testcase' +require 'test/unit' module Test module Unit @@ -66,7 +66,7 @@ module Test check_nothing_fails { assert_block("successful assert_block") {true} } - check_fails { + check_fails("assert_block failed") { assert_block {false} } check_fails("failed assert_block") { @@ -81,7 +81,7 @@ module Test check_nothing_fails { assert(true, "successful assert") } - check_fails { + check_fails("assert failed") { assert(false) } check_fails("failed assert") { @@ -291,7 +291,7 @@ module Test end def test_flunk - check_fails { + check_fails("Assertion flunked") { flunk } check_fails("flunk message") { diff --git a/test/testunit/test_error.rb b/test/testunit/test_error.rb index c95bef38c4..56b275b362 100644 --- a/test/testunit/test_error.rb +++ b/test/testunit/test_error.rb @@ -2,28 +2,24 @@ # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. # License:: Ruby license. -require 'test/unit/error' +require 'test/unit' module Test module Unit class TC_Error < TestCase - def setup - @old_load_path = $:.dup - $:.replace(['C:\some\old\path']) - end - - def test_backtrace_filtering - backtrace = [%q{tc_thing.rb:4:in '/'}] - - backtrace.concat([%q{tc_thing.rb:4:in 'test_stuff'}, - %q{C:\some\old\path/test/unit/testcase.rb:44:in 'send'}, - %q{C:\some\old\path\test\unit\testcase.rb:44:in 'run'}, - %q{tc_thing.rb:3}]) - assert_equal([backtrace[0..1], backtrace[-1]].flatten, Error.filter(backtrace), "Should filter out all TestUnit-specific lines") - end - - def teardown - $:.replace(@old_load_path) + TF_Exception = Struct.new('TF_Exception', :message, :backtrace) + def test_display + ex = TF_Exception.new("message1\nmessage2", ['line1', 'line2']) + e = Error.new("name", ex) + assert_equal("name: #{TF_Exception.name}: message1", e.short_display) + assert_equal(<<EOM.strip, e.long_display) +Error: +name: +Struct::TF_Exception: message1 +message2 + line1 + line2 +EOM end end end diff --git a/test/testunit/test_failure.rb b/test/testunit/test_failure.rb new file mode 100644 index 0000000000..164f942476 --- /dev/null +++ b/test/testunit/test_failure.rb @@ -0,0 +1,33 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2003 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit' +require 'test/unit/failure' + +module Test::Unit + class TestFailure < TestCase + def test_display + f = Failure.new("name", [%q{location:1 in 'l'}], "message1\nmessage2") + assert_equal("name: message1", f.short_display) + assert_equal(<<EOM.strip, f.long_display) +Failure: +name [location:1]: +message1 +message2 +EOM + + f = Failure.new("name", [%q{location1:2 in 'l1'}, 'location2:1', %q{location3:3 in 'l3'}], "message1\nmessage2") + assert_equal("name: message1", f.short_display) + assert_equal(<<EOM.strip, f.long_display) +Failure: +name + [location1:2 in 'l1' + location2:1 + location3:3 in 'l3']: +message1 +message2 +EOM + end + end +end diff --git a/test/testunit/test_testcase.rb b/test/testunit/test_testcase.rb index c087712004..2934a92fef 100644 --- a/test/testunit/test_testcase.rb +++ b/test/testunit/test_testcase.rb @@ -2,7 +2,7 @@ # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. # License:: Ruby license. -require 'test/unit/testcase' +require 'test/unit' module Test module Unit @@ -36,6 +36,12 @@ module Test def test_error 1 / 0 end + def test_nested_failure + nested + end + def nested + assert_block("nested"){false} + end def return_passed? return passed? end @@ -55,7 +61,13 @@ module Test | fault | check("Should have a Failure", fault.instance_of?(Failure)) check("The Failure should have the correct message", "failure" == fault.message) - check("The Failure should have the correct location (was <#{fault.location}>)", fault.location =~ /test_failure\(TC_FailureError\) \[.*#{File.basename(__FILE__)}:\d+\]/) + check("The Failure should have the correct test_name (was <#{fault.test_name}>)", fault.test_name == "test_failure(TC_FailureError)") + r = /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `test_failure'\Z/ + + location = fault.location + check("The location should be an array", location.kind_of?(Array)) + check("The location should have two lines (was: <#{location.inspect}>)", location.size == 2) + check("The Failure should have the correct location (was <#{location[0].inspect}>, expected <#{r.inspect}>)", r =~ location[0]) called = true } progress = [] @@ -64,6 +76,30 @@ module Test check("The failure should have set passed?", !test_case.return_passed?) check("The progress block should have been updated correctly", [[TestCase::STARTED, test_case.name], [TestCase::FINISHED, test_case.name]] == progress) end + + def test_add_failure_nested + test_case = @tc_failure_error.new(:test_nested_failure) + check("passed? should start out true", test_case.return_passed?) + + result = TestResult.new + called = false + result.add_listener(TestResult::FAULT) { + | fault | + check("Should have a Failure", fault.instance_of?(Failure)) + check("The Failure should have the correct message", "nested" == fault.message) + check("The Failure should have the correct test_name (was <#{fault.test_name}>)", fault.test_name == "test_nested_failure(TC_FailureError)") + r = + + location = fault.location + check("The location should be an array", location.kind_of?(Array)) + check("The location should have the correct number of lines (was: <#{location.inspect}>)", location.size == 3) + check("The Failure should have the correct location (was <#{location[0].inspect}>)", /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `nested'\Z/ =~ location[0]) + check("The Failure should have the correct location (was <#{location[1].inspect}>)", /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `test_nested_failure'\Z/ =~ location[1]) + called = true + } + test_case.run(result){} + check("The failure should have triggered the listener", called) + end def test_add_error test_case = @tc_failure_error.new(:test_error) @@ -74,7 +110,7 @@ module Test | fault | check("Should have a TestError", fault.instance_of?(Error)) check("The Error should have the correct message", "ZeroDivisionError: divided by 0" == fault.message) - check("The Error should have the correct location", "test_error(TC_FailureError)" == fault.location) + check("The Error should have the correct test_name", "test_error(TC_FailureError)" == fault.test_name) check("The Error should have the correct exception", fault.exception.instance_of?(ZeroDivisionError)) called = true } diff --git a/test/testunit/util/test_backtracefilter.rb b/test/testunit/util/test_backtracefilter.rb new file mode 100644 index 0000000000..eeabb91d22 --- /dev/null +++ b/test/testunit/util/test_backtracefilter.rb @@ -0,0 +1,38 @@ +require 'test/unit' + +require 'test/unit/util/backtracefilter' + +module Test::Unit::Util + class TestBacktraceFilter < Test::Unit::TestCase + include BacktraceFilter + + def test_filter_backtrace + backtrace = [%q{C:\some\old\path/test/unit/assertions.rb:44:in 'assert'}, + %q{tc_thing.rb:4:in 'a'}, + %q{tc_thing.rb:4:in 'test_stuff'}, + %q{C:\some\old\path/test/unit/testcase.rb:44:in 'send'}, + %q{C:\some\old\path\test\unit\testcase.rb:44:in 'run'}, + %q{C:\some\old\path\test\unit.rb:44:in 'run'}, + %q{tc_thing.rb:3}] + assert_equal(backtrace[1..2], filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Should filter out all TestUnit-specific lines") + +backtrace = [%q{tc_thing.rb:4:in 'a'}, + %q{tc_thing.rb:4:in 'test_stuff'}, + %q{tc_thing.rb:3}] + assert_equal(backtrace, filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Shouldn't filter too much") + + backtrace = [%q{C:\some\old\path/test/unit/assertions.rb:44:in 'assert'}, + %q{tc_thing.rb:4:in 'a'}, + %q{tc_thing.rb:4:in 'test_stuff'}, + %q{tc_thing.rb:3}] + assert_equal(backtrace[1..3], filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Should filter out all TestUnit-specific lines") + + backtrace = [%q{C:\some\old\path/test/unit/assertions.rb:44:in 'assert'}, + %q{C:\some\old\path/test/unit/testcase.rb:44:in 'send'}, + %q{C:\some\old\path\test\unit\testcase.rb:44:in 'run'}, + %q{C:\some\old\path\test\unit.rb:44:in 'run'}] + assert_equal(backtrace, filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Should filter out all TestUnit-specific lines") + + end + end +end |