class RSpec::Matchers::BuiltIn::BeWithin
@api private Provides the implementation for `be_within`. Not intended to be instantiated directly.
Public Class Methods
new(delta)
click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 8 def initialize(delta) @delta = delta end
Public Instance Methods
description()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/be_within.rb, line 52 def description "be within #{@delta}#{@unit} of #{expected_formatted}" end
failure_message()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/be_within.rb, line 40 def failure_message "expected #{actual_formatted} to #{description}#{not_numeric_clause}" end
failure_message_when_negated()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/be_within.rb, line 46 def failure_message_when_negated "expected #{actual_formatted} not to #{description}" end
matches?(actual)
click to toggle source
@private
# File lib/rspec/matchers/built_in/be_within.rb, line 32 def matches?(actual) @actual = actual raise needs_expected unless defined? @expected numeric? && (@actual - @expected).abs <= @tolerance end
of(expected)
click to toggle source
@api public Sets the expected value.
# File lib/rspec/matchers/built_in/be_within.rb, line 14 def of(expected) @expected = expected @tolerance = @delta @unit = '' self end
percent_of(expected)
click to toggle source
@api public Sets the expected value, and makes the matcher do a percent comparison.
# File lib/rspec/matchers/built_in/be_within.rb, line 24 def percent_of(expected) @expected = expected @tolerance = @expected.abs * @delta / 100.0 @unit = '%' self end
Private Instance Methods
needs_expected()
click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 62 def needs_expected ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)" end
not_numeric_clause()
click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 66 def not_numeric_clause ", but it could not be treated as a numeric value" unless numeric? end
numeric?()
click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 58 def numeric? @actual.respond_to?(:-) end