matchi 2.4.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 at least* matcher.
9
- class ByAtLeast < ::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 at least 1 matcher.
18
- # object = []
19
- # Matchi::Matcher::Change::ByAtLeast.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::ByAtLeast.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 at least #{expected.inspect}"
53
- end
54
- end
55
- end
56
- end
57
- 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 at most* matcher.
9
- class ByAtMost < ::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 at most 1 matcher.
18
- # object = []
19
- # Matchi::Matcher::Change::ByAtMost.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::ByAtMost.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 at most #{expected.inspect}"
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative File.join("from", "to")
4
-
5
- module Matchi
6
- module Matcher
7
- class Change
8
- # *Change from to* wrapper.
9
- class From
10
- # Initialize the matcher with an object.
11
- #
12
- # @example The change from "foo" to "FOO" matcher.
13
- # object = "foo"
14
- # Matchi::Matcher::Change::From.new("foo").to("FOO") { object.to_s }
15
- #
16
- # @param expected [#object_id] An expected initial value.
17
- # @param state [Proc] A block of code to execute to get the
18
- # state of the object.
19
- def initialize(expected, &state)
20
- @expected = expected
21
- @state = state
22
- end
23
-
24
- # Specifies the new value to expect.
25
- #
26
- # @param expected_new_value [#object_id] The new value to expect.
27
- #
28
- # @return [#matches?] A *change from to* matcher.
29
- def to(expected_new_value)
30
- To.new(@expected, expected_new_value, &@state)
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,68 +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
- class From
9
- # *Change from to* matcher.
10
- class To < ::Matchi::Matcher::Base
11
- # Returns a symbol identifying the matcher.
12
- def self.to_sym
13
- :change
14
- end
15
-
16
- # Initialize the matcher with an object.
17
- #
18
- # @example The change from "foo" to "FOO" matcher.
19
- # object = "foo"
20
- # Matchi::Matcher::Change::From::To.new("foo", "FOO") { object.to_s }
21
- #
22
- # @param expected_init [#object_id] An expected initial value.
23
- # @param expected_new_value [#object_id] An expected new value.
24
- # @param state [Proc] A block of code to execute to
25
- # get the state of the object.
26
- def initialize(expected_init, expected_new_value, &state)
27
- super()
28
- @expected_init = expected_init
29
- @expected = expected_new_value
30
- @state = state
31
- end
32
-
33
- # (see Base#inspect)
34
- def inspect
35
- "Matchi::Matcher::Change::From(#{@expected_init.inspect})::To(#{expected.inspect})"
36
- end
37
-
38
- # Boolean comparison on the expected change by comparing the value
39
- # before and after the code execution.
40
- #
41
- # @example
42
- # object = "foo"
43
- # change = Matchi::Matcher::Change::To.new("FOO") { object.to_s }
44
- # change.matches? { object.upcase! } # => true
45
- #
46
- # @yieldreturn [#object_id] The block of code to execute.
47
- #
48
- # @return [Boolean] Comparison between the value before and after the
49
- # code execution.
50
- def matches?(*, **)
51
- value_before = @state.call
52
- return false unless @expected_init == value_before
53
-
54
- yield
55
- value_after = @state.call
56
-
57
- expected == value_after
58
- end
59
-
60
- # (see Base#to_s)
61
- def to_s
62
- "change from #{@expected_init.inspect} to #{expected.inspect}"
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,56 +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 to* matcher.
9
- class To < ::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 to "FOO" matcher.
18
- # object = "foo"
19
- # Matchi::Matcher::Change::To.new("FOO") { object.to_s }
20
- #
21
- # @param expected [#object_id] An expected result value.
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 = "foo"
35
- # change = Matchi::Matcher::Change::To.new("FOO") { object.to_s }
36
- # change.matches? { object.upcase! } # => 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
- yield
44
- value_after = @state.call
45
-
46
- expected == value_after
47
- end
48
-
49
- # (see Base#to_s)
50
- def to_s
51
- "change to #{expected.inspect}"
52
- end
53
- end
54
- end
55
- end
56
- 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
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Matchi
6
- module Matcher
7
- # *Identity* matcher.
8
- class Equal < ::Matchi::Matcher::Base
9
- # Initialize the matcher with an object.
10
- #
11
- # @example The number 42 matcher.
12
- # Matchi::Matcher::Equal.new(42)
13
- #
14
- # @param expected [#equal?] An expected 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 equal to :foo?
23
- # equal = Matchi::Matcher::Equal.new(:foo)
24
- # equal.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.equal?(yield)
32
- end
33
- end
34
- end
35
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Matchi
6
- module Matcher
7
- # *Regular expressions* matcher.
8
- class Match < ::Matchi::Matcher::Base
9
- # Initialize the matcher with an instance of Regexp.
10
- #
11
- # @example Username matcher.
12
- # Matchi::Matcher::Match.new(/^[a-z0-9_-]{3,16}$/)
13
- #
14
- # @param expected [#match] A regular expression.
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 matching /^foo$/ regex?
23
- # match = Matchi::Matcher::Match.new(/^foo$/)
24
- # match.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.match?(yield)
32
- end
33
- end
34
- end
35
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Matchi
6
- module Matcher
7
- # *Expecting errors* matcher.
8
- class RaiseException < ::Matchi::Matcher::Base
9
- # Initialize the matcher with a descendant of class Exception.
10
- #
11
- # @example Divided by 0 matcher.
12
- # Matchi::Matcher::RaiseException.new(ZeroDivisionError)
13
- #
14
- # @param expected [Exception] The class of the expected exception.
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 raising NameError?
23
- # matcher = Matchi::Matcher::RaiseException.new(NameError)
24
- # matcher.matches? { Boom } # => 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
- yield
32
- rescue expected => _e
33
- true
34
- else
35
- false
36
- end
37
- end
38
- end
39
- end
@@ -1,45 +0,0 @@
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