[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var test = require('tape');
|
---|
| 4 | var inspect = require('object-inspect');
|
---|
| 5 | var hasBigInts = require('has-bigints')();
|
---|
| 6 | var hasSymbols = require('has-symbols')();
|
---|
| 7 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
| 8 |
|
---|
| 9 | var isBigInt = require('../');
|
---|
| 10 |
|
---|
| 11 | var debug = function (v, m) { return inspect(v) + ' ' + m; };
|
---|
| 12 |
|
---|
| 13 | var forEach = function (arr, func) {
|
---|
| 14 | var i;
|
---|
| 15 | for (i = 0; i < arr.length; ++i) {
|
---|
| 16 | func(arr[i], i, arr);
|
---|
| 17 | }
|
---|
| 18 | };
|
---|
| 19 |
|
---|
| 20 | test('non-BigInt values', function (t) {
|
---|
| 21 | var nonBigInts = [
|
---|
| 22 | true,
|
---|
| 23 | false,
|
---|
| 24 | Object(true),
|
---|
| 25 | Object(false),
|
---|
| 26 | null,
|
---|
| 27 | undefined,
|
---|
| 28 | {},
|
---|
| 29 | [],
|
---|
| 30 | /a/g,
|
---|
| 31 | 'string',
|
---|
| 32 | 42,
|
---|
| 33 | new Date(),
|
---|
| 34 | function () {},
|
---|
| 35 | NaN
|
---|
| 36 | ];
|
---|
| 37 | if (hasSymbols) {
|
---|
| 38 | nonBigInts.push(Symbol.iterator, Symbol('foo'));
|
---|
| 39 | }
|
---|
| 40 | t.plan(nonBigInts.length);
|
---|
| 41 | forEach(nonBigInts, function (nonBigInt) {
|
---|
| 42 | t.equal(false, isBigInt(nonBigInt), debug(nonBigInt, 'is not a BigInt'));
|
---|
| 43 | });
|
---|
| 44 | t.end();
|
---|
| 45 | });
|
---|
| 46 |
|
---|
| 47 | test('faked BigInt values', function (t) {
|
---|
| 48 | t.test('real BigInt valueOf', { skip: !hasBigInts }, function (st) {
|
---|
| 49 | var fakeBigInt = { valueOf: function () { return BigInt(42); } };
|
---|
| 50 | st.equal(false, isBigInt(fakeBigInt), 'object with valueOf returning a BigInt is not a BigInt');
|
---|
| 51 | st.end();
|
---|
| 52 | });
|
---|
| 53 |
|
---|
| 54 | t.test('faked @@toStringTag', { skip: !hasBigInts || !hasToStringTag }, function (st) {
|
---|
| 55 | var fakeBigInt = { valueOf: function () { return BigInt(42); } };
|
---|
| 56 | fakeBigInt[Symbol.toStringTag] = 'BigInt';
|
---|
| 57 | st.equal(false, isBigInt(fakeBigInt), 'object with fake BigInt @@toStringTag and valueOf returning a BigInt is not a BigInt');
|
---|
| 58 |
|
---|
| 59 | var notSoFakeBigInt = { valueOf: function () { return 42; } };
|
---|
| 60 | notSoFakeBigInt[Symbol.toStringTag] = 'BigInt';
|
---|
| 61 | st.equal(false, isBigInt(notSoFakeBigInt), 'object with fake BigInt @@toStringTag and valueOf not returning a BigInt is not a BigInt');
|
---|
| 62 | st.end();
|
---|
| 63 | });
|
---|
| 64 |
|
---|
| 65 | var fakeBigIntString = { toString: function () { return '42n'; } };
|
---|
| 66 | t.equal(false, isBigInt(fakeBigIntString), 'object with toString returning 42n is not a BigInt');
|
---|
| 67 |
|
---|
| 68 | t.end();
|
---|
| 69 | });
|
---|
| 70 |
|
---|
| 71 | test('BigInt support', { skip: !hasBigInts }, function (t) {
|
---|
| 72 | forEach([
|
---|
| 73 | Function('return 42n')(), // eslint-disable-line no-new-func
|
---|
| 74 | BigInt(42),
|
---|
| 75 | Object(BigInt(42))
|
---|
| 76 | ], function (bigInt) {
|
---|
| 77 | t.equal(true, isBigInt(bigInt), debug(bigInt, 'is a BigInt'));
|
---|
| 78 | });
|
---|
| 79 |
|
---|
| 80 | t.end();
|
---|
| 81 | });
|
---|