require "wildcard_matchers"
WildcardMatchers.wildcard_match?("string", /str/) #=> true
require "wildcard_matchers/rspec"
describe "wildcard_matcher" do
it "should wildcard match" do
{ :a => [ "hoge", "fuga" ] }.should wildcard_match(:a => [ is_a_string, /^fu/ ])
end
end
See specs, for more detail.
- is_a(class)
- is_a(String) === String #=> true
- is_a_string === String #=> true
- is_bool
- is_bool === true #=> true
- is_bool === false #=> true
- is_bool === object #=> false
- is_time
- is_time === "2012-05-13" #=> true
- is_uri
- is_uri(:host => "example.com") === "http://example.com" #=> true
- with_uri_template
- with_uri_template("http://example.com/users/{id}", "id" => "1") === "http://example.com/users/1" #=> true
- with_uri_template("http://example.com/users{?id}", "id" => "1") === "http://example.com/users?id=1" #=> true
- with_uri_template and witout_query!
- with_uri_template("http://example.com/users", "id" => "1").without_query! === "http://example.com/users?id=1" #=> true
- hash_includes
- hash_includes(:a) === { :a => 1 } #=> true
- hash_includes(:b) === { :a => 1 } #=> false
- hash_includes(:a => Integer) === { :a => 1 } #=> true
- hash_includes(:a => 2) === { :a => 1 } #=> false
- is_a_member_of
- is_a_member_of(0, 1) === 0 #=> true
- bag_of
- sored array comparison (Note: each item should comparable with every other item)
- nil_or
- nil_or(is_a_string) === nil #=> true
- nil_or(is_a_string) === "a" #=> true
- any_of
- any_of(String, /b/) === "a" #=> true
- any_of(Integer, /b/) === "a" #=> false
- all_of
- all_of(String, /a/) === "a" #=> true
- all_of(String, /b/) === "a" #=> false
- for_all
- for_all(is_a_string) === %w[ a b c ] #=> true
- for_any
- for_any(is_a_string) === [ 1, "1" ] #=> true
- responding
- responding(next: 2) === 1 #=> true (because 1.next #=> 2)
- using &
- for_all(is_a_string) & for_any(/hoge/) # every element is String and one matched /hoge/
- using |
- for_all(is_a_string) | for_all(is_a_integer) # every element is String or Integer
It is very simple. Recursive match using ===, and Class, Range, and Proc act as wildcard matchers.
You can create original matcher using lambda.
original_matcher = lamda do |actual|
(…as you like…)
end
wildcard_matcher?("actual", original_matcher)
Add this line to your application's Gemfile:
gem 'wildcard_matchers'
And then execute:
$ bundle
Or install it yourself as:
$ gem install wildcard_matchers
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request