matchi 3.1.1 → 3.2.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 +11 -8
- data/lib/matchi/be_within.rb +35 -0
- data/lib/matchi/be_within/of.rb +53 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aabf9e773ac6210d1170b3a0f3aa19f38845ea0215bb07280d97c35ef66add16
|
4
|
+
data.tar.gz: 2900c9b936d50bfbe50ba31c6418dcb1402c1fbef5cfbb9f7a1ce289ff6450c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2cfb5b1c32a01b84c1e2eec84cfc3ad34df2a7a17c91ecb1e6012ef84d5b4ef5b11f24817de7dcea77e08006e72c578d0d88947470436f231efef8fed18f061
|
7
|
+
data.tar.gz: 235a73246185fec310960caf6b8c868cbc2daf7598f29c863def9b560ec66ba7a0f43ab303cd164b04c81b5ebbcbddae3224c5550002f9ab924f8202f58a289f
|
data/README.md
CHANGED
@@ -14,6 +14,7 @@
|
|
14
14
|
|
15
15
|
* Adding matchers should be as simple as possible.
|
16
16
|
* Being framework agnostic and easy to integrate.
|
17
|
+
* Avoid false positives/negatives due to malicious actual values.
|
17
18
|
|
18
19
|
## Installation
|
19
20
|
|
@@ -53,11 +54,7 @@ All examples here assume that this has been done.
|
|
53
54
|
|
54
55
|
A __Matchi__ matcher is an object that must respond to the `matches?` method with a block as argument, and return a boolean.
|
55
56
|
|
56
|
-
To facilitate the integration of the matchers in other tools,
|
57
|
-
|
58
|
-
That's all it is.
|
59
|
-
|
60
|
-
Let's see some examples.
|
57
|
+
To facilitate the integration of the matchers in other tools, __Matchi__ matchers may expose expected values via the `expected` method.
|
61
58
|
|
62
59
|
### Built-in matchers
|
63
60
|
|
@@ -81,6 +78,15 @@ matcher.expected # => :foo
|
|
81
78
|
matcher.matches? { :foo } # => true
|
82
79
|
```
|
83
80
|
|
81
|
+
**Comparisons** matcher:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
matcher = Matchi::BeWithin.new(8).of(37)
|
85
|
+
|
86
|
+
matcher.expected # => 37
|
87
|
+
matcher.matches? { 42 } # => true
|
88
|
+
```
|
89
|
+
|
84
90
|
**Regular expressions** matcher:
|
85
91
|
|
86
92
|
```ruby
|
@@ -183,8 +189,6 @@ require "prime"
|
|
183
189
|
|
184
190
|
module Matchi
|
185
191
|
class BePrime
|
186
|
-
attr_reader :expected
|
187
|
-
|
188
192
|
def matches?
|
189
193
|
Prime.prime?(yield)
|
190
194
|
end
|
@@ -193,7 +197,6 @@ end
|
|
193
197
|
|
194
198
|
matcher = Matchi::BePrime.new
|
195
199
|
|
196
|
-
matcher.expected # => nil
|
197
200
|
matcher.matches? { 42 } # => false
|
198
201
|
```
|
199
202
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative File.join("be_within", "of")
|
4
|
+
|
5
|
+
module Matchi
|
6
|
+
# Wraps the target of a be_within matcher.
|
7
|
+
class BeWithin
|
8
|
+
# Initialize a wrapper of the be_within matcher with a numeric value.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# require "matchi/be_within"
|
12
|
+
#
|
13
|
+
# Matchi::BeWithin.new(1)
|
14
|
+
#
|
15
|
+
# @param delta [Numeric] A numeric value.
|
16
|
+
def initialize(delta)
|
17
|
+
@delta = delta
|
18
|
+
end
|
19
|
+
|
20
|
+
# Specifies an expected numeric value.
|
21
|
+
#
|
22
|
+
# @example
|
23
|
+
# require "matchi/be_within"
|
24
|
+
#
|
25
|
+
# be_within_wrapper = Matchi::BeWithin.new(1)
|
26
|
+
# be_within_wrapper.of(41)
|
27
|
+
#
|
28
|
+
# @param expected [Numeric] The expected value.
|
29
|
+
#
|
30
|
+
# @return [#matches?] A *be_within of* matcher.
|
31
|
+
def of(expected)
|
32
|
+
Of.new(@delta, expected)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Matchi
|
4
|
+
class BeWithin
|
5
|
+
# *BeWithin of* matcher.
|
6
|
+
class Of
|
7
|
+
# @return [Numeric] An expected value.
|
8
|
+
attr_reader :expected
|
9
|
+
|
10
|
+
# Initialize the matcher with a delta and an expected value.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# require "matchi/be_within/of"
|
14
|
+
#
|
15
|
+
# Matchi::BeWithin::Of.new(1, 41)
|
16
|
+
#
|
17
|
+
# @param delta [Numeric] The accepted variation of the actual value.
|
18
|
+
# @param expected [Numeric] The expected value.
|
19
|
+
def initialize(delta, expected)
|
20
|
+
@delta = delta
|
21
|
+
@expected = expected
|
22
|
+
end
|
23
|
+
|
24
|
+
# Boolean comparison on the expected be_within by comparing the actual
|
25
|
+
# value and the expected value.
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# require "matchi/be_within/of"
|
29
|
+
#
|
30
|
+
# matcher = Matchi::BeWithin::Of.new(1, 41)
|
31
|
+
#
|
32
|
+
# matcher.expected # => 41
|
33
|
+
# matcher.matches? { 42 } # => true
|
34
|
+
#
|
35
|
+
# @yieldreturn [Numeric] The block of code to execute.
|
36
|
+
#
|
37
|
+
# @return [Boolean] Comparison between the actual and the expected values.
|
38
|
+
def matches?
|
39
|
+
(expected - yield).abs <= @delta
|
40
|
+
end
|
41
|
+
|
42
|
+
# A string containing a human-readable representation of the matcher.
|
43
|
+
def inspect
|
44
|
+
"#{self.class}(#{@delta}, #{expected})"
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns a string representing the matcher.
|
48
|
+
def to_s
|
49
|
+
"be within #{@delta} of #{expected}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
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: 3.
|
4
|
+
version: 3.2.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-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -133,6 +133,8 @@ files:
|
|
133
133
|
- lib/matchi.rb
|
134
134
|
- lib/matchi/be.rb
|
135
135
|
- lib/matchi/be_an_instance_of.rb
|
136
|
+
- lib/matchi/be_within.rb
|
137
|
+
- lib/matchi/be_within/of.rb
|
136
138
|
- lib/matchi/change.rb
|
137
139
|
- lib/matchi/change/by.rb
|
138
140
|
- lib/matchi/change/by_at_least.rb
|