diff options
| author | KOBAYASHI Shuji <[email protected]> | 2019-02-13 21:57:25 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2019-02-14 09:04:42 +0900 |
| commit | 6cdac5efb625aea72740acb7403896a44fe56209 (patch) | |
| tree | 49df1dfacb766c9cad56ef4b05b96c048a9fdcdb /test/assert.rb | |
| parent | a6c0aa8306de9d8a3932777deb7bc3f15ec96441 (diff) | |
| download | mruby-6cdac5efb625aea72740acb7403896a44fe56209.tar.gz mruby-6cdac5efb625aea72740acb7403896a44fe56209.zip | |
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
Diffstat (limited to 'test/assert.rb')
| -rw-r--r-- | test/assert.rb | 143 |
1 files changed, 75 insertions, 68 deletions
diff --git a/test/assert.rb b/test/assert.rb index 3ca8c8a3c..ec6fa98ad 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -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) @@ -198,29 +212,13 @@ def assert_nothing_raised(msg = nil) 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. 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 |
