Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change toBeCloseTo matcher to be more consistent #264

Merged
merged 1 commit into from
Aug 12, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/jasmine-core/jasmine.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1445,10 +1445,7 @@ jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) {
if (!(precision === 0)) { if (!(precision === 0)) {
precision = precision || 2; precision = precision || 2;
} }
var multiplier = Math.pow(10, precision); return Math.abs(expected - this.actual) < (Math.pow(10, -precision) / 2);
var actual = Math.round(this.actual * multiplier);
expected = Math.round(expected * multiplier);
return expected == actual;
}; };


/** /**
Expand Down Expand Up @@ -2537,5 +2534,5 @@ jasmine.version_= {
"major": 1, "major": 1,
"minor": 2, "minor": 2,
"build": 0, "build": 0,
"revision": 1337006083 "revision": 1343710612
}; };
5 changes: 5 additions & 0 deletions spec/core/MatchersSpec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -510,6 +510,11 @@ describe("jasmine.Matchers", function() {
expect(-1.23).not.toBeCloseTo(-1.24); expect(-1.23).not.toBeCloseTo(-1.24);
}); });


it("expects close numbers to 'be close' and further numbers not to", function() {
expect(1.225).not.toBeCloseTo(1.234); // difference is 0.009
expect(1.225).toBeCloseTo(1.224); // difference is 0.001
});

it("accepts an optional precision argument", function() { it("accepts an optional precision argument", function() {
expect(1).toBeCloseTo(1.1, 0); expect(1).toBeCloseTo(1.1, 0);
expect(1.2).toBeCloseTo(1.23, 1); expect(1.2).toBeCloseTo(1.23, 1);
Expand Down
5 changes: 1 addition & 4 deletions src/core/Matchers.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -302,10 +302,7 @@ jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) {
if (!(precision === 0)) { if (!(precision === 0)) {
precision = precision || 2; precision = precision || 2;
} }
var multiplier = Math.pow(10, precision); return Math.abs(expected - this.actual) < (Math.pow(10, -precision) / 2);
var actual = Math.round(this.actual * multiplier);
expected = Math.round(expected * multiplier);
return expected == actual;
}; };


/** /**
Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ jasmine.version_= {
"major": 1, "major": 1,
"minor": 2, "minor": 2,
"build": 0, "build": 0,
"revision": 1337006083 "revision": 1343710612
}; };