Class: Matchi::Change::ByAtMost
- Inherits:
-
Object
- Object
- Matchi::Change::ByAtMost
- Defined in:
- lib/matchi/change/by_at_most.rb
Overview
*Change by at most* matcher.
Instance Method Summary collapse
-
#initialize(expected, &state) ⇒ ByAtMost
constructor
Initialize the matcher with an object and a block.
-
#match? ⇒ Boolean
Boolean comparison on the expected change by comparing the value before and after the code execution.
-
#to_s ⇒ String
Returns a string representing the matcher.
Constructor Details
#initialize(expected, &state) ⇒ ByAtMost
Initialize the matcher with an object and a block.
19 20 21 22 23 24 25 26 |
# File 'lib/matchi/change/by_at_most.rb', line 19 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
Boolean comparison on the expected change by comparing the value before and after the code execution.
43 44 45 46 47 48 49 50 51 |
# File 'lib/matchi/change/by_at_most.rb', line 43 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 string representing the matcher.
56 57 58 |
# File 'lib/matchi/change/by_at_most.rb', line 56 def to_s "change by at most #{@expected.inspect}" end |