matchi 2.4.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Satisfy* matcher.
5
+ class Satisfy
6
+ # Initialize the matcher with a block.
7
+ #
8
+ # @example
9
+ # require "matchi/satisfy"
10
+ #
11
+ # Matchi::Satisfy.new { |value| value == 42 }
12
+ #
13
+ # @param block [Proc] A block of code.
14
+ def initialize(&block)
15
+ @expected = block
16
+ end
17
+
18
+ # Boolean comparison between the actual value and the expected value.
19
+ #
20
+ # @example
21
+ # require "matchi/satisfy"
22
+ #
23
+ # matcher = Matchi::Satisfy.new { |value| value == 42 }
24
+ # matcher.matches? { 42 } # => true
25
+ #
26
+ # @yieldreturn [#object_id] The actual value to compare to the expected
27
+ # one.
28
+ #
29
+ # @return [Boolean] Comparison between actual and expected values.
30
+ def matches?(*, **)
31
+ @expected.call(yield)
32
+ end
33
+
34
+ # A string containing a human-readable representation of the matcher.
35
+ def inspect
36
+ "#{self.class}(&block)"
37
+ end
38
+
39
+ # Returns a string representing the matcher.
40
+ def to_s
41
+ "satisfy &block"
42
+ end
43
+ end
44
+ 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.4.0
4
+ version: 3.0.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-21 00:00:00.000000000 Z
11
+ date: 2021-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,7 +122,7 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: "Collection of expectation matchers for Ruby \U0001F939"
125
+ description: "Collection of expectation matchers for Rubyists \U0001F939"
126
126
  email: contact@cyril.email
127
127
  executables: []
128
128
  extensions: []
@@ -131,25 +131,19 @@ files:
131
131
  - LICENSE.md
132
132
  - README.md
133
133
  - lib/matchi.rb
134
- - lib/matchi/helper.rb
135
- - lib/matchi/matcher.rb
136
- - lib/matchi/matcher/base.rb
137
- - lib/matchi/matcher/be_an_instance_of.rb
138
- - lib/matchi/matcher/be_false.rb
139
- - lib/matchi/matcher/be_nil.rb
140
- - lib/matchi/matcher/be_true.rb
141
- - lib/matchi/matcher/change.rb
142
- - lib/matchi/matcher/change/by.rb
143
- - lib/matchi/matcher/change/by_at_least.rb
144
- - lib/matchi/matcher/change/by_at_most.rb
145
- - lib/matchi/matcher/change/from.rb
146
- - lib/matchi/matcher/change/from/to.rb
147
- - lib/matchi/matcher/change/to.rb
148
- - lib/matchi/matcher/eql.rb
149
- - lib/matchi/matcher/equal.rb
150
- - lib/matchi/matcher/match.rb
151
- - lib/matchi/matcher/raise_exception.rb
152
- - lib/matchi/matcher/satisfy.rb
134
+ - lib/matchi/be.rb
135
+ - lib/matchi/be_an_instance_of.rb
136
+ - lib/matchi/change.rb
137
+ - lib/matchi/change/by.rb
138
+ - lib/matchi/change/by_at_least.rb
139
+ - lib/matchi/change/by_at_most.rb
140
+ - lib/matchi/change/from.rb
141
+ - lib/matchi/change/from/to.rb
142
+ - lib/matchi/change/to.rb
143
+ - lib/matchi/eq.rb
144
+ - lib/matchi/match.rb
145
+ - lib/matchi/raise_exception.rb
146
+ - lib/matchi/satisfy.rb
153
147
  homepage: https://github.com/fixrb/matchi
154
148
  licenses:
155
149
  - MIT
@@ -172,5 +166,5 @@ requirements: []
172
166
  rubygems_version: 3.1.6
173
167
  signing_key:
174
168
  specification_version: 4
175
- summary: "Collection of expectation matchers for Ruby \U0001F939"
169
+ summary: "Collection of expectation matchers for Rubyists \U0001F939"
176
170
  test_files: []
