[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var test = require('tape');
|
---|
[79a0317] | 4 | var forEach = require('for-each');
|
---|
| 5 | var v = require('es-value-fixtures');
|
---|
[d565449] | 6 |
|
---|
[79a0317] | 7 | var isSymbol = require('../index');
|
---|
[d565449] | 8 |
|
---|
| 9 | var hasSymbols = require('has-symbols')();
|
---|
[79a0317] | 10 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
[d565449] | 11 | var inspect = require('object-inspect');
|
---|
| 12 |
|
---|
| 13 | test('non-symbol values', function (t) {
|
---|
[79a0317] | 14 | var nonSymbols = v.nonSymbolPrimitives.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 () {},
|
---|
| 23 | NaN
|
---|
[79a0317] | 24 | );
|
---|
[d565449] | 25 | t.plan(nonSymbols.length);
|
---|
| 26 | forEach(nonSymbols, function (nonSymbol) {
|
---|
[79a0317] | 27 | t.equal(isSymbol(nonSymbol), false, inspect(nonSymbol) + ' is not a symbol');
|
---|
[d565449] | 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'); } };
|
---|
[79a0317] | 35 | st.equal(isSymbol(fakeSymbol), false, 'object with valueOf returning a symbol is not a symbol');
|
---|
[d565449] | 36 | st.end();
|
---|
| 37 | });
|
---|
| 38 |
|
---|
[79a0317] | 39 | t.test('faked @@toStringTag', { skip: !hasToStringTag }, function (st) {
|
---|
| 40 | /** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
|
---|
[d565449] | 41 | var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
|
---|
| 42 | fakeSymbol[Symbol.toStringTag] = 'Symbol';
|
---|
[79a0317] | 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 }} */
|
---|
[d565449] | 46 | var notSoFakeSymbol = { valueOf: function () { return 42; } };
|
---|
| 47 | notSoFakeSymbol[Symbol.toStringTag] = 'Symbol';
|
---|
[79a0317] | 48 | st.equal(isSymbol(notSoFakeSymbol), false, 'object with fake Symbol @@toStringTag and valueOf not returning a symbol is not a symbol');
|
---|
[d565449] | 49 | st.end();
|
---|
| 50 | });
|
---|
| 51 |
|
---|
| 52 | var fakeSymbolString = { toString: function () { return 'Symbol(foo)'; } };
|
---|
[79a0317] | 53 | t.equal(isSymbol(fakeSymbolString), false, 'object with toString returning Symbol(foo) is not a symbol');
|
---|
[d565449] | 54 |
|
---|
| 55 | t.end();
|
---|
| 56 | });
|
---|
| 57 |
|
---|
| 58 | test('Symbol support', { skip: !hasSymbols }, function (t) {
|
---|
| 59 | t.test('well-known Symbols', function (st) {
|
---|
[79a0317] | 60 | /** @type {(name: string) => name is Exclude<keyof SymbolConstructor, 'for' | 'keyFor'>} */
|
---|
[d565449] | 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) {
|
---|
[79a0317] | 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');
|
---|
[d565449] | 69 | });
|
---|
| 70 | st.end();
|
---|
| 71 | });
|
---|
| 72 |
|
---|
| 73 | t.test('user-created symbols', function (st) {
|
---|
[79a0317] | 74 | var symbols = v.symbols.concat(
|
---|
[d565449] | 75 | Symbol(),
|
---|
| 76 | Symbol('foo'),
|
---|
| 77 | Symbol['for']('foo'),
|
---|
| 78 | Object(Symbol('object'))
|
---|
[79a0317] | 79 | );
|
---|
[d565449] | 80 | symbols.forEach(function (sym) {
|
---|
[79a0317] | 81 | st.equal(isSymbol(sym), true, inspect(sym) + ' is a symbol');
|
---|
[d565449] | 82 | });
|
---|
| 83 | st.end();
|
---|
| 84 | });
|
---|
| 85 |
|
---|
| 86 | t.end();
|
---|
| 87 | });
|
---|
| 88 |
|
---|