mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/test/unit/assertions.rb (Test::Unit::Assertions#assert_throws):
throw won't raise NameError nor ThreadError but ArgumentError on 1.9. (Test::Unit::Assertions#assert_not_throws): ditto. * test/testunit/test_assertions.rb: add assertions for throwing some objects other than Symbol. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14777 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b3c2e224d3
commit
d356ccc987
3 changed files with 36 additions and 11 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
Sat Dec 29 04:46:58 2007 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
||||||
|
|
||||||
|
* lib/test/unit/assertions.rb (Test::Unit::Assertions#assert_throws):
|
||||||
|
throw won't raise NameError nor ThreadError but ArgumentError on 1.9.
|
||||||
|
(Test::Unit::Assertions#assert_not_throws): ditto.
|
||||||
|
|
||||||
|
* test/testunit/test_assertions.rb: add assertions for throwing some
|
||||||
|
objects other than Symbol.
|
||||||
|
|
||||||
Sat Dec 29 03:10:12 2007 NAKAMURA Usaku <usa@ruby-lang.org>
|
Sat Dec 29 03:10:12 2007 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||||
|
|
||||||
* io.c (io_unread): fix typo.
|
* io.c (io_unread): fix typo.
|
||||||
|
|
|
@ -369,11 +369,12 @@ EOT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
UncaughtThrow = {NameError => /^uncaught throw \`(.+)\'$/,
|
UncaughtThrow = {
|
||||||
ThreadError => /^uncaught throw \`(.+)\' in thread /} #`
|
ArgumentError => /^uncaught throw (.+)$/,
|
||||||
|
} #`
|
||||||
|
|
||||||
##
|
##
|
||||||
# Passes if the block throws +expected_symbol+
|
# Passes if the block throws +expected_value+
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# assert_throws :done do
|
# assert_throws :done do
|
||||||
|
@ -381,23 +382,22 @@ EOT
|
||||||
# end
|
# end
|
||||||
|
|
||||||
public
|
public
|
||||||
def assert_throws(expected_symbol, message="", &proc)
|
def assert_throws(expected_value, message="", &proc)
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
assert_instance_of(Symbol, expected_symbol, "assert_throws expects the symbol that should be thrown for its first argument")
|
|
||||||
assert_block("Should have passed a block to assert_throws."){block_given?}
|
assert_block("Should have passed a block to assert_throws."){block_given?}
|
||||||
caught = true
|
caught = true
|
||||||
begin
|
begin
|
||||||
catch(expected_symbol) do
|
catch(expected_value) do
|
||||||
proc.call
|
proc.call
|
||||||
caught = false
|
caught = false
|
||||||
end
|
end
|
||||||
full_message = build_message(message, "<?> should have been thrown.", expected_symbol)
|
full_message = build_message(message, "<?> should have been thrown.", expected_value)
|
||||||
assert_block(full_message){caught}
|
assert_block(full_message){caught}
|
||||||
rescue NameError, ThreadError => error
|
rescue ArgumentError => error
|
||||||
if UncaughtThrow[error.class] !~ error.message
|
if UncaughtThrow[error.class] !~ error.message
|
||||||
raise error
|
raise error
|
||||||
end
|
end
|
||||||
full_message = build_message(message, "<?> expected to be thrown but\n<?> was thrown.", expected_symbol, $1.intern)
|
full_message = build_message(message, "<?> expected to be thrown but\n<#$1> was thrown.", expected_value)
|
||||||
flunk(full_message)
|
flunk(full_message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -417,11 +417,11 @@ EOT
|
||||||
assert(block_given?, "Should have passed a block to assert_nothing_thrown")
|
assert(block_given?, "Should have passed a block to assert_nothing_thrown")
|
||||||
begin
|
begin
|
||||||
proc.call
|
proc.call
|
||||||
rescue NameError, ThreadError => error
|
rescue ArgumentError => error
|
||||||
if UncaughtThrow[error.class] !~ error.message
|
if UncaughtThrow[error.class] !~ error.message
|
||||||
raise error
|
raise error
|
||||||
end
|
end
|
||||||
full_message = build_message(message, "<?> was thrown when nothing was expected", $1.intern)
|
full_message = build_message(message, "<#$1> was thrown when nothing was expected")
|
||||||
flunk(full_message)
|
flunk(full_message)
|
||||||
end
|
end
|
||||||
assert(true, "Expected nothing to be thrown")
|
assert(true, "Expected nothing to be thrown")
|
||||||
|
|
|
@ -403,6 +403,22 @@ Message: <"Error">
|
||||||
throw :thing
|
throw :thing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
check_nothing_fails {
|
||||||
|
assert_throws(0, "message") {
|
||||||
|
throw 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
obj = Object.new
|
||||||
|
check_nothing_fails {
|
||||||
|
assert_throws(obj, "message") {
|
||||||
|
throw obj
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check_fails("message.\n<\"string\"> expected to be thrown but\n<\"string\"> was thrown.") {
|
||||||
|
assert_throws("string", "message") {
|
||||||
|
throw "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
check_fails("message.\n<:thing> expected to be thrown but\n<:thing2> was thrown.") {
|
check_fails("message.\n<:thing> expected to be thrown but\n<:thing2> was thrown.") {
|
||||||
assert_throws(:thing, "message") {
|
assert_throws(:thing, "message") {
|
||||||
throw :thing2
|
throw :thing2
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue