Commit 79b4f61b authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #3799 from christopheraue/assert_raise_refactoring

Refactored #assert_raise and #assert_nothing_raised
parents 8976f47e 4b0b8f7e
...@@ -143,52 +143,41 @@ def assert_not_include(collection, obj, msg = nil) ...@@ -143,52 +143,41 @@ def assert_not_include(collection, obj, msg = nil)
assert_false(collection.include?(obj), msg, diff) assert_false(collection.include?(obj), msg, diff)
end end
def assert_raise(*exp) def assert_raise(exc, msg = nil)
ret = true return true unless $mrbtest_assert
if $mrbtest_assert $mrbtest_assert_idx += 1
$mrbtest_assert_idx += 1
msg = exp.last.class == String ? exp.pop : nil
msg = msg.to_s + " : " if msg
should_raise = false
begin
yield
should_raise = true
rescue Exception => e
msg = "#{msg}#{exp.inspect} exception expected, not"
diff = " Class: <#{e.class}>\n" +
" Message: #{e.message}"
unless exp.any?{|ex| ex.instance_of?(Module) ? e.kind_of?(ex) : ex == e.class }
$mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
ret = false
end
end
exp = exp.first if exp.first begin
if should_raise yield
msg = "#{msg}#{exp.inspect} expected but nothing was raised." msg ||= "Expected to raise #{exc} but nothing was raised."
$mrbtest_assert.push([$mrbtest_assert_idx, msg, nil]) diff = nil
ret = false $mrbtest_assert.push [$mrbtest_assert_idx, msg, diff]
end false
rescue exc
true
rescue Exception => e
msg ||= "Expected to raise #{exc}, not"
diff = " Class: <#{e.class}>\n" +
" Message: #{e.message}"
$mrbtest_assert.push [$mrbtest_assert_idx, msg, diff]
false
end end
ret
end end
def assert_nothing_raised(*exp) def assert_nothing_raised(msg = nil)
ret = true return true unless $mrbtest_assert
if $mrbtest_assert $mrbtest_assert_idx += 1
$mrbtest_assert_idx += 1
msg = exp.last.class == String ? exp.pop : "" begin
begin yield
yield true
rescue Exception => e rescue Exception => e
msg = "#{msg} exception raised." msg ||= "Expected not to raise #{exc.join(', ')} but it raised"
diff = " Class: <#{e.class}>\n" + diff = " Class: <#{e.class}>\n" +
" Message: #{e.message}" " Message: #{e.message}"
$mrbtest_assert.push([$mrbtest_assert_idx, msg, diff]) $mrbtest_assert.push [$mrbtest_assert_idx, msg, diff]
ret = false false
end
end end
ret
end end
## ##
......
...@@ -635,7 +635,7 @@ end ...@@ -635,7 +635,7 @@ end
c.singleton_class.class_eval do c.singleton_class.class_eval do
define_method(:method_removed) {|id| removed = id} define_method(:method_removed) {|id| removed = id}
end end
assert_nothing_raised(NoMethodError, NameError, '[Bug #7843]') do assert_nothing_raised('[Bug #7843]') do
c.class_eval do c.class_eval do
remove_method(:foo) remove_method(:foo)
end end
...@@ -724,7 +724,7 @@ end ...@@ -724,7 +724,7 @@ end
end end
a = c.new a = c.new
assert_true a.respond_to?(:foo), bug8005 assert_true a.respond_to?(:foo), bug8005
assert_nothing_raised(NoMethodError, bug8005) {a.send :foo} assert_nothing_raised(bug8005) {a.send :foo}
end end
# mruby has no visibility control # mruby has no visibility control
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment