Class: Matchi::Change::ByAtLeast
- Inherits:
-
Object
- Object
- Matchi::Change::ByAtLeast
- Defined in:
- lib/matchi/change/by_at_least.rb
Overview
This matcher verifies minimum changes only. For exact changes, use By, and for maximum changes, use ByAtMost.
Minimum delta matcher that verifies numeric changes meet or exceed a threshold.
This matcher ensures that a numeric value changes by at least the specified amount after executing a block of code. It’s particularly useful when testing operations where you want to ensure a minimum change occurs but larger changes are acceptable, such as performance improvements, resource allocation, or progressive counters.
Instance Method Summary collapse
-
#initialize(expected, &state) ⇒ ByAtLeast
constructor
Initialize the matcher with a minimum expected change and a state block.
-
#match? { ... } ⇒ Boolean
Checks if the value changes by at least the expected amount.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(expected, &state) ⇒ ByAtLeast
Initialize the matcher with a minimum expected change and a state block.
101 102 103 104 105 106 107 108 |
# File 'lib/matchi/change/by_at_least.rb', line 101 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? raise ::ArgumentError, "expected must be non-negative" if expected.negative? @expected = expected @state = state end |
Instance Method Details
#match? { ... } ⇒ Boolean
Checks if the value changes by at least the expected amount.
This method compares the value before and after executing the provided block, ensuring that the difference is greater than or equal to the expected minimum. This is useful when you want to verify that a change meets a minimum threshold.
134 135 136 137 138 139 140 141 142 |
# File 'lib/matchi/change/by_at_least.rb', line 134 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.
153 154 155 |
# File 'lib/matchi/change/by_at_least.rb', line 153 def to_s "change by at least #{@expected.inspect}" end |