matchi 2.2.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -1
- data/lib/matchi.rb +2 -0
- data/lib/matchi/helper.rb +2 -2
- data/lib/matchi/matcher/be_an_instance_of.rb +14 -4
- data/lib/matchi/matcher/change.rb +78 -0
- data/lib/matchi/matcher/change/by.rb +57 -0
- data/lib/matchi/matcher/change/by_at_least.rb +57 -0
- data/lib/matchi/matcher/change/by_at_most.rb +57 -0
- data/lib/matchi/matcher/change/from.rb +35 -0
- data/lib/matchi/matcher/change/from/to.rb +68 -0
- data/lib/matchi/matcher/change/to.rb +56 -0
- data/lib/matchi/matcher/satisfy.rb +45 -0
- metadata +10 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 99d5b67cbb65b32a588adc888741aa1b43d86dd26835404388de5444bee6c54c
|
|
4
|
+
data.tar.gz: 6188812229591bbb66a7fe458eaaaf4cd5707d00cefcc8502dfc3e4ed41195a2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3f00907f22ba9615eeab7541ddba62e6507f6c3609268699b4c20fda2435c338e9307fec63a942a9c49ad105131c6d29f47354fdf8d7e5d8b28fc418f060e9f0
|
|
7
|
+
data.tar.gz: 4aeea7b5f52278b733c58180753e95d6ab88a47fbc162242ecf63bcfc3bc44ce018976fa544ef48887724d247e6fcb37ccc6372c89fedb0661d008dcc46061b5
|
data/README.md
CHANGED
|
@@ -102,6 +102,37 @@ be_an_instance_of = Matchi::Matcher::BeAnInstanceOf.new(:String)
|
|
|
102
102
|
be_an_instance_of.matches? { "foo" } # => true
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
+
**Change** matcher:
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
object = []
|
|
109
|
+
change = Matchi::Matcher::Change.new(object, :length).by(1)
|
|
110
|
+
change.matches? { object << 1 } # => true
|
|
111
|
+
|
|
112
|
+
object = []
|
|
113
|
+
change = Matchi::Matcher::Change.new(object, :length).by_at_least(1)
|
|
114
|
+
change.matches? { object << 1 } # => true
|
|
115
|
+
|
|
116
|
+
object = []
|
|
117
|
+
change = Matchi::Matcher::Change.new(object, :length).by_at_most(1)
|
|
118
|
+
change.matches? { object << 1 } # => true
|
|
119
|
+
|
|
120
|
+
object = "foo"
|
|
121
|
+
change = Matchi::Matcher::Change.new(object, :to_s).from("foo").to("FOO")
|
|
122
|
+
change.matches? { object.upcase! } # => true
|
|
123
|
+
|
|
124
|
+
object = "foo"
|
|
125
|
+
change = Matchi::Matcher::Change.new(object, :to_s).to("FOO")
|
|
126
|
+
change.matches? { object.upcase! } # => true
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Satisfy** matcher:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
satisfy = Matchi::Matcher::Satisfy.new { |value| value == 42 }
|
|
133
|
+
satisfy.matches? { 42 } # => true
|
|
134
|
+
```
|
|
135
|
+
|
|
105
136
|
### Custom matchers
|
|
106
137
|
|
|
107
138
|
Custom matchers can easily be defined for expressing expectations.
|
|
@@ -183,7 +214,7 @@ The set of loaded matcher then becomes accessible via a dynamically generated in
|
|
|
183
214
|
```ruby
|
|
184
215
|
matcher = MatcherCollection.new
|
|
185
216
|
matcher.equal(42).matches? { 44 } # => false
|
|
186
|
-
matcher.be_an_instance_of(String).matches? { "안녕하세요" } # => true
|
|
217
|
+
matcher.be_an_instance_of(:String).matches? { "안녕하세요" } # => true
|
|
187
218
|
```
|
|
188
219
|
|
|
189
220
|
## 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
14
|
# @param expected [#to_s] The name of a module.
|
|
15
15
|
def initialize(expected)
|
|
16
16
|
super()
|
|
17
|
-
@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
|
|
@@ -34,7 +39,12 @@ module Matchi
|
|
|
34
39
|
#
|
|
35
40
|
# @return [Boolean] Comparison between actual and expected values.
|
|
36
41
|
def matches?(*, **)
|
|
37
|
-
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}"
|
|
38
48
|
end
|
|
39
49
|
end
|
|
40
50
|
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative File.join("change", "by_at_least")
|
|
4
|
+
require_relative File.join("change", "by_at_most")
|
|
5
|
+
require_relative File.join("change", "by")
|
|
6
|
+
require_relative File.join("change", "from")
|
|
7
|
+
require_relative File.join("change", "to")
|
|
8
|
+
|
|
9
|
+
module Matchi
|
|
10
|
+
module Matcher
|
|
11
|
+
# Wraps the target of a change matcher.
|
|
12
|
+
class Change
|
|
13
|
+
# Returns a symbol identifying the matcher.
|
|
14
|
+
def self.to_sym
|
|
15
|
+
:change
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Initialize a wrapper of the change matcher with an object and the name
|
|
19
|
+
# of one of its methods.
|
|
20
|
+
#
|
|
21
|
+
# @example The wrapper of the change matcher on `"foo".to_s` value.
|
|
22
|
+
# Matchi::Matcher::Change.new("foo", :to_s)
|
|
23
|
+
#
|
|
24
|
+
# @param object [#object_id] An object.
|
|
25
|
+
# @param method [Symbol] The name of a method.
|
|
26
|
+
# @param args [Array] A list of arguments.
|
|
27
|
+
# @param kwargs [Hash] A list of keyword arguments.
|
|
28
|
+
def initialize(object, method, *args, **kwargs, &block)
|
|
29
|
+
@state = -> { object.send(method, *args, **kwargs, &block) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Specifies a minimum delta of the expected change.
|
|
33
|
+
#
|
|
34
|
+
# @param expected [#object_id] The minimum delta of the expected change.
|
|
35
|
+
#
|
|
36
|
+
# @return [#matches?] A *change by at least* matcher.
|
|
37
|
+
def by_at_least(expected)
|
|
38
|
+
ByAtLeast.new(expected, &@state)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Specifies a maximum delta of the expected change.
|
|
42
|
+
#
|
|
43
|
+
# @param expected [#object_id] The maximum delta of the expected change.
|
|
44
|
+
#
|
|
45
|
+
# @return [#matches?] A *change by at most* matcher.
|
|
46
|
+
def by_at_most(expected)
|
|
47
|
+
ByAtMost.new(expected, &@state)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Specifies the delta of the expected change.
|
|
51
|
+
#
|
|
52
|
+
# @param expected [#object_id] The delta of the expected change.
|
|
53
|
+
#
|
|
54
|
+
# @return [#matches?] A *change by* matcher.
|
|
55
|
+
def by(expected)
|
|
56
|
+
By.new(expected, &@state)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Specifies the original value.
|
|
60
|
+
#
|
|
61
|
+
# @param expected [#object_id] The original value.
|
|
62
|
+
#
|
|
63
|
+
# @return [#matches?] A *change from* wrapper.
|
|
64
|
+
def from(expected)
|
|
65
|
+
From.new(expected, &@state)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Specifies the new value to expect.
|
|
69
|
+
#
|
|
70
|
+
# @param expected [#object_id] The new value to expect.
|
|
71
|
+
#
|
|
72
|
+
# @return [#matches?] A *change to* matcher.
|
|
73
|
+
def to(expected)
|
|
74
|
+
To.new(expected, &@state)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative File.join("..", "base")
|
|
4
|
+
|
|
5
|
+
module Matchi
|
|
6
|
+
module Matcher
|
|
7
|
+
class Change
|
|
8
|
+
# *Change by* matcher.
|
|
9
|
+
class By < ::Matchi::Matcher::Base
|
|
10
|
+
# Returns a symbol identifying the matcher.
|
|
11
|
+
def self.to_sym
|
|
12
|
+
:change
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Initialize the matcher with an object.
|
|
16
|
+
#
|
|
17
|
+
# @example The change by 1 matcher.
|
|
18
|
+
# object = []
|
|
19
|
+
# Matchi::Matcher::Change::By.new(1) { object.length }
|
|
20
|
+
#
|
|
21
|
+
# @param expected [#object_id] An expected delta.
|
|
22
|
+
# @param state [Proc] A block of code to execute to get the
|
|
23
|
+
# state of the object.
|
|
24
|
+
def initialize(expected, &state)
|
|
25
|
+
super()
|
|
26
|
+
@expected = expected
|
|
27
|
+
@state = state
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Boolean comparison on the expected change by comparing the value
|
|
31
|
+
# before and after the code execution.
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
# object = []
|
|
35
|
+
# change = Matchi::Matcher::Change::By.new(1) { object.length }
|
|
36
|
+
# change.matches? { object << "foo" } # => true
|
|
37
|
+
#
|
|
38
|
+
# @yieldreturn [#object_id] The block of code to execute.
|
|
39
|
+
#
|
|
40
|
+
# @return [Boolean] Comparison between the value before and after the
|
|
41
|
+
# code execution.
|
|
42
|
+
def matches?(*, **)
|
|
43
|
+
value_before = @state.call
|
|
44
|
+
yield
|
|
45
|
+
value_after = @state.call
|
|
46
|
+
|
|
47
|
+
expected == (value_after - value_before)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# (see Base#to_s)
|
|
51
|
+
def to_s
|
|
52
|
+
"change by #{expected.inspect}"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative File.join("..", "base")
|
|
4
|
+
|
|
5
|
+
module Matchi
|
|
6
|
+
module Matcher
|
|
7
|
+
class Change
|
|
8
|
+
# *Change by at least* matcher.
|
|
9
|
+
class ByAtLeast < ::Matchi::Matcher::Base
|
|
10
|
+
# Returns a symbol identifying the matcher.
|
|
11
|
+
def self.to_sym
|
|
12
|
+
:change
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Initialize the matcher with an object.
|
|
16
|
+
#
|
|
17
|
+
# @example The change by at least 1 matcher.
|
|
18
|
+
# object = []
|
|
19
|
+
# Matchi::Matcher::Change::ByAtLeast.new(1) { object.length }
|
|
20
|
+
#
|
|
21
|
+
# @param expected [#object_id] An expected delta.
|
|
22
|
+
# @param state [Proc] A block of code to execute to get the
|
|
23
|
+
# state of the object.
|
|
24
|
+
def initialize(expected, &state)
|
|
25
|
+
super()
|
|
26
|
+
@expected = expected
|
|
27
|
+
@state = state
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Boolean comparison on the expected change by comparing the value
|
|
31
|
+
# before and after the code execution.
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
# object = []
|
|
35
|
+
# change = Matchi::Matcher::Change::ByAtLeast.new(1) { object.length }
|
|
36
|
+
# change.matches? { object << "foo" } # => true
|
|
37
|
+
#
|
|
38
|
+
# @yieldreturn [#object_id] The block of code to execute.
|
|
39
|
+
#
|
|
40
|
+
# @return [Boolean] Comparison between the value before and after the
|
|
41
|
+
# code execution.
|
|
42
|
+
def matches?(*, **)
|
|
43
|
+
value_before = @state.call
|
|
44
|
+
yield
|
|
45
|
+
value_after = @state.call
|
|
46
|
+
|
|
47
|
+
expected <= (value_after - value_before)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# (see Base#to_s)
|
|
51
|
+
def to_s
|
|
52
|
+
"change by at least #{expected.inspect}"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative File.join("..", "base")
|
|
4
|
+
|
|
5
|
+
module Matchi
|
|
6
|
+
module Matcher
|
|
7
|
+
class Change
|
|
8
|
+
# *Change by at most* matcher.
|
|
9
|
+
class ByAtMost < ::Matchi::Matcher::Base
|
|
10
|
+
# Returns a symbol identifying the matcher.
|
|
11
|
+
def self.to_sym
|
|
12
|
+
:change
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Initialize the matcher with an object.
|
|
16
|
+
#
|
|
17
|
+
# @example The change by at most 1 matcher.
|
|
18
|
+
# object = []
|
|
19
|
+
# Matchi::Matcher::Change::ByAtMost.new(1) { object.length }
|
|
20
|
+
#
|
|
21
|
+
# @param expected [#object_id] An expected delta.
|
|
22
|
+
# @param state [Proc] A block of code to execute to get the
|
|
23
|
+
# state of the object.
|
|
24
|
+
def initialize(expected, &state)
|
|
25
|
+
super()
|
|
26
|
+
@expected = expected
|
|
27
|
+
@state = state
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Boolean comparison on the expected change by comparing the value
|
|
31
|
+
# before and after the code execution.
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
# object = []
|
|
35
|
+
# change = Matchi::Matcher::Change::ByAtMost.new(1) { object.length }
|
|
36
|
+
# change.matches? { object << "foo" } # => true
|
|
37
|
+
#
|
|
38
|
+
# @yieldreturn [#object_id] The block of code to execute.
|
|
39
|
+
#
|
|
40
|
+
# @return [Boolean] Comparison between the value before and after the
|
|
41
|
+
# code execution.
|
|
42
|
+
def matches?(*, **)
|
|
43
|
+
value_before = @state.call
|
|
44
|
+
yield
|
|
45
|
+
value_after = @state.call
|
|
46
|
+
|
|
47
|
+
expected >= (value_after - value_before)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# (see Base#to_s)
|
|
51
|
+
def to_s
|
|
52
|
+
"change by at most #{expected.inspect}"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative File.join("from", "to")
|
|
4
|
+
|
|
5
|
+
module Matchi
|
|
6
|
+
module Matcher
|
|
7
|
+
class Change
|
|
8
|
+
# *Change from to* wrapper.
|
|
9
|
+
class From
|
|
10
|
+
# Initialize the matcher with an object.
|
|
11
|
+
#
|
|
12
|
+
# @example The change from "foo" to "FOO" matcher.
|
|
13
|
+
# object = "foo"
|
|
14
|
+
# Matchi::Matcher::Change::From.new("foo").to("FOO") { object.to_s }
|
|
15
|
+
#
|
|
16
|
+
# @param expected [#object_id] An expected initial value.
|
|
17
|
+
# @param state [Proc] A block of code to execute to get the
|
|
18
|
+
# state of the object.
|
|
19
|
+
def initialize(expected, &state)
|
|
20
|
+
@expected = expected
|
|
21
|
+
@state = state
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Specifies the new value to expect.
|
|
25
|
+
#
|
|
26
|
+
# @param expected_new_value [#object_id] The new value to expect.
|
|
27
|
+
#
|
|
28
|
+
# @return [#matches?] A *change from to* matcher.
|
|
29
|
+
def to(expected_new_value)
|
|
30
|
+
To.new(@expected, expected_new_value, &@state)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative File.join("..", "..", "base")
|
|
4
|
+
|
|
5
|
+
module Matchi
|
|
6
|
+
module Matcher
|
|
7
|
+
class Change
|
|
8
|
+
class From
|
|
9
|
+
# *Change from to* matcher.
|
|
10
|
+
class To < ::Matchi::Matcher::Base
|
|
11
|
+
# Returns a symbol identifying the matcher.
|
|
12
|
+
def self.to_sym
|
|
13
|
+
:change
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Initialize the matcher with an object.
|
|
17
|
+
#
|
|
18
|
+
# @example The change from "foo" to "FOO" matcher.
|
|
19
|
+
# object = "foo"
|
|
20
|
+
# Matchi::Matcher::Change::From::To.new("foo", "FOO") { object.to_s }
|
|
21
|
+
#
|
|
22
|
+
# @param expected_init [#object_id] An expected initial value.
|
|
23
|
+
# @param expected_new_value [#object_id] An expected new value.
|
|
24
|
+
# @param state [Proc] A block of code to execute to
|
|
25
|
+
# get the state of the object.
|
|
26
|
+
def initialize(expected_init, expected_new_value, &state)
|
|
27
|
+
super()
|
|
28
|
+
@expected_init = expected_init
|
|
29
|
+
@expected = expected_new_value
|
|
30
|
+
@state = state
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# (see Base#inspect)
|
|
34
|
+
def inspect
|
|
35
|
+
"Matchi::Matcher::Change::From(#{@expected_init.inspect})::To(#{expected.inspect})"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Boolean comparison on the expected change by comparing the value
|
|
39
|
+
# before and after the code execution.
|
|
40
|
+
#
|
|
41
|
+
# @example
|
|
42
|
+
# object = "foo"
|
|
43
|
+
# change = Matchi::Matcher::Change::To.new("FOO") { object.to_s }
|
|
44
|
+
# change.matches? { object.upcase! } # => true
|
|
45
|
+
#
|
|
46
|
+
# @yieldreturn [#object_id] The block of code to execute.
|
|
47
|
+
#
|
|
48
|
+
# @return [Boolean] Comparison between the value before and after the
|
|
49
|
+
# code execution.
|
|
50
|
+
def matches?(*, **)
|
|
51
|
+
value_before = @state.call
|
|
52
|
+
return false unless @expected_init == value_before
|
|
53
|
+
|
|
54
|
+
yield
|
|
55
|
+
value_after = @state.call
|
|
56
|
+
|
|
57
|
+
expected == value_after
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# (see Base#to_s)
|
|
61
|
+
def to_s
|
|
62
|
+
"change from #{@expected_init.inspect} to #{expected.inspect}"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative File.join("..", "base")
|
|
4
|
+
|
|
5
|
+
module Matchi
|
|
6
|
+
module Matcher
|
|
7
|
+
class Change
|
|
8
|
+
# *Change to* matcher.
|
|
9
|
+
class To < ::Matchi::Matcher::Base
|
|
10
|
+
# Returns a symbol identifying the matcher.
|
|
11
|
+
def self.to_sym
|
|
12
|
+
:change
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Initialize the matcher with an object.
|
|
16
|
+
#
|
|
17
|
+
# @example The change to "FOO" matcher.
|
|
18
|
+
# object = "foo"
|
|
19
|
+
# Matchi::Matcher::Change::To.new("FOO") { object.to_s }
|
|
20
|
+
#
|
|
21
|
+
# @param expected [#object_id] An expected result value.
|
|
22
|
+
# @param state [Proc] A block of code to execute to get the
|
|
23
|
+
# state of the object.
|
|
24
|
+
def initialize(expected, &state)
|
|
25
|
+
super()
|
|
26
|
+
@expected = expected
|
|
27
|
+
@state = state
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Boolean comparison on the expected change by comparing the value
|
|
31
|
+
# before and after the code execution.
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
# object = "foo"
|
|
35
|
+
# change = Matchi::Matcher::Change::To.new("FOO") { object.to_s }
|
|
36
|
+
# change.matches? { object.upcase! } # => true
|
|
37
|
+
#
|
|
38
|
+
# @yieldreturn [#object_id] The block of code to execute.
|
|
39
|
+
#
|
|
40
|
+
# @return [Boolean] Comparison between the value before and after the
|
|
41
|
+
# code execution.
|
|
42
|
+
def matches?(*, **)
|
|
43
|
+
yield
|
|
44
|
+
value_after = @state.call
|
|
45
|
+
|
|
46
|
+
expected == value_after
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# (see Base#to_s)
|
|
50
|
+
def to_s
|
|
51
|
+
"change to #{expected.inspect}"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
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.4.0
|
|
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-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -138,10 +138,18 @@ files:
|
|
|
138
138
|
- lib/matchi/matcher/be_false.rb
|
|
139
139
|
- lib/matchi/matcher/be_nil.rb
|
|
140
140
|
- lib/matchi/matcher/be_true.rb
|
|
141
|
+
- lib/matchi/matcher/change.rb
|
|
142
|
+
- lib/matchi/matcher/change/by.rb
|
|
143
|
+
- lib/matchi/matcher/change/by_at_least.rb
|
|
144
|
+
- lib/matchi/matcher/change/by_at_most.rb
|
|
145
|
+
- lib/matchi/matcher/change/from.rb
|
|
146
|
+
- lib/matchi/matcher/change/from/to.rb
|
|
147
|
+
- lib/matchi/matcher/change/to.rb
|
|
141
148
|
- lib/matchi/matcher/eql.rb
|
|
142
149
|
- lib/matchi/matcher/equal.rb
|
|
143
150
|
- lib/matchi/matcher/match.rb
|
|
144
151
|
- lib/matchi/matcher/raise_exception.rb
|
|
152
|
+
- lib/matchi/matcher/satisfy.rb
|
|
145
153
|
homepage: https://github.com/fixrb/matchi
|
|
146
154
|
licenses:
|
|
147
155
|
- MIT
|