Skip to content

Commit

Permalink
be_within matcher to class
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Oct 21, 2011
1 parent 93f6ac4 commit 148192b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
10 changes: 10 additions & 0 deletions lib/rspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ def be_an_instance_of(expected)
end

alias_method :be_instance_of, :be_an_instance_of

# 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)
BeWithin.new(delta)
end
end
end

Expand Down
51 changes: 26 additions & 25 deletions lib/rspec/matchers/be_within.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
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
class BeWithin
include BaseMatcher

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
attr_reader :delta

failure_message_for_should do |actual|
"expected #{actual} to #{description}"
end
def initialize(delta)
@delta = delta
end

failure_message_for_should_not do |actual|
"expected #{actual} not to #{description}"
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

description do
"be within #{_delta_} of #{@_expected}"
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
end
Expand Down

0 comments on commit 148192b

Please sign in to comment.