matchi 2.3.1 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.3.1
4
+ version: 3.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-07-20 00:00:00.000000000 Z
11
+ date: 2021-07-23 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,18 +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/eql.rb
142
- - lib/matchi/matcher/equal.rb
143
- - lib/matchi/matcher/match.rb
144
- - lib/matchi/matcher/raise_exception.rb
145
- - 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
146
147
  homepage: https://github.com/fixrb/matchi
147
148
  licenses:
148
149
  - MIT
@@ -165,5 +166,5 @@ requirements: []
165
166
  rubygems_version: 3.1.6
166
167
  signing_key:
167
168
  specification_version: 4
168
- summary: "Collection of expectation matchers for Ruby \U0001F939"
169
+ summary: "Collection of expectation matchers for Rubyists \U0001F939"
169
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,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Matchi
6
- module Matcher
7
- # *Equivalence* matcher.
8
- class Eql < ::Matchi::Matcher::Base
9
- # Initialize the matcher with an object.
10
- #
11
- # @example The string "foo" matcher.
12
- # Matchi::Matcher::Eql.new("foo")
13
- #
14
- # @param expected [#eql?] An expected equivalent object.
15
- def initialize(expected)
16
- super()
17
- @expected = expected
18
- end
19
-
20
- # Boolean comparison between the actual value and the expected value.
21
- #
22
- # @example Is it equivalent to "foo"?
23
- # eql = Matchi::Matcher::Eql.new("foo")
24
- # eql.matches? { "foo" } # => 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.eql?(yield)
32
- end
33
- end
34
- end
35
- end