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,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change by* matcher.
6
+ class By
7
+ # Initialize the matcher with an object and a block.
8
+ #
9
+ # @example
10
+ # require "matchi/change/by"
11
+ #
12
+ # object = []
13
+ #
14
+ # Matchi::Change::By.new(1) { object.length }
15
+ #
16
+ # @param expected [#object_id] An expected delta.
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
+ # Boolean comparison on the expected change by comparing the value
25
+ # before and after the code execution.
26
+ #
27
+ # @example
28
+ # require "matchi/change/by"
29
+ #
30
+ # object = []
31
+ #
32
+ # matcher = Matchi::Change::By.new(1) { object.length }
33
+ # matcher.matches? { object << "foo" } # => true
34
+ #
35
+ # @yieldreturn [#object_id] The block of code to execute.
36
+ #
37
+ # @return [Boolean] Comparison between the value before and after the
38
+ # code execution.
39
+ def matches?(*, **)
40
+ value_before = @state.call
41
+ yield
42
+ value_after = @state.call
43
+
44
+ @expected == (value_after - value_before)
45
+ end
46
+
47
+ # A string containing a human-readable representation of the matcher.
48
+ def inspect
49
+ "#{self.class}(#{@expected.inspect})"
50
+ end
51
+
52
+ # Returns a string representing the matcher.
53
+ def to_s
54
+ "change by #{@expected.inspect}"
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change by at least* matcher.
6
+ class ByAtLeast
7
+ # Initialize the matcher with an object and a block.
8
+ #
9
+ # @example
10
+ # require "matchi/change/by_at_least"
11
+ #
12
+ # object = []
13
+ #
14
+ # Matchi::Change::ByAtLeast.new(1) { object.length }
15
+ #
16
+ # @param expected [#object_id] An expected delta.
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
+ # Boolean comparison on the expected change by comparing the value
25
+ # before and after the code execution.
26
+ #
27
+ # @example
28
+ # require "matchi/change/by_at_least"
29
+ #
30
+ # object = []
31
+ #
32
+ # matcher = Matchi::Change::ByAtLeast.new(1) { object.length }
33
+ # matcher.matches? { object << "foo" } # => true
34
+ #
35
+ # @yieldreturn [#object_id] The block of code to execute.
36
+ #
37
+ # @return [Boolean] Comparison between the value before and after the
38
+ # code execution.
39
+ def matches?(*, **)
40
+ value_before = @state.call
41
+ yield
42
+ value_after = @state.call
43
+
44
+ @expected <= (value_after - value_before)
45
+ end
46
+
47
+ # A string containing a human-readable representation of the matcher.
48
+ def inspect
49
+ "#{self.class}(#{@expected.inspect})"
50
+ end
51
+
52
+ # Returns a string representing the matcher.
53
+ def to_s
54
+ "change by at least #{@expected.inspect}"
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change by at most* matcher.
6
+ class ByAtMost
7
+ # Initialize the matcher with an object and a block.
8
+ #
9
+ # @example
10
+ # require "matchi/change/by_at_most"
11
+ #
12
+ # object = []
13
+ #
14
+ # Matchi::Change::ByAtMost.new(1) { object.length }
15
+ #
16
+ # @param expected [#object_id] An expected delta.
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
+ # Boolean comparison on the expected change by comparing the value
25
+ # before and after the code execution.
26
+ #
27
+ # @example
28
+ # require "matchi/change/by_at_most"
29
+ #
30
+ # object = []
31
+ #
32
+ # matcher = Matchi::Change::ByAtMost.new(1) { object.length }
33
+ # matcher.matches? { object << "foo" } # => true
34
+ #
35
+ # @yieldreturn [#object_id] The block of code to execute.
36
+ #
37
+ # @return [Boolean] Comparison between the value before and after the
38
+ # code execution.
39
+ def matches?(*, **)
40
+ value_before = @state.call
41
+ yield
42
+ value_after = @state.call
43
+
44
+ @expected >= (value_after - value_before)
45
+ end
46
+
47
+ # A string containing a human-readable representation of the matcher.
48
+ def inspect
49
+ "#{self.class}(#{@expected.inspect})"
50
+ end
51
+
52
+ # Returns a string representing the matcher.
53
+ def to_s
54
+ "change by at most #{@expected.inspect}"
55
+ end
56
+ end
57
+ end
58
+ 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 = Matchi::Change::From.new("foo") { object.to_s }
34
+ # change.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,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ class From
6
+ # *Change from to* matcher.
7
+ class To
8
+ # Initialize the matcher with two objects and a block.
9
+ #
10
+ # @example
11
+ # require "matchi/change/from/to"
12
+ #
13
+ # object = "foo"
14
+ #
15
+ # Matchi::Change::From::To.new("foo", "FOO") { object.to_s }
16
+ #
17
+ # @param expected_init [#object_id] An expected initial value.
18
+ # @param expected_new_value [#object_id] An expected new value.
19
+ # @param state [Proc] A block of code to execute to
20
+ # get the state of the object.
21
+ def initialize(expected_init, expected_new_value, &state)
22
+ @expected_init = expected_init
23
+ @expected = expected_new_value
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/from/to"
32
+ #
33
+ # object = "foo"
34
+ #
35
+ # matcher = Matchi::Change::From::To.new("foo", "FOO") { object.to_s }
36
+ # matcher.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
+ value_before = @state.call
44
+ return false unless @expected_init == value_before
45
+
46
+ yield
47
+ value_after = @state.call
48
+
49
+ @expected == value_after
50
+ end
51
+
52
+ # A string containing a human-readable representation of the matcher.
53
+ def inspect
54
+ "#{self.class}(#{@expected_init.inspect}, #{@expected.inspect})"
55
+ end
56
+
57
+ # Returns a string representing the matcher.
58
+ def to_s
59
+ "change from #{@expected_init.inspect} to #{@expected.inspect}"
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ class Change
5
+ # *Change to* matcher.
6
+ class To
7
+ # Initialize the matcher with an object and a block.
8
+ #
9
+ # @example
10
+ # require "matchi/change/to"
11
+ #
12
+ # object = "foo"
13
+ #
14
+ # Matchi::Change::To.new("FOO") { object.to_s }
15
+ #
16
+ # @param expected [#object_id] An expected result 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
+ # Boolean comparison on the expected change by comparing the value
25
+ # before and after the code execution.
26
+ #
27
+ # @example
28
+ # require "matchi/change/to"
29
+ #
30
+ # object = "foo"
31
+ #
32
+ # matcher = Matchi::Change::To.new("FOO") { object.to_s }
33
+ # matcher.matches? { object.upcase! } # => true
34
+ #
35
+ # @yieldreturn [#object_id] The block of code to execute.
36
+ #
37
+ # @return [Boolean] Comparison between the value before and after the
38
+ # code execution.
39
+ def matches?(*, **)
40
+ yield
41
+ value_after = @state.call
42
+
43
+ @expected == value_after
44
+ end
45
+
46
+ # A string containing a human-readable representation of the matcher.
47
+ def inspect
48
+ "#{self.class}(#{@expected.inspect})"
49
+ end
50
+
51
+ # Returns a string representing the matcher.
52
+ def to_s
53
+ "change to #{@expected.inspect}"
54
+ end
55
+ end
56
+ end
57
+ end
data/lib/matchi/eq.rb ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Equivalence* matcher.
5
+ class Eq
6
+ # Initialize the matcher with an object.
7
+ #
8
+ # @example
9
+ # require "matchi/eq"
10
+ #
11
+ # Matchi::Eq.new("foo")
12
+ #
13
+ # @param expected [#eql?] An expected equivalent object.
14
+ def initialize(expected)
15
+ @expected = expected
16
+ end
17
+
18
+ # Boolean comparison between the actual value and the expected value.
19
+ #
20
+ # @example
21
+ # require "matchi/eq"
22
+ #
23
+ # matcher = Matchi::Eq.new("foo")
24
+ # matcher.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
+
34
+ # A string containing a human-readable representation of the matcher.
35
+ def inspect
36
+ "#{self.class}(#{@expected.inspect})"
37
+ end
38
+
39
+ # Returns a string representing the matcher.
40
+ def to_s
41
+ "eq #{@expected.inspect}"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Regular expressions* matcher.
5
+ class Match
6
+ # Initialize the matcher with an instance of Regexp.
7
+ #
8
+ # @example
9
+ # require "matchi/match"
10
+ #
11
+ # Matchi::Match.new(/^foo$/)
12
+ #
13
+ # @param expected [#match] A regular expression.
14
+ def initialize(expected)
15
+ @expected = expected
16
+ end
17
+
18
+ # Boolean comparison between the actual value and the expected value.
19
+ #
20
+ # @example
21
+ # require "matchi/match"
22
+ #
23
+ # matcher = Matchi::Match.new(/^foo$/)
24
+ # matcher.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
+
34
+ # A string containing a human-readable representation of the matcher.
35
+ def inspect
36
+ "#{self.class}(#{@expected.inspect})"
37
+ end
38
+
39
+ # Returns a string representing the matcher.
40
+ def to_s
41
+ "match #{@expected.inspect}"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Matchi
4
+ # *Expecting errors* matcher.
5
+ class RaiseException
6
+ # Initialize the matcher with a descendant of class Exception.
7
+ #
8
+ # @example
9
+ # require "matchi/raise_exception"
10
+ #
11
+ # Matchi::RaiseException.new(NameError)
12
+ #
13
+ # @param expected [Exception] The class of the expected exception.
14
+ def initialize(expected)
15
+ @expected = expected
16
+ end
17
+
18
+ # Boolean comparison between the actual value and the expected value.
19
+ #
20
+ # @example
21
+ # require "matchi/raise_exception"
22
+ #
23
+ # matcher = Matchi::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
+
38
+ # A string containing a human-readable representation of the matcher.
39
+ def inspect
40
+ "#{self.class}(#{@expected.inspect})"
41
+ end
42
+
43
+ # Returns a string representing the matcher.
44
+ def to_s
45
+ "raise exception #{@expected.inspect}"
46
+ end
47
+ end
48
+ end