Sha256: 493e3713b5fab9f78e883c3556723d692702838de3d2c22d2cfac2b6571c9499

Contents?: true

Size: 1011 Bytes

Versions: 18

Compression:

Stored size: 1011 Bytes

Contents

module RSpec
  module Matchers
    # :call-seq:
    #   should be_within(delta).of(expected)
    #   should_not be_within(delta).of(expected)
    #
    # Passes if actual == expected +/- delta
    #
    # == Example
    #
    #   result.should 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 @_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

18 entries across 18 versions & 3 rubygems

Version Path
messagebus_ruby_api-0.4.7 spec/ruby/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/matchers/be_within.rb
messagebus_ruby_api-0.4.4 spec/ruby/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/matchers/be_within.rb
rspec-expectations-2.6.0 lib/rspec/matchers/be_within.rb
rspec-expectations-2.6.0.rc6 lib/rspec/matchers/be_within.rb
rspec-expectations-2.6.0.rc4 lib/rspec/matchers/be_within.rb
rspec-expectations-2.6.0.rc2 lib/rspec/matchers/be_within.rb
rspec-expectations-2.5.0 lib/rspec/matchers/be_within.rb
vim-jar-0.1.2.0001 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/be_within.rb
rspec-expectations-2.4.0 lib/rspec/matchers/be_within.rb
vim-jar-0.1.2 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/be_within.rb
vim-jar-0.1.1 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/be_within.rb
vim-jar-0.1.0 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/be_within.rb
rspec-expectations-2.3.0 lib/rspec/matchers/be_within.rb
vim-jar-0.0.3 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/be_within.rb
vim-jar-0.0.2 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/be_within.rb
vim-jar-0.0.1 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/matchers/be_within.rb
rspec-expectations-2.2.0 lib/rspec/matchers/be_within.rb
rspec-expectations-2.1.0 lib/rspec/matchers/be_within.rb