Sha256: 2fdb59026e2531d40cc5f19ac92ab69ab4bef2f1fbb15a9f0db215465f148bc2

Contents?: true

Size: 954 Bytes

Versions: 10

Compression:

Stored size: 954 Bytes

Contents

module RSpec
  module Matchers
    # Passes if actual == expected +/- delta
    #
    # == Examples
    #
    #   result.should be_within(0.5).of(3.0)
    #   result.should_not be_within(0.5).of(3.0)
    def be_within(delta)
      Matcher.new :be_within, delta do |_delta_|
        chain :of do |_expected_|
          @_expected = _expected_
        end

        match do |actual|
          unless defined?(@_expected)
            raise ArgumentError.new("You must set an expected value using #of: be_within(#{_delta_}).of(expected_value)")
          end
          (actual - @_expected).abs < _delta_
        end

        failure_message_for_should do |actual|
          "expected #{actual} to #{description}"
        end

        failure_message_for_should_not do |actual|
          "expected #{actual} not to #{description}"
        end

        description do
          "be within #{_delta_} of #{@_expected}"
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
horseman-0.0.4 vendor/ruby/1.9.1/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
horseman-0.0.3 vendor/ruby/1.9.1/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
horseman-0.0.2 vendor/ruby/1.9.1/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
nutshell-crm-0.0.6.alpha vendor/bundle/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
nutshell-crm-0.0.5 vendor/bundle/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
nutshell-crm-0.0.4 vendor/bundle/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
nutshell-crm-0.0.3 vendor/bundle/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
nutshell-crm-0.0.2 vendor/bundle/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
nutshell-crm-0.0.1 vendor/bundle/gems/rspec-expectations-2.7.0/lib/rspec/matchers/be_within.rb
rspec-expectations-2.7.0 lib/rspec/matchers/be_within.rb