Class: Matchi::Change::By
- Inherits:
-
Object
- Object
- Matchi::Change::By
- Defined in:
- lib/matchi/change/by.rb
Overview
This matcher checks for exact changes, use ByAtLeast or ByAtMost for more flexible delta comparisons
Exact delta matcher that verifies precise numeric changes in object state.
This matcher ensures that a numeric value changes by exactly the specified amount after executing a block of code. It’s particularly useful for testing operations that should produce precise, predictable changes in numeric attributes, such as counters, quantities, or calculated values.
Instance Method Summary collapse
-
#initialize(expected, &state) ⇒ By
constructor
Initialize the matcher with an expected delta and a state block.
-
#match? { ... } ⇒ Boolean
Verifies that the value changes by exactly the expected amount.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(expected, &state) ⇒ By
Initialize the matcher with an expected delta and a state block.
80 81 82 83 84 85 86 |
# File 'lib/matchi/change/by.rb', line 80 def initialize(expected, &state) raise ::ArgumentError, "expected must be a Numeric" unless expected.is_a?(::Numeric) raise ::ArgumentError, "a block must be provided" unless block_given? @expected = expected @state = state end |
Instance Method Details
#match? { ... } ⇒ Boolean
Verifies that the value changes by exactly the expected amount.
This method compares the value before and after executing the provided block, ensuring that the difference matches the expected delta exactly. It’s useful for cases where precision is important and approximate changes are not acceptable.
112 113 114 115 116 117 118 119 120 |
# File 'lib/matchi/change/by.rb', line 112 def match? raise ::ArgumentError, "a block must be provided" unless block_given? value_before = @state.call yield value_after = @state.call @expected == (value_after - value_before) end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
132 133 134 |
# File 'lib/matchi/change/by.rb', line 132 def to_s "change by #{@expected.inspect}" end |