class RSpec::Matchers::BuiltIn::BeWithin

Public Class Methods

new(delta) click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 5
def initialize(delta)
  @delta = delta
end

Public Instance Methods

==(actual)
Alias for: matches?
description() click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 39
def description
  "be within #{@delta}#{@unit} of #{@expected}"
end
failure_message_for_should() click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 31
def failure_message_for_should
  "expected #{@actual} to #{description}"
end
failure_message_for_should_not() click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 35
def failure_message_for_should_not
  "expected #{@actual} not to #{description}"
end
matches?(actual) click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 9
def matches?(actual)
  @actual = actual
  raise needs_expected     unless defined? @expected
  raise needs_subtractable unless @actual.respond_to? :-
  (@actual - @expected).abs <= @tolerance
end
Also aliased as: ==
of(expected) click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 17
def of(expected)
  @expected  = expected
  @tolerance = @delta
  @unit      = ''
  self
end
percent_of(expected) click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 24
def percent_of(expected)
  @expected  = expected
  @tolerance = @delta * @expected / 100
  @unit      = '%'
  self
end

Private Instance Methods

needs_expected() click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 49
def needs_expected
  ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)"
end
needs_subtractable() click to toggle source
# File lib/rspec/matchers/built_in/be_within.rb, line 45
def needs_subtractable
  ArgumentError.new "The actual value (#{@actual.inspect}) must respond to `-`"
end