matchi 2.3.0 → 3.1.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,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change by* matcher.
6
+ class By
7
+ # @return [#object_id] An expected delta.
8
+ attr_reader :expected
9
+
10
+ # Initialize the matcher with an object and a block.
11
+ #
12
+ # @example
13
+ # require "matchi/change/by"
14
+ #
15
+ # object = []
16
+ #
17
+ # Matchi::Change::By.new(1) { object.length }
18
+ #
19
+ # @param expected [#object_id] An expected delta.
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/by"
32
+ #
33
+ # object = []
34
+ #
35
+ # matcher = Matchi::Change::By.new(1) { object.length }
36
+ #
37
+ # matcher.expected # => 1
38
+ # matcher.matches? { object << "foo" } # => 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
+ value_before = @state.call
46
+ yield
47
+ value_after = @state.call
48
+
49
+ expected == (value_after - value_before)
50
+ end
51
+
52
+ # A string containing a human-readable representation of the matcher.
53
+ def inspect
54
+ "#{self.class}(#{expected.inspect})"
55
+ end
56
+
57
+ # Returns a string representing the matcher.
58
+ def to_s
59
+ "change by #{expected.inspect}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change by at least* matcher.
6
+ class ByAtLeast
7
+ # @return [#object_id] An expected delta.
8
+ attr_reader :expected
9
+
10
+ # Initialize the matcher with an object and a block.
11
+ #
12
+ # @example
13
+ # require "matchi/change/by_at_least"
14
+ #
15
+ # object = []
16
+ #
17
+ # Matchi::Change::ByAtLeast.new(1) { object.length }
18
+ #
19
+ # @param expected [#object_id] An expected delta.
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/by_at_least"
32
+ #
33
+ # object = []
34
+ #
35
+ # matcher = Matchi::Change::ByAtLeast.new(1) { object.length }
36
+ #
37
+ # matcher.expected # => 1
38
+ # matcher.matches? { object << "foo" } # => 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
+ value_before = @state.call
46
+ yield
47
+ value_after = @state.call
48
+
49
+ expected <= (value_after - value_before)
50
+ end
51
+
52
+ # A string containing a human-readable representation of the matcher.
53
+ def inspect
54
+ "#{self.class}(#{expected.inspect})"
55
+ end
56
+
57
+ # Returns a string representing the matcher.
58
+ def to_s
59
+ "change by at least #{expected.inspect}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change by at most* matcher.
6
+ class ByAtMost
7
+ # @return [#object_id] An expected delta.
8
+ attr_reader :expected
9
+
10
+ # Initialize the matcher with an object and a block.
11
+ #
12
+ # @example
13
+ # require "matchi/change/by_at_most"
14
+ #
15
+ # object = []
16
+ #
17
+ # Matchi::Change::ByAtMost.new(1) { object.length }
18
+ #
19
+ # @param expected [#object_id] An expected delta.
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/by_at_most"
32
+ #
33
+ # object = []
34
+ #
35
+ # matcher = Matchi::Change::ByAtMost.new(1) { object.length }
36
+ #
37
+ # matcher.expected # => 1
38
+ # matcher.matches? { object << "foo" } # => 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
+ value_before = @state.call
46
+ yield
47
+ value_after = @state.call
48
+
49
+ expected >= (value_after - value_before)
50
+ end
51
+
52
+ # A string containing a human-readable representation of the matcher.
53
+ def inspect
54
+ "#{self.class}(#{expected.inspect})"
55
+ end
56
+
57
+ # Returns a string representing the matcher.
58
+ def to_s
59
+ "change by at most #{expected.inspect}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative File.join("from", "to")
4
+
5
+ module Matchi
6
+ class Change
7
+ # *Change from to* wrapper.
8
+ class From
9
+ # Initialize the wrapper with an object and a block.
10
+ #
11
+ # @example
12
+ # require "matchi/change/from"
13
+ #
14
+ # object = "foo"
15
+ #
16
+ # Matchi::Change::From.new("foo") { object.to_s }
17
+ #
18
+ # @param expected [#object_id] An expected initial value.
19
+ # @param state [Proc] A block of code to execute to get the
20
+ # state of the object.
21
+ def initialize(expected, &state)
22
+ @expected = expected
23
+ @state = state
24
+ end
25
+
26
+ # Specifies the new value to expect.
27
+ #
28
+ # @example
29
+ # require "matchi/change/from"
30
+ #
31
+ # object = "foo"
32
+ #
33
+ # change_from_wrapper = Matchi::Change::From.new("foo") { object.to_s }
34
+ # change_from_wrapper.to("FOO")
35
+ #
36
+ # @param expected_new_value [#object_id] The new value to expect.
37
+ #
38
+ # @return [#matches?] A *change from to* matcher.
39
+ def to(expected_new_value)
40
+ To.new(@expected, expected_new_value, &@state)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ class From
6
+ # *Change from to* matcher.
7
+ class To
8
+ # @return [#object_id] An expected new value.
9
+ attr_reader :expected
10
+
11
+ # Initialize the matcher with two objects and a block.
12
+ #
13
+ # @example
14
+ # require "matchi/change/from/to"
15
+ #
16
+ # object = "foo"
17
+ #
18
+ # Matchi::Change::From::To.new("foo", "FOO") { object.to_s }
19
+ #
20
+ # @param expected_init [#object_id] An expected initial value.
21
+ # @param expected_new_value [#object_id] An expected new value.
22
+ # @param state [Proc] A block of code to execute to
23
+ # get the state of the object.
24
+ def initialize(expected_init, expected_new_value, &state)
25
+ @expected_init = expected_init
26
+ @expected = expected_new_value
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
+ # require "matchi/change/from/to"
35
+ #
36
+ # object = "foo"
37
+ #
38
+ # matcher = Matchi::Change::From::To.new("foo", "FOO") { object.to_s }
39
+ #
40
+ # matcher.expected # => "FOO"
41
+ # matcher.matches? { object.upcase! } # => true
42
+ #
43
+ # @yieldreturn [#object_id] The block of code to execute.
44
+ #
45
+ # @return [Boolean] Comparison between the value before and after the
46
+ # code execution.
47
+ def matches?
48
+ value_before = @state.call
49
+ return false unless @expected_init == value_before
50
+
51
+ yield
52
+ value_after = @state.call
53
+
54
+ expected == value_after
55
+ end
56
+
57
+ # A string containing a human-readable representation of the matcher.
58
+ def inspect
59
+ "#{self.class}(#{@expected_init.inspect}, #{expected.inspect})"
60
+ end
61
+
62
+ # Returns a string representing the matcher.
63
+ def to_s
64
+ "change from #{@expected_init.inspect} to #{expected.inspect}"
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -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