matchi 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -5
- data/lib/matchi/be.rb +9 -4
- data/lib/matchi/be_an_instance_of.rb +10 -5
- data/lib/matchi/change.rb +10 -10
- data/lib/matchi/change/by.rb +9 -4
- data/lib/matchi/change/by_at_least.rb +9 -4
- data/lib/matchi/change/by_at_most.rb +9 -4
- data/lib/matchi/change/from.rb +2 -2
- data/lib/matchi/change/from/to.rb +9 -4
- data/lib/matchi/change/to.rb +10 -5
- data/lib/matchi/eq.rb +9 -4
- data/lib/matchi/match.rb +9 -4
- data/lib/matchi/raise_exception.rb +11 -6
- data/lib/matchi/satisfy.rb +7 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8da45849405d96c572ada45280a0acb673276092d25bc56c6a423892f1e1c1b8
|
4
|
+
data.tar.gz: 61f642b16e141704436eb2295fd0c5bc3bda93bb3991c992d40b71f491a328f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ddb08586b88d7bc3a1447b1ba503aa3170e587ba7a82134d660706be1e5a46ecf5f6180e16feb4736ebd2558e7bfc13f8474bdcca8f0a676945daa04d4ce04f
|
7
|
+
data.tar.gz: f0433134ba8bc1d93e4d4dbc6c8223c3033eda63525defb8a385c4cefc4bc43618b89cb97a47668060b744cfdac57b39e19dbdfebc7867a43ff18f7065d373c1
|
data/README.md
CHANGED
@@ -12,7 +12,6 @@
|
|
12
12
|
|
13
13
|
## Project goals
|
14
14
|
|
15
|
-
* Provide a collection of useful generic matchers.
|
16
15
|
* Adding matchers should be as simple as possible.
|
17
16
|
* Being framework agnostic and easy to integrate.
|
18
17
|
|
@@ -52,16 +51,24 @@ All examples here assume that this has been done.
|
|
52
51
|
|
53
52
|
### Anatomy of a matcher
|
54
53
|
|
55
|
-
A __Matchi__ matcher is
|
54
|
+
A __Matchi__ matcher is an object that must respond to the `matches?` method with a block as argument, and return a boolean.
|
56
55
|
|
57
|
-
|
56
|
+
To facilitate the integration of the matchers in other tools, it is recommended to expose the expected value via the `expected` method.
|
57
|
+
|
58
|
+
That's all it is.
|
59
|
+
|
60
|
+
Let's see some examples.
|
58
61
|
|
59
62
|
### Built-in matchers
|
60
63
|
|
64
|
+
Here is the collection of useful generic matchers.
|
65
|
+
|
61
66
|
**Equivalence** matcher:
|
62
67
|
|
63
68
|
```ruby
|
64
69
|
matcher = Matchi::Eq.new("foo")
|
70
|
+
|
71
|
+
matcher.expected # => "foo"
|
65
72
|
matcher.matches? { "foo" } # => true
|
66
73
|
```
|
67
74
|
|
@@ -69,6 +76,8 @@ matcher.matches? { "foo" } # => true
|
|
69
76
|
|
70
77
|
```ruby
|
71
78
|
matcher = Matchi::Be.new(:foo)
|
79
|
+
|
80
|
+
matcher.expected # => :foo
|
72
81
|
matcher.matches? { :foo } # => true
|
73
82
|
```
|
74
83
|
|
@@ -76,13 +85,17 @@ matcher.matches? { :foo } # => true
|
|
76
85
|
|
77
86
|
```ruby
|
78
87
|
matcher = Matchi::Match.new(/^foo$/)
|
88
|
+
|
89
|
+
matcher.expected # => /^foo$/
|
79
90
|
matcher.matches? { "foo" } # => true
|
80
91
|
```
|
81
92
|
|
82
93
|
**Expecting errors** matcher:
|
83
94
|
|
84
95
|
```ruby
|
85
|
-
matcher = Matchi::RaiseException.new(NameError)
|
96
|
+
matcher = Matchi::RaiseException.new(:NameError)
|
97
|
+
|
98
|
+
matcher.expected # => :NameError
|
86
99
|
matcher.matches? { Boom } # => true
|
87
100
|
```
|
88
101
|
|
@@ -90,6 +103,8 @@ matcher.matches? { Boom } # => true
|
|
90
103
|
|
91
104
|
```ruby
|
92
105
|
matcher = Matchi::BeAnInstanceOf.new(:String)
|
106
|
+
|
107
|
+
matcher.expected # => :String
|
93
108
|
matcher.matches? { "foo" } # => true
|
94
109
|
```
|
95
110
|
|
@@ -98,22 +113,32 @@ matcher.matches? { "foo" } # => true
|
|
98
113
|
```ruby
|
99
114
|
object = []
|
100
115
|
matcher = Matchi::Change.new(object, :length).by(1)
|
116
|
+
|
117
|
+
matcher.expected # => 1
|
101
118
|
matcher.matches? { object << 1 } # => true
|
102
119
|
|
103
120
|
object = []
|
104
121
|
matcher = Matchi::Change.new(object, :length).by_at_least(1)
|
122
|
+
|
123
|
+
matcher.expected # => 1
|
105
124
|
matcher.matches? { object << 1 } # => true
|
106
125
|
|
107
126
|
object = []
|
108
127
|
matcher = Matchi::Change.new(object, :length).by_at_most(1)
|
128
|
+
|
129
|
+
matcher.expected # => 1
|
109
130
|
matcher.matches? { object << 1 } # => true
|
110
131
|
|
111
132
|
object = "foo"
|
112
133
|
matcher = Matchi::Change.new(object, :to_s).from("foo").to("FOO")
|
134
|
+
|
135
|
+
matcher.expected # => "FOO"
|
113
136
|
matcher.matches? { object.upcase! } # => true
|
114
137
|
|
115
138
|
object = "foo"
|
116
139
|
matcher = Matchi::Change.new(object, :to_s).to("FOO")
|
140
|
+
|
141
|
+
matcher.expected # => "FOO"
|
117
142
|
matcher.matches? { object.upcase! } # => true
|
118
143
|
```
|
119
144
|
|
@@ -121,6 +146,8 @@ matcher.matches? { object.upcase! } # => true
|
|
121
146
|
|
122
147
|
```ruby
|
123
148
|
matcher = Matchi::Satisfy.new { |value| value == 42 }
|
149
|
+
|
150
|
+
matcher.expected # => #<Proc:0x00007fbaafc65540>
|
124
151
|
matcher.matches? { 42 } # => true
|
125
152
|
```
|
126
153
|
|
@@ -133,13 +160,19 @@ A **Be the answer** matcher:
|
|
133
160
|
```ruby
|
134
161
|
module Matchi
|
135
162
|
class BeTheAnswer
|
163
|
+
def expected
|
164
|
+
42
|
165
|
+
end
|
166
|
+
|
136
167
|
def matches?
|
137
|
-
|
168
|
+
expected.equal?(yield)
|
138
169
|
end
|
139
170
|
end
|
140
171
|
end
|
141
172
|
|
142
173
|
matcher = Matchi::BeTheAnswer.new
|
174
|
+
|
175
|
+
matcher.expected # => 42
|
143
176
|
matcher.matches? { 42 } # => true
|
144
177
|
```
|
145
178
|
|
@@ -150,6 +183,8 @@ require "prime"
|
|
150
183
|
|
151
184
|
module Matchi
|
152
185
|
class BePrime
|
186
|
+
attr_reader :expected
|
187
|
+
|
153
188
|
def matches?
|
154
189
|
Prime.prime?(yield)
|
155
190
|
end
|
@@ -157,6 +192,8 @@ module Matchi
|
|
157
192
|
end
|
158
193
|
|
159
194
|
matcher = Matchi::BePrime.new
|
195
|
+
|
196
|
+
matcher.expected # => nil
|
160
197
|
matcher.matches? { 42 } # => false
|
161
198
|
```
|
162
199
|
|
@@ -178,6 +215,8 @@ module Matchi
|
|
178
215
|
end
|
179
216
|
|
180
217
|
matcher = Matchi::StartWith.new("foo")
|
218
|
+
|
219
|
+
matcher.expected # => "foo"
|
181
220
|
matcher.matches? { "foobar" } # => true
|
182
221
|
```
|
183
222
|
|
data/lib/matchi/be.rb
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
module Matchi
|
4
4
|
# *Identity* matcher.
|
5
5
|
class Be
|
6
|
+
# @return [#equal?] The expected identical object.
|
7
|
+
attr_reader :expected
|
8
|
+
|
6
9
|
# Initialize the matcher with an object.
|
7
10
|
#
|
8
11
|
# @example
|
@@ -21,24 +24,26 @@ module Matchi
|
|
21
24
|
# require "matchi/be"
|
22
25
|
#
|
23
26
|
# matcher = Matchi::Be.new(:foo)
|
27
|
+
#
|
28
|
+
# matcher.expected # => :foo
|
24
29
|
# matcher.matches? { :foo } # => true
|
25
30
|
#
|
26
31
|
# @yieldreturn [#object_id] The actual value to compare to the expected
|
27
32
|
# one.
|
28
33
|
#
|
29
34
|
# @return [Boolean] Comparison between actual and expected values.
|
30
|
-
def matches?
|
31
|
-
|
35
|
+
def matches?
|
36
|
+
expected.equal?(yield)
|
32
37
|
end
|
33
38
|
|
34
39
|
# A string containing a human-readable representation of the matcher.
|
35
40
|
def inspect
|
36
|
-
"#{self.class}(#{
|
41
|
+
"#{self.class}(#{expected.inspect})"
|
37
42
|
end
|
38
43
|
|
39
44
|
# Returns a string representing the matcher.
|
40
45
|
def to_s
|
41
|
-
"be #{
|
46
|
+
"be #{expected.inspect}"
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
@@ -3,6 +3,9 @@
|
|
3
3
|
module Matchi
|
4
4
|
# *Type/class* matcher.
|
5
5
|
class BeAnInstanceOf
|
6
|
+
# @return [Symbol] The expected class name.
|
7
|
+
attr_reader :expected
|
8
|
+
|
6
9
|
# Initialize the matcher with (the name of) a class or module.
|
7
10
|
#
|
8
11
|
# @example
|
@@ -10,7 +13,7 @@ module Matchi
|
|
10
13
|
#
|
11
14
|
# Matchi::BeAnInstanceOf.new(String)
|
12
15
|
#
|
13
|
-
# @param expected [#to_s]
|
16
|
+
# @param expected [Class, #to_s] The expected class name.
|
14
17
|
def initialize(expected)
|
15
18
|
@expected = String(expected).to_sym
|
16
19
|
end
|
@@ -22,23 +25,25 @@ module Matchi
|
|
22
25
|
# require "matchi/be_an_instance_of"
|
23
26
|
#
|
24
27
|
# matcher = Matchi::BeAnInstanceOf.new(String)
|
28
|
+
#
|
29
|
+
# matcher.expected # => :String
|
25
30
|
# matcher.matches? { "foo" } # => true
|
26
31
|
#
|
27
32
|
# @yieldreturn [#class] the actual value to compare to the expected one.
|
28
33
|
#
|
29
34
|
# @return [Boolean] Comparison between actual and expected values.
|
30
|
-
def matches?
|
31
|
-
self.class.const_get(
|
35
|
+
def matches?
|
36
|
+
self.class.const_get(expected).equal?(yield.class)
|
32
37
|
end
|
33
38
|
|
34
39
|
# A string containing a human-readable representation of the matcher.
|
35
40
|
def inspect
|
36
|
-
"#{self.class}(#{
|
41
|
+
"#{self.class}(#{expected})"
|
37
42
|
end
|
38
43
|
|
39
44
|
# Returns a string representing the matcher.
|
40
45
|
def to_s
|
41
|
-
"be an instance of #{
|
46
|
+
"be an instance of #{expected}"
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
data/lib/matchi/change.rb
CHANGED
@@ -32,8 +32,8 @@ module Matchi
|
|
32
32
|
#
|
33
33
|
# object = []
|
34
34
|
#
|
35
|
-
#
|
36
|
-
#
|
35
|
+
# change_wrapper = Matchi::Change.new(object, :length)
|
36
|
+
# change_wrapper.by_at_least(1)
|
37
37
|
#
|
38
38
|
# @param expected [#object_id] The minimum delta of the expected change.
|
39
39
|
#
|
@@ -49,8 +49,8 @@ module Matchi
|
|
49
49
|
#
|
50
50
|
# object = []
|
51
51
|
#
|
52
|
-
#
|
53
|
-
#
|
52
|
+
# change_wrapper = Matchi::Change.new(object, :length)
|
53
|
+
# change_wrapper.by_at_most(1)
|
54
54
|
#
|
55
55
|
# @param expected [#object_id] The maximum delta of the expected change.
|
56
56
|
#
|
@@ -66,8 +66,8 @@ module Matchi
|
|
66
66
|
#
|
67
67
|
# object = []
|
68
68
|
#
|
69
|
-
#
|
70
|
-
#
|
69
|
+
# change_wrapper = Matchi::Change.new(object, :length)
|
70
|
+
# change_wrapper.by(1)
|
71
71
|
#
|
72
72
|
# @param expected [#object_id] The delta of the expected change.
|
73
73
|
#
|
@@ -81,8 +81,8 @@ module Matchi
|
|
81
81
|
# @example
|
82
82
|
# require "matchi/change"
|
83
83
|
#
|
84
|
-
#
|
85
|
-
#
|
84
|
+
# change_wrapper = Matchi::Change.new("foo", :to_s)
|
85
|
+
# change_wrapper.from("foo")
|
86
86
|
#
|
87
87
|
# @param expected [#object_id] The original value.
|
88
88
|
#
|
@@ -96,8 +96,8 @@ module Matchi
|
|
96
96
|
# @example
|
97
97
|
# require "matchi/change"
|
98
98
|
#
|
99
|
-
#
|
100
|
-
#
|
99
|
+
# change_wrapper = Matchi::Change.new("foo", :to_s)
|
100
|
+
# change_wrapper.to("FOO")
|
101
101
|
#
|
102
102
|
# @param expected [#object_id] The new value to expect.
|
103
103
|
#
|
data/lib/matchi/change/by.rb
CHANGED
@@ -4,6 +4,9 @@ module Matchi
|
|
4
4
|
class Change
|
5
5
|
# *Change by* matcher.
|
6
6
|
class By
|
7
|
+
# @return [#object_id] An expected delta.
|
8
|
+
attr_reader :expected
|
9
|
+
|
7
10
|
# Initialize the matcher with an object and a block.
|
8
11
|
#
|
9
12
|
# @example
|
@@ -30,28 +33,30 @@ module Matchi
|
|
30
33
|
# object = []
|
31
34
|
#
|
32
35
|
# matcher = Matchi::Change::By.new(1) { object.length }
|
36
|
+
#
|
37
|
+
# matcher.expected # => 1
|
33
38
|
# matcher.matches? { object << "foo" } # => true
|
34
39
|
#
|
35
40
|
# @yieldreturn [#object_id] The block of code to execute.
|
36
41
|
#
|
37
42
|
# @return [Boolean] Comparison between the value before and after the
|
38
43
|
# code execution.
|
39
|
-
def matches?
|
44
|
+
def matches?
|
40
45
|
value_before = @state.call
|
41
46
|
yield
|
42
47
|
value_after = @state.call
|
43
48
|
|
44
|
-
|
49
|
+
expected == (value_after - value_before)
|
45
50
|
end
|
46
51
|
|
47
52
|
# A string containing a human-readable representation of the matcher.
|
48
53
|
def inspect
|
49
|
-
"#{self.class}(#{
|
54
|
+
"#{self.class}(#{expected.inspect})"
|
50
55
|
end
|
51
56
|
|
52
57
|
# Returns a string representing the matcher.
|
53
58
|
def to_s
|
54
|
-
"change by #{
|
59
|
+
"change by #{expected.inspect}"
|
55
60
|
end
|
56
61
|
end
|
57
62
|
end
|
@@ -4,6 +4,9 @@ module Matchi
|
|
4
4
|
class Change
|
5
5
|
# *Change by at least* matcher.
|
6
6
|
class ByAtLeast
|
7
|
+
# @return [#object_id] An expected delta.
|
8
|
+
attr_reader :expected
|
9
|
+
|
7
10
|
# Initialize the matcher with an object and a block.
|
8
11
|
#
|
9
12
|
# @example
|
@@ -30,28 +33,30 @@ module Matchi
|
|
30
33
|
# object = []
|
31
34
|
#
|
32
35
|
# matcher = Matchi::Change::ByAtLeast.new(1) { object.length }
|
36
|
+
#
|
37
|
+
# matcher.expected # => 1
|
33
38
|
# matcher.matches? { object << "foo" } # => true
|
34
39
|
#
|
35
40
|
# @yieldreturn [#object_id] The block of code to execute.
|
36
41
|
#
|
37
42
|
# @return [Boolean] Comparison between the value before and after the
|
38
43
|
# code execution.
|
39
|
-
def matches?
|
44
|
+
def matches?
|
40
45
|
value_before = @state.call
|
41
46
|
yield
|
42
47
|
value_after = @state.call
|
43
48
|
|
44
|
-
|
49
|
+
expected <= (value_after - value_before)
|
45
50
|
end
|
46
51
|
|
47
52
|
# A string containing a human-readable representation of the matcher.
|
48
53
|
def inspect
|
49
|
-
"#{self.class}(#{
|
54
|
+
"#{self.class}(#{expected.inspect})"
|
50
55
|
end
|
51
56
|
|
52
57
|
# Returns a string representing the matcher.
|
53
58
|
def to_s
|
54
|
-
"change by at least #{
|
59
|
+
"change by at least #{expected.inspect}"
|
55
60
|
end
|
56
61
|
end
|
57
62
|
end
|
@@ -4,6 +4,9 @@ module Matchi
|
|
4
4
|
class Change
|
5
5
|
# *Change by at most* matcher.
|
6
6
|
class ByAtMost
|
7
|
+
# @return [#object_id] An expected delta.
|
8
|
+
attr_reader :expected
|
9
|
+
|
7
10
|
# Initialize the matcher with an object and a block.
|
8
11
|
#
|
9
12
|
# @example
|
@@ -30,28 +33,30 @@ module Matchi
|
|
30
33
|
# object = []
|
31
34
|
#
|
32
35
|
# matcher = Matchi::Change::ByAtMost.new(1) { object.length }
|
36
|
+
#
|
37
|
+
# matcher.expected # => 1
|
33
38
|
# matcher.matches? { object << "foo" } # => true
|
34
39
|
#
|
35
40
|
# @yieldreturn [#object_id] The block of code to execute.
|
36
41
|
#
|
37
42
|
# @return [Boolean] Comparison between the value before and after the
|
38
43
|
# code execution.
|
39
|
-
def matches?
|
44
|
+
def matches?
|
40
45
|
value_before = @state.call
|
41
46
|
yield
|
42
47
|
value_after = @state.call
|
43
48
|
|
44
|
-
|
49
|
+
expected >= (value_after - value_before)
|
45
50
|
end
|
46
51
|
|
47
52
|
# A string containing a human-readable representation of the matcher.
|
48
53
|
def inspect
|
49
|
-
"#{self.class}(#{
|
54
|
+
"#{self.class}(#{expected.inspect})"
|
50
55
|
end
|
51
56
|
|
52
57
|
# Returns a string representing the matcher.
|
53
58
|
def to_s
|
54
|
-
"change by at most #{
|
59
|
+
"change by at most #{expected.inspect}"
|
55
60
|
end
|
56
61
|
end
|
57
62
|
end
|
data/lib/matchi/change/from.rb
CHANGED
@@ -30,8 +30,8 @@ module Matchi
|
|
30
30
|
#
|
31
31
|
# object = "foo"
|
32
32
|
#
|
33
|
-
#
|
34
|
-
#
|
33
|
+
# change_from_wrapper = Matchi::Change::From.new("foo") { object.to_s }
|
34
|
+
# change_from_wrapper.to("FOO")
|
35
35
|
#
|
36
36
|
# @param expected_new_value [#object_id] The new value to expect.
|
37
37
|
#
|
@@ -5,6 +5,9 @@ module Matchi
|
|
5
5
|
class From
|
6
6
|
# *Change from to* matcher.
|
7
7
|
class To
|
8
|
+
# @return [#object_id] An expected new value.
|
9
|
+
attr_reader :expected
|
10
|
+
|
8
11
|
# Initialize the matcher with two objects and a block.
|
9
12
|
#
|
10
13
|
# @example
|
@@ -33,30 +36,32 @@ module Matchi
|
|
33
36
|
# object = "foo"
|
34
37
|
#
|
35
38
|
# matcher = Matchi::Change::From::To.new("foo", "FOO") { object.to_s }
|
39
|
+
#
|
40
|
+
# matcher.expected # => "FOO"
|
36
41
|
# matcher.matches? { object.upcase! } # => true
|
37
42
|
#
|
38
43
|
# @yieldreturn [#object_id] The block of code to execute.
|
39
44
|
#
|
40
45
|
# @return [Boolean] Comparison between the value before and after the
|
41
46
|
# code execution.
|
42
|
-
def matches?
|
47
|
+
def matches?
|
43
48
|
value_before = @state.call
|
44
49
|
return false unless @expected_init == value_before
|
45
50
|
|
46
51
|
yield
|
47
52
|
value_after = @state.call
|
48
53
|
|
49
|
-
|
54
|
+
expected == value_after
|
50
55
|
end
|
51
56
|
|
52
57
|
# A string containing a human-readable representation of the matcher.
|
53
58
|
def inspect
|
54
|
-
"#{self.class}(#{@expected_init.inspect}, #{
|
59
|
+
"#{self.class}(#{@expected_init.inspect}, #{expected.inspect})"
|
55
60
|
end
|
56
61
|
|
57
62
|
# Returns a string representing the matcher.
|
58
63
|
def to_s
|
59
|
-
"change from #{@expected_init.inspect} to #{
|
64
|
+
"change from #{@expected_init.inspect} to #{expected.inspect}"
|
60
65
|
end
|
61
66
|
end
|
62
67
|
end
|
data/lib/matchi/change/to.rb
CHANGED
@@ -4,6 +4,9 @@ module Matchi
|
|
4
4
|
class Change
|
5
5
|
# *Change to* matcher.
|
6
6
|
class To
|
7
|
+
# @return [#object_id] An expected new value.
|
8
|
+
attr_reader :expected
|
9
|
+
|
7
10
|
# Initialize the matcher with an object and a block.
|
8
11
|
#
|
9
12
|
# @example
|
@@ -13,7 +16,7 @@ module Matchi
|
|
13
16
|
#
|
14
17
|
# Matchi::Change::To.new("FOO") { object.to_s }
|
15
18
|
#
|
16
|
-
# @param expected [#object_id] An expected
|
19
|
+
# @param expected [#object_id] An expected new value.
|
17
20
|
# @param state [Proc] A block of code to execute to get the
|
18
21
|
# state of the object.
|
19
22
|
def initialize(expected, &state)
|
@@ -30,27 +33,29 @@ module Matchi
|
|
30
33
|
# object = "foo"
|
31
34
|
#
|
32
35
|
# matcher = Matchi::Change::To.new("FOO") { object.to_s }
|
36
|
+
#
|
37
|
+
# matcher.expected # => "FOO"
|
33
38
|
# matcher.matches? { object.upcase! } # => true
|
34
39
|
#
|
35
40
|
# @yieldreturn [#object_id] The block of code to execute.
|
36
41
|
#
|
37
42
|
# @return [Boolean] Comparison between the value before and after the
|
38
43
|
# code execution.
|
39
|
-
def matches?
|
44
|
+
def matches?
|
40
45
|
yield
|
41
46
|
value_after = @state.call
|
42
47
|
|
43
|
-
|
48
|
+
expected == value_after
|
44
49
|
end
|
45
50
|
|
46
51
|
# A string containing a human-readable representation of the matcher.
|
47
52
|
def inspect
|
48
|
-
"#{self.class}(#{
|
53
|
+
"#{self.class}(#{expected.inspect})"
|
49
54
|
end
|
50
55
|
|
51
56
|
# Returns a string representing the matcher.
|
52
57
|
def to_s
|
53
|
-
"change to #{
|
58
|
+
"change to #{expected.inspect}"
|
54
59
|
end
|
55
60
|
end
|
56
61
|
end
|
data/lib/matchi/eq.rb
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
module Matchi
|
4
4
|
# *Equivalence* matcher.
|
5
5
|
class Eq
|
6
|
+
# @return [#eql?] An expected equivalent object.
|
7
|
+
attr_reader :expected
|
8
|
+
|
6
9
|
# Initialize the matcher with an object.
|
7
10
|
#
|
8
11
|
# @example
|
@@ -21,24 +24,26 @@ module Matchi
|
|
21
24
|
# require "matchi/eq"
|
22
25
|
#
|
23
26
|
# matcher = Matchi::Eq.new("foo")
|
27
|
+
#
|
28
|
+
# matcher.expected # => "foo"
|
24
29
|
# matcher.matches? { "foo" } # => true
|
25
30
|
#
|
26
31
|
# @yieldreturn [#object_id] The actual value to compare to the expected
|
27
32
|
# one.
|
28
33
|
#
|
29
34
|
# @return [Boolean] Comparison between actual and expected values.
|
30
|
-
def matches?
|
31
|
-
|
35
|
+
def matches?
|
36
|
+
expected.eql?(yield)
|
32
37
|
end
|
33
38
|
|
34
39
|
# A string containing a human-readable representation of the matcher.
|
35
40
|
def inspect
|
36
|
-
"#{self.class}(#{
|
41
|
+
"#{self.class}(#{expected.inspect})"
|
37
42
|
end
|
38
43
|
|
39
44
|
# Returns a string representing the matcher.
|
40
45
|
def to_s
|
41
|
-
"eq #{
|
46
|
+
"eq #{expected.inspect}"
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
data/lib/matchi/match.rb
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
module Matchi
|
4
4
|
# *Regular expressions* matcher.
|
5
5
|
class Match
|
6
|
+
# @return [#match] A regular expression.
|
7
|
+
attr_reader :expected
|
8
|
+
|
6
9
|
# Initialize the matcher with an instance of Regexp.
|
7
10
|
#
|
8
11
|
# @example
|
@@ -21,24 +24,26 @@ module Matchi
|
|
21
24
|
# require "matchi/match"
|
22
25
|
#
|
23
26
|
# matcher = Matchi::Match.new(/^foo$/)
|
27
|
+
#
|
28
|
+
# matcher.expected # => /^foo$/
|
24
29
|
# matcher.matches? { "foo" } # => true
|
25
30
|
#
|
26
31
|
# @yieldreturn [#object_id] The actual value to compare to the expected
|
27
32
|
# one.
|
28
33
|
#
|
29
34
|
# @return [Boolean] Comparison between actual and expected values.
|
30
|
-
def matches?
|
31
|
-
|
35
|
+
def matches?
|
36
|
+
expected.match?(yield)
|
32
37
|
end
|
33
38
|
|
34
39
|
# A string containing a human-readable representation of the matcher.
|
35
40
|
def inspect
|
36
|
-
"#{self.class}(#{
|
41
|
+
"#{self.class}(#{expected.inspect})"
|
37
42
|
end
|
38
43
|
|
39
44
|
# Returns a string representing the matcher.
|
40
45
|
def to_s
|
41
|
-
"match #{
|
46
|
+
"match #{expected.inspect}"
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
@@ -3,6 +3,9 @@
|
|
3
3
|
module Matchi
|
4
4
|
# *Expecting errors* matcher.
|
5
5
|
class RaiseException
|
6
|
+
# @return [Symbol] The expected exception name.
|
7
|
+
attr_reader :expected
|
8
|
+
|
6
9
|
# Initialize the matcher with a descendant of class Exception.
|
7
10
|
#
|
8
11
|
# @example
|
@@ -10,9 +13,9 @@ module Matchi
|
|
10
13
|
#
|
11
14
|
# Matchi::RaiseException.new(NameError)
|
12
15
|
#
|
13
|
-
# @param expected [Exception] The
|
16
|
+
# @param expected [Exception, #to_s] The expected exception name.
|
14
17
|
def initialize(expected)
|
15
|
-
@expected = expected
|
18
|
+
@expected = String(expected).to_sym
|
16
19
|
end
|
17
20
|
|
18
21
|
# Boolean comparison between the actual value and the expected value.
|
@@ -21,15 +24,17 @@ module Matchi
|
|
21
24
|
# require "matchi/raise_exception"
|
22
25
|
#
|
23
26
|
# matcher = Matchi::RaiseException.new(NameError)
|
27
|
+
#
|
28
|
+
# matcher.expected # => :NameError
|
24
29
|
# matcher.matches? { Boom } # => true
|
25
30
|
#
|
26
31
|
# @yieldreturn [#object_id] The actual value to compare to the expected
|
27
32
|
# one.
|
28
33
|
#
|
29
34
|
# @return [Boolean] Comparison between actual and expected values.
|
30
|
-
def matches?
|
35
|
+
def matches?
|
31
36
|
yield
|
32
|
-
rescue
|
37
|
+
rescue self.class.const_get(expected) => _e
|
33
38
|
true
|
34
39
|
else
|
35
40
|
false
|
@@ -37,12 +42,12 @@ module Matchi
|
|
37
42
|
|
38
43
|
# A string containing a human-readable representation of the matcher.
|
39
44
|
def inspect
|
40
|
-
"#{self.class}(#{
|
45
|
+
"#{self.class}(#{expected})"
|
41
46
|
end
|
42
47
|
|
43
48
|
# Returns a string representing the matcher.
|
44
49
|
def to_s
|
45
|
-
"raise exception #{
|
50
|
+
"raise exception #{expected}"
|
46
51
|
end
|
47
52
|
end
|
48
53
|
end
|
data/lib/matchi/satisfy.rb
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
module Matchi
|
4
4
|
# *Satisfy* matcher.
|
5
5
|
class Satisfy
|
6
|
+
# @return [Proc] A block of code.
|
7
|
+
attr_reader :expected
|
8
|
+
|
6
9
|
# Initialize the matcher with a block.
|
7
10
|
#
|
8
11
|
# @example
|
@@ -21,14 +24,16 @@ module Matchi
|
|
21
24
|
# require "matchi/satisfy"
|
22
25
|
#
|
23
26
|
# matcher = Matchi::Satisfy.new { |value| value == 42 }
|
27
|
+
#
|
28
|
+
# matcher.expected # => #<Proc:0x00007fbaafc65540>
|
24
29
|
# matcher.matches? { 42 } # => true
|
25
30
|
#
|
26
31
|
# @yieldreturn [#object_id] The actual value to compare to the expected
|
27
32
|
# one.
|
28
33
|
#
|
29
34
|
# @return [Boolean] Comparison between actual and expected values.
|
30
|
-
def matches?
|
31
|
-
|
35
|
+
def matches?
|
36
|
+
expected.call(yield)
|
32
37
|
end
|
33
38
|
|
34
39
|
# A string containing a human-readable representation of the matcher.
|