Browse files

Change the be_within matcher to be inclusive of the delta.

- Fixes #131.
- This changes the way the matcher behaves, which is something we would
  generally avoid in anything but a major release, however we believe
  that this is the correct behavior and that this change is very unlikely
  to impact anybody negatively. Specifically:

  1. 17.4.should be_within(0.5).of(17) # used to pass and continues to pass
  2. 17.5.should be_within(0.5).of(17) # used to fail due to lack of inclusivity, but passes after this change
  3. 17.6.should be_within(0.5).of(17) # used to fail and continues to fail
  4. 17.4.should_not be_within(0.5).of(17) # used to fail and continues to fail
  5. 17.5.should_not be_within(0.5).of(17) # used to pass due to lack of inclusivity, but fails after this change
  6. 17.6.should_not be_within(0.5).of(17) # used to fail and continues to fail

  Scenario 2 used to fail, so it's unlikely to be present.
  Scenario 5 will result in new failures, but we believe that it is very unlikely to be used.
  • Loading branch information...
1 parent ca5cc09 commit a9bf7ab62e93761f9f7ba8fff466f327f8b3d453 @dchelimsky dchelimsky committed Apr 2, 2012
View
1 Changelog.md
@@ -8,6 +8,7 @@ Bug fixes
* Fix for error with using custom matchers inside other custom matchers
(mirasrael)
* Fix using execution context methods in nested DSL matchers (mirasrael)
+* Fix `be_within` matcher to be inclusive of delta.
### 2.9.0 / 2012-03-17
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0)
View
25 features/built_in_matchers/be_within.feature
@@ -23,21 +23,24 @@ Feature: be_within matcher
"""
describe 27.5 do
it { should be_within(0.5).of(27.9) }
+ it { should be_within(0.5).of(28.0) }
it { should be_within(0.5).of(27.1) }
- it { should_not be_within(0.5).of(28) }
- it { should_not be_within(0.5).of(27) }
+ it { should be_within(0.5).of(27.0) }
+
+ it { should_not be_within(0.5).of(28.1) }
+ it { should_not be_within(0.5).of(26.9) }
# deliberate failures
- it { should_not be_within(0.5).of(27.9) }
- it { should_not be_within(0.5).of(27.1) }
- it { should be_within(0.5).of(28) }
- it { should be_within(0.5).of(27) }
+ it { should_not be_within(0.5).of(28) }
+ it { should_not be_within(0.5).of(27) }
+ it { should be_within(0.5).of(28.1) }
+ it { should be_within(0.5).of(26.9) }
end
"""
When I run `rspec be_within_matcher_spec.rb`
Then the output should contain all of these:
- | 8 examples, 4 failures |
- | expected 27.5 not to be within 0.5 of 27.9 |
- | expected 27.5 not to be within 0.5 of 27.1 |
- | expected 27.5 to be within 0.5 of 28 |
- | expected 27.5 to be within 0.5 of 27 |
+ | 10 examples, 4 failures |
+ | expected 27.5 not to be within 0.5 of 28 |
+ | expected 27.5 not to be within 0.5 of 27 |
+ | expected 27.5 to be within 0.5 of 28.1 |
+ | expected 27.5 to be within 0.5 of 26.9 |
View
2 lib/rspec/matchers/built_in/be_within.rb
@@ -14,7 +14,7 @@ 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
+ (super(actual) - expected).abs <= delta
end
def of(expected)
View
8 spec/rspec/matchers/be_within_spec.rb
@@ -15,16 +15,16 @@ module Matchers
be_within(0.5).of(5.0).matches?(4.51).should be_true
end
- it "does not match when actual == (expected - delta)" do
- be_within(0.5).of(5.0).matches?(4.5).should be_false
+ it "matches when actual == (expected - delta)" do
+ be_within(0.5).of(5.0).matches?(4.5).should be_true
end
it "does not match when actual < (expected - delta)" do
be_within(0.5).of(5.0).matches?(4.49).should be_false
end
- it "does not match when actual == (expected + delta)" do
- be_within(0.5).of(5.0).matches?(5.5).should be_false
+ it "matches when actual == (expected + delta)" do
+ be_within(0.5).of(5.0).matches?(5.5).should be_true
end
it "does not match when actual > (expected + delta)" do

0 comments on commit a9bf7ab

Please sign in to comment.