[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var test = require('tape');
|
---|
| 4 | var inspect = require('object-inspect');
|
---|
| 5 | var hasBigInts = require('has-bigints')();
|
---|
| 6 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
[79a0317] | 7 | var forEach = require('for-each');
|
---|
| 8 | var v = require('es-value-fixtures');
|
---|
[d565449] | 9 |
|
---|
| 10 | var isBigInt = require('../');
|
---|
| 11 |
|
---|
| 12 | test('non-BigInt values', function (t) {
|
---|
[79a0317] | 13 | /** @type {(typeof v.primitives[number] | object)[]} */
|
---|
| 14 | var nonBigInts = v.nonBigInts.concat(
|
---|
[d565449] | 15 | Object(true),
|
---|
| 16 | Object(false),
|
---|
[79a0317] | 17 | // @ts-expect-error TS sucks with concat
|
---|
[d565449] | 18 | {},
|
---|
| 19 | [],
|
---|
| 20 | /a/g,
|
---|
| 21 | new Date(),
|
---|
| 22 | function () {},
|
---|
[79a0317] | 23 | NaN,
|
---|
| 24 | v.symbols
|
---|
| 25 | );
|
---|
[d565449] | 26 | t.plan(nonBigInts.length);
|
---|
| 27 | forEach(nonBigInts, function (nonBigInt) {
|
---|
[79a0317] | 28 | t.equal(false, isBigInt(nonBigInt), inspect(nonBigInt) + ' is not a BigInt');
|
---|
[d565449] | 29 | });
|
---|
| 30 | t.end();
|
---|
| 31 | });
|
---|
| 32 |
|
---|
| 33 | test('faked BigInt values', function (t) {
|
---|
| 34 | t.test('real BigInt valueOf', { skip: !hasBigInts }, function (st) {
|
---|
| 35 | var fakeBigInt = { valueOf: function () { return BigInt(42); } };
|
---|
| 36 | st.equal(false, isBigInt(fakeBigInt), 'object with valueOf returning a BigInt is not a BigInt');
|
---|
| 37 | st.end();
|
---|
| 38 | });
|
---|
| 39 |
|
---|
| 40 | t.test('faked @@toStringTag', { skip: !hasBigInts || !hasToStringTag }, function (st) {
|
---|
[79a0317] | 41 | /** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
|
---|
[d565449] | 42 | var fakeBigInt = { valueOf: function () { return BigInt(42); } };
|
---|
| 43 | fakeBigInt[Symbol.toStringTag] = 'BigInt';
|
---|
| 44 | st.equal(false, isBigInt(fakeBigInt), 'object with fake BigInt @@toStringTag and valueOf returning a BigInt is not a BigInt');
|
---|
| 45 |
|
---|
[79a0317] | 46 | /** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
|
---|
[d565449] | 47 | var notSoFakeBigInt = { valueOf: function () { return 42; } };
|
---|
| 48 | notSoFakeBigInt[Symbol.toStringTag] = 'BigInt';
|
---|
| 49 | st.equal(false, isBigInt(notSoFakeBigInt), 'object with fake BigInt @@toStringTag and valueOf not returning a BigInt is not a BigInt');
|
---|
| 50 | st.end();
|
---|
| 51 | });
|
---|
| 52 |
|
---|
| 53 | var fakeBigIntString = { toString: function () { return '42n'; } };
|
---|
| 54 | t.equal(false, isBigInt(fakeBigIntString), 'object with toString returning 42n is not a BigInt');
|
---|
| 55 |
|
---|
| 56 | t.end();
|
---|
| 57 | });
|
---|
| 58 |
|
---|
| 59 | test('BigInt support', { skip: !hasBigInts }, function (t) {
|
---|
[79a0317] | 60 | forEach(v.bigints.concat(Object(BigInt(42))), function (bigInt) {
|
---|
| 61 | t.equal(true, isBigInt(bigInt), inspect(bigInt) + ' is a BigInt');
|
---|
[d565449] | 62 | });
|
---|
| 63 |
|
---|
| 64 | t.end();
|
---|
| 65 | });
|
---|