mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
test/unit: assert_raise_with_message
* lib/test/unit/assertions.rb (assert_raise_with_message): move from test/fileutils/test_fileutils.rb. this is still experimental and the interface may be changed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41790 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5716c5ef2c
commit
9d93d4df6f
3 changed files with 48 additions and 24 deletions
|
@ -1,3 +1,9 @@
|
|||
Fri Jul 5 11:29:47 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* lib/test/unit/assertions.rb (assert_raise_with_message): move from
|
||||
test/fileutils/test_fileutils.rb. this is still experimental and
|
||||
the interface may be changed.
|
||||
|
||||
Fri Jul 5 11:08:00 2013 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||
|
||||
* win32/win32.c (w32_spawn): r41710 made that if the command starts with
|
||||
|
|
|
@ -65,6 +65,42 @@ module Test
|
|||
assert_raises(*args, &b)
|
||||
end
|
||||
|
||||
# :call-seq:
|
||||
# assert_raise_with_message(exception, expected, msg = nil, &block)
|
||||
#
|
||||
#Tests if the given block raises an exception with the expected
|
||||
#message.
|
||||
#
|
||||
# assert_raise_with_message(RuntimeError, "foo") do
|
||||
# nil #Fails, no Exceptions are raised
|
||||
# end
|
||||
#
|
||||
# assert_raise_with_message(RuntimeError, "foo") do
|
||||
# raise ArgumentError, "foo" #Fails, different Exception is raised
|
||||
# end
|
||||
#
|
||||
# assert_raise_with_message(RuntimeError, "foo") do
|
||||
# raise "bar" #Fails, RuntimeError is raised but the message differs
|
||||
# end
|
||||
#
|
||||
# assert_raise_with_message(RuntimeError, "foo") do
|
||||
# raise "foo" #Raises RuntimeError with the message, so assertion succeeds
|
||||
# end
|
||||
def assert_raise_with_message(exception, expected, msg = nil)
|
||||
case expected
|
||||
when String
|
||||
assert = :assert_equal
|
||||
when Regexp
|
||||
assert = :assert_match
|
||||
else
|
||||
raise TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}"
|
||||
end
|
||||
|
||||
ex = assert_raise(exception, msg) {yield}
|
||||
msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"}
|
||||
__send__(assert, expected, ex.message, msg)
|
||||
end
|
||||
|
||||
# :call-seq:
|
||||
# assert_nothing_raised( *args, &block )
|
||||
#
|
||||
|
|
|
@ -962,23 +962,23 @@ class TestFileUtils
|
|||
assert_equal 0500, File.stat('tmp/a').mode & 07777
|
||||
end
|
||||
|
||||
assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
chmod "a", 'tmp/a'
|
||||
}
|
||||
|
||||
assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
chmod "x+a", 'tmp/a'
|
||||
}
|
||||
|
||||
assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
chmod "u+z", 'tmp/a'
|
||||
}
|
||||
|
||||
assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
chmod ",+x", 'tmp/a'
|
||||
}
|
||||
|
||||
assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
|
||||
chmod "755", 'tmp/a'
|
||||
}
|
||||
|
||||
|
@ -1187,29 +1187,11 @@ class TestFileUtils
|
|||
uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')]
|
||||
}
|
||||
# [Bug #6708] [ruby-core:46256]
|
||||
assert_raises_with_message(ArgumentError, "wrong number of arguments (3 for 2)") {
|
||||
assert_raise_with_message(ArgumentError, "wrong number of arguments (3 for 2)") {
|
||||
uptodate?('new',['old', 'oldest'], {})
|
||||
}
|
||||
end
|
||||
|
||||
def assert_raises_with_message(klass, message)
|
||||
begin
|
||||
yield
|
||||
flunk("Expected Exception #{klass} didn't raise")
|
||||
rescue klass => ex
|
||||
if message.kind_of? String
|
||||
flag = !!(ex.message == message)
|
||||
assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match")
|
||||
elsif message.kind_of? Regexp
|
||||
flag = !!(ex.message =~ message)
|
||||
assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match")
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
private :assert_raises_with_message
|
||||
|
||||
def test_cd
|
||||
check_singleton :cd
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue