assertions.rb: last match in assert_raise_with_message

* lib/test/unit/assertions.rb (assert_raise_with_message): set
  caller's last match if expected is a Regexp.

* test/ruby/test_arity.rb (err_mess): use assert_raise_with_message.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44172 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-12-13 09:17:55 +00:00
Родитель 3ba2006aa4
Коммит 287d2adab0
2 изменённых файлов: 10 добавлений и 5 удалений

Просмотреть файл

@ -88,7 +88,7 @@ module Test
# 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)
def assert_raise_with_message(exception, expected, msg = nil, &block)
case expected
when String
assert = :assert_equal
@ -100,7 +100,14 @@ module Test
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)
if assert == :assert_equal
assert_equal(expected, ex.message, msg)
else
msg = message(msg) { "Expected #{mu_pp expected} to match #{mu_pp ex.message}" }
assert expected =~ ex.message, msg
block.binding.eval("proc{|_|$~=_}").call($~)
end
ex
end

Просмотреть файл

@ -3,7 +3,7 @@ require 'test/unit'
class TestArity < Test::Unit::TestCase
def err_mess(method_proc = nil, argc = 0)
args = (1..argc).to_a
err = assert_raise(ArgumentError) do
assert_raise_with_message(ArgumentError, /wrong number of arguments \((.*)\)/) do
case method_proc
when nil
yield
@ -13,8 +13,6 @@ class TestArity < Test::Unit::TestCase
method_proc.call(*args)
end
end
s = err.to_s
assert s =~ /wrong number of arguments \((.*)\)/, "Unexpected ArgumentError's message: #{s}"
$1
end