зеркало из https://github.com/github/ruby.git
test/unit/assertions.rb: return exception
* lib/test/unit/assertions.rb (assert_raise_with_message): return raised exception same as assert_raise. * test/ruby, test/-ext-: use assert_raise_with_message. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
289d6bb30f
Коммит
9ef55da910
|
@ -99,6 +99,7 @@ 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)
|
||||
ex
|
||||
end
|
||||
|
||||
# :call-seq:
|
||||
|
|
|
@ -21,8 +21,7 @@ module Bug::Marshal
|
|||
end
|
||||
|
||||
def test_incompat
|
||||
e = assert_raise(ArgumentError) {::Marshal.load(old_dump)}
|
||||
assert_equal("dump format error", e.message)
|
||||
assert_raise_with_message(ArgumentError, "dump format error") {::Marshal.load(old_dump)}
|
||||
end
|
||||
|
||||
def test_compat
|
||||
|
|
|
@ -3,19 +3,14 @@ require "-test-/typeddata/typeddata"
|
|||
|
||||
class Test_TypedData < Test::Unit::TestCase
|
||||
def test_wrong_argtype
|
||||
e = assert_raise(TypeError) {Bug::TypedData.check(false)}
|
||||
assert_equal("wrong argument type false (expected typed_data)", e.message)
|
||||
assert_raise_with_message(TypeError, "wrong argument type false (expected typed_data)") {Bug::TypedData.check(false)}
|
||||
|
||||
e = assert_raise(TypeError) {Bug::TypedData.check(true)}
|
||||
assert_equal("wrong argument type true (expected typed_data)", e.message)
|
||||
assert_raise_with_message(TypeError, "wrong argument type true (expected typed_data)") {Bug::TypedData.check(true)}
|
||||
|
||||
e = assert_raise(TypeError) {Bug::TypedData.check(:e)}
|
||||
assert_equal("wrong argument type Symbol (expected typed_data)", e.message)
|
||||
assert_raise_with_message(TypeError, "wrong argument type Symbol (expected typed_data)") {Bug::TypedData.check(:e)}
|
||||
|
||||
e = assert_raise(TypeError) {Bug::TypedData.check(0)}
|
||||
assert_equal("wrong argument type Fixnum (expected typed_data)", e.message)
|
||||
assert_raise_with_message(TypeError, "wrong argument type Fixnum (expected typed_data)") {Bug::TypedData.check(0)}
|
||||
|
||||
e = assert_raise(TypeError) {Bug::TypedData.check("a")}
|
||||
assert_equal("wrong argument type String (expected typed_data)", e.message)
|
||||
assert_raise_with_message(TypeError, "wrong argument type String (expected typed_data)") {Bug::TypedData.check("a")}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,22 +32,19 @@ class TestBeginEndBlock < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_begininmethod
|
||||
e = assert_raise(SyntaxError) do
|
||||
assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
|
||||
eval("def foo; BEGIN {}; end")
|
||||
end
|
||||
assert_match(/BEGIN is permitted only at toplevel/, e.message)
|
||||
|
||||
e = assert_raise(SyntaxError) do
|
||||
assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
|
||||
eval('eval("def foo; BEGIN {}; end")')
|
||||
end
|
||||
assert_match(/BEGIN is permitted only at toplevel/, e.message)
|
||||
end
|
||||
|
||||
def test_begininclass
|
||||
e = assert_raise(SyntaxError) do
|
||||
assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
|
||||
eval("class TestBeginEndBlock; BEGIN {}; end")
|
||||
end
|
||||
assert_match(/BEGIN is permitted only at toplevel/, e.message)
|
||||
end
|
||||
|
||||
def test_endblockwarn
|
||||
|
|
|
@ -634,8 +634,7 @@ class TestBignum < Test::Unit::TestCase
|
|||
if (big = 2**31-1).is_a?(Fixnum)
|
||||
return
|
||||
end
|
||||
e = assert_raise(RangeError) {(1 << big).to_s}
|
||||
assert_match(/too big to convert/, e.message)
|
||||
assert_raise_with_message(RangeError, /too big to convert/) {(1 << big).to_s}
|
||||
end
|
||||
|
||||
def test_fix_fdiv
|
||||
|
|
|
@ -28,7 +28,7 @@ class TestException < Test::Unit::TestCase
|
|||
|
||||
def test_exception_in_rescue
|
||||
string = "this must be handled no.3"
|
||||
e = assert_raise(RuntimeError) do
|
||||
assert_raise_with_message(RuntimeError, string) do
|
||||
begin
|
||||
raise "exception in rescue clause"
|
||||
rescue
|
||||
|
@ -36,12 +36,11 @@ class TestException < Test::Unit::TestCase
|
|||
end
|
||||
assert(false)
|
||||
end
|
||||
assert_equal(string, e.message)
|
||||
end
|
||||
|
||||
def test_exception_in_ensure
|
||||
string = "exception in ensure clause"
|
||||
e = assert_raise(RuntimeError) do
|
||||
assert_raise_with_message(RuntimeError, string) do
|
||||
begin
|
||||
raise "this must be handled no.4"
|
||||
ensure
|
||||
|
@ -51,7 +50,6 @@ class TestException < Test::Unit::TestCase
|
|||
end
|
||||
assert(false)
|
||||
end
|
||||
assert_equal(string, e.message)
|
||||
end
|
||||
|
||||
def test_exception_ensure
|
||||
|
@ -333,8 +331,10 @@ end.join
|
|||
bug3237 = '[ruby-core:29948]'
|
||||
str = "\u2600"
|
||||
id = :"\u2604"
|
||||
e = assert_raise(NoMethodError) {str.__send__(id)}
|
||||
assert_equal("undefined method `#{id}' for #{str.inspect}:String", e.message, bug3237)
|
||||
msg = "undefined method `#{id}' for #{str.inspect}:String"
|
||||
assert_raise_with_message(NoMethodError, msg, bug3237) do
|
||||
str.__send__(id)
|
||||
end
|
||||
end
|
||||
|
||||
def test_errno
|
||||
|
|
|
@ -339,8 +339,9 @@ class TestFile < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_file_open_double_mode
|
||||
e = assert_raise(ArgumentError) { File.open("a", 'w', :mode => 'rw+') }
|
||||
assert_equal 'mode specified twice', e.message
|
||||
assert_raise_with_message(ArgumentError, 'mode specified twice') {
|
||||
File.open("a", 'w', :mode => 'rw+')
|
||||
}
|
||||
end
|
||||
|
||||
def test_conflicting_encodings
|
||||
|
|
|
@ -2098,8 +2098,9 @@ End
|
|||
assert_equal(["foo\n", "bar\n", "baz\n"], a, bug)
|
||||
|
||||
bug6054 = '[ruby-dev:45267]'
|
||||
e = assert_raise(IOError, bug6054) {IO.foreach(t.path, mode:"w").next}
|
||||
assert_match(/not opened for reading/, e.message, bug6054)
|
||||
assert_raise_with_message(IOError, /not opened for reading/, bug6054) do
|
||||
IO.foreach(t.path, mode:"w").next
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -763,10 +763,12 @@ EOT
|
|||
assert_equal(eucjp, r.read)
|
||||
end)
|
||||
|
||||
e = assert_raise(ArgumentError) {with_pipe("UTF-8", "UTF-8".encode("UTF-32BE")) {}}
|
||||
assert_match(/invalid name encoding/, e.message)
|
||||
e = assert_raise(ArgumentError) {with_pipe("UTF-8".encode("UTF-32BE")) {}}
|
||||
assert_match(/invalid name encoding/, e.message)
|
||||
assert_raise_with_message(ArgumentError, /invalid name encoding/) do
|
||||
with_pipe("UTF-8", "UTF-8".encode("UTF-32BE")) {}
|
||||
end
|
||||
assert_raise_with_message(ArgumentError, /invalid name encoding/) do
|
||||
with_pipe("UTF-8".encode("UTF-32BE")) {}
|
||||
end
|
||||
|
||||
ENCS.each {|enc|
|
||||
pipe(enc,
|
||||
|
|
|
@ -49,8 +49,7 @@ class TestISeq < Test::Unit::TestCase
|
|||
def test_unsupport_type
|
||||
ary = RubyVM::InstructionSequence.compile("p").to_a
|
||||
ary[9] = :foobar
|
||||
e = assert_raise(TypeError) {RubyVM::InstructionSequence.load(ary)}
|
||||
assert_match(/:foobar/, e.message)
|
||||
assert_raise_with_message(TypeError, /:foobar/) {RubyVM::InstructionSequence.load(ary)}
|
||||
end if defined?(RubyVM::InstructionSequence.load)
|
||||
|
||||
def test_disasm_encoding
|
||||
|
|
|
@ -248,10 +248,11 @@ class TestModule < Test::Unit::TestCase
|
|||
":",
|
||||
["String::", "[Bug #7573]"],
|
||||
].each do |name, msg|
|
||||
e = assert_raise(NameError, "#{msg}#{': ' if msg}wrong constant name #{name.dump}") {
|
||||
expected = "wrong constant name %s" % name
|
||||
msg = "#{msg}#{': ' if msg}wrong constant name #{name.dump}"
|
||||
assert_raise_with_message(NameError, expected, msg) {
|
||||
Object.const_get name
|
||||
}
|
||||
assert_equal("wrong constant name %s" % name, e.message)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -624,8 +625,9 @@ class TestModule < Test::Unit::TestCase
|
|||
bug5084 = '[ruby-dev:44200]'
|
||||
assert_raise(TypeError, bug5084) { c1.const_get(1) }
|
||||
bug7574 = '[ruby-dev:46749]'
|
||||
e = assert_raise(NameError) { Object.const_get("String\0") }
|
||||
assert_equal("wrong constant name \"String\\u0000\"", e.message, bug7574)
|
||||
assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
|
||||
Object.const_get("String\0")
|
||||
}
|
||||
end
|
||||
|
||||
def test_const_defined_invalid_name
|
||||
|
@ -634,8 +636,9 @@ class TestModule < Test::Unit::TestCase
|
|||
bug5084 = '[ruby-dev:44200]'
|
||||
assert_raise(TypeError, bug5084) { c1.const_defined?(1) }
|
||||
bug7574 = '[ruby-dev:46749]'
|
||||
e = assert_raise(NameError) { Object.const_defined?("String\0") }
|
||||
assert_equal("wrong constant name \"String\\u0000\"", e.message, bug7574)
|
||||
assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
|
||||
Object.const_defined?("String\0")
|
||||
}
|
||||
end
|
||||
|
||||
def test_const_get_no_inherited
|
||||
|
@ -1656,7 +1659,7 @@ class TestModule < Test::Unit::TestCase
|
|||
@@foo
|
||||
$foo
|
||||
].each do |name|
|
||||
assert_raise(NameError) do
|
||||
assert_raise_with_message(NameError, /#{Regexp.quote(name)}/) do
|
||||
Module.new { attr_accessor name.to_sym }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -488,11 +488,10 @@ class TestObject < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
e = assert_raise(ArgumentError, '[bug:6000]') do
|
||||
msg = 'respond_to? must accept 1 or 2 arguments (requires 3)'
|
||||
assert_raise_with_message(ArgumentError, msg, '[bug:6000]') do
|
||||
[[p]].flatten
|
||||
end
|
||||
|
||||
assert_equal('respond_to? must accept 1 or 2 arguments (requires 3)', e.message)
|
||||
end
|
||||
|
||||
def test_method_missing_passed_block
|
||||
|
@ -803,9 +802,7 @@ class TestObject < Test::Unit::TestCase
|
|||
|
||||
def test_type_error_message
|
||||
issue = "Bug #7539"
|
||||
err = assert_raise(TypeError){ Integer([42]) }
|
||||
assert_equal "can't convert Array into Integer", err.message, issue
|
||||
err = assert_raise(TypeError){ [].first([42]) }
|
||||
assert_equal 'no implicit conversion of Array into Integer', err.message, issue
|
||||
assert_raise_with_message(TypeError, "can't convert Array into Integer") {Integer([42])}
|
||||
assert_raise_with_message(TypeError, 'no implicit conversion of Array into Integer') {[].first([42])}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -946,8 +946,9 @@ class TestRegexp < Test::Unit::TestCase
|
|||
def test_error_message_on_failed_conversion
|
||||
bug7539 = '[ruby-core:50733]'
|
||||
assert_equal false, /x/=== 42
|
||||
err = assert_raise(TypeError){ Regexp.quote(42) }
|
||||
assert_equal 'no implicit conversion of Fixnum into String', err.message, bug7539
|
||||
assert_raise_with_message(TypeError, 'no implicit conversion of Fixnum into String', bug7539) {
|
||||
Regexp.quote(42)
|
||||
}
|
||||
end
|
||||
|
||||
def test_conditional_expression
|
||||
|
|
|
@ -50,8 +50,7 @@ class TestRequire < Test::Unit::TestCase
|
|||
def test_require_nonascii
|
||||
bug3758 = '[ruby-core:31915]'
|
||||
["\u{221e}", "\x82\xa0".force_encoding("cp932")].each do |path|
|
||||
e = assert_raise(LoadError, bug3758) {require path}
|
||||
assert_match(/#{path}\z/, e.message, bug3758)
|
||||
assert_raise_with_message(LoadError, /#{path}\z/, bug3758) {require path}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -16,12 +16,10 @@ class TestSignal < Test::Unit::TestCase
|
|||
assert_equal 2, x
|
||||
|
||||
Signal.trap(:INT) { raise "Interrupt" }
|
||||
ex = assert_raise(RuntimeError) {
|
||||
assert_raise_with_message(RuntimeError, /Interrupt/) {
|
||||
Process.kill :INT, Process.pid
|
||||
sleep 0.1
|
||||
}
|
||||
assert_kind_of Exception, ex
|
||||
assert_match(/Interrupt/, ex.message)
|
||||
ensure
|
||||
Signal.trap :INT, oldtrap if oldtrap
|
||||
end
|
||||
|
|
|
@ -325,70 +325,52 @@ class TestSprintf < Test::Unit::TestCase
|
|||
|
||||
def test_named_untyped
|
||||
assert_equal("value", sprintf("%<key>s", :key => "value"))
|
||||
e = assert_raise(ArgumentError) {sprintf("%1$<key2>s", :key => "value")}
|
||||
assert_equal("named<key2> after numbered", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%s%s%<key2>s", "foo", "bar", :key => "value")}
|
||||
assert_equal("named<key2> after unnumbered(2)", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%<key><key2>s", :key => "value")}
|
||||
assert_equal("named<key2> after <key>", e.message)
|
||||
e = assert_raise(KeyError) {sprintf("%<key>s", {})}
|
||||
assert_equal("key<key> not found", e.message)
|
||||
assert_raise_with_message(ArgumentError, "named<key2> after numbered") {sprintf("%1$<key2>s", :key => "value")}
|
||||
assert_raise_with_message(ArgumentError, "named<key2> after unnumbered(2)") {sprintf("%s%s%<key2>s", "foo", "bar", :key => "value")}
|
||||
assert_raise_with_message(ArgumentError, "named<key2> after <key>") {sprintf("%<key><key2>s", :key => "value")}
|
||||
assert_raise_with_message(KeyError, "key<key> not found") {sprintf("%<key>s", {})}
|
||||
end
|
||||
|
||||
def test_named_untyped_enc
|
||||
key = "\u{3012}"
|
||||
[Encoding::UTF_8, Encoding::EUC_JP].each do |enc|
|
||||
k = key.encode(enc)
|
||||
e = assert_raise(ArgumentError) {sprintf("%1$<#{k}>s", key: "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named<#{k}> after numbered") {sprintf("%1$<#{k}>s", key: "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named<#{k}> after numbered", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%s%s%<#{k}>s", "foo", "bar", key: "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named<#{k}> after unnumbered(2)") {sprintf("%s%s%<#{k}>s", "foo", "bar", key: "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named<#{k}> after unnumbered(2)", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%<key><#{k}>s", key: "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named<#{k}> after <key>") {sprintf("%<key><#{k}>s", key: "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named<#{k}> after <key>", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%<#{k}><key>s", k.to_sym => "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named<key> after <#{k}>") {sprintf("%<#{k}><key>s", k.to_sym => "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named<key> after <#{k}>", e.message)
|
||||
e = assert_raise(KeyError) {sprintf("%<#{k}>s", {})}
|
||||
e = assert_raise_with_message(KeyError, "key<#{k}> not found") {sprintf("%<#{k}>s", {})}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("key<#{k}> not found", e.message)
|
||||
end
|
||||
end
|
||||
|
||||
def test_named_typed
|
||||
assert_equal("value", sprintf("%{key}", :key => "value"))
|
||||
e = assert_raise(ArgumentError) {sprintf("%1${key2}", :key => "value")}
|
||||
assert_equal("named{key2} after numbered", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%s%s%{key2}", "foo", "bar", :key => "value")}
|
||||
assert_equal("named{key2} after unnumbered(2)", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%<key>{key2}", :key => "value")}
|
||||
assert_equal("named{key2} after <key>", e.message)
|
||||
assert_raise_with_message(ArgumentError, "named{key2} after numbered") {sprintf("%1${key2}", :key => "value")}
|
||||
assert_raise_with_message(ArgumentError, "named{key2} after unnumbered(2)") {sprintf("%s%s%{key2}", "foo", "bar", :key => "value")}
|
||||
assert_raise_with_message(ArgumentError, "named{key2} after <key>") {sprintf("%<key>{key2}", :key => "value")}
|
||||
assert_equal("value{key2}", sprintf("%{key}{key2}", :key => "value"))
|
||||
e = assert_raise(KeyError) {sprintf("%{key}", {})}
|
||||
assert_equal("key{key} not found", e.message)
|
||||
assert_raise_with_message(KeyError, "key{key} not found") {sprintf("%{key}", {})}
|
||||
end
|
||||
|
||||
def test_named_typed_enc
|
||||
key = "\u{3012}"
|
||||
[Encoding::UTF_8, Encoding::EUC_JP].each do |enc|
|
||||
k = key.encode(enc)
|
||||
e = assert_raise(ArgumentError) {sprintf("%1${#{k}}s", key: "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named{#{k}} after numbered") {sprintf("%1${#{k}}s", key: "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named{#{k}} after numbered", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%s%s%{#{k}}s", "foo", "bar", key: "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named{#{k}} after unnumbered(2)") {sprintf("%s%s%{#{k}}s", "foo", "bar", key: "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named{#{k}} after unnumbered(2)", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%<key>{#{k}}s", key: "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named{#{k}} after <key>") {sprintf("%<key>{#{k}}s", key: "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named{#{k}} after <key>", e.message)
|
||||
e = assert_raise(ArgumentError) {sprintf("%<#{k}>{key}s", k.to_sym => "value")}
|
||||
e = assert_raise_with_message(ArgumentError, "named{key} after <#{k}>") {sprintf("%<#{k}>{key}s", k.to_sym => "value")}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("named{key} after <#{k}>", e.message)
|
||||
e = assert_raise(KeyError) {sprintf("%{#{k}}", {})}
|
||||
e = assert_raise_with_message(KeyError, "key{#{k}} not found") {sprintf("%{#{k}}", {})}
|
||||
assert_equal(enc, e.message.encoding)
|
||||
assert_equal("key{#{k}} not found", e.message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -152,14 +152,13 @@ class TestSuper < Test::Unit::TestCase
|
|||
a = A.new
|
||||
a.uu(12)
|
||||
assert_equal("A#tt", a.tt(12), "[ruby-core:3856]")
|
||||
e = assert_raise(RuntimeError, "[ruby-core:24244]") {
|
||||
assert_raise_with_message(RuntimeError, /implicit argument passing of super from method defined by define_method/, "[ruby-core:24244]") {
|
||||
lambda {
|
||||
Class.new {
|
||||
define_method(:a) {super}
|
||||
}.new.a
|
||||
}.call
|
||||
}
|
||||
assert_match(/implicit argument passing of super from method defined by define_method/, e.message)
|
||||
end
|
||||
|
||||
class SubSeq
|
||||
|
|
Загрузка…
Ссылка в новой задаче