Sha256: 8e99227b0f969b44749c0d57f9390d9e3043c3141d518395d9a610ef09341e26

Contents?: true

Size: 1009 Bytes

Versions: 7

Compression:

Stored size: 1009 Bytes

Contents

module RSpec
  module Matchers
    class BeWithin
      include BaseMatcher

      attr_reader :delta

      def initialize(delta)
        @delta = delta
      end

      def matches?(actual)
        unless defined?(@expected)
          raise ArgumentError.new("You must set an expected value using #of: be_within(#{delta}).of(expected_value)")
        end
        (super(actual) - expected).abs < delta
      end

      def of(expected)
        @expected = expected
        self
      end

      def failure_message_for_should
        "expected #{actual} to #{description}"
      end

      def failure_message_for_should_not
        "expected #{actual} not to #{description}"
      end

      def description
        "be within #{delta} of #{expected}"
      end
    end

    # Passes if actual == expected +/- delta
    #
    # @example
    #
    #   result.should be_within(0.5).of(3.0)
    #   result.should_not be_within(0.5).of(3.0)
    def be_within(delta)
      BeWithin.new(delta)
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/rspec-expectations-2.8.0/lib/rspec/matchers/be_within.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/rspec-expectations-2.8.0/lib/rspec/matchers/be_within.rb
horseman-0.0.4 vendor/ruby/1.9.1/gems/rspec-expectations-2.8.0/lib/rspec/matchers/be_within.rb
horseman-0.0.3 vendor/ruby/1.9.1/gems/rspec-expectations-2.8.0/lib/rspec/matchers/be_within.rb
rspec-expectations-2.8.0 lib/rspec/matchers/be_within.rb
rspec-expectations-2.8.0.rc2 lib/rspec/matchers/be_within.rb
rspec-expectations-2.8.0.rc1 lib/rspec/matchers/be_within.rb