1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16var a = Symbol ('foo'); 17var b = Symbol ('bar'); 18 19assert (a !== b); 20assert (a === a); 21assert (typeof a === 'symbol'); 22assert (a.toString() == 'Symbol(foo)'); 23assert (Object.prototype.toString.call (a) === "[object Symbol]"); 24assert (JSON.stringify (a) === undefined); 25 26var obj = { c : 10, d : 20}; 27obj[a] = 'EnumerableSymbolProp'; 28assert (obj[a] === 'EnumerableSymbolProp'); 29 30// Symbol properties are not listed via for in 31Object.defineProperty(obj, b, { value : 'NonEnumerableSymbolProp' }); 32 33var counter = 0; 34 35for (var v in obj) { 36 assert (v === 'c' || v === 'd'); 37 counter++; 38} 39 40assert (counter === 2); 41 42var keys = Object.keys (obj); 43assert (keys.length === 2); 44assert (keys[0] === 'c' || keys[0] === 'd'); 45assert (keys[1] === 'd' || keys[1] === 'c'); 46 47var c = Symbol ('bar'); 48var obj2 = {}; 49obj2[b] = 'foo'; 50obj2[c] = 'bar'; 51 52assert (obj2[b] == 'foo'); 53assert (obj2[c] == 'bar'); 54 55try { 56 new Date (Symbol ('2018-11-09')); 57 assert (false); 58} catch (e) { 59 assert (e instanceof TypeError); 60} 61 62try { 63 a + 'append_string'; 64 assert (false); 65} catch (e) { 66 assert (e instanceof TypeError); 67} 68 69assert (Object (a) == a); 70assert (Object (a) !== a); 71 72// Test built-in symbols 73var a = ['hasInstance', 74 'isConcatSpreadable', 75 'iterator', 76 'match', 77 'replace', 78 'search', 79 'species', 80 'split', 81 'toPrimitive', 82 'toStringTag', 83 'unscopables']; 84 85a.forEach (function (e) { 86 assert (Symbol[e].toString() === ('Symbol(Symbol.' + e +')')); 87 assert (typeof Symbol[e] === 'symbol'); 88 /* Check for property descriptor ES 6 19.4.2.2 - 19.4.2.14 */ 89 var desc = Object.getOwnPropertyDescriptor(Symbol, e) 90 assert (desc.writable === false); 91 assert (desc.enumerable === false); 92 assert (desc.configurable === false); 93}); 94 95var obj = {}; 96Object.defineProperty(obj, a, { 'get' : function () {throw new ReferenceError ('foo'); } }); 97Object.defineProperty(obj, b, { value : 5 }); 98assert (obj[b] === 5); 99 100try { 101 obj[a]; 102 assert (false); 103} catch (e) { 104 assert (e instanceof ReferenceError); 105 assert (e.message === 'foo'); 106} 107 108var descriptor = Object.getOwnPropertyDescriptor(obj, b); 109 110assert (descriptor.configurable === false); 111assert (descriptor.enumerable === false); 112assert (descriptor.writable === false); 113assert (descriptor.value === 5); 114 115var foo = Symbol ('foo'); 116assert (foo[Symbol.toStringTag] === 'Symbol'); 117 118// Test same descriptions 119var symA = Symbol ('foobar'); 120var symB = Symbol ('foobar'); 121assert (symA !== symB); 122assert (symA != symB); 123 124var obj = { foobar : 55 }; 125obj[symA] = 77; 126assert (obj["foobar"] !== obj[symA]); 127assert (obj["foobar"] != obj[symA]); 128