wildcard_matchers 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +0 -2
- data/README.md +9 -0
- data/VERSION +1 -1
- data/lib/wildcard_matchers/helpers.rb +20 -0
- data/lib/wildcard_matchers/matchers.rb +6 -0
- data/lib/wildcard_matchers/rspec.rb +5 -0
- data/lib/wildcard_matchers.rb +2 -0
- data/spec/support/shared_examples.rb +21 -0
- data/spec/wildcard_matchers/helpers_spec.rb +18 -0
- data/spec/wildcard_matchers/matchers_spec.rb +4 -2
- data/spec/wildcard_matchers/rspec_spec.rb +10 -3
- metadata +5 -2
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -33,6 +33,15 @@ See specs, for more detail.
|
|
33
33
|
* hash_includes(:b) === { :a => 1 } #=> false
|
34
34
|
* hash_includes(:a => Integer) === { :a => 1 } #=> true
|
35
35
|
* hash_includes(:a => 2) === { :a => 1 } #=> false
|
36
|
+
* is_a_member_of
|
37
|
+
* is_a_member_of(0, 1) === 0 #=> true
|
38
|
+
|
39
|
+
### helpers
|
40
|
+
* nil_or
|
41
|
+
* nil_or(is_a_string) === nil #=> true
|
42
|
+
* nil_or(is_a_string) === "a" #=> true
|
43
|
+
* all
|
44
|
+
* all(is_a_string) === %w[ a b c ] #=> true
|
36
45
|
|
37
46
|
## How it works
|
38
47
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module WildcardMatchers
|
2
|
+
module Helpers
|
3
|
+
def nil_or(expected, &on_failure)
|
4
|
+
lambda do |actual|
|
5
|
+
nil === actual or wildcard_match?(actual, expected, &on_failure)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def all(expected = nil, &on_failure)
|
10
|
+
raise "expected or block is mandatory" unless expected or block_given?
|
11
|
+
|
12
|
+
expected ||= block
|
13
|
+
lambda do |actual|
|
14
|
+
actual.all? do |item|
|
15
|
+
wildcard_match(item, expected, &on_failure)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/wildcard_matchers.rb
CHANGED
@@ -23,3 +23,24 @@ shared_examples_for "not wildcard match" do |actual, matcher, *args|
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
shared_examples_for "wildcard match with helper" do |actual, helper, matcher, *args|
|
28
|
+
matcher_string =if matcher.is_a?(Symbol) and WildcardMatchers.respond_to?(matcher)
|
29
|
+
matcher.to_s
|
30
|
+
else
|
31
|
+
matcher.inspect
|
32
|
+
end
|
33
|
+
|
34
|
+
expected = helper.to_s + "(" + (args.size > 0 ?
|
35
|
+
"(#{matcher_string}(#{args.map(&:inspect).join(",")})" :
|
36
|
+
matcher_string) + ")"
|
37
|
+
|
38
|
+
it "#{actual.inspect} with #{expected}" do
|
39
|
+
if matcher.is_a?(Symbol) and WildcardMatchers.respond_to?(matcher)
|
40
|
+
# Note: some symbol comes here and may fail
|
41
|
+
wildcard_match?(actual, send(helper, send(matcher, *args))).should be_true
|
42
|
+
else
|
43
|
+
wildcard_match?(actual, send(helper, matcher)).should be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe WildcardMatchers::Helpers do
|
4
|
+
[ [ nil, :nil_or, String ],
|
5
|
+
[ "aaa", :nil_or, String ],
|
6
|
+
[ nil, :nil_or, :is_a_string ],
|
7
|
+
[ "aaa", :nil_or, :is_a_string ],
|
8
|
+
[ %w[ a b c ], :all, String ],
|
9
|
+
[ %w[ a b c ], :all, :is_a_string ],
|
10
|
+
[ %w[ a b c ], :all, :is_a, String ],
|
11
|
+
].each do |actual, helper, matcher, *args|
|
12
|
+
it_should_behave_like "wildcard match with helper", actual, helper, matcher, *args
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should match using lambda with helper" do
|
16
|
+
[ 2, 4, 6].should wildcard_match(all ->(item) { item % 2 == 0 })
|
17
|
+
end
|
18
|
+
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe WildcardMatchers::Matchers do
|
4
|
-
include WildcardMatchers
|
5
|
-
|
6
4
|
[ [ "string", String ],
|
7
5
|
[ 0, Integer ],
|
8
6
|
[ 0.1, Float ],
|
@@ -40,6 +38,8 @@ describe WildcardMatchers::Matchers do
|
|
40
38
|
[ [ { :a => 1, :b => 1, :c => 1 }, :hash_includes, :a, :b ],
|
41
39
|
[ { :a => 1, :b => 1, :c => 1 }, :hash_includes, :a, :b => 1 ],
|
42
40
|
[ { :a => 1, :b => 1, :c => 1 }, :hash_includes, :a, :b => Integer ],
|
41
|
+
[ "a", :is_a_member_of, %w[ a b ] ],
|
42
|
+
[ "a", :is_a_member_of, [ String ] ],
|
43
43
|
].each do |actual, matcher, *args|
|
44
44
|
it_should_behave_like "wildcard match", actual, matcher, *args
|
45
45
|
end
|
@@ -47,6 +47,8 @@ describe WildcardMatchers::Matchers do
|
|
47
47
|
[ [ { :a => 1, :b => 1, :c => 1 }, :hash_includes, :a, :d ],
|
48
48
|
[ { :a => 1, :b => 1, :c => 1 }, :hash_includes, :a, :b => 2 ],
|
49
49
|
[ { :a => 1, :b => 1, :c => 1 }, :hash_includes, :a, :b => String ],
|
50
|
+
[ "a", :is_a_member_of, %w[ b c ] ],
|
51
|
+
[ "a", :is_a_member_of, [ Integer ] ],
|
50
52
|
].each do |actual, matcher, *args|
|
51
53
|
it_should_behave_like "not wildcard match", actual, matcher, *args
|
52
54
|
end
|
@@ -1,13 +1,20 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe "matcher wildcard_match" do
|
4
|
-
[ [ 1, Integer ]
|
4
|
+
[ [ 1, Integer ],
|
5
5
|
].each do |actual, expected|
|
6
|
-
it "match #{actual} with #{expected}" do
|
6
|
+
it "match #{actual.inspect} with #{expected}" do
|
7
7
|
actual.should wildcard_match(expected)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
+
[ [ "1", :is_a_string ],
|
12
|
+
].each do |actual, expected|
|
13
|
+
it "match #{actual.inspect} with #{expected}" do
|
14
|
+
actual.should wildcard_match(send(expected))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
11
18
|
[ [ 1, String, ".: expect 1 to String" ],
|
12
19
|
[ [ 1 ], [ String ], ".[0]: expect 1 to String" ],
|
13
20
|
[ [ 1 ], [], ".: expect Array size 1 to 0" ],
|
@@ -15,7 +22,7 @@ describe "matcher wildcard_match" do
|
|
15
22
|
[ {}, {:a => 1}, "-keys: [:a]" ],
|
16
23
|
[ { :a => 1}, {:a => 0}, ".[:a]: expect 1 to 0" ],
|
17
24
|
].each do |actual, expected, failure_message|
|
18
|
-
it "not match #{actual} with #{expected} and return #{failure_message.inspect} as failure_message" do
|
25
|
+
it "not match #{actual.inspect} with #{expected} and return #{failure_message.inspect} as failure_message" do
|
19
26
|
begin
|
20
27
|
actual.should wildcard_match(expected)
|
21
28
|
fail # if matched come here and must fail
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wildcard_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -93,11 +93,13 @@ files:
|
|
93
93
|
- Rakefile
|
94
94
|
- VERSION
|
95
95
|
- lib/wildcard_matchers.rb
|
96
|
+
- lib/wildcard_matchers/helpers.rb
|
96
97
|
- lib/wildcard_matchers/matchers.rb
|
97
98
|
- lib/wildcard_matchers/rspec.rb
|
98
99
|
- spec/spec.watchr
|
99
100
|
- spec/spec_helper.rb
|
100
101
|
- spec/support/shared_examples.rb
|
102
|
+
- spec/wildcard_matchers/helpers_spec.rb
|
101
103
|
- spec/wildcard_matchers/matchers_spec.rb
|
102
104
|
- spec/wildcard_matchers/rspec_spec.rb
|
103
105
|
- spec/wildcard_matchers_spec.rb
|
@@ -130,6 +132,7 @@ test_files:
|
|
130
132
|
- spec/spec.watchr
|
131
133
|
- spec/spec_helper.rb
|
132
134
|
- spec/support/shared_examples.rb
|
135
|
+
- spec/wildcard_matchers/helpers_spec.rb
|
133
136
|
- spec/wildcard_matchers/matchers_spec.rb
|
134
137
|
- spec/wildcard_matchers/rspec_spec.rb
|
135
138
|
- spec/wildcard_matchers_spec.rb
|