@api private Provides the implementation for `be_within`. Not intended to be instantiated directly.
# File lib/rspec/matchers/built_in/be_within.rb, line 10 def initialize(delta) @delta = delta end
@api private @return [String]
# File lib/rspec/matchers/built_in/be_within.rb, line 54 def description "be within #{@delta}#{@unit} of #{@expected}" end
@api private @return [String]
# File lib/rspec/matchers/built_in/be_within.rb, line 42 def failure_message "expected #{@actual.inspect} to #{description}#{not_numeric_clause}" end
@api private @return [String]
# File lib/rspec/matchers/built_in/be_within.rb, line 48 def failure_message_when_negated "expected #{@actual.inspect} not to #{description}" end
@private
# File lib/rspec/matchers/built_in/be_within.rb, line 34 def matches?(actual) @actual = actual raise needs_expected unless defined? @expected numeric? && (@actual - @expected).abs <= @tolerance end
@api public Sets the expected value.
# File lib/rspec/matchers/built_in/be_within.rb, line 16 def of(expected) @expected = expected @tolerance = @delta @unit = '' self end
@api public Sets the expected value, and makes the matcher do a percent comparison.
# File lib/rspec/matchers/built_in/be_within.rb, line 26 def percent_of(expected) @expected = expected @tolerance = @delta * @expected.abs / 100.0 @unit = '%' self end
@private
# File lib/rspec/matchers/built_in/be_within.rb, line 59 def supports_block_expectations? false end
# File lib/rspec/matchers/built_in/be_within.rb, line 69 def needs_expected ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)" end
# File lib/rspec/matchers/built_in/be_within.rb, line 73 def not_numeric_clause ", but it could not be treated as a numeric value" unless numeric? end
# File lib/rspec/matchers/built_in/be_within.rb, line 65 def numeric? @actual.respond_to?(:-) end