summaryrefslogtreecommitdiffhomepage
path: root/test/assert.rb
diff options
context:
space:
mode:
authorKOBAYASHI Shuji <[email protected]>2019-04-26 21:36:29 +0900
committerKOBAYASHI Shuji <[email protected]>2019-04-26 21:48:40 +0900
commit1fb635ac03d3948898623126a8b3d7705e9cdb0f (patch)
tree9980e7943b584b8b67b36394133869548106bd5a /test/assert.rb
parentc078758644443fdac6f91867e00abb152f670599 (diff)
downloadmruby-1fb635ac03d3948898623126a8b3d7705e9cdb0f.tar.gz
mruby-1fb635ac03d3948898623126a8b3d7705e9cdb0f.zip
Add `assert_raise_with_message` and `assert_raise_with_message_pattern`
Diffstat (limited to 'test/assert.rb')
-rw-r--r--test/assert.rb36
1 files changed, 30 insertions, 6 deletions
diff --git a/test/assert.rb b/test/assert.rb
index 121bd0a8e..da313bf6a 100644
--- a/test/assert.rb
+++ b/test/assert.rb
@@ -6,13 +6,17 @@ $skip_test = 0
$asserts = []
$test_start = Time.now if Object.const_defined?(:Time)
+# For bintest on Ruby
unless RUBY_ENGINE == "mruby"
- # For bintest on Ruby
def t_print(*args)
print(*args)
$stdout.flush
nil
end
+
+ def _str_match?(pattern, str)
+ File.fnmatch?(pattern, str, File::FNM_EXTGLOB|File::FNM_DOTMATCH)
+ end
end
##
@@ -180,10 +184,7 @@ end
def assert_match(*args); _assert_match(true, *args) end
def assert_not_match(*args); _assert_match(false, *args) end
def _assert_match(affirmed, pattern, str, msg = nil)
- receiver, *args = RUBY_ENGINE == "mruby" ?
- [self, :_str_match?, pattern, str] :
- [File, :fnmatch?, pattern, str, File::FNM_EXTGLOB|File::FNM_DOTMATCH]
- unless ret = !receiver.__send__(*args) == !affirmed
+ unless ret = _str_match?(pattern, str) == affirmed
diff = " Expected #{pattern.inspect} to #{'not ' unless affirmed}match #{str.inspect}."
end
assert_true(ret, msg, diff)
@@ -217,8 +218,9 @@ def assert_raise(*exc)
exc = exc.empty? ? StandardError : exc.size == 1 ? exc[0] : exc
begin
yield
- rescue *exc
+ rescue *exc => e
pass
+ e
rescue Exception => e
diff = " #{exc} exception expected, not\n" \
" Class: <#{e.class}>\n" \
@@ -243,6 +245,28 @@ def assert_nothing_raised(msg = nil)
end
end
+def assert_raise_with_message(*args, &block)
+ _assert_raise_with_message(:plain, *args, &block)
+end
+def assert_raise_with_message_pattern(*args, &block)
+ _assert_raise_with_message(:pattern, *args, &block)
+end
+def _assert_raise_with_message(type, exc, exp_msg, msg = nil, &block)
+ e = msg ? assert_raise(exc, msg, &block) : assert_raise(exc, &block)
+ e ? ($mrbtest_assert_idx -= 1) : (return e)
+
+ err_msg = e.message
+ unless ret = type == :pattern ? _str_match?(exp_msg, err_msg) : exp_msg == err_msg
+ diff = " Expected Exception(#{exc}) was raised, but the message doesn't match.\n"
+ if type == :pattern
+ diff += " Expected #{exp_msg.inspect} to match #{err_msg.inspect}."
+ else
+ diff += assertion_diff(exp_msg, err_msg)
+ end
+ end
+ assert_true(ret, msg, diff)
+end
+
def pass
assert_true(true)
end