matchi 2.4.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +96 -90
- data/lib/matchi.rb +4 -2
- data/lib/matchi/be.rb +49 -0
- data/lib/matchi/be_an_instance_of.rb +49 -0
- data/lib/matchi/be_within.rb +35 -0
- data/lib/matchi/be_within/of.rb +53 -0
- data/lib/matchi/change.rb +109 -0
- data/lib/matchi/change/by.rb +63 -0
- data/lib/matchi/change/by_at_least.rb +63 -0
- data/lib/matchi/change/by_at_most.rb +63 -0
- data/lib/matchi/change/from.rb +44 -0
- data/lib/matchi/change/from/to.rb +69 -0
- data/lib/matchi/change/to.rb +62 -0
- data/lib/matchi/eq.rb +49 -0
- data/lib/matchi/match.rb +49 -0
- data/lib/matchi/raise_exception.rb +53 -0
- data/lib/matchi/satisfy.rb +49 -0
- metadata +19 -23
- data/lib/matchi/helper.rb +0 -40
- data/lib/matchi/matcher.rb +0 -11
- data/lib/matchi/matcher/base.rb +0 -63
- data/lib/matchi/matcher/be_an_instance_of.rb +0 -51
- data/lib/matchi/matcher/be_false.rb +0 -24
- data/lib/matchi/matcher/be_nil.rb +0 -24
- data/lib/matchi/matcher/be_true.rb +0 -24
- data/lib/matchi/matcher/change.rb +0 -78
- data/lib/matchi/matcher/change/by.rb +0 -57
- data/lib/matchi/matcher/change/by_at_least.rb +0 -57
- data/lib/matchi/matcher/change/by_at_most.rb +0 -57
- data/lib/matchi/matcher/change/from.rb +0 -35
- data/lib/matchi/matcher/change/from/to.rb +0 -68
- data/lib/matchi/matcher/change/to.rb +0 -56
- data/lib/matchi/matcher/eql.rb +0 -35
- data/lib/matchi/matcher/equal.rb +0 -35
- data/lib/matchi/matcher/match.rb +0 -35
- data/lib/matchi/matcher/raise_exception.rb +0 -39
- data/lib/matchi/matcher/satisfy.rb +0 -45
@@ -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,78 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative File.join("change", "by_at_least")
|
4
|
-
require_relative File.join("change", "by_at_most")
|
5
|
-
require_relative File.join("change", "by")
|
6
|
-
require_relative File.join("change", "from")
|
7
|
-
require_relative File.join("change", "to")
|
8
|
-
|
9
|
-
module Matchi
|
10
|
-
module Matcher
|
11
|
-
# Wraps the target of a change matcher.
|
12
|
-
class Change
|
13
|
-
# Returns a symbol identifying the matcher.
|
14
|
-
def self.to_sym
|
15
|
-
:change
|
16
|
-
end
|
17
|
-
|
18
|
-
# Initialize a wrapper of the change matcher with an object and the name
|
19
|
-
# of one of its methods.
|
20
|
-
#
|
21
|
-
# @example The wrapper of the change matcher on `"foo".to_s` value.
|
22
|
-
# Matchi::Matcher::Change.new("foo", :to_s)
|
23
|
-
#
|
24
|
-
# @param object [#object_id] An object.
|
25
|
-
# @param method [Symbol] The name of a method.
|
26
|
-
# @param args [Array] A list of arguments.
|
27
|
-
# @param kwargs [Hash] A list of keyword arguments.
|
28
|
-
def initialize(object, method, *args, **kwargs, &block)
|
29
|
-
@state = -> { object.send(method, *args, **kwargs, &block) }
|
30
|
-
end
|
31
|
-
|
32
|
-
# Specifies a minimum delta of the expected change.
|
33
|
-
#
|
34
|
-
# @param expected [#object_id] The minimum delta of the expected change.
|
35
|
-
#
|
36
|
-
# @return [#matches?] A *change by at least* matcher.
|
37
|
-
def by_at_least(expected)
|
38
|
-
ByAtLeast.new(expected, &@state)
|
39
|
-
end
|
40
|
-
|
41
|
-
# Specifies a maximum delta of the expected change.
|
42
|
-
#
|
43
|
-
# @param expected [#object_id] The maximum delta of the expected change.
|
44
|
-
#
|
45
|
-
# @return [#matches?] A *change by at most* matcher.
|
46
|
-
def by_at_most(expected)
|
47
|
-
ByAtMost.new(expected, &@state)
|
48
|
-
end
|
49
|
-
|
50
|
-
# Specifies the delta of the expected change.
|
51
|
-
#
|
52
|
-
# @param expected [#object_id] The delta of the expected change.
|
53
|
-
#
|
54
|
-
# @return [#matches?] A *change by* matcher.
|
55
|
-
def by(expected)
|
56
|
-
By.new(expected, &@state)
|
57
|
-
end
|
58
|
-
|
59
|
-
# Specifies the original value.
|
60
|
-
#
|
61
|
-
# @param expected [#object_id] The original value.
|
62
|
-
#
|
63
|
-
# @return [#matches?] A *change from* wrapper.
|
64
|
-
def from(expected)
|
65
|
-
From.new(expected, &@state)
|
66
|
-
end
|
67
|
-
|
68
|
-
# Specifies the new value to expect.
|
69
|
-
#
|
70
|
-
# @param expected [#object_id] The new value to expect.
|
71
|
-
#
|
72
|
-
# @return [#matches?] A *change to* matcher.
|
73
|
-
def to(expected)
|
74
|
-
To.new(expected, &@state)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
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* matcher.
|
9
|
-
class By < ::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 1 matcher.
|
18
|
-
# object = []
|
19
|
-
# Matchi::Matcher::Change::By.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::By.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 #{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 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
|