Class: Matchi::Change
- Inherits:
-
Object
- Object
- Matchi::Change
- Defined in:
- lib/matchi/change.rb,
lib/matchi/change/by.rb,
lib/matchi/change/to.rb,
lib/matchi/change/from.rb,
lib/matchi/change/from/to.rb,
lib/matchi/change/by_at_most.rb,
lib/matchi/change/by_at_least.rb
Overview
Wraps the target of a change matcher.
Defined Under Namespace
Classes: By, ByAtLeast, ByAtMost, From, To
Instance Method Summary collapse
-
#by(delta) ⇒ #match?
Specifies the exact delta of the expected change.
-
#by_at_least(minimum_delta) ⇒ #match?
Specifies a minimum delta of the expected change.
-
#by_at_most(maximum_delta) ⇒ #match?
Specifies a maximum delta of the expected change.
-
#from(old_value) ⇒ #to
Specifies the original value in a value transition check.
-
#initialize(object, method, *args, **kwargs, &block) ⇒ Change
constructor
Initialize a wrapper of the change matcher with an object and the name of one of its methods.
-
#match? { ... } ⇒ Boolean
Checks if the tracked method’s return value changes when executing the block.
-
#to(new_value) ⇒ #match?
Specifies the final value to expect.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(object, method, *args, **kwargs, &block) ⇒ Change
Initialize a wrapper of the change matcher with an object and the name of one of its methods.
43 44 45 46 47 48 |
# File 'lib/matchi/change.rb', line 43 def initialize(object, method, *args, **kwargs, &block) raise ::ArgumentError, "method must be a Symbol" unless method.is_a?(::Symbol) raise ::ArgumentError, "object must respond to method" unless object.respond_to?(method) @state = -> { object.send(method, *args, **kwargs, &block) } end |
Instance Method Details
#by(delta) ⇒ #match?
Specifies the exact delta of the expected change.
149 150 151 |
# File 'lib/matchi/change.rb', line 149 def by(delta) By.new(delta, &@state) end |
#by_at_least(minimum_delta) ⇒ #match?
Specifies a minimum delta of the expected change.
117 118 119 |
# File 'lib/matchi/change.rb', line 117 def by_at_least(minimum_delta) ByAtLeast.new(minimum_delta, &@state) end |
#by_at_most(maximum_delta) ⇒ #match?
Specifies a maximum delta of the expected change.
133 134 135 |
# File 'lib/matchi/change.rb', line 133 def by_at_most(maximum_delta) ByAtMost.new(maximum_delta, &@state) end |
#from(old_value) ⇒ #to
Specifies the original value in a value transition check.
164 165 166 |
# File 'lib/matchi/change.rb', line 164 def from(old_value) From.new(old_value, &@state) end |
#match? { ... } ⇒ Boolean
Checks if the tracked method’s return value changes when executing the block.
This method verifies that the value changes in any way between the start and end of the block execution. It doesn’t care about the type or magnitude of the change, only that it’s different.
83 84 85 86 87 88 89 90 91 |
# File 'lib/matchi/change.rb', line 83 def match? raise ::ArgumentError, "a block must be provided" unless block_given? value_before = @state.call yield value_after = @state.call !value_before.eql?(value_after) end |
#to(new_value) ⇒ #match?
Specifies the final value to expect.
180 181 182 |
# File 'lib/matchi/change.rb', line 180 def to(new_value) To.new(new_value, &@state) end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
101 102 103 |
# File 'lib/matchi/change.rb', line 101 def to_s "change #{@state.inspect}" end |