MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, summary } from './helpers.js';
2
3console.log('Symbol Tests\n');
4
5const sym1 = Symbol('test');
6test('symbol typeof', typeof sym1, 'symbol');
7
8const sym2 = Symbol('test');
9test('symbols are unique', sym1 !== sym2, true);
10
11test('symbol description', sym1.description, 'test');
12
13const sym3 = Symbol();
14test('symbol without description', sym3.description, undefined);
15
16const obj = {};
17const symKey = Symbol('key');
18obj[symKey] = 'value';
19test('symbol as key', obj[symKey], 'value');
20
21const symFor1 = Symbol.for('shared');
22const symFor2 = Symbol.for('shared');
23test('Symbol.for same', symFor1 === symFor2, true);
24
25test('Symbol.keyFor', Symbol.keyFor(symFor1), 'shared');
26test('Symbol.keyFor local', Symbol.keyFor(sym1), undefined);
27
28test('Symbol.iterator exists', typeof Symbol.iterator, 'symbol');
29test('Symbol.toStringTag exists', typeof Symbol.toStringTag, 'symbol');
30test('Symbol.hasInstance exists', typeof Symbol.hasInstance, 'symbol');
31
32const custom = { [Symbol.toStringTag]: 'MyCustomType' };
33test('Symbol.toStringTag custom', Object.prototype.toString.call(custom), '[object MyCustomType]');
34
35summary();