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 // WeakSet constructor and prototype
  44 
  45 var desc = Object.getOwnPropertyDescriptor(this, "WeakSet");
  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 WeakSet()) === WeakSet.prototype);
  51 Assert.assertTrue(Object.getPrototypeOf(WeakSet.prototype) === Object.prototype);
  52 Assert.assertTrue(Object.getPrototypeOf(new WeakSet("")) === WeakSet.prototype);
  53 Assert.assertTrue(Object.getPrototypeOf(new WeakSet([])) === WeakSet.prototype);
  54 
  55 assertThrows("WeakSet()", TypeError);
  56 assertThrows("new WeakSet(3)", TypeError);
  57 assertThrows("new WeakSet({})", TypeError);
  58 assertThrows("new WeakSet(['a'])", TypeError);
  59 assertThrows("new WeakSet([3])", TypeError);
  60 assertThrows("new WeakSet([true])", TypeError);
  61 assertThrows("new WeakSet([Symbol.iterator])", TypeError);
  62 
  63 assertThrows("WeakSet.prototype.add.apply({}, [''])", TypeError);
  64 assertThrows("WeakSet.prototype.has.apply(3, [''])", TypeError);
  65 assertThrows("WeakSet.prototype.delete.apply('', [3])", TypeError);
  66 
  67 // WeakSet methods
  68 
  69 let values = [{}, {}, {}];
  70 let s = new WeakSet(values);
  71 
  72 for (let i = 0; i < values.length; i++) {
  73     Assert.assertTrue(s.has(values[i]) === true);
  74     Assert.assertTrue(s.delete(values[i]) === true);
  75     Assert.assertTrue(s.has(values[i]) === false);
  76 }
  77 
  78 values.forEach(function(v) {
  79     Assert.assertTrue(s.add(v) === s);
  80 });
  81 
  82 for (let i = 0; i < values.length; i++) {
  83     Assert.assertTrue(s.has(values[i]) === true);
  84     Assert.assertTrue(s.delete(values[i]) === true);
  85     Assert.assertTrue(s.has(values[i]) === false);
  86 }
  87 
  88 // Primitive keys
  89 
  90 assertThrows("s.add('a')", TypeError);
  91 assertThrows("s.add(3)", TypeError);
  92 assertThrows("s.add(false)", TypeError);
  93 assertThrows("s.add(Symbol.iterator)", TypeError);
  94 
  95 Assert.assertTrue(s.has('a') === false);
  96 Assert.assertTrue(s.delete(3) === false);
  97 Assert.assertTrue(s.has(Symbol.iterator) === false);
  98 Assert.assertTrue(s.delete(true) === false);
  99 
 100