You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.5 KiB

4 years ago
  1. 'use strict';
  2. var test = require('tape');
  3. var isSymbol = require('../index');
  4. var forEach = function (arr, func) {
  5. var i;
  6. for (i = 0; i < arr.length; ++i) {
  7. func(arr[i], i, arr);
  8. }
  9. };
  10. var hasSymbols = require('has-symbols')();
  11. var inspect = require('object-inspect');
  12. var debug = function (v, m) { return inspect(v) + ' ' + m; };
  13. test('non-symbol values', function (t) {
  14. var nonSymbols = [
  15. true,
  16. false,
  17. Object(true),
  18. Object(false),
  19. null,
  20. undefined,
  21. {},
  22. [],
  23. /a/g,
  24. 'string',
  25. 42,
  26. new Date(),
  27. function () {},
  28. NaN
  29. ];
  30. t.plan(nonSymbols.length);
  31. forEach(nonSymbols, function (nonSymbol) {
  32. t.equal(false, isSymbol(nonSymbol), debug(nonSymbol, 'is not a symbol'));
  33. });
  34. t.end();
  35. });
  36. test('faked symbol values', function (t) {
  37. t.test('real symbol valueOf', { skip: !hasSymbols }, function (st) {
  38. var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
  39. st.equal(false, isSymbol(fakeSymbol), 'object with valueOf returning a symbol is not a symbol');
  40. st.end();
  41. });
  42. t.test('faked @@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (st) {
  43. var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
  44. fakeSymbol[Symbol.toStringTag] = 'Symbol';
  45. st.equal(false, isSymbol(fakeSymbol), 'object with fake Symbol @@toStringTag and valueOf returning a symbol is not a symbol');
  46. var notSoFakeSymbol = { valueOf: function () { return 42; } };
  47. notSoFakeSymbol[Symbol.toStringTag] = 'Symbol';
  48. st.equal(false, isSymbol(notSoFakeSymbol), 'object with fake Symbol @@toStringTag and valueOf not returning a symbol is not a symbol');
  49. st.end();
  50. });
  51. var fakeSymbolString = { toString: function () { return 'Symbol(foo)'; } };
  52. t.equal(false, isSymbol(fakeSymbolString), 'object with toString returning Symbol(foo) is not a symbol');
  53. t.end();
  54. });
  55. test('Symbol support', { skip: !hasSymbols }, function (t) {
  56. t.test('well-known Symbols', function (st) {
  57. var isWellKnown = function filterer(name) {
  58. return name !== 'for' && name !== 'keyFor' && !(name in filterer);
  59. };
  60. var wellKnownSymbols = Object.getOwnPropertyNames(Symbol).filter(isWellKnown);
  61. wellKnownSymbols.forEach(function (name) {
  62. var sym = Symbol[name];
  63. st.equal(true, isSymbol(sym), debug(sym, ' is a symbol'));
  64. });
  65. st.end();
  66. });
  67. t.test('user-created symbols', function (st) {
  68. var symbols = [
  69. Symbol(),
  70. Symbol('foo'),
  71. Symbol['for']('foo'),
  72. Object(Symbol('object'))
  73. ];
  74. symbols.forEach(function (sym) {
  75. st.equal(true, isSymbol(sym), debug(sym, ' is a symbol'));
  76. });
  77. st.end();
  78. });
  79. t.end();
  80. });