matchi 2.1.1 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 168258c942f6e9390a6bc6ad65112de3acbcd835438a750dd1dc4692e256db6d
4
- data.tar.gz: 8b64c5cbdf54f665f1982cb9e8d44a16685904b626680e22cab80969304108be
3
+ metadata.gz: d0160e87dd2bb35e34120f11d16c0ebb5a5d4b4c984612e37a8139cb65814ed6
4
+ data.tar.gz: 10ce199e0511fda23304b1f8b64bf1c7911512e77c2af6b2d90ad078d56caed0
5
5
  SHA512:
6
- metadata.gz: 40d8993bb7bae28eff3ef0d86d4d8e9dc51f36195466fa3f3016c4f1c52dfccac27e0eb02c18c1f34a4c4f2df16a8ba566c89633e2da7d595ad433ac4e03563f
7
- data.tar.gz: d2e8b2cbb554f76cf9a0e25dbbf41e591d962ed3e1af7c64d5de84d823e86fc317e3d76e174d4f559f526022aa6afcf1e1411200b4789821d7ffc8917b7e4e3d
6
+ metadata.gz: 5bb3da470e32e8332b4fb4106ebec65d06f23c92f9fdc18ce6de655b0fdc3408142210aebfcfe45d30e79ce0b3856488658f3c107c5dab7164125561e2a44bd0
7
+ data.tar.gz: 55dce8aca5a1b3aeacb396da47e41e008bde7c189b9acc389a39ba03beb9a3eb883732b5dfaf476158662d1c396fd7a454f0b0a10cd8ed87489b88440771158c
data/README.md CHANGED
@@ -98,10 +98,17 @@ be_nil.matches? { nil } # => true
98
98
  **Type/class** matcher:
99
99
 
100
100
  ```ruby
101
- be_an_instance_of = Matchi::Matcher::BeAnInstanceOf.new(String)
101
+ be_an_instance_of = Matchi::Matcher::BeAnInstanceOf.new(:String)
102
102
  be_an_instance_of.matches? { "foo" } # => true
103
103
  ```
104
104
 
105
+ **Satisfy** matcher:
106
+
107
+ ```ruby
108
+ satisfy = Matchi::Matcher::Satisfy.new { |value| value == 42 }
109
+ satisfy.matches? { 42 } # => true
110
+ ```
111
+
105
112
  ### Custom matchers
106
113
 
107
114
  Custom matchers can easily be defined for expressing expectations.
@@ -183,7 +190,7 @@ The set of loaded matcher then becomes accessible via a dynamically generated in
183
190
  ```ruby
184
191
  matcher = MatcherCollection.new
185
192
  matcher.equal(42).matches? { 44 } # => false
186
- matcher.be_an_instance_of(String).matches? { "안녕하세요" } # => true
193
+ matcher.be_an_instance_of(:String).matches? { "안녕하세요" } # => true
187
194
  ```
188
195
 
189
196
  ## Contact
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
@@ -32,8 +32,8 @@ module Matchi
32
32
 
33
33
  matcher_klass = ::Matchi::Matcher.const_get(matcher_const)
34
34
 
35
- define_method(matcher_klass.to_sym) do |*args|
36
- matcher_klass.new(*args)
35
+ define_method(matcher_klass.to_sym) do |*args, **kwargs, &block|
36
+ matcher_klass.new(*args, **kwargs, &block)
37
37
  end
38
38
  end
39
39
  end
@@ -8,13 +8,18 @@ module Matchi
8
8
  class BeAnInstanceOf < ::Matchi::Matcher::Base
9
9
  # Initialize the matcher with an object.
10
10
  #
11
- # @example A string matcher
12
- # Matchi::Matcher::BeAnInstanceOf.new(String)
11
+ # @example A duck matcher
12
+ # Matchi::Matcher::BeAnInstanceOf.new(:Duck)
13
13
  #
14
- # @param expected [Class] An expected class.
14
+ # @param expected [#to_s] The name of a module.
15
15
  def initialize(expected)
16
16
  super()
17
- @expected = expected
17
+ @expected = String(expected).to_sym
18
+ end
19
+
20
+ # (see Base#inspect)
21
+ def inspect
22
+ "#{self.class}(#{expected})"
18
23
  end
19
24
 
20
25
  # Boolean comparison between the class of the actual value and the
@@ -24,11 +29,22 @@ module Matchi
24
29
  # be_an_instance_of = Matchi::Matcher::BeInstanceOf.new(String)
25
30
  # be_an_instance_of.matches? { "foo" } # => true
26
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
+ #
27
38
  # @yieldreturn [#class] the actual value to compare to the expected one.
28
39
  #
29
40
  # @return [Boolean] Comparison between actual and expected values.
30
41
  def matches?(*, **)
31
- expected.equal?(yield.class)
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}"
32
48
  end
33
49
  end
34
50
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Matchi
6
+ module Matcher
7
+ # *Satisfy* matcher.
8
+ class Satisfy < ::Matchi::Matcher::Base
9
+ # Initialize the matcher with a block.
10
+ #
11
+ # @example The number 42 matcher.
12
+ # Matchi::Matcher::Satisfy.new { |value| value == 42 }
13
+ #
14
+ # @param block [Proc] A block of code.
15
+ def initialize(&block)
16
+ super()
17
+ @expected = block
18
+ end
19
+
20
+ # (see Base#inspect)
21
+ def inspect
22
+ "#{self.class}(&block)"
23
+ end
24
+
25
+ # Boolean comparison between the actual value and the expected value.
26
+ #
27
+ # @example Is it equal to 42
28
+ # equal = Matchi::Matcher::Satisfy.new { |value| value == 42 }
29
+ # equal.matches? { 42 } # => true
30
+ #
31
+ # @yieldreturn [#object_id] The actual value to compare to the expected
32
+ # one.
33
+ #
34
+ # @return [Boolean] Comparison between actual and expected values.
35
+ def matches?(*, **)
36
+ expected.call(yield)
37
+ end
38
+
39
+ # (see Base#to_s)
40
+ def to_s
41
+ "#{self.class.to_sym} &block"
42
+ end
43
+ end
44
+ end
45
+ 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: 2.1.1
4
+ version: 2.3.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-07-14 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
@@ -142,6 +142,7 @@ files:
142
142
  - lib/matchi/matcher/equal.rb
143
143
  - lib/matchi/matcher/match.rb
144
144
  - lib/matchi/matcher/raise_exception.rb
145
+ - lib/matchi/matcher/satisfy.rb
145
146
  homepage: https://github.com/fixrb/matchi
146
147
  licenses:
147
148
  - MIT