Sha256: 2309410f78b7555a7b1622f862e7c1c23b9a7cef374a2acb3b0300bd1872ab06

Contents?: true

Size: 1.79 KB

Versions: 402

Compression:

Stored size: 1.79 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 = @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

Version data entries

402 entries across 328 versions & 79 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
passbase-1.3.0 vendor/bundle/ruby/2.7.0/gems/rspec-expectations-3.9.2/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/rspec-expectations-3.8.4/lib/rspec/matchers/built_in/be_within.rb