View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://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  package org.htmlunit.javascript.host;
16  
17  import java.util.Set;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link Set}.
25   *
26   * @author Ahmed Ashour
27   * @author Ronald Brill
28   */
29  public class SetTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"3", "true"})
36      public void has() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html>\n"
39              + "<head>\n"
40              + "<script>\n"
41              + LOG_TITLE_FUNCTION
42              + "  function test() {\n"
43              + "    var myArray = ['value1', 'value2', 'value3'];\n"
44              + "    var mySet = new Set(myArray);\n"
45              + "    mySet.add('value1');\n"
46              + "    log(mySet.size);\n"
47              + "    log(mySet.has('value2'));\n"
48              + "  }\n"
49              + "</script>\n"
50              + "</head>\n"
51              + "<body onload='test()'>\n"
52              + "</body></html>";
53  
54          loadPageVerifyTitle2(html);
55      }
56  
57      /**
58       * @throws Exception if the test fails
59       */
60      @Test
61      @Alerts({"function values() { [native code] }",
62               "[object Set Iterator]", "0", "1", "[object Object]"})
63      public void iterator() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html>\n"
66              + "<head>\n"
67              + "<script>\n"
68              + LOG_TITLE_FUNCTION
69              + "  function test() {\n"
70              + "    if (window.Symbol) {\n"
71              + "      var set = new Set();\n"
72              + "      set.add('0');\n"
73              + "      set.add(1);\n"
74              + "      set.add({});\n"
75              + "      log(set[Symbol.iterator]);\n"
76              + "      var iter = set[Symbol.iterator]();\n"
77              + "      log(iter);\n"
78              + "      log(iter.next().value);\n"
79              + "      log(iter.next().value);\n"
80              + "      log(iter.next().value);\n"
81              + "    }\n"
82              + "  }\n"
83              + "</script>\n"
84              + "</head>\n"
85              + "<body onload='test()'>\n"
86              + "</body></html>";
87  
88          loadPageVerifyTitle2(html);
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts({"function values() { [native code] }",
96               "[object Set Iterator]", "0", "1", "[object Object]"})
97      public void values() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html>\n"
100             + "<head>\n"
101             + "<script>\n"
102             + LOG_TITLE_FUNCTION
103             + "  function test() {\n"
104             + "    if (window.Symbol) {\n"
105             + "      var set = new Set();\n"
106             + "      set.add('0');\n"
107             + "      set.add(1);\n"
108             + "      set.add({});\n"
109             + "      log(set.values);\n"
110             + "      var iter = set.values();\n"
111             + "      log(iter);\n"
112             + "      log(iter.next().value);\n"
113             + "      log(iter.next().value);\n"
114             + "      log(iter.next().value);\n"
115             + "    }\n"
116             + "  }\n"
117             + "</script>\n"
118             + "</head>\n"
119             + "<body onload='test()'>\n"
120             + "</body></html>";
121 
122         loadPageVerifyTitle2(html);
123     }
124 
125     /**
126      * @throws Exception if the test fails
127      */
128     @Test
129     @Alerts("2")
130     public void constructorArray() throws Exception {
131         final String html = DOCTYPE_HTML
132             + "<html><head>\n"
133             + "<script>\n"
134             + LOG_TITLE_FUNCTION
135             + "function test() {\n"
136             + "  var mySet = new Set([2, 7]);\n"
137             + "  log(mySet.size);\n"
138             + "}\n"
139             + "</script></head><body onload='test()'>\n"
140             + "</body></html>";
141 
142         loadPageVerifyTitle2(html);
143     }
144 
145     /**
146      * @throws Exception if the test fails
147      */
148     @Test
149     @Alerts("2")
150     public void constructorInt32Array() throws Exception {
151         final String html = DOCTYPE_HTML
152             + "<html><head>\n"
153             + "<script>\n"
154             + LOG_TITLE_FUNCTION
155             + "function test() {\n"
156             + "  var array = new Int32Array([2, 7]);\n"
157             + "  var mySet = new Set(array);\n"
158             + "  log(mySet.size);\n"
159             + "}\n"
160             + "</script></head><body onload='test()'>\n"
161             + "</body></html>";
162 
163         loadPageVerifyTitle2(html);
164     }
165 
166     /**
167      * @throws Exception if the test fails
168      */
169     @Test
170     @Alerts({"3", "true", "false"})
171     public void constructorStringParam() throws Exception {
172         final String html = DOCTYPE_HTML
173             + "<html><head>\n"
174             + "<script>\n"
175             + LOG_TITLE_FUNCTION
176             + "function test() {\n"
177             + "  var mySet = new Set('test');\n"
178             + "  log(mySet.size);\n"
179             + "  log(mySet.has('t'));\n"
180             + "  log(mySet.has('x'));\n"
181             + "}\n"
182             + "</script></head><body onload='test()'>\n"
183             + "</body></html>";
184 
185         loadPageVerifyTitle2(html);
186     }
187 
188     /**
189      * @throws Exception if the test fails
190      */
191     @Test
192     @Alerts({"7", "true", "false"})
193     public void constructorStringIteratorParam() throws Exception {
194         final String html = DOCTYPE_HTML
195             + "<html><head>\n"
196             + "<script>\n"
197             + LOG_TITLE_FUNCTION
198             + "function test() {\n"
199             + "  if (window.Symbol) {\n"
200             + "    var strIter = 'HtmlUnit'[Symbol.iterator]();"
201             + "    var mySet = new Set(strIter);\n"
202             + "    log(mySet.size);\n"
203             + "    log(mySet.has('t'));\n"
204             + "    log(mySet.has('x'));\n"
205             + "  }\n"
206             + "}\n"
207             + "</script></head><body onload='test()'>\n"
208             + "</body></html>";
209 
210         loadPageVerifyTitle2(html);
211     }
212 
213     /**
214      * @throws Exception if the test fails
215      */
216     @Test
217     @Alerts({"3", "true", "false"})
218     public void constructorSetParam() throws Exception {
219         final String html = DOCTYPE_HTML
220             + "<html><head>\n"
221             + "<script>\n"
222             + LOG_TITLE_FUNCTION
223             + "function test() {\n"
224             + "  var mySet = new Set(new Set('test'));\n"
225             + "  log(mySet.size);\n"
226             + "  log(mySet.has('t'));\n"
227             + "  log(mySet.has('x'));\n"
228             + "}\n"
229             + "</script></head><body onload='test()'>\n"
230             + "</body></html>";
231 
232         loadPageVerifyTitle2(html);
233     }
234 
235     /**
236      * @throws Exception if the test fails
237      */
238     @Test
239     @Alerts({"2", "false", "false"})
240     public void constructorMapParam() throws Exception {
241         final String html = DOCTYPE_HTML
242             + "<html><head>\n"
243             + "<script>\n"
244             + LOG_TITLE_FUNCTION
245             + "function test() {\n"
246             + "  var kvArray = [['key1', 'value1'], ['key2', 'value2']];\n"
247             + "  var myMap = new Map(kvArray);\n"
248             + "  var mySet = new Set(myMap);\n"
249             + "  log(mySet.size);\n"
250             + "  log(mySet.has('key1'));\n"
251             + "  log(mySet.has('value2'));\n"
252             + "}\n"
253             + "</script></head><body onload='test()'>\n"
254             + "</body></html>";
255 
256         loadPageVerifyTitle2(html);
257     }
258 
259     /**
260      * @throws Exception if the test fails
261      */
262     @Test
263     @Alerts({"1", "#77"})
264     public void constructorIteratorParam() throws Exception {
265         final String html = DOCTYPE_HTML
266             + "<html><head>\n"
267             + "<script>\n"
268             + LOG_TITLE_FUNCTION
269             + "function logElement(value) {\n"
270             + "  log(value);\n"
271             + "}\n"
272             + "function test() {\n"
273             + "  try {\n"
274             + "    var myIterable = {};\n"
275             + "    myIterable[Symbol.iterator] = function() {\n"
276             + "      return {\n"
277             + "        next: function() {\n"
278             + "          if (this._first) {;\n"
279             + "            this._first = false;\n"
280             + "            return { value: '#77', done: false };\n"
281             + "          }\n"
282             + "          return { done: true };\n"
283             + "        },\n"
284             + "        _first: true\n"
285             + "      };\n"
286             + "    };\n"
287             + "    var mySet = new Set(myIterable);\n"
288             + "    log(mySet.size);\n"
289             + "    mySet.forEach(logElement);\n"
290             + "  } catch(e) { logEx(e); }"
291             + "}\n"
292             + "</script></head>\n"
293             + "<body onload='test()'>\n"
294             + "</body></html>";
295 
296         loadPageVerifyTitle2(html);
297     }
298 
299     /**
300      * @throws Exception if the test fails
301      */
302     @Test
303     @Alerts({"ab", "ab", "[object Set]", "[object Window]",
304              "undefined", "undefined", "[object Set]", "[object Window]",
305              "null", "null", "[object Set]", "[object Window]"})
306     public void forEach() throws Exception {
307         final String html = DOCTYPE_HTML
308             + "<html><head>\n"
309             + "<script>\n"
310             + LOG_TITLE_FUNCTION
311             + "function logElement(value1, value2, s) {\n"
312             + "  log(value1);\n"
313             + "  log(value2);\n"
314             + "  log(s);\n"
315             + "  log(this);\n"
316             + "}\n"
317             + "function test() {\n"
318             + "  var mySet = new Set(['ab', undefined, null]);\n"
319             + "  mySet.forEach(logElement);\n"
320             + "}\n"
321             + "</script>\n"
322             + "</head>\n"
323             + "<body onload='test()'>\n"
324             + "</body></html>";
325 
326         loadPageVerifyTitle2(html);
327     }
328 
329     /**
330      * @throws Exception if the test fails
331      */
332     @Test
333     @Alerts({"ab", "ab", "[object Set]", "undefined",
334              "undefined", "undefined", "[object Set]", "undefined",
335              "null", "null", "[object Set]", "undefined"})
336     public void forEachStrict() throws Exception {
337         final String html = DOCTYPE_HTML
338             + "<html><head>\n"
339             + "<script>\n"
340             + "'use strict';\n"
341             + LOG_TITLE_FUNCTION
342             + "function logElement(value1, value2, s) {\n"
343             + "  log(value1);\n"
344             + "  log(value2);\n"
345             + "  log(s);\n"
346             + "  log(this);\n"
347             + "}\n"
348             + "function test() {\n"
349             + "  var mySet = new Set(['ab', undefined, null]);\n"
350             + "  mySet.forEach(logElement);\n"
351             + "}\n"
352             + "</script>\n"
353             + "</head>\n"
354             + "<body onload='test()'>\n"
355             + "</body></html>";
356 
357         loadPageVerifyTitle2(html);
358     }
359 
360     /**
361      * @throws Exception if the test fails
362      */
363     @Test
364     @Alerts({"ab", "ab", "[object Set]", "hello", "undefined", "undefined", "[object Set]", "hello",
365              "null", "null", "[object Set]", "hello"})
366     public void forEachThis() throws Exception {
367         final String html = DOCTYPE_HTML
368             + "<html><head>\n"
369             + "<script>\n"
370             + LOG_TITLE_FUNCTION
371             + "function logElement(value1, value2, s) {\n"
372             + "  log(value1);\n"
373             + "  log(value2);\n"
374             + "  log(s);\n"
375             + "  log(this);\n"
376             + "}\n"
377             + "function test() {\n"
378             + "  var mySet = new Set(['ab', undefined, null]);\n"
379             + "  mySet.forEach(logElement, 'hello');\n"
380             + "}\n"
381             + "</script></head><body onload='test()'>\n"
382             + "</body></html>";
383 
384         loadPageVerifyTitle2(html);
385     }
386 
387     /**
388      * Test case for Bug #1868.
389      *
390      * @throws Exception if the test fails
391      */
392     @Test
393     @Alerts("[object Set Iterator]")
394     public void iteratorPrototype() throws Exception {
395         final String html = DOCTYPE_HTML
396             + "<html>\n"
397             + "<head>\n"
398             + "<script>\n"
399             + LOG_TITLE_FUNCTION
400             + "  function test() {\n"
401             + "    if (window.Symbol) {\n"
402             + "      var mySet = new Set();\n"
403             + "      var iter = mySet[Symbol.iterator]();\n"
404             + "      log(Object.getPrototypeOf(iter));\n"
405             + "    }\n"
406             + "  }\n"
407             + "</script>\n"
408             + "</head>\n"
409             + "<body onload='test()'>\n"
410             + "</body></html>";
411 
412         loadPageVerifyTitle2(html);
413     }
414 
415     /**
416      * Test case for Bug #1886.
417      *
418      * @throws Exception if the test fails
419      */
420     @Test
421     @Alerts({"undefined", "0"})
422     public void forEach_withElision() throws Exception {
423         final String html = DOCTYPE_HTML
424                 + "<html><head>\n"
425                 + "<script>\n"
426                 + LOG_TITLE_FUNCTION
427                 + "function logElement(value) {\n"
428                 + "  log(value);\n"
429                 + "}\n"
430                 + "function test() {\n"
431                 + "  var mySet = new Set([, 0]);\n"
432                 + "  mySet.forEach(logElement);\n"
433                 + "}\n"
434                 + "</script></head><body onload='test()'>\n"
435                 + "</body></html>";
436 
437         loadPageVerifyTitle2(html);
438     }
439 }