Class: Ward::Matchers::CloseTo
- Defined in:
- lib/ward/matchers/close_to.rb
Overview
Tests whether the validation value is within the delta of the expected value.
Instance Attribute Summary
Attributes inherited from Matcher
Instance Method Summary collapse
-
#customise_error_values(values) ⇒ String
Adds extra information to the error message.
-
#initialize(expected, delta, *extra_args) ⇒ CloseTo
constructor
Creates a new CloseTo matcher instance.
-
#matches?(actual) ⇒ Boolean
Returns whether the given value is close to the expected value.
Methods inherited from Matcher
Constructor Details
#initialize(expected, delta, *extra_args) ⇒ CloseTo
Creates a new CloseTo matcher instance.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ward/matchers/close_to.rb', line 23 def initialize(expected, delta, *extra_args) raise ArgumentError, 'The CloseTo matcher requires that the +expected+ value ' \ 'responds to +-+' unless expected.respond_to?(:-) raise ArgumentError, 'The CloseTo matcher requires that a Numeric +delta+ value ' \ 'is supplied' unless delta.kind_of?(Numeric) super(expected, *extra_args) @delta = delta end |
Instance Method Details
#customise_error_values(values) ⇒ String
Adds extra information to the error message.
53 54 55 56 |
# File 'lib/ward/matchers/close_to.rb', line 53 def customise_error_values(values) values[:delta] = @delta values end |
#matches?(actual) ⇒ Boolean
Returns whether the given value is close to the expected value.
44 45 46 |
# File 'lib/ward/matchers/close_to.rb', line 44 def matches?(actual) (actual - @expected).abs <= @delta end |