mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/test/unit/assertions.rb: Modules are allowed to rescue.
* lib/test/unit/autorunner.rb: show output_level in order. * lib/test/unit/collector/dir.rb: get rid of successive same directories in load path. * test/testunit/test_assertions.rb (test_assert_nothing_raised, test_assert_raise): test for modules. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5276 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d05b3b1e48
commit
73d77c8f57
6 changed files with 68 additions and 31 deletions
14
ChangeLog
14
ChangeLog
|
@ -1,7 +1,19 @@
|
|||
Wed Dec 24 14:07:55 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* lib/test/unit/assertions.rb: Modules are allowed to rescue.
|
||||
|
||||
* lib/test/unit/autorunner.rb: show output_level in order.
|
||||
|
||||
* lib/test/unit/collector/dir.rb: get rid of successive same
|
||||
directories in load path.
|
||||
|
||||
* test/testunit/test_assertions.rb (test_assert_nothing_raised,
|
||||
test_assert_raise): test for modules.
|
||||
|
||||
Wed Dec 24 13:43:34 2003 Shugo Maeda <shugo@ruby-lang.org>
|
||||
|
||||
* lib/net/imap.rb (authenticate): remove "\n" from base64 encoded
|
||||
strings.
|
||||
strings.
|
||||
|
||||
Wed Dec 24 11:26:41 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
|
|
|
@ -56,18 +56,31 @@ EOT
|
|||
assert_block(full_message) { expected == actual }
|
||||
end
|
||||
|
||||
private
|
||||
def _check_exception_class(args)
|
||||
args.partition do |klass|
|
||||
next if klass.instance_of?(Module)
|
||||
assert(Exception >= klass, "Should expect a class of exception, #{klass}")
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def _expected_exception?(actual_exception, exceptions, modules)
|
||||
exceptions.include?(actual_exception.class) or
|
||||
modules.any? {|mod| actual_exception.is_a?(mod)}
|
||||
end
|
||||
|
||||
# Passes if block raises one of the given exceptions.
|
||||
public
|
||||
def assert_raise(*args)
|
||||
_wrap_assertion do
|
||||
if Class === args.last
|
||||
if Module === args.last
|
||||
message = ""
|
||||
else
|
||||
message = args.pop
|
||||
end
|
||||
args.each do |klass|
|
||||
assert(Exception >= klass, "Should expect a class of exception, #{klass}")
|
||||
end
|
||||
exceptions, modules = _check_exception_class(args)
|
||||
expected = args.size == 1 ? args.first : args
|
||||
actual_exception = nil
|
||||
full_message = build_message(message, "<?> exception expected but none was thrown.", expected)
|
||||
|
@ -80,7 +93,7 @@ EOT
|
|||
false
|
||||
end
|
||||
full_message = build_message(message, "<?> exception expected but was\n?", expected, actual_exception)
|
||||
assert_block(full_message) { args.include?(actual_exception.class) }
|
||||
assert_block(full_message) {_expected_exception?(actual_exception, exceptions, modules)}
|
||||
actual_exception
|
||||
end
|
||||
end
|
||||
|
@ -188,21 +201,20 @@ EOT
|
|||
public
|
||||
def assert_nothing_raised(*args)
|
||||
_wrap_assertion do
|
||||
if Class === args.last
|
||||
if Module === args.last
|
||||
message = ""
|
||||
else
|
||||
message = args.pop
|
||||
end
|
||||
args.each do |klass|
|
||||
assert(Exception >= klass, "Should expect a class of exception, #{klass}")
|
||||
end
|
||||
exceptions, modules = _check_exception_class(args)
|
||||
begin
|
||||
yield
|
||||
rescue Exception => e
|
||||
if ((args.empty? && !e.instance_of?(AssertionFailedError)) || args.include?(e.class))
|
||||
if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
|
||||
_expected_exception?(e, exceptions, modules))
|
||||
assert_block(build_message(message, "Exception raised:\n?", e)){false}
|
||||
else
|
||||
raise e.class, e.message, e.backtrace
|
||||
raise
|
||||
end
|
||||
end
|
||||
nil
|
||||
|
|
|
@ -37,12 +37,12 @@ module Test
|
|||
end,
|
||||
}
|
||||
|
||||
OUTPUT_LEVELS = {
|
||||
:silent => UI::SILENT,
|
||||
:progress => UI::PROGRESS_ONLY,
|
||||
:normal => UI::NORMAL,
|
||||
:verbose => UI::VERBOSE,
|
||||
}
|
||||
OUTPUT_LEVELS = [
|
||||
[:silent, UI::SILENT],
|
||||
[:progress, UI::PROGRESS_ONLY],
|
||||
[:normal, UI::NORMAL],
|
||||
[:verbose, UI::VERBOSE],
|
||||
]
|
||||
|
||||
COLLECTORS = {
|
||||
:objectspace => proc do |r|
|
||||
|
@ -95,10 +95,10 @@ module Test
|
|||
o.banner << "\nUsage: #{$0} [options] [-- untouched arguments]"
|
||||
|
||||
o.on
|
||||
o.on('-r', '--runner=RUNNER', RUNNERS.keys,
|
||||
o.on('-r', '--runner=RUNNER', RUNNERS,
|
||||
"Use the given RUNNER.",
|
||||
"(" + keyword_display(RUNNERS.keys) + ")") do |r|
|
||||
@runner = RUNNERS[r]
|
||||
"(" + keyword_display(RUNNERS) + ")") do |r|
|
||||
@runner = r
|
||||
end
|
||||
|
||||
if(@standalone)
|
||||
|
@ -138,10 +138,10 @@ module Test
|
|||
end
|
||||
end
|
||||
|
||||
o.on('-v', '--verbose=[LEVEL]', OUTPUT_LEVELS.keys,
|
||||
o.on('-v', '--verbose=[LEVEL]', OUTPUT_LEVELS,
|
||||
"Set the output level (default is verbose).",
|
||||
"(" + keyword_display(OUTPUT_LEVELS.keys) + ")") do |l|
|
||||
@output_level = (l ? OUTPUT_LEVELS[l] : OUTPUT_LEVELS[:verbose])
|
||||
"(" + keyword_display(OUTPUT_LEVELS) + ")") do |l|
|
||||
@output_level = l || UI::VERBOSE
|
||||
end
|
||||
|
||||
o.on('--',
|
||||
|
@ -174,7 +174,9 @@ module Test
|
|||
end
|
||||
|
||||
def keyword_display(array)
|
||||
array.collect{|e| e.to_s.sub(/^(.)([A-Za-z]+)(?=\w*$)/, '\\1[\\2]')}.sort.join(", ")
|
||||
list = array.collect {|e, *| e.to_s}
|
||||
Array === array or list.sort!
|
||||
list.collect {|e| e.sub(/^(.)([A-Za-z]+)(?=\w*$)/, '\\1[\\2]')}.join(", ")
|
||||
end
|
||||
|
||||
def run
|
||||
|
|
|
@ -68,7 +68,8 @@ module Test
|
|||
|
||||
def collect_file(name, suites, already_gathered)
|
||||
loadpath = $:.dup
|
||||
$:.unshift(File.dirname(name))
|
||||
dir = File.dirname(File.expand_path(name))
|
||||
$:.unshift(dir) unless $:.first == dir
|
||||
if(@req)
|
||||
@req.require(name)
|
||||
else
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
$:.unshift(File.dirname(File.expand_path(__FILE__)))
|
||||
require 'drbtest'
|
||||
|
||||
class TestDRbCore < Test::Unit::TestCase
|
||||
|
|
|
@ -145,17 +145,28 @@ module Test
|
|||
}
|
||||
|
||||
exceptions = [ArgumentError, TypeError]
|
||||
modules = [Math, Comparable]
|
||||
rescues = exceptions + modules
|
||||
exceptions.each do |exc|
|
||||
check_nothing_fails(true) {
|
||||
return_value = assert_raise(*exceptions) {
|
||||
return_value = assert_raise(*rescues) {
|
||||
raise exc, "Error"
|
||||
}
|
||||
}
|
||||
check(return_value.instance_of?(exc), "Should have returned #{exc} but was #{return_value.class}")
|
||||
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
||||
end
|
||||
check_fails("<[ArgumentError, TypeError]> exception expected but none was thrown.") {
|
||||
assert_raise(*exceptions) {
|
||||
modules.each do |mod|
|
||||
check_nothing_fails(true) {
|
||||
return_value = assert_raise(*rescues) {
|
||||
raise Exception.new("Error").extend(mod)
|
||||
}
|
||||
}
|
||||
check(mod === return_value, "Should have returned #{mod}")
|
||||
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
||||
end
|
||||
check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception expected but none was thrown.") {
|
||||
assert_raise(*rescues) {
|
||||
1 + 1
|
||||
}
|
||||
}
|
||||
|
@ -298,7 +309,7 @@ Message: <"Error">
|
|||
}
|
||||
check_nothing_fails {
|
||||
begin
|
||||
assert_nothing_raised(RuntimeError, StandardError, "successful assert_nothing_raised") {
|
||||
assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
|
||||
raise ZeroDivisionError.new("ArgumentError")
|
||||
}
|
||||
rescue ZeroDivisionError
|
||||
|
|
Loading…
Reference in a new issue