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>
|
Wed Dec 24 13:43:34 2003 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
* lib/net/imap.rb (authenticate): remove "\n" from base64 encoded
|
* 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>
|
Wed Dec 24 11:26:41 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
|
|
@ -56,18 +56,31 @@ EOT
|
||||||
assert_block(full_message) { expected == actual }
|
assert_block(full_message) { expected == actual }
|
||||||
end
|
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.
|
# Passes if block raises one of the given exceptions.
|
||||||
public
|
public
|
||||||
def assert_raise(*args)
|
def assert_raise(*args)
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
if Class === args.last
|
if Module === args.last
|
||||||
message = ""
|
message = ""
|
||||||
else
|
else
|
||||||
message = args.pop
|
message = args.pop
|
||||||
end
|
end
|
||||||
args.each do |klass|
|
exceptions, modules = _check_exception_class(args)
|
||||||
assert(Exception >= klass, "Should expect a class of exception, #{klass}")
|
|
||||||
end
|
|
||||||
expected = args.size == 1 ? args.first : args
|
expected = args.size == 1 ? args.first : args
|
||||||
actual_exception = nil
|
actual_exception = nil
|
||||||
full_message = build_message(message, "<?> exception expected but none was thrown.", expected)
|
full_message = build_message(message, "<?> exception expected but none was thrown.", expected)
|
||||||
|
@ -80,7 +93,7 @@ EOT
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
full_message = build_message(message, "<?> exception expected but was\n?", expected, actual_exception)
|
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
|
actual_exception
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -188,21 +201,20 @@ EOT
|
||||||
public
|
public
|
||||||
def assert_nothing_raised(*args)
|
def assert_nothing_raised(*args)
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
if Class === args.last
|
if Module === args.last
|
||||||
message = ""
|
message = ""
|
||||||
else
|
else
|
||||||
message = args.pop
|
message = args.pop
|
||||||
end
|
end
|
||||||
args.each do |klass|
|
exceptions, modules = _check_exception_class(args)
|
||||||
assert(Exception >= klass, "Should expect a class of exception, #{klass}")
|
|
||||||
end
|
|
||||||
begin
|
begin
|
||||||
yield
|
yield
|
||||||
rescue Exception => e
|
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}
|
assert_block(build_message(message, "Exception raised:\n?", e)){false}
|
||||||
else
|
else
|
||||||
raise e.class, e.message, e.backtrace
|
raise
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
|
|
|
@ -37,12 +37,12 @@ module Test
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT_LEVELS = {
|
OUTPUT_LEVELS = [
|
||||||
:silent => UI::SILENT,
|
[:silent, UI::SILENT],
|
||||||
:progress => UI::PROGRESS_ONLY,
|
[:progress, UI::PROGRESS_ONLY],
|
||||||
:normal => UI::NORMAL,
|
[:normal, UI::NORMAL],
|
||||||
:verbose => UI::VERBOSE,
|
[:verbose, UI::VERBOSE],
|
||||||
}
|
]
|
||||||
|
|
||||||
COLLECTORS = {
|
COLLECTORS = {
|
||||||
:objectspace => proc do |r|
|
:objectspace => proc do |r|
|
||||||
|
@ -95,10 +95,10 @@ module Test
|
||||||
o.banner << "\nUsage: #{$0} [options] [-- untouched arguments]"
|
o.banner << "\nUsage: #{$0} [options] [-- untouched arguments]"
|
||||||
|
|
||||||
o.on
|
o.on
|
||||||
o.on('-r', '--runner=RUNNER', RUNNERS.keys,
|
o.on('-r', '--runner=RUNNER', RUNNERS,
|
||||||
"Use the given RUNNER.",
|
"Use the given RUNNER.",
|
||||||
"(" + keyword_display(RUNNERS.keys) + ")") do |r|
|
"(" + keyword_display(RUNNERS) + ")") do |r|
|
||||||
@runner = RUNNERS[r]
|
@runner = r
|
||||||
end
|
end
|
||||||
|
|
||||||
if(@standalone)
|
if(@standalone)
|
||||||
|
@ -138,10 +138,10 @@ module Test
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
o.on('-v', '--verbose=[LEVEL]', OUTPUT_LEVELS.keys,
|
o.on('-v', '--verbose=[LEVEL]', OUTPUT_LEVELS,
|
||||||
"Set the output level (default is verbose).",
|
"Set the output level (default is verbose).",
|
||||||
"(" + keyword_display(OUTPUT_LEVELS.keys) + ")") do |l|
|
"(" + keyword_display(OUTPUT_LEVELS) + ")") do |l|
|
||||||
@output_level = (l ? OUTPUT_LEVELS[l] : OUTPUT_LEVELS[:verbose])
|
@output_level = l || UI::VERBOSE
|
||||||
end
|
end
|
||||||
|
|
||||||
o.on('--',
|
o.on('--',
|
||||||
|
@ -174,7 +174,9 @@ module Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def keyword_display(array)
|
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
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
|
|
@ -68,7 +68,8 @@ module Test
|
||||||
|
|
||||||
def collect_file(name, suites, already_gathered)
|
def collect_file(name, suites, already_gathered)
|
||||||
loadpath = $:.dup
|
loadpath = $:.dup
|
||||||
$:.unshift(File.dirname(name))
|
dir = File.dirname(File.expand_path(name))
|
||||||
|
$:.unshift(dir) unless $:.first == dir
|
||||||
if(@req)
|
if(@req)
|
||||||
@req.require(name)
|
@req.require(name)
|
||||||
else
|
else
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
$:.unshift(File.dirname(File.expand_path(__FILE__)))
|
|
||||||
require 'drbtest'
|
require 'drbtest'
|
||||||
|
|
||||||
class TestDRbCore < Test::Unit::TestCase
|
class TestDRbCore < Test::Unit::TestCase
|
||||||
|
|
|
@ -145,17 +145,28 @@ module Test
|
||||||
}
|
}
|
||||||
|
|
||||||
exceptions = [ArgumentError, TypeError]
|
exceptions = [ArgumentError, TypeError]
|
||||||
|
modules = [Math, Comparable]
|
||||||
|
rescues = exceptions + modules
|
||||||
exceptions.each do |exc|
|
exceptions.each do |exc|
|
||||||
check_nothing_fails(true) {
|
check_nothing_fails(true) {
|
||||||
return_value = assert_raise(*exceptions) {
|
return_value = assert_raise(*rescues) {
|
||||||
raise exc, "Error"
|
raise exc, "Error"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
check(return_value.instance_of?(exc), "Should have returned #{exc} but was #{return_value.class}")
|
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")
|
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
||||||
end
|
end
|
||||||
check_fails("<[ArgumentError, TypeError]> exception expected but none was thrown.") {
|
modules.each do |mod|
|
||||||
assert_raise(*exceptions) {
|
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
|
1 + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,7 +309,7 @@ Message: <"Error">
|
||||||
}
|
}
|
||||||
check_nothing_fails {
|
check_nothing_fails {
|
||||||
begin
|
begin
|
||||||
assert_nothing_raised(RuntimeError, StandardError, "successful assert_nothing_raised") {
|
assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
|
||||||
raise ZeroDivisionError.new("ArgumentError")
|
raise ZeroDivisionError.new("ArgumentError")
|
||||||
}
|
}
|
||||||
rescue ZeroDivisionError
|
rescue ZeroDivisionError
|
||||||
|
|
Loading…
Reference in a new issue