/usr/lib/ruby/vendor_ruby/rspec/matchers/built_in/be_within.rb is in ruby-rspec-expectations 3.4.0c3e0m1s1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | module RSpec
module Matchers
module BuiltIn
# @api private
# Provides the implementation for `be_within`.
# Not intended to be instantiated directly.
class BeWithin < BaseMatcher
def initialize(delta)
@delta = delta
end
# @api public
# Sets the expected value.
def of(expected)
@expected = expected
@tolerance = @delta
@unit = ''
self
end
# @api public
# Sets the expected value, and makes the matcher do
# a percent comparison.
def percent_of(expected)
@expected = expected
@tolerance = @delta * @expected.abs / 100.0
@unit = '%'
self
end
# @private
def matches?(actual)
@actual = actual
raise needs_expected unless defined? @expected
numeric? && (@actual - @expected).abs <= @tolerance
end
# @api private
# @return [String]
def failure_message
"expected #{actual_formatted} to #{description}#{not_numeric_clause}"
end
# @api private
# @return [String]
def failure_message_when_negated
"expected #{actual_formatted} not to #{description}"
end
# @api private
# @return [String]
def description
"be within #{@delta}#{@unit} of #{@expected}"
end
private
def numeric?
@actual.respond_to?(:-)
end
def needs_expected
ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)"
end
def not_numeric_clause
", but it could not be treated as a numeric value" unless numeric?
end
end
end
end
end
|