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