data/lib/matchi/helper.rb DELETED
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "matcher"
4
-
5
- module Matchi
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
29
- module Helper
30
- ::Matchi::Matcher.constants.each do |matcher_const|
31
- next if matcher_const.equal?(:Base)
32
-
33
- matcher_klass = ::Matchi::Matcher.const_get(matcher_const)
34
-
35
- define_method(matcher_klass.to_sym) do |*args, **kwargs, &block|
36
- matcher_klass.new(*args, **kwargs, &block)
37
- end
38
- end
39
- end
40
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Matchi
4
- # Collection of matcher classes.
5
- module Matcher
6
- end
7
- end
8
-
9
- Dir[File.join(File.dirname(__FILE__), "matcher", "*.rb")].each do |fname|
10
- require_relative fname
11
- end
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Matchi
4
- module Matcher
5
- # Abstract matcher class.
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
- #
13
- # @return [Symbol] A symbol identifying the matcher.
14
- def self.to_sym
15
- name.split("::")
16
- .fetch(-1)
17
- .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
18
- .gsub(/([a-z\d])([A-Z])/, '\1_\2')
19
- .downcase
20
- .to_sym
21
- end
22
-
23
- # @return [#object_id] Any value to give to the matcher.
24
- attr_reader :expected
25
-
26
- # A string containing a human-readable representation of the matcher.
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
- #
32
- # @return [String] The human-readable representation of the matcher.
33
- def inspect
34
- "#{self.class}(#{expected&.inspect})"
35
- end
36
-
37
- # Abstract matcher class.
38
- #
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.
47
- def matches?
48
- raise ::NotImplementedError, "matcher MUST respond to this method."
49
- end
50
-
51
- # Returns a string representing the matcher instance.
52
- #
53
- # @example The readable definition of a FooBar matcher instance.
54
- # matcher = Matchi::Matcher::FooBar.new(42)
55
- # matcher.to_s # => "foo_bar 42"
56
- #
57
- # @return [String] A string representing the matcher instance.
58
- def to_s
59
- [self.class.to_sym, expected&.inspect].compact.join(" ")
60
- end
61
- end
62
- end
63
- end
@@ -1,51 +0,0 @@
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
@@ -1,24 +0,0 @@
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
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Matchi
6
- module Matcher
7
- # *Nil* matcher.
8
- class BeNil < ::Matchi::Matcher::Base
9
- # Boolean comparison between the actual value and the expected value.
10
- #
11
- # @example Is it nil?
12
- # be_nil = Matchi::Matcher::BeNil.new
13
- # be_nil.matches? { nil } # => 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
- nil.equal?(yield)
21
- end
22
- end
23
- end
24
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Matchi
6
- module Matcher
7
- # *Truth* matcher.
8
- class BeTrue < ::Matchi::Matcher::Base
9
- # Boolean comparison between the actual value and the expected value.
10
- #
11
- # @example Is it true?
12
- # be_true = Matchi::Matcher::BeTrue.new
13
- # be_true.matches? { true } # => 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
- true.equal?(yield)
21
- end
22
- end
23
- end
24
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative File.join("change", "by_at_least")
4
- require_relative File.join("change", "by_at_most")
5
- require_relative File.join("change", "by")
6
- require_relative File.join("change", "from")
7
- require_relative File.join("change", "to")
8
-
9
- module Matchi
10
- module Matcher
11
- # Wraps the target of a change matcher.
12
- class Change
13
- # Returns a symbol identifying the matcher.
14
- def self.to_sym
15
- :change
16
- end
17
-
18
- # Initialize a wrapper of the change matcher with an object and the name
19
- # of one of its methods.
20
- #
21
- # @example The wrapper of the change matcher on `"foo".to_s` value.
22
- # Matchi::Matcher::Change.new("foo", :to_s)
23
- #
24
- # @param object [#object_id] An object.
25
- # @param method [Symbol] The name of a method.
26
- # @param args [Array] A list of arguments.
27
- # @param kwargs [Hash] A list of keyword arguments.
28
- def initialize(object, method, *args, **kwargs, &block)
29
- @state = -> { object.send(method, *args, **kwargs, &block) }
30
- end
31
-
32
- # Specifies a minimum delta of the expected change.
33
- #
34
- # @param expected [#object_id] The minimum delta of the expected change.
35
- #
36
- # @return [#matches?] A *change by at least* matcher.
37
- def by_at_least(expected)
38
- ByAtLeast.new(expected, &@state)
39
- end
40
-
41
- # Specifies a maximum delta of the expected change.
42
- #
43
- # @param expected [#object_id] The maximum delta of the expected change.
44
- #
45
- # @return [#matches?] A *change by at most* matcher.
46
- def by_at_most(expected)
47
- ByAtMost.new(expected, &@state)
48
- end
49
-
50
- # Specifies the delta of the expected change.
51
- #
52
- # @param expected [#object_id] The delta of the expected change.
53
- #
54
- # @return [#matches?] A *change by* matcher.
55
- def by(expected)
56
- By.new(expected, &@state)
57
- end
58
-
59
- # Specifies the original value.
60
- #
61
- # @param expected [#object_id] The original value.
62
- #
63
- # @return [#matches?] A *change from* wrapper.
64
- def from(expected)
65
- From.new(expected, &@state)
66
- end
67
-
68
- # Specifies the new value to expect.
69
- #
70
- # @param expected [#object_id] The new value to expect.
71
- #
72
- # @return [#matches?] A *change to* matcher.
73
- def to(expected)
74
- To.new(expected, &@state)
75
- end
76
- end
77
- end
78
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative File.join("..", "base")
4
-
5
- module Matchi
6
- module Matcher
7
- class Change
8
- # *Change by* matcher.
9
- class By < ::Matchi::Matcher::Base
10
- # Returns a symbol identifying the matcher.
11
- def self.to_sym
12
- :change
13
- end
14
-
15
- # Initialize the matcher with an object.
16
- #
17
- # @example The change by 1 matcher.
18
- # object = []
19
- # Matchi::Matcher::Change::By.new(1) { object.length }
20
- #
21
- # @param expected [#object_id] An expected delta.
22
- # @param state [Proc] A block of code to execute to get the
23
- # state of the object.
24
- def initialize(expected, &state)
25
- super()
26
- @expected = expected
27
- @state = state
28
- end
29
-
30
- # Boolean comparison on the expected change by comparing the value
31
- # before and after the code execution.
32
- #
33
- # @example
34
- # object = []
35
- # change = Matchi::Matcher::Change::By.new(1) { object.length }
36
- # change.matches? { object << "foo" } # => true
37
- #
38
- # @yieldreturn [#object_id] The block of code to execute.
39
- #
40
- # @return [Boolean] Comparison between the value before and after the
41
- # code execution.
42
- def matches?(*, **)
43
- value_before = @state.call
44
- yield
45
- value_after = @state.call
46
-
47
- expected == (value_after - value_before)
48
- end
49
-
50
- # (see Base#to_s)
51
- def to_s
52
- "change by #{expected.inspect}"
53
- end
54
- end
55
- end
56
- end
57
- end