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 16try { 17 Symbol.for (Symbol('foo')); 18 assert (false); 19} catch (e) { 20 assert (e instanceof TypeError) 21} 22 23var a = Symbol.for ('foo'); 24var b = Symbol.for ('foo'); 25 26assert (a === b); 27assert (a == b); 28 29assert (Symbol.keyFor (a) === 'foo'); 30assert (Symbol.keyFor (b) === 'foo'); 31 32// Test non-string arguments 33var c = Symbol.for (5); 34var d = Symbol.for (5.58); 35var e = Symbol.for ({}); 36 37assert (Symbol.keyFor (c) === '5'); 38assert (Symbol.keyFor (d) === '5.58'); 39assert (Symbol.keyFor (e) === '[object Object]'); 40 41// Test global symbol table 42var array = []; 43for (var i = 0; i < 15; i++) { 44 array[i] = Symbol.for ('foo' + i); 45 46 for (var j = 0; j < i; j++) { 47 assert (array[j] !== array[i]); 48 } 49} 50 51try { 52 Symbol.keyFor ('NonSymbolValue'); 53 assert (false); 54} catch (e) { 55 assert (e instanceof TypeError); 56} 57 58for (var i = 0; i < 15; i++) { 59 assert (Symbol.keyFor (array[i]) === ('foo' + i)); 60} 61