1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22
23
24
25
26
27
28 @RunWith(BrowserRunner.class)
29 public class FunctionsRestParametersTest extends WebDriverTestCase {
30
31
32
33
34 @Test
35 @Alerts("1,abc,2,##")
36 public void oneRestArg() throws Exception {
37 final String html = DOCTYPE_HTML
38 + "<html><head>\n"
39 + "<script>\n"
40 + LOG_TITLE_FUNCTION
41 + "</script></head>\n"
42 + "<body>\n"
43 + "<script>\n"
44 + "function rest(...restArgs) {\n"
45 + " return restArgs;\n"
46 + "}\n"
47 + "try {\n"
48 + " log(rest(1, 'abc', 2, '##').toString());\n"
49 + "} catch(e) { log(e.message) }"
50 + "</script>\n"
51 + "</body></html>";
52
53 loadPageVerifyTitle2(html);
54 }
55
56
57
58
59 @Test
60 @Alerts("true-0")
61 public void oneRestArgNothingProvided() throws Exception {
62 final String html = DOCTYPE_HTML
63 + "<html><head>\n"
64 + "<script>\n"
65 + LOG_TITLE_FUNCTION
66 + "</script></head>\n"
67 + "<body>\n"
68 + "<script>\n"
69 + "function rest(...restArgs) {\n"
70 + " return restArgs;\n"
71 + "}\n"
72 + "try {\n"
73 + " var r = rest();\n"
74 + " log('' + Array.isArray(r) + '-' + r.length);\n"
75 + "} catch(e) { log(e.message) }"
76 + "</script>\n"
77 + "</body></html>";
78
79 loadPageVerifyTitle2(html);
80 }
81
82
83
84
85 @Test
86 @Alerts("true-1")
87 public void oneRestArgOneProvided() throws Exception {
88 final String html = DOCTYPE_HTML
89 + "<html><head>\n"
90 + "<script>\n"
91 + LOG_TITLE_FUNCTION
92 + "</script></head>\n"
93 + "<body>\n"
94 + "<script>\n"
95 + "function rest(...restArgs) {\n"
96 + " return restArgs;\n"
97 + "}\n"
98 + "try {\n"
99 + " var r = rest('xy');\n"
100 + " log('' + Array.isArray(r) + '-' + r.length);\n"
101 + "} catch(e) { log(e.message) }"
102 + "</script>\n"
103 + "</body></html>";
104
105 loadPageVerifyTitle2(html);
106 }
107
108
109
110
111 @Test
112 @Alerts("abc,2,##")
113 public void twoRestArg() throws Exception {
114 final String html = DOCTYPE_HTML
115 + "<html><head>\n"
116 + "<script>\n"
117 + LOG_TITLE_FUNCTION
118 + "</script></head>\n"
119 + "<body>\n"
120 + "<script>\n"
121 + "function rest(arg, ...restArgs) {\n"
122 + " return restArgs;\n"
123 + "}\n"
124 + "try {\n"
125 + " log(rest(1, 'abc', 2, '##').toString());\n"
126 + "} catch(e) { log(e.message) }"
127 + "</script>\n"
128 + "</body></html>";
129
130 loadPageVerifyTitle2(html);
131 }
132
133
134
135
136 @Test
137 @Alerts("undefined - true-0")
138 public void twoRestArgNothingProvided() throws Exception {
139 final String html = DOCTYPE_HTML
140 + "<html><head>\n"
141 + "<script>\n"
142 + LOG_TITLE_FUNCTION
143 + "</script></head>\n"
144 + "<body>\n"
145 + "<script>\n"
146 + "function rest(arg, ...restArgs) {\n"
147 + " return '' + typeof arg + ' - ' + Array.isArray(restArgs) + '-' + restArgs.length;\n"
148 + "}\n"
149 + "try {\n"
150 + " log(rest());\n"
151 + "} catch(e) { log(e.message) }"
152 + "</script>\n"
153 + "</body></html>";
154
155 loadPageVerifyTitle2(html);
156 }
157
158
159
160
161 @Test
162 @Alerts("77 - true-0")
163 public void twoRestArgOneProvided() throws Exception {
164 final String html = DOCTYPE_HTML
165 + "<html><head>\n"
166 + "<script>\n"
167 + LOG_TITLE_FUNCTION
168 + "</script></head>\n"
169 + "<body>\n"
170 + "<script>\n"
171 + "function rest(arg, ...restArgs) {\n"
172 + " return '' + arg + ' - ' + Array.isArray(restArgs) + '-' + restArgs.length;\n"
173 + "}\n"
174 + "try {\n"
175 + " log(rest('77'));\n"
176 + "} catch(e) { log(e.message) }"
177 + "</script>\n"
178 + "</body></html>";
179
180 loadPageVerifyTitle2(html);
181 }
182
183
184
185
186 @Test
187 @Alerts("1-4")
188 public void arguments() throws Exception {
189 final String html = DOCTYPE_HTML
190 + "<html><head>\n"
191 + "<script>\n"
192 + LOG_TITLE_FUNCTION
193 + "</script></head>\n"
194 + "<body>\n"
195 + "<script>\n"
196 + "function rest(arg, ...restArgs) {\n"
197 + " return arguments.length;\n"
198 + "}\n"
199 + "try {\n"
200 + " log('' + rest('77') + '-' + rest(1, 2, 3, 4));\n"
201 + "} catch(e) { log(e.message) }"
202 + "</script>\n"
203 + "</body></html>";
204
205 loadPageVerifyTitle2(html);
206 }
207
208
209
210
211 @Test
212 @Alerts("0-1")
213 public void length() throws Exception {
214 final String html = DOCTYPE_HTML
215 + "<html><head>\n"
216 + "<script>\n"
217 + LOG_TITLE_FUNCTION
218 + "</script></head>\n"
219 + "<body>\n"
220 + "<script>\n"
221 + "function foo1(...theArgs) {}\n"
222 + "function foo2(arg, ...theArgs) {}\n"
223 + "try {\n"
224 + " log(foo1.length + '-' + foo2.length);\n"
225 + "} catch(e) { log(e.message) }"
226 + "</script>\n"
227 + "</body></html>";
228
229 loadPageVerifyTitle2(html);
230 }
231
232
233
234
235 @Test
236 @Alerts("2-1-0")
237 public void argLength() throws Exception {
238 final String html = DOCTYPE_HTML
239 + "<html><head>\n"
240 + "<script>\n"
241 + LOG_TITLE_FUNCTION
242 + "</script></head>\n"
243 + "<body>\n"
244 + "<script>\n"
245 + "function rest(...restArgs) {\n"
246 + " return restArgs.length;\n"
247 + "}\n"
248 + "try {\n"
249 + " log(rest(1,2) + '-' + rest(1) + '-' + rest());\n"
250 + "} catch(e) { log(e.message) }"
251 + "</script>\n"
252 + "</body></html>";
253
254 loadPageVerifyTitle2(html);
255 }
256
257
258
259
260 @Test
261 @Alerts("function rest(...restArgs) { return restArgs.length; }")
262 public void string() throws Exception {
263 final String html = DOCTYPE_HTML
264 + "<html><head>\n"
265 + "<script>\n"
266 + LOG_TITLE_FUNCTION
267 + "</script></head>\n"
268 + "<body>\n"
269 + "<script>\n"
270 + "function rest(...restArgs) {\n"
271 + " return restArgs.length;\n"
272 + "}\n"
273 + "try {\n"
274 + " log(rest.toString());\n"
275 + "} catch(e) { log(e.message) }"
276 + "</script>\n"
277 + "</body></html>";
278
279 loadPageVerifyTitle2(html);
280 }
281 }