Sha256: f94822c2112357ee1400077086bf9fa06395506b7de933c1ae2de1ecfd4b967a

Contents?: true

Size: 1.8 KB

Versions: 6658

Compression:

Stored size: 1.8 KB

Contents

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 = @expected.abs * @delta / 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_formatted}"
        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

Version data entries

6,658 entries across 6,646 versions & 50 rubygems

Version Path
cybrid_api_bank_ruby-0.122.47 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_id_ruby-0.122.47 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_organization_ruby-0.122.47 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_id_ruby-0.122.46 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_bank_ruby-0.122.46 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_organization_ruby-0.122.46 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_organization_ruby-0.122.45 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_id_ruby-0.122.45 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_bank_ruby-0.122.45 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_bank_ruby-0.122.44 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_id_ruby-0.122.44 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_organization_ruby-0.122.44 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_bank_ruby-0.122.43 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_organization_ruby-0.122.43 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_id_ruby-0.122.43 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_bank_ruby-0.122.42 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_id_ruby-0.122.42 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_organization_ruby-0.122.42 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
ory-client-1.15.10 vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb
cybrid_api_organization_ruby-0.122.41 vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.3/lib/rspec/matchers/built_in/be_within.rb