symbols.js revision 1594:9ab6b645c428
1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * JDK-8141702: Add support for Symbol property keys 26 * 27 * @test 28 * @run 29 * @option --language=es6 30 */ 31 32Assert.assertTrue(typeof Symbol === 'function'); 33Assert.assertTrue(typeof Symbol() === 'symbol'); 34 35Assert.assertTrue(Symbol().toString() === 'Symbol()'); 36Assert.assertTrue(Symbol('foo').toString() === 'Symbol(foo)'); 37Assert.assertTrue(Symbol(1).toString() === 'Symbol(1)'); 38Assert.assertTrue(Symbol(true).toString() === 'Symbol(true)'); 39Assert.assertTrue(Symbol([1, 2, 3]).toString() === 'Symbol(1,2,3)'); 40Assert.assertTrue(Symbol(null).toString() === 'Symbol(null)'); 41Assert.assertTrue(Symbol(undefined).toString() === 'Symbol()'); 42 43const s1 = Symbol(); 44const s2 = Symbol("s2"); 45Assert.assertFalse(s1 instanceof Symbol); // not an object 46 47let obj = {}; 48obj['foo'] = 'foo'; 49obj[s1] = s1; 50obj['bar'] = 'bar'; 51obj[1] = 1; 52obj[s2] = s2; 53 54Assert.assertTrue(obj['foo'] === 'foo'); 55Assert.assertTrue(obj[s1] === s1); 56Assert.assertTrue(obj['bar'] === 'bar'); 57Assert.assertTrue(obj[1] === 1); 58Assert.assertTrue(obj[s2] === s2); 59 60const expectedNames = ['1', 'foo', 'bar']; 61const expectedSymbols = [s1, s2]; 62const actualNames = Object.getOwnPropertyNames(obj); 63let actualSymbols = Object.getOwnPropertySymbols(obj); 64Assert.assertTrue(expectedNames.length == actualNames.length); 65Assert.assertTrue(expectedSymbols.length == actualSymbols.length); 66 67for (let key in expectedNames) { 68 Assert.assertTrue(expectedNames[key] === actualNames[key]); 69} 70for (let key in expectedSymbols) { 71 Assert.assertTrue(expectedSymbols[key] === actualSymbols[key]); 72} 73 74// Delete 75Assert.assertTrue(delete obj[s1]); 76Assert.assertTrue(Object.getOwnPropertySymbols(obj).length === 1); 77Assert.assertTrue(Object.getOwnPropertySymbols(obj)[0] === s2); 78 79// Object.defineProperty 80Object.defineProperty(obj, s1, {value : 'hello'}); 81Assert.assertTrue(obj[s1] === 'hello'); 82actualSymbols = Object.getOwnPropertySymbols(obj); 83Assert.assertTrue(Object.getOwnPropertySymbols(obj).length === 2); 84Assert.assertTrue(Object.getOwnPropertySymbols(obj)[1] === s1); 85 86// Symbol called as constructor 87try { 88 new Symbol(); 89 Assert.fail("Symbol invoked as constructor"); 90} catch (e) { 91 if (e.name !== "TypeError" || e.message !== "Symbol is not a constructor.") { 92 Assert.fail("Unexpected error: " + e); 93 } 94} 95 96// Implicit conversion to string or number should throw 97try { 98 ' ' + s1; 99 Assert.fail("Symbol converted to string"); 100} catch (e) { 101 if (e.name !== "TypeError" || e.message !== "Can not convert Symbol value to string.") { 102 Assert.fail("Unexpected error: " + e); 103 } 104} 105 106try { 107 4 * s1; 108 Assert.fail("Symbol converted to number"); 109} catch (e) { 110 if (e.name !== "TypeError" || e.message !== "Can not convert Symbol value to number.") { 111 Assert.fail("Unexpected error: " + e); 112 } 113} 114 115// Symbol.for and Symbol.keyFor 116 117const uncached = Symbol('foo'); 118const cached = Symbol.for('foo'); 119 120Assert.assertTrue(uncached !== cached); 121Assert.assertTrue(Symbol.keyFor(uncached) === undefined); 122Assert.assertTrue(Symbol.keyFor(cached) === 'foo'); 123Assert.assertTrue(cached === Symbol.for('foo')); 124Assert.assertTrue(cached === Symbol.for('f' + 'oo')); 125 126// JDK-8147008: Make sure symbols are handled by primitive linker 127Symbol.prototype.foo = 123; 128Symbol.prototype[s2] = s2; 129Assert.assertEquals(s1.foo, 123); 130Assert.assertEquals(s2[s2], s2); 131 132// Object wrapper 133 134const o = Object(s1); 135obj = {}; 136obj[s1] = "s1"; 137Assert.assertTrue(o == s1); 138Assert.assertTrue(o !== s1); 139Assert.assertTrue(typeof o === 'object'); 140Assert.assertTrue(o instanceof Symbol); 141Assert.assertTrue(obj[o] == 's1'); 142Assert.assertTrue(o in obj); 143Assert.assertEquals(o.foo, 123); 144Assert.assertEquals(o[s2], s2); 145 146// various non-strict comparisons that should fail 147 148Assert.assertFalse(0 == Symbol()); 149Assert.assertFalse(1 == Symbol(1)); 150Assert.assertFalse(null == Symbol()); 151Assert.assertFalse(undefined == Symbol); 152Assert.assertFalse('Symbol()' == Symbol()); 153Assert.assertFalse('Symbol(foo)' == Symbol('foo')); 154 155