Skip to content

Commit

Permalink
Use numeric assertion in approximately
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Feb 20, 2024
1 parent 1d3a516 commit f78345a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 37 deletions.
13 changes: 3 additions & 10 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3009,16 +3009,9 @@ function closeTo(expected, delta, msg) {
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

new Assertion(obj, flagMsg, ssfi, true).is.a('number');
if (typeof expected !== 'number' || typeof delta !== 'number') {
flagMsg = flagMsg ? flagMsg + ': ' : '';
var deltaMessage = delta === undefined ? ", and a delta is required" : "";
throw new AssertionError(
flagMsg + 'the arguments to closeTo or approximately must be numbers' + deltaMessage,
undefined,
ssfi
);
}
new Assertion(obj, flagMsg, ssfi, true).is.numeric;
new Assertion(delta, flagMsg, ssfi, true).is.numeric;
new Assertion(expected, flagMsg, ssfi, true).is.numeric;

this.assert(
Math.abs(obj - expected) <= delta
Expand Down
16 changes: 8 additions & 8 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1858,19 +1858,19 @@ describe('assert', function () {

err(function() {
assert.closeTo([1.5], 1.0, 0.5, 'blah');
}, "blah: expected [ 1.5 ] to be a number");
}, "blah: expected [ 1.5 ] to be numeric");

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

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

err(function() {
assert.closeTo(1.5, 1.0, undefined, 'blah');
}, "blah: the arguments to closeTo or approximately must be numbers, and a delta is required");
}, "blah: expected undefined to be numeric");
});

it('approximately', function(){
Expand All @@ -1888,19 +1888,19 @@ describe('assert', function () {

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

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

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

err(function() {
assert.approximately(1.5, 1.0, undefined, 'blah');
}, "blah: the arguments to closeTo or approximately must be numbers, and a delta is required");
}, "blah: expected undefined to be numeric");
});

it('sameMembers', function() {
Expand Down
22 changes: 11 additions & 11 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3279,31 +3279,31 @@ describe('expect', function () {

err(function() {
expect([1.5]).to.be.closeTo(1.0, 0.5, 'blah');
}, "blah: expected [ 1.5 ] to be a number");
}, "blah: expected [ 1.5 ] to be numeric");

err(function() {
expect([1.5], 'blah').to.be.closeTo(1.0, 0.5);
}, "blah: expected [ 1.5 ] to be a number");
}, "blah: expected [ 1.5 ] to be numeric");

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

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

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

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

err(function() {
expect(1.5, 'blah').to.be.closeTo(1.0);
}, "blah: the arguments to closeTo or approximately must be numbers, and a delta is required");
}, "blah: expected undefined to be numeric");
});

it('approximately', function(){
Expand All @@ -3321,19 +3321,19 @@ describe('expect', function () {

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

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

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

err(function() {
expect(1.5).to.be.approximately(1.0);
}, "the arguments to closeTo or approximately must be numbers, and a delta is required");
}, "expected undefined to be numeric");
});

it('oneOf', function() {
Expand Down
16 changes: 8 additions & 8 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -2756,19 +2756,19 @@ describe('should', function() {

err(function() {
[1.5].should.be.closeTo(1.0, 0.5, 'blah');
}, "blah: expected [ 1.5 ] to be a number");
}, "blah: expected [ 1.5 ] to be numeric");

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

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

err(function() {
(1.5).should.be.closeTo(1.0, undefined, 'blah');
}, "blah: the arguments to closeTo or approximately must be numbers, and a delta is required");
}, "blah: expected undefined to be numeric");
});

it('approximately', function(){
Expand All @@ -2780,19 +2780,19 @@ describe('should', function() {

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

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

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

err(function() {
(1.5).should.be.approximately(1.0);
}, "the arguments to closeTo or approximately must be numbers, and a delta is required");
}, "expected undefined to be numeric");
});

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

0 comments on commit f78345a

Please sign in to comment.