matchi 2.1.0 β 2.1.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 +42 -10
- data/lib/matchi/helper.rb +23 -11
- data/lib/matchi/matcher/base.rb +23 -5
- data/lib/matchi/matcher/eql.rb +5 -5
- data/lib/matchi/matcher/match.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 168258c942f6e9390a6bc6ad65112de3acbcd835438a750dd1dc4692e256db6d
|
4
|
+
data.tar.gz: 8b64c5cbdf54f665f1982cb9e8d44a16685904b626680e22cab80969304108be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40d8993bb7bae28eff3ef0d86d4d8e9dc51f36195466fa3f3016c4f1c52dfccac27e0eb02c18c1f34a4c4f2df16a8ba566c89633e2da7d595ad433ac4e03563f
|
7
|
+
data.tar.gz: d2e8b2cbb554f76cf9a0e25dbbf41e591d962ed3e1af7c64d5de84d823e86fc317e3d76e174d4f559f526022aa6afcf1e1411200b4789821d7ffc8917b7e4e3d
|
data/README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
# Matchi
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![
|
6
|
-
[![
|
3
|
+
[![Version](https://img.shields.io/github/v/tag/fixrb/matchi?label=Version&logo=github)](https://github.com/fixrb/matchi/releases)
|
4
|
+
[![Yard documentation](https://img.shields.io/badge/Yard-documentation-blue.svg?logo=github)](https://rubydoc.info/github/fixrb/matchi/main)
|
5
|
+
[![CI](https://github.com/fixrb/matchi/workflows/CI/badge.svg?branch=main)](https://github.com/fixrb/matchi/actions?query=workflow%3Aci+branch%3Amain)
|
6
|
+
[![RuboCop](https://github.com/fixrb/matchi/workflows/RuboCop/badge.svg?branch=main)](https://github.com/fixrb/matchi/actions?query=workflow%3Arubocop+branch%3Amain)
|
7
|
+
[![License](https://img.shields.io/github/license/fixrb/matchi?label=License&logo=github)](https://github.com/fixrb/matchi/raw/main/LICENSE.md)
|
7
8
|
|
8
9
|
> Collection of expectation matchers for Ruby π€Ή
|
9
10
|
|
11
|
+
![A rubyist juggling between colored balls representing expectation matchers](https://github.com/fixrb/matchi/raw/main/img/matchi.jpg)
|
12
|
+
|
10
13
|
## Installation
|
11
14
|
|
12
15
|
Add this line to your application's Gemfile:
|
@@ -27,8 +30,20 @@ Or install it yourself as:
|
|
27
30
|
gem install matchi
|
28
31
|
```
|
29
32
|
|
33
|
+
## Overview
|
34
|
+
|
35
|
+
__Matchi__ provides a collection of damn simple expectation matchers.
|
36
|
+
|
30
37
|
## Usage
|
31
38
|
|
39
|
+
To make __Matchi__ available:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require "matchi"
|
43
|
+
```
|
44
|
+
|
45
|
+
All examples here assume that this has been done.
|
46
|
+
|
32
47
|
### Built-in matchers
|
33
48
|
|
34
49
|
**Equivalence** matcher:
|
@@ -89,7 +104,8 @@ be_an_instance_of.matches? { "foo" } # => true
|
|
89
104
|
|
90
105
|
### Custom matchers
|
91
106
|
|
92
|
-
Custom matchers can easily be defined for expressing expectations.
|
107
|
+
Custom matchers can easily be defined for expressing expectations.
|
108
|
+
They can be any Ruby class that responds to `matches?` instance method with a block.
|
93
109
|
|
94
110
|
A **Be the answer** matcher:
|
95
111
|
|
@@ -149,6 +165,27 @@ start_with = Matchi::Matcher::StartWith.new("foo")
|
|
149
165
|
start_with.matches? { "foobar" } # => true
|
150
166
|
```
|
151
167
|
|
168
|
+
### Helper methods
|
169
|
+
|
170
|
+
For convenience, it is possible to instantiate a matcher with a method rather than with its class.
|
171
|
+
To do so, the `Helper` module can be included like this:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
require "matchi/helper"
|
175
|
+
|
176
|
+
class MatcherCollection
|
177
|
+
include ::Matchi::Helper
|
178
|
+
end
|
179
|
+
```
|
180
|
+
|
181
|
+
The set of loaded matcher then becomes accessible via a dynamically generated instance method, like these:
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
matcher = MatcherCollection.new
|
185
|
+
matcher.equal(42).matches? { 44 } # => false
|
186
|
+
matcher.be_an_instance_of(String).matches? { "μλ
νμΈμ" } # => true
|
187
|
+
```
|
188
|
+
|
152
189
|
## Contact
|
153
190
|
|
154
191
|
* Home page: https://github.com/fixrb/matchi
|
@@ -170,8 +207,3 @@ The [gem](https://rubygems.org/gems/matchi) is available as open source under th
|
|
170
207
|
src="https://github.com/fixrb/matchi/raw/main/img/sashite.png"
|
171
208
|
alt="Sashite" /></a>
|
172
209
|
</p>
|
173
|
-
|
174
|
-
[gem]: https://rubygems.org/gems/matchi
|
175
|
-
[travis]: https://travis-ci.org/fixrb/matchi
|
176
|
-
[codeclimate]: https://codeclimate.com/github/fixrb/matchi
|
177
|
-
[rubydoc]: https://rubydoc.info/gems/matchi/frames
|
data/lib/matchi/helper.rb
CHANGED
@@ -3,23 +3,35 @@
|
|
3
3
|
require_relative "matcher"
|
4
4
|
|
5
5
|
module Matchi
|
6
|
-
#
|
6
|
+
# When included, this module defines a helper instance method per matcher.
|
7
|
+
#
|
8
|
+
# @example Define and use the dynamic helper instance method of a custom matcher
|
9
|
+
# require "matchi/matcher/base"
|
10
|
+
#
|
11
|
+
# module Matchi
|
12
|
+
# module Matcher
|
13
|
+
# class BeTheAnswer < ::Matchi::Matcher::Base
|
14
|
+
# def matches?
|
15
|
+
# 42.equal?(yield)
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# require "matchi/helper"
|
22
|
+
#
|
23
|
+
# class MatcherBase
|
24
|
+
# include ::Matchi::Helper
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# matcher_base = MatcherBase.new
|
28
|
+
# matcher_base.be_the_answer.matches? { 42 } # => true
|
7
29
|
module Helper
|
8
30
|
::Matchi::Matcher.constants.each do |matcher_const|
|
9
31
|
next if matcher_const.equal?(:Base)
|
10
32
|
|
11
33
|
matcher_klass = ::Matchi::Matcher.const_get(matcher_const)
|
12
34
|
|
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
35
|
define_method(matcher_klass.to_sym) do |*args|
|
24
36
|
matcher_klass.new(*args)
|
25
37
|
end
|
data/lib/matchi/matcher/base.rb
CHANGED
@@ -4,9 +4,16 @@ module Matchi
|
|
4
4
|
module Matcher
|
5
5
|
# Abstract matcher class.
|
6
6
|
class Base
|
7
|
+
# Returns a symbol identifying the matcher.
|
8
|
+
#
|
9
|
+
# @example The readable definition of a FooBar matcher class.
|
10
|
+
# matcher_class = Matchi::Matcher::FooBar
|
11
|
+
# matcher_class.to_sym # => "foo_bar"
|
12
|
+
#
|
7
13
|
# @return [Symbol] A symbol identifying the matcher.
|
8
14
|
def self.to_sym
|
9
|
-
name.
|
15
|
+
name.split("::")
|
16
|
+
.fetch(-1)
|
10
17
|
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
11
18
|
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
12
19
|
.downcase
|
@@ -18,6 +25,10 @@ module Matchi
|
|
18
25
|
|
19
26
|
# A string containing a human-readable representation of the matcher.
|
20
27
|
#
|
28
|
+
# @example The human-readable representation of a FooBar matcher instance.
|
29
|
+
# matcher = Matchi::Matcher::FooBar.new(42)
|
30
|
+
# matcher.inspect # => "Matchi::Matcher::FooBar(42)"
|
31
|
+
#
|
21
32
|
# @return [String] The human-readable representation of the matcher.
|
22
33
|
def inspect
|
23
34
|
"#{self.class}(#{expected&.inspect})"
|
@@ -25,18 +36,25 @@ module Matchi
|
|
25
36
|
|
26
37
|
# Abstract matcher class.
|
27
38
|
#
|
28
|
-
# @
|
39
|
+
# @example Test the equivalence between two "foo" strings.
|
40
|
+
# eql = Matchi::Matcher::Eql.new("foo")
|
41
|
+
# eql.matches? { "foo" } # => true
|
42
|
+
#
|
43
|
+
# @yieldreturn [#object_id] The actual value to compare to the expected
|
44
|
+
# one.
|
45
|
+
#
|
46
|
+
# @raise [NotImplementedError] Override this method inside a matcher.
|
29
47
|
def matches?
|
30
48
|
raise ::NotImplementedError, "matcher MUST respond to this method."
|
31
49
|
end
|
32
50
|
|
33
|
-
# Returns a string representing the matcher.
|
51
|
+
# Returns a string representing the matcher instance.
|
34
52
|
#
|
35
|
-
# @example The readable definition of a FooBar matcher.
|
53
|
+
# @example The readable definition of a FooBar matcher instance.
|
36
54
|
# matcher = Matchi::Matcher::FooBar.new(42)
|
37
55
|
# matcher.to_s # => "foo_bar 42"
|
38
56
|
#
|
39
|
-
# @return [String] A string representing the matcher.
|
57
|
+
# @return [String] A string representing the matcher instance.
|
40
58
|
def to_s
|
41
59
|
[self.class.to_sym, expected&.inspect].compact.join(" ")
|
42
60
|
end
|
data/lib/matchi/matcher/eql.rb
CHANGED
@@ -8,8 +8,8 @@ module Matchi
|
|
8
8
|
class Eql < ::Matchi::Matcher::Base
|
9
9
|
# Initialize the matcher with an object.
|
10
10
|
#
|
11
|
-
# @example The string
|
12
|
-
# Matchi::Matcher::Eql.new(
|
11
|
+
# @example The string "foo" matcher.
|
12
|
+
# Matchi::Matcher::Eql.new("foo")
|
13
13
|
#
|
14
14
|
# @param expected [#eql?] An expected equivalent object.
|
15
15
|
def initialize(expected)
|
@@ -19,9 +19,9 @@ module Matchi
|
|
19
19
|
|
20
20
|
# Boolean comparison between the actual value and the expected value.
|
21
21
|
#
|
22
|
-
# @example Is it equivalent to
|
23
|
-
# eql = Matchi::Matcher::Eql.new(
|
24
|
-
# eql.matches? {
|
22
|
+
# @example Is it equivalent to "foo"?
|
23
|
+
# eql = Matchi::Matcher::Eql.new("foo")
|
24
|
+
# eql.matches? { "foo" } # => true
|
25
25
|
#
|
26
26
|
# @yieldreturn [#object_id] The actual value to compare to the expected
|
27
27
|
# one.
|
data/lib/matchi/matcher/match.rb
CHANGED
@@ -21,7 +21,7 @@ module Matchi
|
|
21
21
|
#
|
22
22
|
# @example Is it matching /^foo$/ regex?
|
23
23
|
# match = Matchi::Matcher::Match.new(/^foo$/)
|
24
|
-
# match.matches? {
|
24
|
+
# match.matches? { "foo" } # => true
|
25
25
|
#
|
26
26
|
# @yieldreturn [#object_id] The actual value to compare to the expected
|
27
27
|
# one.
|
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.1.
|
4
|
+
version: 2.1.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-
|
11
|
+
date: 2021-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|