1 | 'use strict';
|
---|
2 |
|
---|
3 | var test = require('tape');
|
---|
4 | var forEach = require('for-each');
|
---|
5 | var v = require('es-value-fixtures');
|
---|
6 |
|
---|
7 | var isSymbol = require('../index');
|
---|
8 |
|
---|
9 | var hasSymbols = require('has-symbols')();
|
---|
10 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
11 | var inspect = require('object-inspect');
|
---|
12 |
|
---|
13 | test('non-symbol values', function (t) {
|
---|
14 | var nonSymbols = v.nonSymbolPrimitives.concat(
|
---|
15 | Object(true),
|
---|
16 | Object(false),
|
---|
17 | // @ts-expect-error TS sucks with concat
|
---|
18 | {},
|
---|
19 | [],
|
---|
20 | /a/g,
|
---|
21 | new Date(),
|
---|
22 | function () {},
|
---|
23 | NaN
|
---|
24 | );
|
---|
25 | t.plan(nonSymbols.length);
|
---|
26 | forEach(nonSymbols, function (nonSymbol) {
|
---|
27 | t.equal(isSymbol(nonSymbol), false, inspect(nonSymbol) + ' is not a symbol');
|
---|
28 | });
|
---|
29 | t.end();
|
---|
30 | });
|
---|
31 |
|
---|
32 | test('faked symbol values', function (t) {
|
---|
33 | t.test('real symbol valueOf', { skip: !hasSymbols }, function (st) {
|
---|
34 | var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
|
---|
35 | st.equal(isSymbol(fakeSymbol), false, 'object with valueOf returning a symbol is not a symbol');
|
---|
36 | st.end();
|
---|
37 | });
|
---|
38 |
|
---|
39 | t.test('faked @@toStringTag', { skip: !hasToStringTag }, function (st) {
|
---|
40 | /** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
|
---|
41 | var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
|
---|
42 | fakeSymbol[Symbol.toStringTag] = 'Symbol';
|
---|
43 | st.equal(isSymbol(fakeSymbol), false, 'object with fake Symbol @@toStringTag and valueOf returning a symbol is not a symbol');
|
---|
44 |
|
---|
45 | /** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
|
---|
46 | var notSoFakeSymbol = { valueOf: function () { return 42; } };
|
---|
47 | notSoFakeSymbol[Symbol.toStringTag] = 'Symbol';
|
---|
48 | st.equal(isSymbol(notSoFakeSymbol), false, 'object with fake Symbol @@toStringTag and valueOf not returning a symbol is not a symbol');
|
---|
49 | st.end();
|
---|
50 | });
|
---|
51 |
|
---|
52 | var fakeSymbolString = { toString: function () { return 'Symbol(foo)'; } };
|
---|
53 | t.equal(isSymbol(fakeSymbolString), false, 'object with toString returning Symbol(foo) is not a symbol');
|
---|
54 |
|
---|
55 | t.end();
|
---|
56 | });
|
---|
57 |
|
---|
58 | test('Symbol support', { skip: !hasSymbols }, function (t) {
|
---|
59 | t.test('well-known Symbols', function (st) {
|
---|
60 | /** @type {(name: string) => name is Exclude<keyof SymbolConstructor, 'for' | 'keyFor'>} */
|
---|
61 | var isWellKnown = function filterer(name) {
|
---|
62 | return name !== 'for' && name !== 'keyFor' && !(name in filterer);
|
---|
63 | };
|
---|
64 | var wellKnownSymbols = Object.getOwnPropertyNames(Symbol).filter(isWellKnown);
|
---|
65 | wellKnownSymbols.forEach(function (name) {
|
---|
66 | // eslint-disable-next-line no-extra-parens
|
---|
67 | var sym = Symbol[/** @type {keyof SymbolConstructor} */ (name)];
|
---|
68 | st.equal(isSymbol(sym), true, inspect(sym) + ' is a symbol');
|
---|
69 | });
|
---|
70 | st.end();
|
---|
71 | });
|
---|
72 |
|
---|
73 | t.test('user-created symbols', function (st) {
|
---|
74 | var symbols = v.symbols.concat(
|
---|
75 | Symbol(),
|
---|
76 | Symbol('foo'),
|
---|
77 | Symbol['for']('foo'),
|
---|
78 | Object(Symbol('object'))
|
---|
79 | );
|
---|
80 | symbols.forEach(function (sym) {
|
---|
81 | st.equal(isSymbol(sym), true, inspect(sym) + ' is a symbol');
|
---|
82 | });
|
---|
83 | st.end();
|
---|
84 | });
|
---|
85 |
|
---|
86 | t.end();
|
---|
87 | });
|
---|
88 |
|
---|