Sha256: 515d6a2b8340c78fe58e501a5557fc0dc66f2c46f326372d6b4089c163a72610

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      class BeWithin
        def initialize(delta)
          @delta = delta
        end

        def matches?(actual)
          @actual = actual
          raise needs_expected     unless defined? @expected 
          raise needs_subtractable unless @actual.respond_to? :-
          (@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

        private

        def needs_subtractable
          ArgumentError.new "The actual value (#{@actual.inspect}) must respond to `-`"
        end

        def needs_expected
          ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)"
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
gem_repackager-0.1.0 support/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/be_within.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/be_within.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/be_within.rb
rspec-expectations-2.11.2 lib/rspec/matchers/built_in/be_within.rb
rspec-expectations-2.11.1 lib/rspec/matchers/built_in/be_within.rb