1 /* 2 * Copyright (c) 2016, 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-8147558: Add support for ES6 collections 26 * 27 * @test 28 * @run 29 * @option --language=es6 30 */ 31 32 function assertThrows(src, error) { 33 try { 34 eval(src); 35 Assert.fail("No error, expected " + error); 36 } catch (e) { 37 if (!(e instanceof error)) { 38 Assert.fail("Wrong error, expected " + error + " but got " + e); 39 } 40 } 41 } 42 43 // WeakMap constructor and prototype 44 45 var desc = Object.getOwnPropertyDescriptor(this, "WeakMap"); 46 Assert.assertEquals(desc.writable, true); 47 Assert.assertEquals(desc.configurable, true); 48 Assert.assertEquals(desc.enumerable, false); 49 50 Assert.assertTrue(Object.getPrototypeOf(new WeakMap()) === WeakMap.prototype); 51 Assert.assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype); 52 Assert.assertTrue(Object.getPrototypeOf(new WeakMap("")) === WeakMap.prototype); 53 Assert.assertTrue(Object.getPrototypeOf(new WeakMap([])) === WeakMap.prototype); 54 55 assertThrows("WeakMap()", TypeError); 56 assertThrows("new WeakMap(3)", TypeError); 57 assertThrows("new WeakMap({})", TypeError); 58 assertThrows("new WeakMap([['a', {}]])", TypeError); 59 assertThrows("new WeakMap([[3, {}]])", TypeError); 60 assertThrows("new WeakMap([[true, {}]])", TypeError); 61 assertThrows("new WeakMap([[Symbol.iterator, {}]])", TypeError); 62 63 assertThrows("WeakMap.prototype.set.apply({}, [{}, {}])", TypeError); 64 assertThrows("WeakMap.prototype.has.apply(3, [{}])", TypeError); 65 assertThrows("WeakMap.prototype.delete.apply('', [3])", TypeError); 66 67 // WeakMap methods 68 69 let values = [[{}, 1], [{}, 2], [{}, 3]]; 70 let m = new WeakMap(values); 71 72 for (let i = 0; i < values.length; i++) { 73 Assert.assertTrue(m.has(values[i][0]) === true); 74 Assert.assertTrue(m.get(values[i][0]) === values[i][1]); 75 Assert.assertTrue(m.delete(values[i][0]) === true); 76 Assert.assertTrue(m.has(values[i][0]) === false); 77 } 78 79 values.forEach(function(v) { 80 Assert.assertTrue(m.set(v[0], v[1]) === m); 81 }); 82 83 for (let i = 0; i < values.length; i++) { 84 Assert.assertTrue(m.has(values[i][0]) === true); 85 Assert.assertTrue(m.get(values[i][0]) === values[i][1]); 86 Assert.assertTrue(m.delete(values[i][0]) === true); 87 Assert.assertTrue(m.has(values[i][0]) === false); 88 } 89 90 // Primitive keys 91 92 assertThrows("m.set('a', {})", TypeError); 93 assertThrows("m.set(3, {})", TypeError); 94 assertThrows("m.set(false, {})", TypeError); 95 assertThrows("m.set(Symbol.iterator, {})", TypeError); 96 97 Assert.assertTrue(m.has('a') === false); 98 Assert.assertTrue(m.delete(3) === false); 99 Assert.assertTrue(m.get(Symbol.iterator) === undefined); 100 Assert.assertTrue(m.get(true) === undefined);