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)
if $mrbtest_assert
$mrbtest_assert_idx += 1
unless ret
msg = "Expected #{ret.inspect} to be true" unless msg
diff = assertion_diff(true, ret) unless diff
msg ||= "Expected #{ret.inspect} to be true"
diff ||= assertion_diff(true, ret)
$mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
end
end
......@@ -96,74 +96,88 @@ def assert_false(ret, msg = nil, diff = nil)
!ret
end
def assert_equal(arg1, arg2 = nil, arg3 = nil)
if block_given?
exp, act, msg = arg1, yield, arg2
else
exp, act, msg = arg1, arg2, arg3
def assert_equal(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
unless ret
msg ||= "Expected to be equal"
diff = assertion_diff(exp, act)
end
msg = "Expected to be equal" unless msg
diff = assertion_diff(exp, act)
assert_true(exp == act, msg, diff)
assert_true(ret, msg, diff)
end
def assert_not_equal(arg1, arg2 = nil, arg3 = nil)
if block_given?
exp, act, msg = arg1, yield, arg2
else
exp, act, msg = arg1, arg2, arg3
def assert_not_equal(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
if ret
msg ||= "Expected to be not equal"
diff = assertion_diff(exp, act)
end
msg = "Expected to be not equal" unless msg
diff = assertion_diff(exp, act)
assert_false(exp == act, msg, diff)
assert_true(!ret, msg, diff)
end
def assert_same(arg1, arg2 = nil, arg3 = nil)
if block_given?
exp, act, msg = arg1, yield, arg2
else
exp, act, msg = arg1, arg2, arg3
def assert_same(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:equal?, exp, act_or_msg, msg, block)
unless ret
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__})"
end
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)
assert_true(ret, msg, diff)
end
def assert_not_same(arg1, arg2 = nil, arg3 = nil)
if block_given?
exp, act, msg = arg1, yield, arg2
else
exp, act, msg = arg1, arg2, arg3
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
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)
assert_true(!ret, msg, diff)
end
def assert_nil(obj, msg = nil)
msg = "Expected #{obj.inspect} to be nil" unless msg
diff = assertion_diff(nil, obj)
assert_true(obj.nil?, msg, diff)
unless ret = obj.nil?
msg ||= "Expected #{obj.inspect} to be nil"
diff = assertion_diff(nil, obj)
end
assert_true(ret, msg, diff)
end
def assert_include(collection, obj, msg = nil)
msg = "Expected #{collection.inspect} to include #{obj.inspect}" unless msg
diff = " Collection: #{collection.inspect}\n" +
" Object: #{obj.inspect}"
assert_true(collection.include?(obj), msg, diff)
unless ret = collection.include?(obj)
msg ||= "Expected #{collection.inspect} to include #{obj.inspect}"
diff = " Collection: #{collection.inspect}\n" +
" Object: #{obj.inspect}"
end
assert_true(ret, msg, diff)
end
def assert_not_include(collection, obj, msg = nil)
msg = "Expected #{collection.inspect} to not include #{obj.inspect}" unless msg
diff = " Collection: #{collection.inspect}\n" +
" Object: #{obj.inspect}"
assert_false(collection.include?(obj), msg, diff)
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+.
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
def assert_raise(*exc)
......@@ -197,22 +211,6 @@ def assert_nothing_raised(msg = nil)
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
# which were reported broken.
......@@ -220,7 +218,7 @@ def report()
t_print("\n")
$asserts.each do |msg|
t_print "#{msg}\n"
t_print("#{msg}\n")
end
$total_test = $ok_test+$ko_test+$kill_test
......@@ -249,6 +247,15 @@ def check_float(a, b)
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
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