Skip to content

Commit

Permalink
Added .toBeNearTo() matcher that checks if the expected is within a c…
Browse files Browse the repository at this point in the history
…ertain range of the actual
  • Loading branch information
mattyod committed Nov 20, 2012
1 parent 74cdc5a commit 5a211cc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/jasmine-core/jasmine.js
Expand Up @@ -1482,6 +1482,18 @@ jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) {
return Math.abs(expected - this.actual) < (Math.pow(10, -precision) / 2);
};

/**
* Matcher that checks if a number is close enough to the actual to be
* acceptable. For example if the expected is 100 and the precision is 2 the
* numbers 98, 99, 100, 101 & 102 will return true.
*
* @param {Number} expected
* @param {Number} precision
*/
jasmine.Matchers.prototype.toBeNearTo = function(expected, precision) {
return Math.abs((expected - this.actual)) <= precision;
};

/**
* Matcher that checks that the expected exception was thrown by the actual.
*
Expand Down
31 changes: 31 additions & 0 deletions spec/core/MatchersSpec.js
Expand Up @@ -574,6 +574,37 @@ describe("jasmine.Matchers", function() {
});
});

describe('toBeNearTo', function() {
it("Returns true if the expected and actual numbers are within the given precision", function() {
expect(98).toBeNearTo(100, 2);
expect(99).toBeNearTo(100, 2);
expect(100).toBeNearTo(100, 2);
expect(101).toBeNearTo(100, 2);
expect(102).toBeNearTo(100, 2);
});

it("Returns false if the expected and actual numbers are outside the given precision", function () {
expect(97).not.toBeNearTo(100, 2);
expect(103).not.toBeNearTo(100, 2);
});

it("Builds an expectation result", function() {
var actual = 100;
var matcher = match(actual);
var expected = 97;
var precision = 2;
matcher.toBeNearTo(expected, precision);

var result = lastResult();

expect(result.matcherName).toBe("toBeNearTo");
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual) + ' to be near to');
expect(result.actual).toEqual(actual);
expect(result.expected).toEqual([expected, precision]);
});
});

describe("toThrow", function() {
describe("when code block throws an exception", function() {
var throwingFn;
Expand Down
12 changes: 12 additions & 0 deletions src/core/Matchers.js
Expand Up @@ -312,6 +312,18 @@ jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) {
return Math.abs(expected - this.actual) < (Math.pow(10, -precision) / 2);
};

/**
* Matcher that checks if a number is close enough to the actual to be
* acceptable. For example if the expected is 100 and the precision is 2 the
* numbers 98, 99, 100, 101 & 102 will return true.
*
* @param {Number} expected
* @param {Number} precision
*/
jasmine.Matchers.prototype.toBeNearTo = function(expected, precision) {
return Math.abs((expected - this.actual)) <= precision;
};

/**
* Matcher that checks that the expected exception was thrown by the actual.
*
Expand Down

0 comments on commit 5a211cc

Please sign in to comment.