| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var test = require('tape');
|
|---|
| 4 | var debug = require('object-inspect');
|
|---|
| 5 | var forEach = require('for-each');
|
|---|
| 6 | var hasOwn = require('hasown');
|
|---|
| 7 | var v = require('es-value-fixtures');
|
|---|
| 8 |
|
|---|
| 9 | var getSymbolDescription = require('../');
|
|---|
| 10 | var getInferredName = require('../getInferredName');
|
|---|
| 11 |
|
|---|
| 12 | test('getSymbolDescription', function (t) {
|
|---|
| 13 | t.test('no symbols', { skip: v.hasSymbols }, function (st) {
|
|---|
| 14 | st['throws'](
|
|---|
| 15 | // @ts-expect-error
|
|---|
| 16 | getSymbolDescription,
|
|---|
| 17 | SyntaxError,
|
|---|
| 18 | 'requires Symbol support'
|
|---|
| 19 | );
|
|---|
| 20 |
|
|---|
| 21 | st.end();
|
|---|
| 22 | });
|
|---|
| 23 |
|
|---|
| 24 | forEach([].concat(
|
|---|
| 25 | // @ts-expect-error TS sucks with concat
|
|---|
| 26 | v.nonSymbolPrimitives,
|
|---|
| 27 | v.objects
|
|---|
| 28 | ), function (nonSymbol) {
|
|---|
| 29 | t['throws'](
|
|---|
| 30 | function () { getSymbolDescription(nonSymbol); },
|
|---|
| 31 | v.hasSymbols ? TypeError : SyntaxError,
|
|---|
| 32 | debug(nonSymbol) + ' is not a Symbol'
|
|---|
| 33 | );
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | t.test('with symbols', { skip: !v.hasSymbols }, function (st) {
|
|---|
| 37 | forEach(
|
|---|
| 38 | // eslint-disable-next-line no-extra-parens
|
|---|
| 39 | /** @type {[symbol, undefined | string][]} */ ([
|
|---|
| 40 | [Symbol(), undefined],
|
|---|
| 41 | [Symbol(undefined), undefined],
|
|---|
| 42 | // @ts-expect-error
|
|---|
| 43 | [Symbol(null), 'null'],
|
|---|
| 44 | [Symbol.iterator, 'Symbol.iterator'],
|
|---|
| 45 | [Symbol('foo'), 'foo']
|
|---|
| 46 | ]),
|
|---|
| 47 | function (pair) {
|
|---|
| 48 | var sym = pair[0];
|
|---|
| 49 | var desc = pair[1];
|
|---|
| 50 | st.equal(getSymbolDescription(sym), desc, debug(sym) + ' description is ' + debug(desc));
|
|---|
| 51 | }
|
|---|
| 52 | );
|
|---|
| 53 |
|
|---|
| 54 | st.test('only possible when inference or native `Symbol.prototype.description` is supported', {
|
|---|
| 55 | skip: !getInferredName && !hasOwn(Symbol.prototype, 'description')
|
|---|
| 56 | }, function (s2t) {
|
|---|
| 57 | s2t.equal(getSymbolDescription(Symbol('')), '', 'Symbol("") description is ""');
|
|---|
| 58 |
|
|---|
| 59 | s2t.end();
|
|---|
| 60 | });
|
|---|
| 61 |
|
|---|
| 62 | st.test('only possible when global symbols are supported', {
|
|---|
| 63 | skip: !hasOwn(Symbol, 'for') || !hasOwn(Symbol, 'keyFor')
|
|---|
| 64 | }, function (s2t) {
|
|---|
| 65 | // eslint-disable-next-line no-restricted-properties
|
|---|
| 66 | s2t.equal(getSymbolDescription(Symbol['for']('')), '', 'Symbol.for("") description is ""');
|
|---|
| 67 | s2t.end();
|
|---|
| 68 | });
|
|---|
| 69 |
|
|---|
| 70 | st.end();
|
|---|
| 71 | });
|
|---|
| 72 |
|
|---|
| 73 | t.end();
|
|---|
| 74 | });
|
|---|