matchi 2.1.1 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -2
- data/lib/matchi.rb +2 -0
- data/lib/matchi/helper.rb +2 -2
- data/lib/matchi/matcher/be_an_instance_of.rb +21 -5
- data/lib/matchi/matcher/satisfy.rb +45 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0160e87dd2bb35e34120f11d16c0ebb5a5d4b4c984612e37a8139cb65814ed6
|
4
|
+
data.tar.gz: 10ce199e0511fda23304b1f8b64bf1c7911512e77c2af6b2d90ad078d56caed0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bb3da470e32e8332b4fb4106ebec65d06f23c92f9fdc18ce6de655b0fdc3408142210aebfcfe45d30e79ce0b3856488658f3c107c5dab7164125561e2a44bd0
|
7
|
+
data.tar.gz: 55dce8aca5a1b3aeacb396da47e41e008bde7c189b9acc389a39ba03beb9a3eb883732b5dfaf476158662d1c396fd7a454f0b0a10cd8ed87489b88440771158c
|
data/README.md
CHANGED
@@ -98,10 +98,17 @@ be_nil.matches? { nil } # => true
|
|
98
98
|
**Type/class** matcher:
|
99
99
|
|
100
100
|
```ruby
|
101
|
-
be_an_instance_of = Matchi::Matcher::BeAnInstanceOf.new(String)
|
101
|
+
be_an_instance_of = Matchi::Matcher::BeAnInstanceOf.new(:String)
|
102
102
|
be_an_instance_of.matches? { "foo" } # => true
|
103
103
|
```
|
104
104
|
|
105
|
+
**Satisfy** matcher:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
satisfy = Matchi::Matcher::Satisfy.new { |value| value == 42 }
|
109
|
+
satisfy.matches? { 42 } # => true
|
110
|
+
```
|
111
|
+
|
105
112
|
### Custom matchers
|
106
113
|
|
107
114
|
Custom matchers can easily be defined for expressing expectations.
|
@@ -183,7 +190,7 @@ The set of loaded matcher then becomes accessible via a dynamically generated in
|
|
183
190
|
```ruby
|
184
191
|
matcher = MatcherCollection.new
|
185
192
|
matcher.equal(42).matches? { 44 } # => false
|
186
|
-
matcher.be_an_instance_of(String).matches? { "안녕하세요" } # => true
|
193
|
+
matcher.be_an_instance_of(:String).matches? { "안녕하세요" } # => true
|
187
194
|
```
|
188
195
|
|
189
196
|
## Contact
|
data/lib/matchi.rb
CHANGED
data/lib/matchi/helper.rb
CHANGED
@@ -32,8 +32,8 @@ module Matchi
|
|
32
32
|
|
33
33
|
matcher_klass = ::Matchi::Matcher.const_get(matcher_const)
|
34
34
|
|
35
|
-
define_method(matcher_klass.to_sym) do |*args|
|
36
|
-
matcher_klass.new(*args)
|
35
|
+
define_method(matcher_klass.to_sym) do |*args, **kwargs, &block|
|
36
|
+
matcher_klass.new(*args, **kwargs, &block)
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -8,13 +8,18 @@ module Matchi
|
|
8
8
|
class BeAnInstanceOf < ::Matchi::Matcher::Base
|
9
9
|
# Initialize the matcher with an object.
|
10
10
|
#
|
11
|
-
# @example A
|
12
|
-
# Matchi::Matcher::BeAnInstanceOf.new(
|
11
|
+
# @example A duck matcher
|
12
|
+
# Matchi::Matcher::BeAnInstanceOf.new(:Duck)
|
13
13
|
#
|
14
|
-
# @param expected [
|
14
|
+
# @param expected [#to_s] The name of a module.
|
15
15
|
def initialize(expected)
|
16
16
|
super()
|
17
|
-
@expected = expected
|
17
|
+
@expected = String(expected).to_sym
|
18
|
+
end
|
19
|
+
|
20
|
+
# (see Base#inspect)
|
21
|
+
def inspect
|
22
|
+
"#{self.class}(#{expected})"
|
18
23
|
end
|
19
24
|
|
20
25
|
# Boolean comparison between the class of the actual value and the
|
@@ -24,11 +29,22 @@ module Matchi
|
|
24
29
|
# be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(String)
|
25
30
|
# be_an_instance_of.matches? { "foo" } # => true
|
26
31
|
#
|
32
|
+
# be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(:String)
|
33
|
+
# be_an_instance_of.matches? { "foo" } # => true
|
34
|
+
#
|
35
|
+
# be_an_instance_of = Matchi::Matcher::BeInstanceOf.new("String")
|
36
|
+
# be_an_instance_of.matches? { "foo" } # => true
|
37
|
+
#
|
27
38
|
# @yieldreturn [#class] the actual value to compare to the expected one.
|
28
39
|
#
|
29
40
|
# @return [Boolean] Comparison between actual and expected values.
|
30
41
|
def matches?(*, **)
|
31
|
-
expected.equal?(yield.class)
|
42
|
+
self.class.const_get(expected).equal?(yield.class)
|
43
|
+
end
|
44
|
+
|
45
|
+
# (see Base#to_s)
|
46
|
+
def to_s
|
47
|
+
"#{self.class.to_sym} #{expected}"
|
32
48
|
end
|
33
49
|
end
|
34
50
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "base"
|
4
|
+
|
5
|
+
module Matchi
|
6
|
+
module Matcher
|
7
|
+
# *Satisfy* matcher.
|
8
|
+
class Satisfy < ::Matchi::Matcher::Base
|
9
|
+
# Initialize the matcher with a block.
|
10
|
+
#
|
11
|
+
# @example The number 42 matcher.
|
12
|
+
# Matchi::Matcher::Satisfy.new { |value| value == 42 }
|
13
|
+
#
|
14
|
+
# @param block [Proc] A block of code.
|
15
|
+
def initialize(&block)
|
16
|
+
super()
|
17
|
+
@expected = block
|
18
|
+
end
|
19
|
+
|
20
|
+
# (see Base#inspect)
|
21
|
+
def inspect
|
22
|
+
"#{self.class}(&block)"
|
23
|
+
end
|
24
|
+
|
25
|
+
# Boolean comparison between the actual value and the expected value.
|
26
|
+
#
|
27
|
+
# @example Is it equal to 42
|
28
|
+
# equal = Matchi::Matcher::Satisfy.new { |value| value == 42 }
|
29
|
+
# equal.matches? { 42 } # => true
|
30
|
+
#
|
31
|
+
# @yieldreturn [#object_id] The actual value to compare to the expected
|
32
|
+
# one.
|
33
|
+
#
|
34
|
+
# @return [Boolean] Comparison between actual and expected values.
|
35
|
+
def matches?(*, **)
|
36
|
+
expected.call(yield)
|
37
|
+
end
|
38
|
+
|
39
|
+
# (see Base#to_s)
|
40
|
+
def to_s
|
41
|
+
"#{self.class.to_sym} &block"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: matchi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- lib/matchi/matcher/equal.rb
|
143
143
|
- lib/matchi/matcher/match.rb
|
144
144
|
- lib/matchi/matcher/raise_exception.rb
|
145
|
+
- lib/matchi/matcher/satisfy.rb
|
145
146
|
homepage: https://github.com/fixrb/matchi
|
146
147
|
licenses:
|
147
148
|
- MIT
|