matchi 2.0.2 β†’ 2.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbd646c1f880291334b1f98426cfdedab736a2f87bbb44e149e81bcb5f8affce
4
- data.tar.gz: 9e285052b96848df8be1c22c2033032d2dcbe143ac4d84fa26e9e61b42ac8ee4
3
+ metadata.gz: 43879fd564f4fe9dfab4116b3e59e452edd5e700dd4cd0f71164965214a17f47
4
+ data.tar.gz: c4f2eda14d387063449b71e42ea80af8a61888a676b986106c40062735c19342
5
5
  SHA512:
6
- metadata.gz: d81a615fca3627cf424226c015e63ab09419be15059b83e926afa3d99819153ced25afca5adfe1421d85bbffcaf4048f2c36531b14b6a9f4920a1f7edf9efd61
7
- data.tar.gz: e80cd7a69eef70a3ca0fc80bb4bf94468c14de22a9309d4bf736ea050e5feeeadf69216eadddc6e4ee678c08ccf64949729a3ef830ed9e1b4b3ca3d5ba23d0f7
6
+ metadata.gz: fd575e3e83c363e04d34ea37ed2f66cb5d7a84dfb63c4b18752347ad105bea9074a0c1489a598a9904295d06e5bf051d5126a0d785231ca9df2762f188db7c1d
7
+ data.tar.gz: b9e90e939b96c10c27796d58577e2c8db4269233e689bb17e1845526da2c5e83668b1608a478ffdec4321d7852750eec9f21b92bd5f4932d6190a85caa11c491
data/README.md CHANGED
@@ -1,13 +1,15 @@
1
1
  # Matchi
2
2
 
