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

Support big int in approximently #1606

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
29 changes: 17 additions & 12 deletions lib/chai/core/assertions.js
Expand Up @@ -233,7 +233,6 @@ Assertion.addProperty('any', function () {
* @namespace BDD
* @public
*/

Assertion.addProperty('all', function () {
flag(this, 'all', true);
flag(this, 'any', false);
Expand Down Expand Up @@ -694,6 +693,17 @@ Assertion.addProperty('true', function () {
);
});

Assertion.addProperty('numeric', function () {
const object = flag(this, 'object');

this.assert(
['Number', 'BigInt'].includes(_.type(object))
, 'expected #{this} to be numeric'
, 'expected #{this} to not be numeric'
, flag(this, 'negate') ? false : true
);
});

/**
* ### .callable
*
Expand Down Expand Up @@ -2999,19 +3009,14 @@ 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;

const abs = (x) => x < 0n ? -x : x;

this.assert(
Math.abs(obj - expected) <= delta
abs(obj - expected) <= delta
, 'expected #{this} to be close to ' + expected + ' +/- ' + delta
, 'expected #{this} not to be close to ' + expected + ' +/- ' + delta
);
Expand Down
17 changes: 9 additions & 8 deletions test/assert.js
Expand Up @@ -1858,25 +1858,26 @@ 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(){
assert.approximately(1.5, 1.0, 0.5);
assert.approximately(10, 20, 20);
assert.approximately(-10, 20, 30);
assert.approximately(1n, 2n, 1n);

err(function(){
assert.approximately(2, 1.0, 0.5, 'blah');
Expand All @@ -1888,19 +1889,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
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
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