toBeCloseTo matcher merged in
This commit is contained in:
parent
dd32048383
commit
61de2268fe
|
@ -348,10 +348,12 @@ describe("jasmine.Matchers", function() {
|
||||||
expect(match("foo").toEqual(jasmine.any(Object))).toFail();
|
expect(match("foo").toEqual(jasmine.any(Object))).toFail();
|
||||||
expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toPass();
|
expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toPass();
|
||||||
expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toFail();
|
expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toFail();
|
||||||
expect(match(function() {
|
expect(match(
|
||||||
|
function() {
|
||||||
}).toEqual(jasmine.any(Object))).toFail();
|
}).toEqual(jasmine.any(Object))).toFail();
|
||||||
expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toPass();
|
expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toPass();
|
||||||
expect(match(function() {
|
expect(match(
|
||||||
|
function() {
|
||||||
}).toEqual(jasmine.any(Function))).toPass();
|
}).toEqual(jasmine.any(Function))).toPass();
|
||||||
expect(match(["a", function() {
|
expect(match(["a", function() {
|
||||||
}]).toEqual(["a", jasmine.any(Function)])).toPass();
|
}]).toEqual(["a", jasmine.any(Function)])).toPass();
|
||||||
|
@ -471,6 +473,47 @@ describe("jasmine.Matchers", function() {
|
||||||
expect(result.expected).toEqual(expected);
|
expect(result.expected).toEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("toBeCloseTo", function() {
|
||||||
|
it("returns 'true' iff actual and expected are equal within 2 decimal points of precision", function() {
|
||||||
|
expect(0).toBeCloseTo(0);
|
||||||
|
expect(1).toBeCloseTo(1);
|
||||||
|
expect(1).not.toBeCloseTo(1.1);
|
||||||
|
expect(1).not.toBeCloseTo(1.01);
|
||||||
|
expect(1).toBeCloseTo(1.001);
|
||||||
|
|
||||||
|
expect(1.23).toBeCloseTo(1.234);
|
||||||
|
expect(1.23).toBeCloseTo(1.233);
|
||||||
|
expect(1.23).toBeCloseTo(1.232);
|
||||||
|
expect(1.23).not.toBeCloseTo(1.24);
|
||||||
|
|
||||||
|
expect(-1.23).toBeCloseTo(-1.234);
|
||||||
|
expect(-1.23).not.toBeCloseTo(-1.24);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts an optional precision argument", function() {
|
||||||
|
expect(1).toBeCloseTo(1.1, 0);
|
||||||
|
expect(1.2).toBeCloseTo(1.23, 1);
|
||||||
|
|
||||||
|
expect(1.234).toBeCloseTo(1.2343, 3);
|
||||||
|
expect(1.234).not.toBeCloseTo(1.233, 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rounds", function() {
|
||||||
|
expect(1.23).toBeCloseTo(1.229);
|
||||||
|
expect(1.23).toBeCloseTo(1.226);
|
||||||
|
expect(1.23).toBeCloseTo(1.225);
|
||||||
|
expect(1.23).not.toBeCloseTo(1.2249999);
|
||||||
|
|
||||||
|
expect(1.23).toBeCloseTo(1.234);
|
||||||
|
expect(1.23).toBeCloseTo(1.2349999);
|
||||||
|
expect(1.23).not.toBeCloseTo(1.235);
|
||||||
|
|
||||||
|
expect(-1.23).toBeCloseTo(-1.234);
|
||||||
|
expect(-1.23).not.toBeCloseTo(-1.235);
|
||||||
|
expect(-1.23).not.toBeCloseTo(-1.236);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("toThrow", function() {
|
describe("toThrow", function() {
|
||||||
describe("when code block throws an exception", function() {
|
describe("when code block throws an exception", function() {
|
||||||
var throwingFn;
|
var throwingFn;
|
||||||
|
@ -540,7 +583,8 @@ describe("jasmine.Matchers", function() {
|
||||||
|
|
||||||
describe("when code block does not throw an exception", function() {
|
describe("when code block does not throw an exception", function() {
|
||||||
it("should fail (or pass when inverted with .not)", function() {
|
it("should fail (or pass when inverted with .not)", function() {
|
||||||
expect(match(function() {
|
expect(match(
|
||||||
|
function() {
|
||||||
}).toThrow()).toFail();
|
}).toThrow()).toFail();
|
||||||
expect(lastResult().message).toEqual('Expected function to throw an exception.');
|
expect(lastResult().message).toEqual('Expected function to throw an exception.');
|
||||||
});
|
});
|
||||||
|
@ -596,19 +640,23 @@ describe("jasmine.Matchers", function() {
|
||||||
|
|
||||||
function shouldThrowAnExceptionWhenInvokedOnANonSpy(methodName) {
|
function shouldThrowAnExceptionWhenInvokedOnANonSpy(methodName) {
|
||||||
return function() {
|
return function() {
|
||||||
expect(function() {
|
expect(
|
||||||
|
function() {
|
||||||
match(TestClass.normalFunction)[methodName]();
|
match(TestClass.normalFunction)[methodName]();
|
||||||
}).toThrow('Expected a spy, but got Function.');
|
}).toThrow('Expected a spy, but got Function.');
|
||||||
|
|
||||||
expect(function() {
|
expect(
|
||||||
|
function() {
|
||||||
match(jasmine.undefined)[methodName]();
|
match(jasmine.undefined)[methodName]();
|
||||||
}).toThrow('Expected a spy, but got undefined.');
|
}).toThrow('Expected a spy, but got undefined.');
|
||||||
|
|
||||||
expect(function() {
|
expect(
|
||||||
|
function() {
|
||||||
match({some:'object'})[methodName]();
|
match({some:'object'})[methodName]();
|
||||||
}).toThrow('Expected a spy, but got { some : \'object\' }.');
|
}).toThrow('Expected a spy, but got { some : \'object\' }.');
|
||||||
|
|
||||||
expect(function() {
|
expect(
|
||||||
|
function() {
|
||||||
match("<b>")[methodName]();
|
match("<b>")[methodName]();
|
||||||
}).toThrow('Expected a spy, but got \'<b>\'.');
|
}).toThrow('Expected a spy, but got \'<b>\'.');
|
||||||
};
|
};
|
||||||
|
@ -623,7 +671,8 @@ describe("jasmine.Matchers", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw an exception when invoked with any arguments", function() {
|
it("should throw an exception when invoked with any arguments", function() {
|
||||||
expect(function() {
|
expect(
|
||||||
|
function() {
|
||||||
match(TestClass.normalFunction).toHaveBeenCalled("unwanted argument");
|
match(TestClass.normalFunction).toHaveBeenCalled("unwanted argument");
|
||||||
}).toThrow('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
|
}).toThrow('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
|
||||||
});
|
});
|
||||||
|
@ -651,7 +700,8 @@ describe("jasmine.Matchers", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw an exception when invoked with any arguments", function() {
|
it("should throw an exception when invoked with any arguments", function() {
|
||||||
expect(function() {
|
expect(
|
||||||
|
function() {
|
||||||
match(TestClass.normalFunction).wasNotCalled("unwanted argument");
|
match(TestClass.normalFunction).wasNotCalled("unwanted argument");
|
||||||
}).toThrow('wasNotCalled does not take arguments');
|
}).toThrow('wasNotCalled does not take arguments');
|
||||||
});
|
});
|
||||||
|
|
|
@ -291,6 +291,23 @@ jasmine.Matchers.prototype.toBeGreaterThan = function(expected) {
|
||||||
return this.actual > expected;
|
return this.actual > expected;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matcher that checks that the expected item is equal to the actual item
|
||||||
|
* up to a given level of decimal precision (default 2).
|
||||||
|
*
|
||||||
|
* @param {Number} expected
|
||||||
|
* @param {Number} precision
|
||||||
|
*/
|
||||||
|
jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) {
|
||||||
|
if (!(precision === 0)) {
|
||||||
|
precision = precision || 2;
|
||||||
|
}
|
||||||
|
var multiplier = Math.pow(10, precision);
|
||||||
|
var actual = Math.round(this.actual * multiplier);
|
||||||
|
expected = Math.round(expected * multiplier);
|
||||||
|
return expected == actual;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Matcher that checks that the expected exception was thrown by the actual.
|
* Matcher that checks that the expected exception was thrown by the actual.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue