1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.annotation.Alerts;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25
26
27 public class WeakMapTest extends WebDriverTestCase {
28
29
30
31
32 @Test
33 @Alerts({"true", "one"})
34 public void constructorArray() throws Exception {
35 final String html = DOCTYPE_HTML
36 + "<html><head>\n"
37 + "<script>\n"
38 + LOG_TITLE_FUNCTION
39 + "function test() {\n"
40 + " var obj = {};\n"
41 + " var foo = {};\n"
42 + " var myMap = new WeakMap([[ obj, 'one' ],[ foo, 'two' ]]);\n"
43 + " log(myMap.has(foo));\n"
44 + " log(myMap.get(obj));\n"
45 + "}\n"
46 + "</script></head><body onload='test()'>\n"
47 + "</body></html>";
48
49 loadPageVerifyTitle2(html);
50 }
51
52
53
54
55 @Test
56 @Alerts("TypeError")
57 public void constructorSetParam() throws Exception {
58 final String html = DOCTYPE_HTML
59 + "<html><head>\n"
60 + "<script>\n"
61 + LOG_TITLE_FUNCTION
62 + "function test() {\n"
63 + " try {\n"
64 + " var myMap = new WeakMap(new Set('test'));\n"
65 + " log(myMap.has('test'));\n"
66 + " } catch(e) {\n"
67 + " logEx(e);\n"
68 + " }\n"
69 + "}\n"
70 + "</script></head><body onload='test()'>\n"
71 + "</body></html>";
72
73 loadPageVerifyTitle2(html);
74 }
75
76
77
78
79 @Test
80 @Alerts("true")
81 public void constructorMapParam() throws Exception {
82 final String html = DOCTYPE_HTML
83 + "<html><head>\n"
84 + "<script>\n"
85 + LOG_TITLE_FUNCTION
86 + "function test() {\n"
87 + " var obj = {};\n"
88 + " var foo = {};\n"
89 + " var testMap = new Map([[ obj, 'one' ],[ foo, 'two' ]]);\n"
90 + " try {\n"
91 + " var myMap = new WeakMap(testMap);\n"
92 + " log(myMap.has(foo));\n"
93 + " } catch(e) {\n"
94 + " logEx(e);\n"
95 + " }\n"
96 + "}\n"
97 + "</script></head><body onload='test()'>\n"
98 + "</body></html>";
99
100 loadPageVerifyTitle2(html);
101 }
102
103
104
105
106 @Test
107 @Alerts("true")
108 public void constructorIteratorParam() throws Exception {
109 final String html = DOCTYPE_HTML
110 + "<html><head>\n"
111 + "<script>\n"
112 + LOG_TITLE_FUNCTION
113 + "function test() {\n"
114 + " if (window.WeakSet) {\n"
115 + " var obj = {};\n"
116 + " var foo = {};\n"
117 + " var myIterable = {};\n"
118 + " myIterable[Symbol.iterator] = function() {\n"
119 + " return {\n"
120 + " next: function() {\n"
121 + " if (this._first) {;\n"
122 + " this._first = false;\n"
123 + " return { value: [ foo, 'one' ], done: false };\n"
124 + " }\n"
125 + " return { done: true };\n"
126 + " },\n"
127 + " _first: true\n"
128 + " };\n"
129 + " };\n"
130 + " try {\n"
131 + " var myMap = new WeakMap(myIterable);\n"
132 + " log(myMap.has(foo));\n"
133 + " } catch(e) {\n"
134 + " logEx(e);\n"
135 + " }\n"
136 + " }"
137 + "}\n"
138 + "</script></head>\n"
139 + "<body onload='test()'>\n"
140 + "</body></html>";
141
142 loadPageVerifyTitle2(html);
143 }
144
145
146
147
148 @Test
149 @Alerts({"undefined", "value2"})
150 public void get() 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 kvArray = [[{}, 'value1'], [window, 'value2']];\n"
157 + " var myMap = new WeakMap(kvArray);\n"
158 + " log(myMap.size);\n"
159 + " log(myMap.get(window));\n"
160 + " }\n"
161 + "</script></head><body onload='test()'>\n"
162 + "</body></html>";
163 loadPageVerifyTitle2(html);
164 }
165
166
167
168
169 @Test
170 @Alerts("TypeError")
171 public void setNonObject() throws Exception {
172 final String html = DOCTYPE_HTML
173 + "<html><head>\n"
174 + "<script>\n"
175 + LOG_TITLE_FUNCTION
176 + " function test() {\n"
177 + " if (window.WeakMap) {\n"
178 + " var kvArray = [[{}, 'value1'], [window, 'value2']];\n"
179 + " var myMap = new WeakMap(kvArray);\n"
180 + " try {\n"
181 + " myMap.set(1, 2);\n"
182 + " } catch(e) { logEx(e) }\n"
183 + " }\n"
184 + " }\n"
185 + "</script></head><body onload='test()'>\n"
186 + "</body></html>";
187 loadPageVerifyTitle2(html);
188 }
189 }