Sha256: d0855b788db10b951aac25d6fb7ec8ec095838d1ab3457fd778464c09b06e631

Contents?: true

Size: 1.79 KB

Versions: 33

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.inspect} to #{description}#{not_numeric_clause}"
        end

        # @api private
        # @return [String]
        def failure_message_when_negated
          "expected #{@actual.inspect} 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

33 entries across 30 versions & 9 rubygems

Version Path
opal-rspec-0.8.0 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.8.0.alpha3 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.8.0.alpha2 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.8.0.alpha1 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.7.1 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.7.0 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.6.2 rspec-expectations/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.7.0.rc.2 rspec-expectations/upstream/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.6.1 rspec-expectations/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.6.0 rspec-expectations/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.6.0.beta1 rspec-expectations/lib/rspec/matchers/built_in/be_within.rb
opal-connect-rspec-0.5.0 rspec-expectations/lib/rspec/matchers/built_in/be_within.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_within.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/rspec-expectations-3.1.0/lib/rspec/matchers/built_in/be_within.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/rspec-expectations-3.1.0/lib/rspec/matchers/built_in/be_within.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/rspec-expectations-3.1.0/lib/rspec/matchers/built_in/be_within.rb
opal-rspec-0.5.0 rspec-expectations/lib/rspec/matchers/built_in/be_within.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_within.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_within.rb
logstash-codec-json-2.0.3 vendor/gems/rspec-expectations-3.1.2/lib/rspec/matchers/built_in/be_within.rb