getSymbolDescription.js 1.8 KB

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