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 Mar 28, 2013

Conversation

Projects
None yet
2 participants
Owner

samphippen commented Mar 27, 2013

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
@myronmarston

myronmarston Mar 27, 2013

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.

Owner

myronmarston commented Mar 27, 2013

Also, can you add a Changelog entry for this?

Thanks for tackling this so quick, Sam!

samphippen added some commits Mar 27, 2013

Make expect(a).to be_within(x).percent_of(y) work with integer x and y
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
Add changelog entry for integer be_within percent fix.
Signed-off-by: Sam Phippen <samphippen@googlemail.com>

myronmarston added a commit that referenced this pull request Mar 28, 2013

Merge pull request #229 from samphippen/percent-matcher-with-integers
Make the expect(a).to be_within(x).percent_of(y) matcher work with integer x and y

@myronmarston myronmarston merged commit f12680c into rspec:master Mar 28, 2013

1 check failed

default The Travis build failed
Details
Owner

myronmarston commented Mar 28, 2013

Thanks, Sam!

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