Fail when one of the arguments passed into toBeCloseTo matcher is null
This commit is contained in:
@@ -29,6 +29,22 @@ describe("toBeCloseTo", function() {
|
|||||||
expect(result.pass).toBe(true);
|
expect(result.pass).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("fails when one of the arguments is null", function() {
|
||||||
|
var matcher = jasmineUnderTest.matchers.toBeCloseTo();
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
matcher.compare(null, null);
|
||||||
|
}).toThrowError('Cannot use toBeCloseTo with null. Arguments evaluated to: expect(null).toBeCloseTo(null).');
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
matcher.compare(0, null);
|
||||||
|
}).toThrowError('Cannot use toBeCloseTo with null. Arguments evaluated to: expect(0).toBeCloseTo(null).');
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
matcher.compare(null, 0);
|
||||||
|
}).toThrowError('Cannot use toBeCloseTo with null. Arguments evaluated to: expect(null).toBeCloseTo(0).');
|
||||||
|
});
|
||||||
|
|
||||||
it("rounds expected values", function() {
|
it("rounds expected values", function() {
|
||||||
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
|
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
|
||||||
result;
|
result;
|
||||||
|
|||||||
@@ -15,6 +15,12 @@ getJasmineRequireObj().toBeCloseTo = function() {
|
|||||||
precision = precision || 2;
|
precision = precision || 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (expected === null || actual === null) {
|
||||||
|
throw new Error('Cannot use toBeCloseTo with null. Arguments evaluated to: ' +
|
||||||
|
'expect(' + actual + ').toBeCloseTo(' + expected + ').'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pass: Math.abs(expected - actual) < (Math.pow(10, -precision) / 2)
|
pass: Math.abs(expected - actual) < (Math.pow(10, -precision) / 2)
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user