diff options
| author | KOBAYASHI Shuji <[email protected]> | 2019-04-03 21:38:47 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2019-04-14 19:06:28 +0900 |
| commit | 716a99b069461b11c2b46099119f8578cd2c8f3f (patch) | |
| tree | cea958ad993ca9547c81a00d8630329ca05f5c66 /test/assert.rb | |
| parent | 73feef9766c56c123039392adb0c516d9aafb02e (diff) | |
| download | mruby-716a99b069461b11c2b46099119f8578cd2c8f3f.tar.gz mruby-716a99b069461b11c2b46099119f8578cd2c8f3f.zip | |
Add `assert_match` and `assert_not_match`
Diffstat (limited to 'test/assert.rb')
| -rw-r--r-- | test/assert.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/assert.rb b/test/assert.rb index c57b04c12..4b01bd450 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -137,6 +137,39 @@ def _assert_include(affirmed, collection, obj, msg = nil) end ## +# Fail unless +str+ matches against +pattern+. +# +# +pattern+ is interpreted as pattern for File.fnmatch?. It may contain the +# following metacharacters: +# +# <code>*</code> :: +# Matches any string. +# +# <code>?</code> :: +# Matches any one character. +# +# <code>[_SET_]</code>, <code>[^_SET_]</code> (<code>[!_SET_]</code>) :: +# Matches any one character in _SET_. Behaves like character sets in +# Regexp, including set negation (<code>[^a-z]</code>). +# +# <code>{_A_,_B_}</code> :: +# Matches pattern _A_ or pattern _B_. +# +# <code> \ </code> :: +# Escapes the next character. +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 + diff = " Expected #{pattern.inspect} to #{'not ' unless affirmed}match #{str.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) |
