Skip to content

Commit 6d0ae35

Browse files
committed
Merge pull request #527 from cjqed/should-approximately
Added approximately alias to close to
2 parents 7f5a108 + 7fe23e9 commit 6d0ae35

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed

lib/chai/core/assertions.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,27 +1445,31 @@ module.exports = function (chai, _) {
14451445
* expect(1.5).to.be.closeTo(1, 0.5);
14461446
*
14471447
* @name closeTo
1448+
* @alias approximately
14481449
* @param {Number} expected
14491450
* @param {Number} delta
14501451
* @param {String} message _optional_
14511452
* @api public
14521453
*/
14531454

1454-
Assertion.addMethod('closeTo', function (expected, delta, msg) {
1455+
function closeTo(expected, delta, msg) {
14551456
if (msg) flag(this, 'message', msg);
14561457
var obj = flag(this, 'object');
14571458

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

14631464
this.assert(
14641465
Math.abs(obj - expected) <= delta
14651466
, 'expected #{this} to be close to ' + expected + ' +/- ' + delta
14661467
, 'expected #{this} not to be close to ' + expected + ' +/- ' + delta
14671468
);
1468-
});
1469+
}
1470+
1471+
Assertion.addMethod('closeTo', closeTo);
1472+
Assertion.addMethod('approximately', closeTo);
14691473

14701474
function isSubsetOf(subset, superset, cmp) {
14711475
return subset.every(function(elem) {

lib/chai/interface/assert.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,25 @@ module.exports = function (chai, util) {
11651165
assert.closeTo = function (act, exp, delta, msg) {
11661166
new Assertion(act, msg).to.be.closeTo(exp, delta);
11671167
};
1168+
1169+
/**
1170+
* ### .approximately(actual, expected, delta, [message])
1171+
*
1172+
* Asserts that the target is equal `expected`, to within a +/- `delta` range.
1173+
*
1174+
* assert.approximately(1.5, 1, 0.5, 'numbers are close');
1175+
*
1176+
* @name approximately
1177+
* @param {Number} actual
1178+
* @param {Number} expected
1179+
* @param {Number} delta
1180+
* @param {String} message
1181+
* @api public
1182+
*/
1183+
1184+
assert.approximately = function (act, exp, delta, msg) {
1185+
new Assertion(act, msg).to.be.approximately(exp, delta);
1186+
};
11681187

11691188
/**
11701189
* ### .sameMembers(set1, set2, [message])

test/assert.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,37 @@ describe('assert', function () {
707707

708708
err(function() {
709709
assert.closeTo(1.5, "1.0", 0.5);
710-
}, "the arguments to closeTo must be numbers");
710+
}, "the arguments to closeTo or approximately must be numbers");
711711

712712
err(function() {
713713
assert.closeTo(1.5, 1.0, true);
714-
}, "the arguments to closeTo must be numbers");
714+
}, "the arguments to closeTo or approximately must be numbers");
715+
});
716+
717+
it('approximately', function(){
718+
assert.approximately(1.5, 1.0, 0.5);
719+
assert.approximately(10, 20, 20);
720+
assert.approximately(-10, 20, 30);
721+
722+
err(function(){
723+
assert.approximately(2, 1.0, 0.5);
724+
}, "expected 2 to be close to 1 +/- 0.5");
725+
726+
err(function(){
727+
assert.approximately(-10, 20, 29);
728+
}, "expected -10 to be close to 20 +/- 29");
729+
730+
err(function() {
731+
assert.approximately([1.5], 1.0, 0.5);
732+
}, "expected [ 1.5 ] to be a number");
733+
734+
err(function() {
735+
assert.approximately(1.5, "1.0", 0.5);
736+
}, "the arguments to closeTo or approximately must be numbers");
737+
738+
err(function() {
739+
assert.approximately(1.5, 1.0, true);
740+
}, "the arguments to closeTo or approximately must be numbers");
715741
});
716742

717743
it('members', function() {

test/expect.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,11 +1046,37 @@ describe('expect', function () {
10461046

10471047
err(function() {
10481048
expect(1.5).to.be.closeTo("1.0", 0.5);
1049-
}, "the arguments to closeTo must be numbers");
1049+
}, "the arguments to closeTo or approximately must be numbers");
10501050

10511051
err(function() {
10521052
expect(1.5).to.be.closeTo(1.0, true);
1053-
}, "the arguments to closeTo must be numbers");
1053+
}, "the arguments to closeTo or approximately must be numbers");
1054+
});
1055+
1056+
it('approximately', function(){
1057+
expect(1.5).to.be.approximately(1.0, 0.5);
1058+
expect(10).to.be.approximately(20, 20);
1059+
expect(-10).to.be.approximately(20, 30);
1060+
1061+
err(function(){
1062+
expect(2).to.be.approximately(1.0, 0.5, 'blah');
1063+
}, "blah: expected 2 to be close to 1 +/- 0.5");
1064+
1065+
err(function(){
1066+
expect(-10).to.be.approximately(20, 29, 'blah');
1067+
}, "blah: expected -10 to be close to 20 +/- 29");
1068+
1069+
err(function() {
1070+
expect([1.5]).to.be.approximately(1.0, 0.5);
1071+
}, "expected [ 1.5 ] to be a number");
1072+
1073+
err(function() {
1074+
expect(1.5).to.be.approximately("1.0", 0.5);
1075+
}, "the arguments to closeTo or approximately must be numbers");
1076+
1077+
err(function() {
1078+
expect(1.5).to.be.approximately(1.0, true);
1079+
}, "the arguments to closeTo or approximately must be numbers");
10541080
});
10551081

10561082
it('include.members', function() {

test/should.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,31 @@ describe('should', function() {
875875

876876
err(function() {
877877
(1.5).should.be.closeTo("1.0", 0.5);
878-
}, "the arguments to closeTo must be numbers");
878+
}, "the arguments to closeTo or approximately must be numbers");
879879

880880
err(function() {
881881
(1.5).should.be.closeTo(1.0, true);
882-
}, "the arguments to closeTo must be numbers");
882+
}, "the arguments to closeTo or approximately must be numbers");
883+
});
884+
885+
it('approximately', function(){
886+
(1.5).should.be.approximately(1.0, 0.5);
887+
888+
err(function(){
889+
(2).should.be.approximately(1.0, 0.5, 'blah');
890+
}, "blah: expected 2 to be close to 1 +/- 0.5");
891+
892+
err(function() {
893+
[1.5].should.be.approximately(1.0, 0.5);
894+
}, "expected [ 1.5 ] to be a number");
895+
896+
err(function() {
897+
(1.5).should.be.approximately("1.0", 0.5);
898+
}, "the arguments to closeTo or approximately must be numbers");
899+
900+
err(function() {
901+
(1.5).should.be.approximately(1.0, true);
902+
}, "the arguments to closeTo or approximately must be numbers");
883903
});
884904

885905
it('include.members', function() {

0 commit comments

Comments
 (0)