3
- [![Build Status](https://api.travis-ci.org/fixrb/matchi.svg?branch=main)][travis]
4
- [![Code Climate](https://codeclimate.com/github/fixrb/matchi/badges/gpa.svg)][codeclimate]
5
- [![Gem Version](https://badge.fury.io/rb/matchi.svg)][gem]
6
- [![Inline docs](https://inch-ci.org/github/fixrb/matchi.svg?branch=main)][inchpages]
7
- [![Documentation](https://img.shields.io/:yard-docs-38c800.svg)][rubydoc]
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)
8
8
 
9
9
  > Collection of expectation matchers for Ruby 🀹
10
10
 
11
+ ![A rubyist juggling between colored balls representing expectation matchers](https://github.com/fixrb/matchi/raw/main/img/matchi.jpg)
12
+
11
13
  ## Installation
12
14
 
13
15
  Add this line to your application's Gemfile:
@@ -18,14 +20,30 @@ gem "matchi"
18
20
 
19
21
  And then execute:
20
22
 
21
- $ bundle
23
+ ```sh
24
+ bundle
25
+ ```
22
26
 
23
27
  Or install it yourself as:
24
28
 
25
- $ gem install matchi
29
+ ```sh
30
+ gem install matchi
31
+ ```
32
+
33
+ ## Overview
34
+
35
+ __Matchi__ provides a collection of damn simple expectation matchers.
26
36
 
27
37
  ## Usage
28
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
+
29
47
  ### Built-in matchers
30
48
 
31
49
  **Equivalence** matcher:
@@ -77,9 +95,17 @@ be_nil = Matchi::Matcher::BeNil.new
77
95
  be_nil.matches? { nil } # => true
78
96
  ```
79
97
 
98
+ **Type/class** matcher:
99
+
100
+ ```ruby
101
+ be_an_instance_of = Matchi::Matcher::BeAnInstanceOf.new(:String)
102
+ be_an_instance_of.matches? { "foo" } # => true
103
+ ```
104
+
80
105
  ### Custom matchers
81
106
 
82
- 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.
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.
83
109
 
84
110
  A **Be the answer** matcher:
85
111
 
@@ -139,6 +165,27 @@ start_with = Matchi::Matcher::StartWith.new("foo")
139
165
  start_with.matches? { "foobar" } # => true
140
166
  ```
141
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
+
142
189
  ## Contact
143
190
 
144
191
  * Home page: https://github.com/fixrb/matchi
@@ -160,9 +207,3 @@ The [gem](https://rubygems.org/gems/matchi) is available as open source under th
160
207
  src="https://github.com/fixrb/matchi/raw/main/img/sashite.png"
161
208
  alt="Sashite" /></a>
162
209
  </p>
163
-
164
- [gem]: https://rubygems.org/gems/matchi
165
- [travis]: https://travis-ci.org/fixrb/matchi
166
- [codeclimate]: https://codeclimate.com/github/fixrb/matchi
167
- [inchpages]: https://inch-ci.org/github/fixrb/matchi
168
- [rubydoc]: https://rubydoc.info/gems/matchi/frames
data/lib/matchi.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Namespace for the Matchi library.
4
+ #
5
+ # @api public
4
6
  module Matchi
5
7
  end
6
8
 
data/lib/matchi/helper.rb CHANGED
@@ -3,23 +3,35 @@
3
3
  require_relative "matcher"
4
4
 
5
5
  module Matchi
6
- # Collection of helper methods.
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
@@ -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.delete_prefix("Matchi::Matcher::")
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
- # @raise [NotImplementedError] Override me inside a matcher.
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
@@ -0,0 +1,51 @@
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 duck matcher
12
+ # Matchi::Matcher::BeAnInstanceOf.new(:Duck)
13
+ #
14
+ # @param expected [#to_s] The name of a module.
15
+ def initialize(expected)
16
+ super()
17
+ @expected = String(expected).to_sym
18
+ end
19
+
20
+ # (see Base#inspect)
21
+ def inspect
22
+ "#{self.class}(#{expected})"
23
+ end
24
+
25
+ # Boolean comparison between the class of the actual value and the
26
+ # expected class.
27
+ #
28
+ # @example Is it an instance of string?
29
+ # be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(String)
30
+ # be_an_instance_of.matches? { "foo" } # => true
31
+ #
32
+ # be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(:String)
33
+ # be_an_instance_of.matches? { "foo" } # => true
34
+ #
35
+ # be_an_instance_of = Matchi::Matcher::BeInstanceOf.new("String")
36
+ # be_an_instance_of.matches? { "foo" } # => true
37
+ #
38
+ # @yieldreturn [#class] the actual value to compare to the expected one.
39
+ #
40
+ # @return [Boolean] Comparison between actual and expected values.
41
+ def matches?(*, **)
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}"
48
+ end
49
+ end
50
+ end
51
+ end
@@ -4,7 +4,7 @@ require_relative "base"
4
4
 
5
5
  module Matchi
6
6
  module Matcher
7
- # **Untruth** matcher.
7
+ # *Untruth* matcher.
8
8
  class BeFalse < ::Matchi::Matcher::Base
9
9
  # Boolean comparison between the actual value and the expected value.
10
10
  #
@@ -4,7 +4,7 @@ require_relative "base"
4
4
 
5
5
  module Matchi
6
6
  module Matcher
7
- # **Nil** matcher.
7
+ # *Nil* matcher.
8
8
  class BeNil < ::Matchi::Matcher::Base
9
9
  # Boolean comparison between the actual value and the expected value.
10
10
  #
@@ -4,7 +4,7 @@ require_relative "base"
4
4
 
5
5
  module Matchi
6
6
  module Matcher
7
- # **Truth** matcher.
7
+ # *Truth* matcher.
8
8
  class BeTrue < ::Matchi::Matcher::Base
9
9
  # Boolean comparison between the actual value and the expected value.
10
10
  #
@@ -4,12 +4,12 @@ require_relative "base"
4
4
 
5
5
  module Matchi
6
6
  module Matcher
7
- # **Equivalence** matcher.
7
+ # *Equivalence* matcher.
8
8
  class Eql < ::Matchi::Matcher::Base
9
9
  # Initialize the matcher with an object.
10
10
  #
11
- # @example The string 'foo' matcher.
12
- # Matchi::Matcher::Eql.new('foo')
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 'foo'?
23
- # eql = Matchi::Matcher::Eql.new('foo')
24
- # eql.matches? { 'foo' } # => true
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.
@@ -4,7 +4,7 @@ require_relative "base"
4
4
 
5
5
  module Matchi
6
6
  module Matcher
7
- # **Identity** matcher.
7
+ # *Identity* matcher.
8
8
  class Equal < ::Matchi::Matcher::Base
9
9
  # Initialize the matcher with an object.
10
10
  #
@@ -4,7 +4,7 @@ require_relative "base"
4
4
 
5
5
  module Matchi
6
6
  module Matcher
7
- # **Regular expressions** matcher.
7
+ # *Regular expressions* matcher.
8
8
  class Match < ::Matchi::Matcher::Base
9
9
  # Initialize the matcher with an instance of Regexp.
10
10
  #
@@ -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? { 'foo' } # => true
24
+ # match.matches? { "foo" } # => true
25
25
  #
26
26
  # @yieldreturn [#object_id] The actual value to compare to the expected
27
27
  # one.
@@ -4,7 +4,7 @@ require_relative "base"
4
4
 
5
5
  module Matchi
6
6
  module Matcher
7
- # **Expecting errors** matcher.
7
+ # *Expecting errors* matcher.
8
8
  class RaiseException < ::Matchi::Matcher::Base
9
9
  # Initialize the matcher with a descendant of class Exception.
10
10
  #
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.0.2
4
+ version: 2.2.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-06-07 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -134,6 +134,7 @@ files:
134
134
  - lib/matchi/helper.rb
135
135
  - lib/matchi/matcher.rb
136
136
  - lib/matchi/matcher/base.rb
137
+ - lib/matchi/matcher/be_an_instance_of.rb
137
138
  - lib/matchi/matcher/be_false.rb
138
139
  - lib/matchi/matcher/be_nil.rb
139
140
  - lib/matchi/matcher/be_true.rb