Skip to content

Commit

Permalink
Change the be_within matcher to be inclusive of the delta.
Browse files Browse the repository at this point in the history
- Fixes rspec#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
dchelimsky committed Apr 2, 2012
1 parent ca5cc09 commit a9bf7ab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -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)
Expand Down
25 changes: 14 additions & 11 deletions features/built_in_matchers/be_within.feature
Expand Up @@ -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 |
2 changes: 1 addition & 1 deletion lib/rspec/matchers/built_in/be_within.rb
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions spec/rspec/matchers/be_within_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit a9bf7ab

Please sign in to comment.