Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added approximately alias to close to #527

Merged
merged 1 commit into from
Oct 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/chai/core/assertions.js
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
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
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
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
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