source: imaps-frontend/node_modules/is-bigint/test/index.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[d565449]1'use strict';
2
3var test = require('tape');
4var inspect = require('object-inspect');
5var hasBigInts = require('has-bigints')();
6var hasSymbols = require('has-symbols')();
7var hasToStringTag = require('has-tostringtag/shams')();
8
9var isBigInt = require('../');
10
11var debug = function (v, m) { return inspect(v) + ' ' + m; };
12
13var forEach = function (arr, func) {
14 var i;
15 for (i = 0; i < arr.length; ++i) {
16 func(arr[i], i, arr);
17 }
18};
19
20test('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
47test('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
71test('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});
Note: See TracBrowser for help on using the repository browser.