Unverified Commit 84241d3d authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4334 from...

Merge pull request #4334 from shuujii/remove-redundant-content-in-assertion-failure-message-and-diff

Remove redundant content in assertion failure message and diff
parents 19191a54 2b72bacc
...@@ -25,7 +25,9 @@ def assertion_string(err, str, iso=nil, e=nil, bt=nil) ...@@ -25,7 +25,9 @@ def assertion_string(err, str, iso=nil, e=nil, bt=nil)
msg += " (#{GEMNAME == 'mruby-test' ? 'core' : "mrbgems: #{GEMNAME}"})" msg += " (#{GEMNAME == 'mruby-test' ? 'core' : "mrbgems: #{GEMNAME}"})"
if $mrbtest_assert && $mrbtest_assert.size > 0 if $mrbtest_assert && $mrbtest_assert.size > 0
$mrbtest_assert.each do |idx, assert_msg, diff| $mrbtest_assert.each do |idx, assert_msg, diff|
msg += "\n - Assertion[#{idx}] Failed: #{assert_msg}\n#{diff}" msg += "\n - Assertion[#{idx}]"
msg += " #{assert_msg}." if assert_msg && !assert_msg.empty?
msg += "\n#{diff}" if diff && !diff.empty?
end end
end end
msg += "\nbacktrace:\n\t#{bt.join("\n\t")}" if bt msg += "\nbacktrace:\n\t#{bt.join("\n\t")}" if bt
...@@ -70,35 +72,31 @@ def assert(str = 'Assertion failed', iso = '') ...@@ -70,35 +72,31 @@ def assert(str = 'Assertion failed', iso = '')
end end
def assertion_diff(exp, act) def assertion_diff(exp, act)
" Expected: #{exp.inspect}\n" + " Expected: #{exp.inspect}\n" \
" Actual: #{act.inspect}" " Actual: #{act.inspect}"
end end
def assert_true(ret, msg = nil, diff = nil) def assert_true(obj, msg = nil, diff = nil)
if $mrbtest_assert if $mrbtest_assert
$mrbtest_assert_idx += 1 $mrbtest_assert_idx += 1
unless ret == true unless obj == true
msg ||= "Expected #{ret.inspect} to be true" diff ||= " Expected #{obj.inspect} to be true."
diff ||= assertion_diff(true, ret)
$mrbtest_assert.push([$mrbtest_assert_idx, msg, diff]) $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
end end
end end
ret obj
end end
def assert_false(ret, msg = nil, diff = nil) def assert_false(obj, msg = nil, diff = nil)
unless ret == false unless obj == false
msg ||= "Expected #{ret.inspect} to be false" diff ||= " Expected #{obj.inspect} to be false."
diff ||= assertion_diff(false, ret)
end end
assert_true(!ret, msg, diff) assert_true(!obj, msg, diff)
!ret
end end
def assert_equal(exp, act_or_msg = nil, msg = nil, &block) def assert_equal(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block) ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
unless ret unless ret
msg ||= "Expected to be equal"
diff = assertion_diff(exp, act) diff = assertion_diff(exp, act)
end end
assert_true(ret, msg, diff) assert_true(ret, msg, diff)
...@@ -107,64 +105,44 @@ end ...@@ -107,64 +105,44 @@ end
def assert_not_equal(exp, act_or_msg = nil, msg = nil, &block) def assert_not_equal(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block) ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
if ret if ret
msg ||= "Expected to be not equal" diff = " Expected #{act.inspect} to not be equal to #{exp.inspect}."
diff = assertion_diff(exp, act)
end end
assert_true(!ret, msg, diff) assert_true(!ret, msg, diff)
end end
def assert_same(exp, act_or_msg = nil, msg = nil, &block) def assert_same(*args); _assert_same(true, *args) end
ret, exp, act, msg = _eval_assertion(:equal?, exp, act_or_msg, msg, block) def assert_not_same(*args); _assert_same(false, *args) end
unless ret def _assert_same(affirmed, exp, act, msg = nil)
msg ||= "Expected #{act.inspect} to be the same object as #{exp.inspect}" unless ret = exp.equal?(act) == affirmed
diff = " Expected: #{exp.inspect} (class=#{exp.class}, oid=#{exp.__id__})\n" + exp_str, act_str = [exp, act].map do |o|
" Actual: #{act.inspect} (class=#{act.class}, oid=#{act.__id__})" "#{o.inspect} (class=#{o.class}, oid=#{o.__id__})"
end
diff = " Expected #{act_str} to #{'not ' unless affirmed}be the same as #{exp_str}."
end end
assert_true(ret, msg, diff) assert_true(ret, msg, diff)
end end
def assert_not_same(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:equal?, exp, act_or_msg, msg, block)
if ret
msg ||= "Expected #{act.inspect} to not be the same object as #{exp.inspect}"
diff = " Expected: #{exp.inspect} (class=#{exp.class}, oid=#{exp.__id__})\n" +
" Actual: #{act.inspect} (class=#{act.class}, oid=#{act.__id__})"
end
assert_true(!ret, msg, diff)
end
def assert_nil(obj, msg = nil) def assert_nil(obj, msg = nil)
unless ret = obj.nil? unless ret = obj.nil?
msg ||= "Expected #{obj.inspect} to be nil" diff = " Expected #{obj.inspect} to be nil."
diff = assertion_diff(nil, obj)
end end
assert_true(ret, msg, diff) assert_true(ret, msg, diff)
end end
def assert_include(collection, obj, msg = nil) def assert_include(*args); _assert_include(true, *args) end
unless ret = collection.include?(obj) def assert_not_include(*args); _assert_include(false, *args) end
msg ||= "Expected #{collection.inspect} to include #{obj.inspect}" def _assert_include(affirmed, collection, obj, msg = nil)
diff = " Collection: #{collection.inspect}\n" + unless ret = collection.include?(obj) == affirmed
" Object: #{obj.inspect}" diff = " Expected #{collection.inspect} to #{'not ' unless affirmed}include #{obj.inspect}."
end end
assert_true(ret, msg, diff) assert_true(ret, msg, diff)
end end
def assert_not_include(collection, obj, msg = nil)
if ret = collection.include?(obj)
msg ||= "Expected #{collection.inspect} to not include #{obj.inspect}"
diff = " Collection: #{collection.inspect}\n" +
" Object: #{obj.inspect}"
end
assert_true(!ret, msg, diff)
end
## ##
# Fails unless +obj+ is a kind of +cls+. # Fails unless +obj+ is a kind of +cls+.
def assert_kind_of(cls, obj, msg = nil) def assert_kind_of(cls, obj, msg = nil)
unless ret = obj.kind_of?(cls) unless ret = obj.kind_of?(cls)
msg ||= "Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}" diff = " Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}."
diff = assertion_diff(cls, obj.class)
end end
assert_true(ret, msg, diff) assert_true(ret, msg, diff)
end end
...@@ -173,26 +151,25 @@ end ...@@ -173,26 +151,25 @@ end
# Fails unless +exp+ is equal to +act+ in terms of a Float # Fails unless +exp+ is equal to +act+ in terms of a Float
def assert_float(exp, act, msg = nil) def assert_float(exp, act, msg = nil)
unless ret = check_float(exp, act) unless ret = check_float(exp, act)
msg ||= "Float #{exp} expected to be equal to float #{act}" diff = " Expected |#{exp} - #{act}| (#{(exp-act).abs}) to be <= #{Mrbtest::FLOAT_TOLERANCE}."
diff = assertion_diff(exp, act)
end end
assert_true(ret, msg, diff) assert_true(ret, msg, diff)
end end
def assert_raise(*exc) def assert_raise(*exc)
msg = (exc.last.is_a? String) ? exc.pop : nil msg = (exc.last.is_a? String) ? exc.pop : nil
exc = exc.empty? ? StandardError : exc.size == 1 ? exc[0] : exc
begin begin
yield yield
rescue *exc rescue *exc
assert_true(true) assert_true(true)
rescue Exception => e rescue Exception => e
msg ||= "Expected to raise #{exc}, not" diff = " #{exc} exception expected, not\n" \
diff = " Class: <#{e.class}>\n" + " Class: <#{e.class}>\n" \
" Message: #{e.message}" " Message: <#{e}>"
assert_true(false, msg, diff) assert_true(false, msg, diff)
else else
msg ||= "Expected to raise #{exc} but nothing was raised." diff = " #{exc} expected but nothing was raised."
diff = ""
assert_true(false, msg, diff) assert_true(false, msg, diff)
end end
end end
...@@ -201,9 +178,9 @@ def assert_nothing_raised(msg = nil) ...@@ -201,9 +178,9 @@ def assert_nothing_raised(msg = nil)
begin begin
yield yield
rescue Exception => e rescue Exception => e
msg ||= "Expected not to raise #{e} but it raised" diff = " Exception raised:\n" \
diff = " Class: <#{e.class}>\n" + " Class: <#{e.class}>\n" \
" Message: #{e.message}" " Message: <#{e}>"
assert_true(false, msg, diff) assert_true(false, msg, diff)
else else
assert_true(true) assert_true(true)
......
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