matchi 1.0.7 → 2.1.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/LICENSE.md +1 -1
- data/README.md +59 -103
- data/lib/matchi.rb +1 -1
- data/lib/matchi/helper.rb +28 -0
- data/lib/matchi/matcher.rb +11 -0
- data/lib/matchi/matcher/base.rb +45 -0
- data/lib/matchi/matcher/be_an_instance_of.rb +35 -0
- data/lib/matchi/matcher/be_false.rb +24 -0
- data/lib/matchi/matcher/be_nil.rb +24 -0
- data/lib/matchi/matcher/be_true.rb +24 -0
- data/lib/matchi/matcher/eql.rb +35 -0
- data/lib/matchi/matcher/equal.rb +35 -0
- data/lib/matchi/matcher/match.rb +35 -0
- data/lib/matchi/matcher/raise_exception.rb +39 -0
- metadata +63 -66
- data/.gitignore +0 -10
- data/.rubocop.yml +0 -1
- data/.rubocop_todo.yml +0 -7
- data/.travis.yml +0 -28
- data/.yardopts +0 -1
- data/CODE_OF_CONDUCT.md +0 -13
- data/Gemfile +0 -5
- data/Rakefile +0 -22
- data/VERSION.semver +0 -1
- data/bin/console +0 -8
- data/bin/setup +0 -6
- data/checksum/matchi-0.0.1.gem.sha512 +0 -1
- data/checksum/matchi-0.0.2.gem.sha512 +0 -1
- data/checksum/matchi-0.0.3.gem.sha512 +0 -1
- data/checksum/matchi-0.0.4.gem.sha512 +0 -1
- data/checksum/matchi-0.0.5.gem.sha512 +0 -1
- data/checksum/matchi-0.0.6.gem.sha512 +0 -1
- data/checksum/matchi-0.0.7.gem.sha512 +0 -1
- data/checksum/matchi-0.0.8.gem.sha512 +0 -1
- data/checksum/matchi-0.0.9.gem.sha512 +0 -1
- data/checksum/matchi-0.1.0.gem.sha512 +0 -1
- data/checksum/matchi-0.1.1.gem.sha512 +0 -1
- data/checksum/matchi-0.1.2.gem.sha512 +0 -1
- data/checksum/matchi-1.0.0.gem.sha512 +0 -1
- data/checksum/matchi-1.0.1.gem.sha512 +0 -1
- data/checksum/matchi-1.0.2.gem.sha512 +0 -1
- data/checksum/matchi-1.0.3.gem.sha512 +0 -1
- data/checksum/matchi-1.0.4.gem.sha512 +0 -1
- data/checksum/matchi-1.0.5.gem.sha512 +0 -1
- data/checksum/matchi-1.0.6.gem.sha512 +0 -1
- data/lib/matchi/matchers.rb +0 -11
- data/lib/matchi/matchers/be_false.rb +0 -30
- data/lib/matchi/matchers/be_nil.rb +0 -30
- data/lib/matchi/matchers/be_true.rb +0 -30
- data/lib/matchi/matchers/eql.rb +0 -40
- data/lib/matchi/matchers/equal.rb +0 -40
- data/lib/matchi/matchers/match.rb +0 -40
- data/lib/matchi/matchers/raise_exception.rb +0 -44
- data/lib/matchi/matchers_base.rb +0 -51
- data/matchi.gemspec +0 -24
- data/pkg_checksum +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c15a21ea2bcdc3ea9c43344bb1ff12216a384bc3d19a996c39a1138c28d18150
|
4
|
+
data.tar.gz: d023d2472ced57399fd14aea2a35cd36b75f7a9ac3705efd48985ccc03a6b9df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be27946a8a1aa3d24b978603be5efc2ce29eea9a18455dba435276329388fd1448c8e0f770b649ca34ad0cfce711a4b34939d665254741406f4bfa7602342475
|
7
|
+
data.tar.gz: dcdf2ece3a08c3dd266a9f01ebcb76013f3ab6e699b2bb8205656f7e3cd3ba76f786921c68516efc940404e72862a0bd5df9a021bb8c0c0b80720048e2738cc8
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,39 +1,31 @@
|
|
1
1
|
# Matchi
|
2
2
|
|
3
|
-
[][travis]
|
4
4
|
[][codeclimate]
|
5
5
|
[][gem]
|
6
|
-
[][inchpages]
|
7
6
|
[][rubydoc]
|
8
7
|
|
9
|
-
> Collection of expectation matchers for Ruby
|
10
|
-
|
11
|
-
## Contact
|
12
|
-
|
13
|
-
* Home page: https://github.com/fixrb/matchi
|
14
|
-
* Bugs/issues: https://github.com/fixrb/matchi/issues
|
15
|
-
|
16
|
-
## Rubies
|
17
|
-
|
18
|
-
* [MRI](https://www.ruby-lang.org/)
|
19
|
-
* [Rubinius](https://rubinius.com/)
|
20
|
-
* [JRuby](https://www.jruby.org/)
|
8
|
+
> Collection of expectation matchers for Ruby 🤹
|
21
9
|
|
22
10
|
## Installation
|
23
11
|
|
24
12
|
Add this line to your application's Gemfile:
|
25
13
|
|
26
14
|
```ruby
|
27
|
-
gem
|
15
|
+
gem "matchi"
|
28
16
|
```
|
29
17
|
|
30
18
|
And then execute:
|
31
19
|
|
32
|
-
|
20
|
+
```sh
|
21
|
+
bundle
|
22
|
+
```
|
33
23
|
|
34
24
|
Or install it yourself as:
|
35
25
|
|
36
|
-
|
26
|
+
```sh
|
27
|
+
gem install matchi
|
28
|
+
```
|
37
29
|
|
38
30
|
## Usage
|
39
31
|
|
@@ -42,52 +34,59 @@ Or install it yourself as:
|
|
42
34
|
**Equivalence** matcher:
|
43
35
|
|
44
36
|
```ruby
|
45
|
-
eql = Matchi::
|
46
|
-
eql.matches? {
|
37
|
+
eql = Matchi::Matcher::Eql.new("foo")
|
38
|
+
eql.matches? { "foo" } # => true
|
47
39
|
```
|
48
40
|
|
49
41
|
**Identity** matcher:
|
50
42
|
|
51
43
|
```ruby
|
52
|
-
equal = Matchi::
|
44
|
+
equal = Matchi::Matcher::Equal.new(:foo)
|
53
45
|
equal.matches? { :foo } # => true
|
54
46
|
```
|
55
47
|
|
56
48
|
**Regular expressions** matcher:
|
57
49
|
|
58
50
|
```ruby
|
59
|
-
match = Matchi::
|
60
|
-
match.matches? {
|
51
|
+
match = Matchi::Matcher::Match.new(/^foo$/)
|
52
|
+
match.matches? { "foo" } # => true
|
61
53
|
```
|
62
54
|
|
63
55
|
**Expecting errors** matcher:
|
64
56
|
|
65
57
|
```ruby
|
66
|
-
raise_exception = Matchi::
|
58
|
+
raise_exception = Matchi::Matcher::RaiseException.new(NameError)
|
67
59
|
raise_exception.matches? { Boom } # => true
|
68
60
|
```
|
69
61
|
|
70
62
|
**Truth** matcher:
|
71
63
|
|
72
64
|
```ruby
|
73
|
-
be_true = Matchi::
|
65
|
+
be_true = Matchi::Matcher::BeTrue.new
|
74
66
|
be_true.matches? { true } # => true
|
75
67
|
```
|
76
68
|
|
77
69
|
**Untruth** matcher:
|
78
70
|
|
79
71
|
```ruby
|
80
|
-
be_false = Matchi::
|
72
|
+
be_false = Matchi::Matcher::BeFalse.new
|
81
73
|
be_false.matches? { false } # => true
|
82
74
|
```
|
83
75
|
|
84
76
|
**Nil** matcher:
|
85
77
|
|
86
78
|
```ruby
|
87
|
-
be_nil = Matchi::
|
79
|
+
be_nil = Matchi::Matcher::BeNil.new
|
88
80
|
be_nil.matches? { nil } # => true
|
89
81
|
```
|
90
82
|
|
83
|
+
**Type/class** matcher:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
be_an_instance_of = Matchi::Matcher::BeAnInstanceOf.new(String)
|
87
|
+
be_an_instance_of.matches? { "foo" } # => true
|
88
|
+
```
|
89
|
+
|
91
90
|
### Custom matchers
|
92
91
|
|
93
92
|
Custom matchers can easily be defined for expressing expectations. They can be any Ruby class that responds to `matches?`, `to_s` and `to_h` instance methods.
|
@@ -96,55 +95,35 @@ A **Be the answer** matcher:
|
|
96
95
|
|
97
96
|
```ruby
|
98
97
|
module Matchi
|
99
|
-
module
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
42.equal? yield
|
104
|
-
end
|
105
|
-
|
106
|
-
def to_s
|
107
|
-
'be_the_answer'
|
108
|
-
end
|
109
|
-
|
110
|
-
def to_h
|
111
|
-
{ BeTheAnswer: [] }
|
112
|
-
end
|
98
|
+
module Matcher
|
99
|
+
class BeTheAnswer < ::Matchi::Matcher::Base
|
100
|
+
def matches?
|
101
|
+
42.equal?(yield)
|
113
102
|
end
|
114
103
|
end
|
115
104
|
end
|
116
105
|
end
|
117
106
|
|
118
|
-
be_the_answer = Matchi::
|
107
|
+
be_the_answer = Matchi::Matcher::BeTheAnswer.new
|
119
108
|
be_the_answer.matches? { 42 } # => true
|
120
109
|
```
|
121
110
|
|
122
111
|
A **Be prime** matcher:
|
123
112
|
|
124
113
|
```ruby
|
125
|
-
require
|
114
|
+
require "prime"
|
126
115
|
|
127
116
|
module Matchi
|
128
|
-
module
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
Prime.prime? yield
|
133
|
-
end
|
134
|
-
|
135
|
-
def to_s
|
136
|
-
'be_prime'
|
137
|
-
end
|
138
|
-
|
139
|
-
def to_h
|
140
|
-
{ BePrime: [] }
|
141
|
-
end
|
117
|
+
module Matcher
|
118
|
+
class BePrime < ::Matchi::Matcher::Base
|
119
|
+
def matches?
|
120
|
+
Prime.prime?(yield)
|
142
121
|
end
|
143
122
|
end
|
144
123
|
end
|
145
124
|
end
|
146
125
|
|
147
|
-
be_prime = Matchi::
|
126
|
+
be_prime = Matchi::Matcher::BePrime.new
|
148
127
|
be_prime.matches? { 42 } # => false
|
149
128
|
```
|
150
129
|
|
@@ -152,70 +131,47 @@ A **Start with** matcher:
|
|
152
131
|
|
153
132
|
```ruby
|
154
133
|
module Matchi
|
155
|
-
module
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
def to_s
|
167
|
-
'start_with'
|
168
|
-
end
|
169
|
-
|
170
|
-
def to_h
|
171
|
-
{ StartWith: [@expected] }
|
172
|
-
end
|
134
|
+
module Matcher
|
135
|
+
class StartWith < ::Matchi::Matcher::Base
|
136
|
+
def initialize(expected)
|
137
|
+
super()
|
138
|
+
@expected = expected
|
139
|
+
end
|
140
|
+
|
141
|
+
def matches?
|
142
|
+
Regexp.new(/\A#{expected}/).match?(yield)
|
173
143
|
end
|
174
144
|
end
|
175
145
|
end
|
176
146
|
end
|
177
147
|
|
178
|
-
start_with = Matchi::
|
179
|
-
start_with.matches? {
|
148
|
+
start_with = Matchi::Matcher::StartWith.new("foo")
|
149
|
+
start_with.matches? { "foobar" } # => true
|
180
150
|
```
|
181
151
|
|
182
|
-
##
|
183
|
-
|
184
|
-
As a basic form of security __Matchi__ provides a set of SHA512 checksums for
|
185
|
-
every Gem release. These checksums can be found in the `checksum/` directory.
|
186
|
-
Although these checksums do not prevent malicious users from tampering with a
|
187
|
-
built Gem they can be used for basic integrity verification purposes.
|
188
|
-
|
189
|
-
The checksum of a file can be checked using the `sha512sum` command. For
|
190
|
-
example:
|
152
|
+
## Contact
|
191
153
|
|
192
|
-
|
193
|
-
|
154
|
+
* Home page: https://github.com/fixrb/matchi
|
155
|
+
* Bugs/issues: https://github.com/fixrb/matchi/issues
|
194
156
|
|
195
157
|
## Versioning
|
196
158
|
|
197
159
|
__Matchi__ follows [Semantic Versioning 2.0](https://semver.org/).
|
198
160
|
|
199
|
-
##
|
161
|
+
## License
|
200
162
|
|
201
|
-
|
202
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
203
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
204
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
205
|
-
5. Create a new Pull Request
|
163
|
+
The [gem](https://rubygems.org/gems/matchi) is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
206
164
|
|
207
|
-
|
165
|
+
***
|
208
166
|
|
209
|
-
|
167
|
+
<p>
|
168
|
+
This project is sponsored by:<br />
|
169
|
+
<a href="https://sashite.com/"><img
|
170
|
+
src="https://github.com/fixrb/matchi/raw/main/img/sashite.png"
|
171
|
+
alt="Sashite" /></a>
|
172
|
+
</p>
|
210
173
|
|
211
174
|
[gem]: https://rubygems.org/gems/matchi
|
212
175
|
[travis]: https://travis-ci.org/fixrb/matchi
|
213
176
|
[codeclimate]: https://codeclimate.com/github/fixrb/matchi
|
214
|
-
[inchpages]: https://inch-ci.org/github/fixrb/matchi
|
215
177
|
[rubydoc]: https://rubydoc.info/gems/matchi/frames
|
216
|
-
|
217
|
-
***
|
218
|
-
|
219
|
-
This project is sponsored by:
|
220
|
-
|
221
|
-
[](https://sashite.com/)
|
data/lib/matchi.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "matcher"
|
4
|
+
|
5
|
+
module Matchi
|
6
|
+
# Collection of helper methods.
|
7
|
+
module Helper
|
8
|
+
::Matchi::Matcher.constants.each do |matcher_const|
|
9
|
+
next if matcher_const.equal?(:Base)
|
10
|
+
|
11
|
+
matcher_klass = ::Matchi::Matcher.const_get(matcher_const)
|
12
|
+
|
13
|
+
# Define a method for the given matcher.
|
14
|
+
#
|
15
|
+
# @example Given the `Matchi::Matchers::Equal::Matcher` matcher, its
|
16
|
+
# method would be:
|
17
|
+
#
|
18
|
+
# def equal(expected)
|
19
|
+
# Matchi::Matchers::Equal::Matcher.new(expected)
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @return [#matches?] The matcher.
|
23
|
+
define_method(matcher_klass.to_sym) do |*args|
|
24
|
+
matcher_klass.new(*args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Matchi
|
4
|
+
module Matcher
|
5
|
+
# Abstract matcher class.
|
6
|
+
class Base
|
7
|
+
# @return [Symbol] A symbol identifying the matcher.
|
8
|
+
def self.to_sym
|
9
|
+
name.delete_prefix("Matchi::Matcher::")
|
10
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
11
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
12
|
+
.downcase
|
13
|
+
.to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [#object_id] Any value to give to the matcher.
|
17
|
+
attr_reader :expected
|
18
|
+
|
19
|
+
# A string containing a human-readable representation of the matcher.
|
20
|
+
#
|
21
|
+
# @return [String] The human-readable representation of the matcher.
|
22
|
+
def inspect
|
23
|
+
"#{self.class}(#{expected&.inspect})"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Abstract matcher class.
|
27
|
+
#
|
28
|
+
# @raise [NotImplementedError] Override me inside a matcher.
|
29
|
+
def matches?
|
30
|
+
raise ::NotImplementedError, "matcher MUST respond to this method."
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns a string representing the matcher.
|
34
|
+
#
|
35
|
+
# @example The readable definition of a FooBar matcher.
|
36
|
+
# matcher = Matchi::Matcher::FooBar.new(42)
|
37
|
+
# matcher.to_s # => "foo_bar 42"
|
38
|
+
#
|
39
|
+
# @return [String] A string representing the matcher.
|
40
|
+
def to_s
|
41
|
+
[self.class.to_sym, expected&.inspect].compact.join(" ")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "base"
|
4
|
+
|
5
|
+
module Matchi
|
6
|
+
module Matcher
|
7
|
+
# *Type/class* matcher.
|
8
|
+
class BeAnInstanceOf < ::Matchi::Matcher::Base
|
9
|
+
# Initialize the matcher with an object.
|
10
|
+
#
|
11
|
+
# @example A string matcher
|
12
|
+
# Matchi::Matcher::BeAnInstanceOf.new(String)
|
13
|
+
#
|
14
|
+
# @param expected [Class] An expected class.
|
15
|
+
def initialize(expected)
|
16
|
+
super()
|
17
|
+
@expected = expected
|
18
|
+
end
|
19
|
+
|
20
|
+
# Boolean comparison between the class of the actual value and the
|
21
|
+
# expected class.
|
22
|
+
#
|
23
|
+
# @example Is it an instance of string?
|
24
|
+
# be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(String)
|
25
|
+
# be_an_instance_of.matches? { "foo" } # => true
|
26
|
+
#
|
27
|
+
# @yieldreturn [#class] the actual value to compare to the expected one.
|
28
|
+
#
|
29
|
+
# @return [Boolean] Comparison between actual and expected values.
|
30
|
+
def matches?(*, **)
|
31
|
+
expected.equal?(yield.class)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "base"
|
4
|
+
|
5
|
+
module Matchi
|
6
|
+
module Matcher
|
7
|
+
# *Untruth* matcher.
|
8
|
+
class BeFalse < ::Matchi::Matcher::Base
|
9
|
+
# Boolean comparison between the actual value and the expected value.
|
10
|
+
#
|
11
|
+
# @example Is it false?
|
12
|
+
# be_false = Matchi::Matcher::BeFalse.new
|
13
|
+
# be_false.matches? { false } # => true
|
14
|
+
#
|
15
|
+
# @yieldreturn [#object_id] The actual value to compare to the expected
|
16
|
+
# one.
|
17
|
+
#
|
18
|
+
# @return [Boolean] Comparison between actual and expected values.
|
19
|
+
def matches?(*, **)
|
20
|
+
false.equal?(yield)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|