Created
June 1, 2010 05:38
-
-
Save juliocesar/420610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Useful matcher for matching proximity of numbers so you can do things like | |
# 4.should be_close_to(5).by(1) | |
# or with Rails | |
# something.created_at.should be_close_to(somethingelse.created_at).by(10.minutes) | |
# "by" is not mandatory, but desirable since proximity is highly contextual | |
module CustomMatchers | |
class BeCloseTo | |
def initialize expected | |
@expected = expected | |
@tolerance = 40 | |
end | |
def matches? actual | |
@actual = actual | |
@actual >= (@expected - @tolerance) and @actual <= (@expected + @tolerance) | |
end | |
def by tolerance | |
@tolerance = tolerance | |
self | |
end | |
def failure_message_for_should | |
"expected #{@actual.inspect} to be close to #{@expected.inspect} by #{@tolerance}" | |
end | |
def failure_message_for_should_not | |
"expected #{@actual.inspect} not to be close to #{@expected.inspect} by #{@tolerance}" | |
end | |
end | |
def be_close_to expected | |
BeCloseTo.new expected | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment