Skip to content

Commit

Permalink
Merge pull request #527 from cjqed/should-approximately
Browse files Browse the repository at this point in the history
Added approximately alias to close to
  • Loading branch information
keithamus committed Oct 4, 2015
2 parents 7f5a108 + 7fe23e9 commit 6d0ae35
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
10 changes: 7 additions & 3 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1445,27 +1445,31 @@ module.exports = function (chai, _) {
* expect(1.5).to.be.closeTo(1, 0.5);
*
* @name closeTo
* @alias approximately
* @param {Number} expected
* @param {Number} delta
* @param {String} message _optional_
* @api public
*/

Assertion.addMethod('closeTo', function (expected, delta, msg) {
function closeTo(expected, delta, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');

new Assertion(obj, msg).is.a('number');
if (_.type(expected) !== 'number' || _.type(delta) !== 'number') {
throw new Error('the arguments to closeTo must be numbers');
throw new Error('the arguments to closeTo or approximately must be numbers');
}

this.assert(
Math.abs(obj - expected) <= delta
, 'expected #{this} to be close to ' + expected + ' +/- ' + delta
, 'expected #{this} not to be close to ' + expected + ' +/- ' + delta
);
});
}

Assertion.addMethod('closeTo', closeTo);
Assertion.addMethod('approximately', closeTo);

function isSubsetOf(subset, superset, cmp) {
return subset.every(function(elem) {
Expand Down
19 changes: 19 additions & 0 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,25 @@ module.exports = function (chai, util) {
assert.closeTo = function (act, exp, delta, msg) {
new Assertion(act, msg).to.be.closeTo(exp, delta);
};

/**
* ### .approximately(actual, expected, delta, [message])
*
* Asserts that the target is equal `expected`, to within a +/- `delta` range.
*
* assert.approximately(1.5, 1, 0.5, 'numbers are close');
*
* @name approximately
* @param {Number} actual
* @param {Number} expected
* @param {Number} delta
* @param {String} message
* @api public
*/

assert.approximately = function (act, exp, delta, msg) {
new Assertion(act, msg).to.be.approximately(exp, delta);
};

/**
* ### .sameMembers(set1, set2, [message])
Expand Down
30 changes: 28 additions & 2 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,37 @@ describe('assert', function () {

err(function() {
assert.closeTo(1.5, "1.0", 0.5);
}, "the arguments to closeTo must be numbers");
}, "the arguments to closeTo or approximately must be numbers");

err(function() {
assert.closeTo(1.5, 1.0, true);
}, "the arguments to closeTo must be numbers");
}, "the arguments to closeTo or approximately must be numbers");
});

it('approximately', function(){
assert.approximately(1.5, 1.0, 0.5);
assert.approximately(10, 20, 20);
assert.approximately(-10, 20, 30);

err(function(){
assert.approximately(2, 1.0, 0.5);
}, "expected 2 to be close to 1 +/- 0.5");

err(function(){
assert.approximately(-10, 20, 29);
}, "expected -10 to be close to 20 +/- 29");

err(function() {
assert.approximately([1.5], 1.0, 0.5);
}, "expected [ 1.5 ] to be a number");

err(function() {
assert.approximately(1.5, "1.0", 0.5);
}, "the arguments to closeTo or approximately must be numbers");

err(function() {
assert.approximately(1.5, 1.0, true);
}, "the arguments to closeTo or approximately must be numbers");
});

it('members', function() {
Expand Down
30 changes: 28 additions & 2 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,37 @@ describe('expect', function () {

err(function() {
expect(1.5).to.be.closeTo("1.0", 0.5);
}, "the arguments to closeTo must be numbers");
}, "the arguments to closeTo or approximately must be numbers");

err(function() {
expect(1.5).to.be.closeTo(1.0, true);
}, "the arguments to closeTo must be numbers");
}, "the arguments to closeTo or approximately must be numbers");
});

it('approximately', function(){
expect(1.5).to.be.approximately(1.0, 0.5);
expect(10).to.be.approximately(20, 20);
expect(-10).to.be.approximately(20, 30);

err(function(){
expect(2).to.be.approximately(1.0, 0.5, 'blah');
}, "blah: expected 2 to be close to 1 +/- 0.5");

err(function(){
expect(-10).to.be.approximately(20, 29, 'blah');
}, "blah: expected -10 to be close to 20 +/- 29");

err(function() {
expect([1.5]).to.be.approximately(1.0, 0.5);
}, "expected [ 1.5 ] to be a number");

err(function() {
expect(1.5).to.be.approximately("1.0", 0.5);
}, "the arguments to closeTo or approximately must be numbers");

err(function() {
expect(1.5).to.be.approximately(1.0, true);
}, "the arguments to closeTo or approximately must be numbers");
});

it('include.members', function() {
Expand Down
24 changes: 22 additions & 2 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,31 @@ describe('should', function() {

err(function() {
(1.5).should.be.closeTo("1.0", 0.5);
}, "the arguments to closeTo must be numbers");
}, "the arguments to closeTo or approximately must be numbers");

err(function() {
(1.5).should.be.closeTo(1.0, true);
}, "the arguments to closeTo must be numbers");
}, "the arguments to closeTo or approximately must be numbers");
});

it('approximately', function(){
(1.5).should.be.approximately(1.0, 0.5);

err(function(){
(2).should.be.approximately(1.0, 0.5, 'blah');
}, "blah: expected 2 to be close to 1 +/- 0.5");

err(function() {
[1.5].should.be.approximately(1.0, 0.5);
}, "expected [ 1.5 ] to be a number");

err(function() {
(1.5).should.be.approximately("1.0", 0.5);
}, "the arguments to closeTo or approximately must be numbers");

err(function() {
(1.5).should.be.approximately(1.0, true);
}, "the arguments to closeTo or approximately must be numbers");
});

it('include.members', function() {
Expand Down

0 comments on commit 6d0ae35

Please sign in to comment.