diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-04-22 22:29:15 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-04-22 22:29:15 +0900 |
| commit | 7cf1bc39a7184e92b0fe534e610b288accc61e5a (patch) | |
| tree | caadc76a38aaec44327fd44558739e15a675d4ce /test | |
| parent | 5ab8ca41a5a64183784c739b82da9e4cd87bbc79 (diff) | |
| parent | 716a99b069461b11c2b46099119f8578cd2c8f3f (diff) | |
| download | mruby-7cf1bc39a7184e92b0fe534e610b288accc61e5a.tar.gz mruby-7cf1bc39a7184e92b0fe534e610b288accc61e5a.zip | |
Merge pull request #4356 from shuujii/add-assert_match-and-assert_not_match
Add `assert_match` and `assert_not_match`
Diffstat (limited to 'test')
| -rw-r--r-- | test/assert.rb | 33 | ||||
| -rw-r--r-- | test/t/module.rb | 7 |
2 files changed, 35 insertions, 5 deletions
diff --git a/test/assert.rb b/test/assert.rb index 385de49bd..121bd0a8e 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -157,6 +157,39 @@ def _assert_operator(affirmed, obj1, op, obj2 = $undefined, 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) diff --git a/test/t/module.rb b/test/t/module.rb index da0f78fad..09613e1bc 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -651,11 +651,8 @@ assert('Module#to_s') do assert_equal 'SetOuter', SetOuter.to_s assert_equal 'SetOuter::SetInner', SetOuter::SetInner.to_s - mod = Module.new - cls = Class.new - - assert_equal "#<Module:0x", mod.to_s[0,11] - assert_equal "#<Class:0x", cls.to_s[0,10] + assert_match "#<Module:0x*>", Module.new.to_s + assert_match "#<Class:0x*>", Class.new.to_s end assert('Module#inspect') do |
