index.js 2.0 KB

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