matchi 2.4.0 → 3.2.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.
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change to* matcher.
6
+ class To
7
+ # @return [#object_id] An expected new value.
8
+ attr_reader :expected
9
+
10
+ # Initialize the matcher with an object and a block.
11
+ #
12
+ # @example
13
+ # require "matchi/change/to"
14
+ #
15
+ # object = "foo"
16
+ #
17
+ # Matchi::Change::To.new("FOO") { object.to_s }
18
+ #
19
+ # @param expected [#object_id] An expected new value.
20
+ # @param state [Proc] A block of code to execute to get the
21
+ # state of the object.
22
+ def initialize(expected, &state)
23
+ @expected = expected
24
+ @state = state
25
+ end
26
+
27
+ # Boolean comparison on the expected change by comparing the value
28
+ # before and after the code execution.
29
+ #
30
+ # @example
31
+ # require "matchi/change/to"
32
+ #
33
+ # object = "foo"
34
+ #
35
+ # matcher = Matchi::Change::To.new("FOO") { object.to_s }
36
+ #
37
+ # matcher.expected # => "FOO"
38
+ # matcher.matches? { object.upcase! } # => true
39
+ #
40
+ # @yieldreturn [#object_id] The block of code to execute.
41
+ #
42
+ # @return [Boolean] Comparison between the value before and after the
43
+ # code execution.
44
+ def matches?
45
+ yield
46
+ value_after = @state.call
47
+
48
+ expected == value_after
49
+ end
50
+
51
+ # A string containing a human-readable representation of the matcher.
52
+ def inspect
53
+ "#{self.class}(#{expected.inspect})"
54
+ end
55
+
56
+ # Returns a string representing the matcher.
57
+ def to_s
58
+ "change to #{expected.inspect}"
59
+ end
60
+ end
61
+ end
62
+ end
data/lib/matchi/eq.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Equivalence* matcher.
5
+ class Eq
6
+ # @return [#eql?] An expected equivalent object.
7
+ attr_reader :expected
8
+
9
+ # Initialize the matcher with an object.
10
+ #
11
+ # @example
12
+ # require "matchi/eq"
13
+ #
14
+ # Matchi::Eq.new("foo")
15
+ #
16
+ # @param expected [#eql?] An expected equivalent object.
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Boolean comparison between the actual value and the expected value.
22
+ #
23
+ # @example
24
+ # require "matchi/eq"
25
+ #
26
+ # matcher = Matchi::Eq.new("foo")
27
+ #
28
+ # matcher.expected # => "foo"
29
+ # matcher.matches? { "foo" } # => 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.eql?(yield)
37
+ end
38
+
39
+ # A string containing a human-readable representation of the matcher.
40
+ def inspect
41
+ "#{self.class}(#{expected.inspect})"
42
+ end
43
+
44
+ # Returns a string representing the matcher.
45
+ def to_s
46
+ "eq #{expected.inspect}"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Regular expressions* matcher.
5
+ class Match
6
+ # @return [#match] A regular expression.
7
+ attr_reader :expected
8
+
9
+ # Initialize the matcher with an instance of Regexp.
10
+ #
11
+ # @example
12
+ # require "matchi/match"
13
+ #
14
+ # Matchi::Match.new(/^foo$/)
15
+ #
16
+ # @param expected [#match] A regular expression.
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Boolean comparison between the actual value and the expected value.
22
+ #
23
+ # @example
24
+ # require "matchi/match"
25
+ #
26
+ # matcher = Matchi::Match.new(/^foo$/)
27
+ #
28
+ # matcher.expected # => /^foo$/
29
+ # matcher.matches? { "foo" } # => 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.match?(yield)
37
+ end
38
+
39
+ # A string containing a human-readable representation of the matcher.
40
+ def inspect
41
+ "#{self.class}(#{expected.inspect})"
42
+ end
43
+
44
+ # Returns a string representing the matcher.
45
+ def to_s
46
+ "match #{expected.inspect}"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Expecting errors* matcher.
5
+ class RaiseException
6
+ # @return [String] The expected exception name.
7
+ attr_reader :expected
8
+
9
+ # Initialize the matcher with a descendant of class Exception.
10
+ #
11
+ # @example
12
+ # require "matchi/raise_exception"
13
+ #
14
+ # Matchi::RaiseException.new(NameError)
15
+ #
16
+ # @param expected [Exception, #to_s] The expected exception name.
17
+ def initialize(expected)
18
+ @expected = String(expected)
19
+ end
20
+
21
+ # Boolean comparison between the actual value and the expected value.
22
+ #
23
+ # @example
24
+ # require "matchi/raise_exception"
25
+ #
26
+ # matcher = Matchi::RaiseException.new(NameError)
27
+ #
28
+ # matcher.expected # => "NameError"
29
+ # matcher.matches? { Boom } # => 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
+ yield
37
+ rescue self.class.const_get(expected) => _e
38
+ true
39
+ else
40
+ false
41
+ end
42
+
43
+ # A string containing a human-readable representation of the matcher.
44
+ def inspect
45
+ "#{self.class}(#{expected})"
46
+ end
47
+
48
+ # Returns a string representing the matcher.
49
+ def to_s
50
+ "raise exception #{expected}"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Satisfy* matcher.
5
+ class Satisfy
6
+ # @return [Proc] A block of code.
7
+ attr_reader :expected
8
+
9
+ # Initialize the matcher with a block.
10
+ #
11
+ # @example
12
+ # require "matchi/satisfy"
13
+ #
14
+ # Matchi::Satisfy.new { |value| value == 42 }
15
+ #
16
+ # @param block [Proc] A block of code.
17
+ def initialize(&block)
18
+ @expected = block
19
+ end
20
+
21
+ # Boolean comparison between the actual value and the expected value.
22
+ #
23
+ # @example
24
+ # require "matchi/satisfy"
25
+ #
26
+ # matcher = Matchi::Satisfy.new { |value| value == 42 }
27
+ #
28
+ # matcher.expected # => #<Proc:0x00007fbaafc65540>
29
+ # matcher.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
+ # A string containing a human-readable representation of the matcher.
40
+ def inspect
41
+ "#{self.class}(&block)"
42
+ end
43
+
44
+ # Returns a string representing the matcher.
45
+ def to_s
46
+ "satisfy &block"
47
+ end
48
+ end
49
+ 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.2.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-24 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,21 @@ 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/be_within.rb
137
+ - lib/matchi/be_within/of.rb
138
+ - lib/matchi/change.rb
139
+ - lib/matchi/change/by.rb
140
+ - lib/matchi/change/by_at_least.rb
141
+ - lib/matchi/change/by_at_most.rb
142
+ - lib/matchi/change/from.rb
143
+ - lib/matchi/change/from/to.rb
144
+ - lib/matchi/change/to.rb
145
+ - lib/matchi/eq.rb
146
+ - lib/matchi/match.rb
147
+ - lib/matchi/raise_exception.rb
148
+ - lib/matchi/satisfy.rb
153
149
  homepage: https://github.com/fixrb/matchi
154
150
  licenses:
155
151
  - MIT
@@ -172,5 +168,5 @@ requirements: []
172
168
  rubygems_version: 3.1.6
173
169
  signing_key:
174
170
  specification_version: 4
175
- summary: "Collection of expectation matchers for Ruby \U0001F939"
171
+ summary: "Collection of expectation matchers for Rubyists \U0001F939"
176
172
  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