Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Make the expect(a).to be_within(x).percent_of(y) matcher work with integer x and y #229

Merged
merged 2 commits into from

2 participants

Sam Phippen Myron Marston
Sam Phippen
Collaborator

Related #189

Watched this fail with integer x and y, added to_f and a spec. Passes now.

lib/rspec/matchers/built_in/be_within.rb
@@ -23,7 +23,7 @@ def of(expected)
def percent_of(expected)
@expected = expected
- @tolerance = @delta * @expected / 100
+ @tolerance = @delta.to_f * @expected.to_f / 100
Myron Marston Owner

I think we only need a single float in this expression, for the entire division operation to be float division rather than integer division (which is the source of the bug). We can get that by changing 100 to 100.0 -- then there's no need for the extra to_f message sends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Myron Marston

Also, can you add a Changelog entry for this?

Thanks for tackling this so quick, Sam!

samphippen added some commits
Sam Phippen samphippen Make expect(a).to be_within(x).percent_of(y) work with integer x and y
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
4b0a77e
Sam Phippen samphippen Add changelog entry for integer be_within percent fix.
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
e717002
Myron Marston myronmarston merged commit f12680c into from
Myron Marston

Thanks, Sam!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2013
  1. Sam Phippen

    Make expect(a).to be_within(x).percent_of(y) work with integer x and y

    samphippen authored
    Signed-off-by: Sam Phippen <samphippen@googlemail.com>
  2. Sam Phippen

    Add changelog entry for integer be_within percent fix.

    samphippen authored
    Signed-off-by: Sam Phippen <samphippen@googlemail.com>
This page is out of date. Refresh to see the latest.
2  Changelog.md
View
@@ -12,6 +12,8 @@ Bug fixes
* Fix differ to not raise errors when dealing with differently-encoded
strings (Jon Rowe).
+* Fix `expect(something).to be_within(x).percent_of(y)` where x and y are both
+ integers (Sam Phippen).
### 2.13.0 / 2013-02-23
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.12.1...v2.13.0)
2  lib/rspec/matchers/built_in/be_within.rb
View
@@ -23,7 +23,7 @@ def of(expected)
def percent_of(expected)
@expected = expected
- @tolerance = @delta * @expected / 100
+ @tolerance = @delta * @expected / 100.0
@unit = '%'
self
end
4 spec/rspec/matchers/be_within_spec.rb
View
@@ -27,6 +27,10 @@ module Matchers
expect(5.5).to be_within(0.5).of(5.0)
end
+ it "passes with integer arguments that are near each other" do
+ expect(1.0001).to be_within(5).percent_of(1)
+ end
+
it "fails when actual < (expected - delta)" do
expect {
expect(4.49).to be_within(0.5).of(5.0)
Something went wrong with that request. Please try again.