Commit 6cdac5ef authored by KOBAYASHI Shuji's avatar KOBAYASHI Shuji

Lazy message/diff creation for assertion in `test/assert.rb`

Include the following changes too:

- Extract part of logic with block to a method
- Use `var ||= ...` instead of `var = ... unless var`
- Unify using parentheses for `t_print`
- Change methods order
parent a6c0aa83
...@@ -79,8 +79,8 @@ def assert_true(ret, msg = nil, diff = nil) ...@@ -79,8 +79,8 @@ def assert_true(ret, msg = nil, diff = nil)
if $mrbtest_assert if $mrbtest_assert
$mrbtest_assert_idx += 1 $mrbtest_assert_idx += 1
unless ret unless ret
msg = "Expected #{ret.inspect} to be true" unless msg msg ||= "Expected #{ret.inspect} to be true"
diff = assertion_diff(true, ret) unless diff 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
...@@ -96,74 +96,88 @@ def assert_false(ret, msg = nil, diff = nil) ...@@ -96,74 +96,88 @@ def assert_false(ret, msg = nil, diff = nil)
!ret !ret
end end
def assert_equal(arg1, arg2 = nil, arg3 = nil) def assert_equal(exp, act_or_msg = nil, msg = nil, &block)
if block_given? ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
exp, act, msg = arg1, yield, arg2 unless ret
else msg ||= "Expected to be equal"
exp, act, msg = arg1, arg2, arg3 diff = assertion_diff(exp, act)
end end
assert_true(ret, msg, diff)
msg = "Expected to be equal" unless msg
diff = assertion_diff(exp, act)
assert_true(exp == act, msg, diff)
end end
def assert_not_equal(arg1, arg2 = nil, arg3 = nil) def assert_not_equal(exp, act_or_msg = nil, msg = nil, &block)
if block_given? ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
exp, act, msg = arg1, yield, arg2 if ret
else msg ||= "Expected to be not equal"
exp, act, msg = arg1, arg2, arg3 diff = assertion_diff(exp, act)
end end
assert_true(!ret, msg, diff)
msg = "Expected to be not equal" unless msg
diff = assertion_diff(exp, act)
assert_false(exp == act, msg, diff)
end end
def assert_same(arg1, arg2 = nil, arg3 = nil) def assert_same(exp, act_or_msg = nil, msg = nil, &block)
if block_given? ret, exp, act, msg = _eval_assertion(:equal?, exp, act_or_msg, msg, block)
exp, act, msg = arg1, yield, arg2 unless ret
else msg ||= "Expected #{act.inspect} to be the same object as #{exp.inspect}"
exp, act, msg = arg1, arg2, arg3 diff = " Expected: #{exp.inspect} (class=#{exp.class}, oid=#{exp.__id__})\n" +
" Actual: #{act.inspect} (class=#{act.class}, oid=#{act.__id__})"
end end
assert_true(ret, msg, diff)
msg ||= "Expected #{act.inspect} to 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__})"
assert_true(exp.equal?(act), msg, diff)
end end
def assert_not_same(arg1, arg2 = nil, arg3 = nil) def assert_not_same(exp, act_or_msg = nil, msg = nil, &block)
if block_given? ret, exp, act, msg = _eval_assertion(:equal?, exp, act_or_msg, msg, block)
exp, act, msg = arg1, yield, arg2 if ret
else msg ||= "Expected #{act.inspect} to not be the same object as #{exp.inspect}"
exp, act, msg = arg1, arg2, arg3 diff = " Expected: #{exp.inspect} (class=#{exp.class}, oid=#{exp.__id__})\n" +
" Actual: #{act.inspect} (class=#{act.class}, oid=#{act.__id__})"
end end
assert_true(!ret, msg, diff)
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__})"
assert_false(exp.equal?(act), msg, diff)
end end
def assert_nil(obj, msg = nil) def assert_nil(obj, msg = nil)
msg = "Expected #{obj.inspect} to be nil" unless msg unless ret = obj.nil?
diff = assertion_diff(nil, obj) msg ||= "Expected #{obj.inspect} to be nil"
assert_true(obj.nil?, msg, diff) diff = assertion_diff(nil, obj)
end
assert_true(ret, msg, diff)
end end
def assert_include(collection, obj, msg = nil) def assert_include(collection, obj, msg = nil)
msg = "Expected #{collection.inspect} to include #{obj.inspect}" unless msg unless ret = collection.include?(obj)
diff = " Collection: #{collection.inspect}\n" + msg ||= "Expected #{collection.inspect} to include #{obj.inspect}"
" Object: #{obj.inspect}" diff = " Collection: #{collection.inspect}\n" +
assert_true(collection.include?(obj), msg, diff) " Object: #{obj.inspect}"
end
assert_true(ret, msg, diff)
end end
def assert_not_include(collection, obj, msg = nil) def assert_not_include(collection, obj, msg = nil)
msg = "Expected #{collection.inspect} to not include #{obj.inspect}" unless msg if ret = collection.include?(obj)
diff = " Collection: #{collection.inspect}\n" + msg ||= "Expected #{collection.inspect} to not include #{obj.inspect}"
" Object: #{obj.inspect}" diff = " Collection: #{collection.inspect}\n" +
assert_false(collection.include?(obj), msg, diff) " Object: #{obj.inspect}"
end
assert_true(!ret, msg, diff)
end
##
# Fails unless +obj+ is a kind of +cls+.
def assert_kind_of(cls, obj, msg = nil)
unless ret = obj.kind_of?(cls)
msg ||= "Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}"
diff = assertion_diff(cls, obj.class)
end
assert_true(ret, msg, diff)
end
##
# Fails unless +exp+ is equal to +act+ in terms of a Float
def assert_float(exp, act, msg = nil)
unless ret = check_float(exp, act)
msg ||= "Float #{exp} expected to be equal to float #{act}"
diff = assertion_diff(exp, act)
end
assert_true(ret, msg, diff)
end end
def assert_raise(*exc) def assert_raise(*exc)
...@@ -197,22 +211,6 @@ def assert_nothing_raised(msg = nil) ...@@ -197,22 +211,6 @@ def assert_nothing_raised(msg = nil)
end end
end end
##
# Fails unless +obj+ is a kind of +cls+.
def assert_kind_of(cls, obj, msg = nil)
msg = "Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}" unless msg
diff = assertion_diff(cls, obj.class)
assert_true(obj.kind_of?(cls), msg, diff)
end
##
# Fails unless +exp+ is equal to +act+ in terms of a Float
def assert_float(exp, act, msg = nil)
msg = "Float #{exp} expected to be equal to float #{act}" unless msg
diff = assertion_diff(exp, act)
assert_true check_float(exp, act), msg, diff
end
## ##
# Report the test result and print all assertions # Report the test result and print all assertions
# which were reported broken. # which were reported broken.
...@@ -220,7 +218,7 @@ def report() ...@@ -220,7 +218,7 @@ def report()
t_print("\n") t_print("\n")
$asserts.each do |msg| $asserts.each do |msg|
t_print "#{msg}\n" t_print("#{msg}\n")
end end
$total_test = $ok_test+$ko_test+$kill_test $total_test = $ok_test+$ko_test+$kill_test
...@@ -249,6 +247,15 @@ def check_float(a, b) ...@@ -249,6 +247,15 @@ def check_float(a, b)
end end
end end
def _eval_assertion(meth, exp, act_or_msg, msg, block)
if block
exp, act, msg = exp, block.call, act_or_msg
else
exp, act, msg = exp, act_or_msg, msg
end
return exp.__send__(meth, act), exp, act, msg
end
## ##
# Skip the test # Skip the test
class MRubyTestSkip < NotImplementedError class MRubyTestSkip < NotImplementedError
......
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