[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
[79a0317] | 3 | /** @type {(t: import('tape').Test) => false | void} */
|
---|
[d565449] | 4 | // eslint-disable-next-line consistent-return
|
---|
| 5 | module.exports = function runSymbolTests(t) {
|
---|
| 6 | t.equal(typeof Symbol, 'function', 'global Symbol is a function');
|
---|
| 7 |
|
---|
| 8 | if (typeof Symbol !== 'function') { return false; }
|
---|
| 9 |
|
---|
| 10 | t.notEqual(Symbol(), Symbol(), 'two symbols are not equal');
|
---|
| 11 |
|
---|
| 12 | /*
|
---|
| 13 | t.equal(
|
---|
| 14 | Symbol.prototype.toString.call(Symbol('foo')),
|
---|
| 15 | Symbol.prototype.toString.call(Symbol('foo')),
|
---|
| 16 | 'two symbols with the same description stringify the same'
|
---|
| 17 | );
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | /*
|
---|
| 21 | var foo = Symbol('foo');
|
---|
| 22 |
|
---|
| 23 | t.notEqual(
|
---|
| 24 | String(foo),
|
---|
| 25 | String(Symbol('bar')),
|
---|
| 26 | 'two symbols with different descriptions do not stringify the same'
|
---|
| 27 | );
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | t.equal(typeof Symbol.prototype.toString, 'function', 'Symbol#toString is a function');
|
---|
| 31 | // t.equal(String(foo), Symbol.prototype.toString.call(foo), 'Symbol#toString equals String of the same symbol');
|
---|
| 32 |
|
---|
| 33 | t.equal(typeof Object.getOwnPropertySymbols, 'function', 'Object.getOwnPropertySymbols is a function');
|
---|
| 34 |
|
---|
[79a0317] | 35 | /** @type {{ [k in symbol]?: unknown }} */
|
---|
[d565449] | 36 | var obj = {};
|
---|
| 37 | var sym = Symbol('test');
|
---|
| 38 | var symObj = Object(sym);
|
---|
| 39 | t.notEqual(typeof sym, 'string', 'Symbol is not a string');
|
---|
| 40 | t.equal(Object.prototype.toString.call(sym), '[object Symbol]', 'symbol primitive Object#toStrings properly');
|
---|
| 41 | t.equal(Object.prototype.toString.call(symObj), '[object Symbol]', 'symbol primitive Object#toStrings properly');
|
---|
| 42 |
|
---|
| 43 | var symVal = 42;
|
---|
| 44 | obj[sym] = symVal;
|
---|
[79a0317] | 45 | // eslint-disable-next-line no-restricted-syntax, no-unused-vars
|
---|
| 46 | for (var _ in obj) { t.fail('symbol property key was found in for..in of object'); }
|
---|
[d565449] | 47 |
|
---|
| 48 | t.deepEqual(Object.keys(obj), [], 'no enumerable own keys on symbol-valued object');
|
---|
| 49 | t.deepEqual(Object.getOwnPropertyNames(obj), [], 'no own names on symbol-valued object');
|
---|
| 50 | t.deepEqual(Object.getOwnPropertySymbols(obj), [sym], 'one own symbol on symbol-valued object');
|
---|
| 51 | t.equal(Object.prototype.propertyIsEnumerable.call(obj, sym), true, 'symbol is enumerable');
|
---|
| 52 | t.deepEqual(Object.getOwnPropertyDescriptor(obj, sym), {
|
---|
| 53 | configurable: true,
|
---|
| 54 | enumerable: true,
|
---|
| 55 | value: 42,
|
---|
| 56 | writable: true
|
---|
| 57 | }, 'property descriptor is correct');
|
---|
| 58 | };
|
---|