Fix rounding in toBeCloseTo
This commit is contained in:
@@ -8,6 +8,9 @@ describe("toBeCloseTo", function() {
|
|||||||
|
|
||||||
result = matcher.compare(0, 0.001);
|
result = matcher.compare(0, 0.001);
|
||||||
expect(result.pass).toBe(true);
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
result = matcher.compare(0, 0.005);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("fails when not within two decimal places by default", function() {
|
it("fails when not within two decimal places by default", function() {
|
||||||
@@ -16,6 +19,9 @@ describe("toBeCloseTo", function() {
|
|||||||
|
|
||||||
result = matcher.compare(0, 0.01);
|
result = matcher.compare(0, 0.01);
|
||||||
expect(result.pass).toBe(false);
|
expect(result.pass).toBe(false);
|
||||||
|
|
||||||
|
result = matcher.compare(0, 0.05);
|
||||||
|
expect(result.pass).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("accepts an optional precision argument", function() {
|
it("accepts an optional precision argument", function() {
|
||||||
@@ -25,8 +31,20 @@ describe("toBeCloseTo", function() {
|
|||||||
result = matcher.compare(0, 0.1, 0);
|
result = matcher.compare(0, 0.1, 0);
|
||||||
expect(result.pass).toBe(true);
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
result = matcher.compare(0, 0.5, 0);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
result = matcher.compare(0, 0.0001, 3);
|
result = matcher.compare(0, 0.0001, 3);
|
||||||
expect(result.pass).toBe(true);
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
result = matcher.compare(0, 0.0005, 3);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
result = matcher.compare(0, 0.00001, 4);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
result = matcher.compare(0, 0.00005, 4);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("fails when one of the arguments is null", function() {
|
it("fails when one of the arguments is null", function() {
|
||||||
@@ -58,7 +76,15 @@ describe("toBeCloseTo", function() {
|
|||||||
result = matcher.compare(1.23, 1.225);
|
result = matcher.compare(1.23, 1.225);
|
||||||
expect(result.pass).toBe(true);
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
result = matcher.compare(1.23, 1.235);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
// 1.2249999 will be rounded to 1.225
|
||||||
result = matcher.compare(1.23, 1.2249999);
|
result = matcher.compare(1.23, 1.2249999);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
|
|
||||||
|
// 1.2249999 will be rounded to 1.224
|
||||||
|
result = matcher.compare(1.23, 1.2244999);
|
||||||
expect(result.pass).toBe(false);
|
expect(result.pass).toBe(false);
|
||||||
|
|
||||||
result = matcher.compare(1.23, 1.234);
|
result = matcher.compare(1.23, 1.234);
|
||||||
|
|||||||
@@ -21,8 +21,9 @@ getJasmineRequireObj().toBeCloseTo = function() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pow = Math.pow(10, precision + 1);
|
||||||
return {
|
return {
|
||||||
pass: Math.abs(expected - actual) < (Math.pow(10, -precision) / 2)
|
pass: +(Math.round(Math.abs(expected - actual) * pow) / pow).toFixed(precision + 1) <= (Math.pow(10, -precision) / 2)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user