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.regexp;
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 {@link HtmlUnitRegExpProxy}.
23   * Test the various properties.
24   *
25   * @author Ronald Brill
26   * @author Frank Danek
27   */
28  public class HtmlUnitRegExpProxyGlobalPropertiesTest extends WebDriverTestCase {
29  
30      private void testExec(final String string, final String regexp) throws Exception {
31          final String html = "<html><head><script>\n"
32              + LOG_TITLE_FUNCTION
33              + "  function test() {\n"
34              + "    var str = '" + string + "';\n"
35              + "    var myRegExp = " + regexp + ";\n"
36              + "    log(myRegExp.exec(str));\n"
37              + "    log('$n');\n"
38              + "    log(RegExp.$1);\n"
39              + "    log(RegExp.$2);\n"
40              + "    log(RegExp.$3);\n"
41              + "    log(RegExp.$4);\n"
42              + "    log(RegExp.$5);\n"
43              + "    log(RegExp.$6);\n"
44              + "    log(RegExp.$7);\n"
45              + "    log(RegExp.$8);\n"
46              + "    log(RegExp.$9);\n"
47              + "    log('-');\n"
48              + "    log(RegExp.lastMatch);\n"
49              + "    log(RegExp.lastParen);\n"
50              + "    log(RegExp.leftContext);\n"
51              + "    log(RegExp.rightContext);\n"
52              + "  }\n"
53              + "</script></head><body onload='test()'>\n"
54              + "</body></html>";
55  
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts({"HtmlUnit", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
64      public void regExpExecNoGroups() throws Exception {
65          testExec("1234HtmlUnitxyz", "new RegExp('HtmlUnit')");
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts({"HtmlUnit,Html", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
73      public void regExpExecOneGroup() throws Exception {
74          testExec("1234HtmlUnitxyz", "new RegExp('(Html)Unit')");
75      }
76  
77      /**
78       * @throws Exception if the test fails
79       */
80      @Test
81      @Alerts({"HtmlUnit,Ht,lU", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
82      public void regExpExecManyGroups() throws Exception {
83          testExec("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit')");
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts({"HtmlUnitxy,H,t,m,l,U,n,i,t,x,y", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-",
91               "HtmlUnitxy", "y", "1234", "z"})
92      public void regExpExecTooManyGroups() throws Exception {
93          testExec("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)')");
94      }
95  
96      /**
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts({"HtmlUnit", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
101     public void regExpExecNoGroupsIgnoreCase() throws Exception {
102         testExec("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'i')");
103     }
104 
105     /**
106      * @throws Exception if the test fails
107      */
108     @Test
109     @Alerts({"HtmlUnit,Html", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
110     public void regExpExecOneGroupIgnoreCase() throws Exception {
111         testExec("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'i')");
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts({"HtmlUnit,Ht,lU", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
119     public void regExpExecManyGroupsIgnoreCase() throws Exception {
120         testExec("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'i')");
121     }
122 
123     /**
124      * @throws Exception if the test fails
125      */
126     @Test
127     @Alerts({"HtmlUnitxy,H,t,m,l,U,n,i,t,x,y", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-",
128              "HtmlUnitxy", "y", "1234", "z"})
129     public void regExpExecTooManyGroupsIgnoreCase() throws Exception {
130         testExec("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'i')");
131     }
132 
133     /**
134      * @throws Exception if the test fails
135      */
136     @Test
137     @Alerts({"HtmlUnit", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
138     public void regExpExecNoGroupsGlobal() throws Exception {
139         testExec("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'g')");
140     }
141 
142     /**
143      * @throws Exception if the test fails
144      */
145     @Test
146     @Alerts({"HtmlUnit,Html", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
147     public void regExpExecOneGroupGlobal() throws Exception {
148         testExec("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'g')");
149     }
150 
151     /**
152      * @throws Exception if the test fails
153      */
154     @Test
155     @Alerts({"Html,Html", "$n", "Html", "", "", "", "", "", "", "", "", "-", "Html", "Html", "1234",
156              "Unit for Htnl; Htolxyz"})
157     public void regExpExecOneGroupGlobalManyMatches() throws Exception {
158         testExec("1234HtmlUnit for Htnl; Htolxyz", "new RegExp('(Ht.l)', 'g')");
159     }
160 
161     /**
162      * @throws Exception if the test fails
163      */
164     @Test
165     @Alerts({"HtmlUnit,Ht,lU", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
166     public void regExpExecManyGroupsGlobal() throws Exception {
167         testExec("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'g')");
168     }
169 
170     /**
171      * @throws Exception if the test fails
172      */
173     @Test
174     @Alerts({"HtmlUnitxy,H,t,m,l,U,n,i,t,x,y", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-",
175              "HtmlUnitxy", "y", "1234", "z"})
176     public void regExpExecTooManyGroupsGlobal() throws Exception {
177         testExec("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'g')");
178     }
179 
180     private void testTest(final String string, final String regexp) throws Exception {
181         final String html = "<html><head><script>\n"
182             + LOG_TITLE_FUNCTION
183             + "  function test() {\n"
184             + "    var str = '" + string + "';\n"
185             + "    var myRegExp = " + regexp + ";\n"
186             + "    log(myRegExp.test(str));\n"
187             + "    log('$n');\n"
188             + "    log(RegExp.$1);\n"
189             + "    log(RegExp.$2);\n"
190             + "    log(RegExp.$3);\n"
191             + "    log(RegExp.$4);\n"
192             + "    log(RegExp.$5);\n"
193             + "    log(RegExp.$6);\n"
194             + "    log(RegExp.$7);\n"
195             + "    log(RegExp.$8);\n"
196             + "    log(RegExp.$9);\n"
197             + "    log('-');\n"
198             + "    log(RegExp.lastMatch);\n"
199             + "    log(RegExp.lastParen);\n"
200             + "    log(RegExp.leftContext);\n"
201             + "    log(RegExp.rightContext);\n"
202             + "  }\n"
203             + "</script></head><body onload='test()'>\n"
204             + "</body></html>";
205 
206         loadPageVerifyTitle2(html);
207     }
208 
209     /**
210      * @throws Exception if the test fails
211      */
212     @Test
213     @Alerts({"true", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
214     public void regExpTestNoGroups() throws Exception {
215         testTest("1234HtmlUnitxyz", "new RegExp('HtmlUnit')");
216     }
217 
218     /**
219      * @throws Exception if the test fails
220      */
221     @Test
222     @Alerts({"true", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
223     public void regExpTestOneGroup() throws Exception {
224         testTest("1234HtmlUnitxyz", "new RegExp('(Html)Unit')");
225     }
226 
227     /**
228      * @throws Exception if the test fails
229      */
230     @Test
231     @Alerts({"true", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
232     public void regExpTestManyGroups() throws Exception {
233         testTest("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit')");
234     }
235 
236     /**
237      * @throws Exception if the test fails
238      */
239     @Test
240     @Alerts({"true", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy", "y", "1234", "z"})
241     public void regExpTestTooManyGroups() throws Exception {
242         testTest("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)')");
243     }
244 
245     /**
246      * @throws Exception if the test fails
247      */
248     @Test
249     @Alerts({"true", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
250     public void regExpTestNoGroupsIgnoreCase() throws Exception {
251         testTest("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'i')");
252     }
253 
254     /**
255      * @throws Exception if the test fails
256      */
257     @Test
258     @Alerts({"true", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
259     public void regExpTestOneGroupIgnoreCase() throws Exception {
260         testTest("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'i')");
261     }
262 
263     /**
264      * @throws Exception if the test fails
265      */
266     @Test
267     @Alerts({"true", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
268     public void regExpTestManyGroupsIgnoreCase() throws Exception {
269         testTest("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'i')");
270     }
271 
272     /**
273      * @throws Exception if the test fails
274      */
275     @Test
276     @Alerts({"true", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy", "y", "1234", "z"})
277     public void regExpTestTooManyGroupsIgnoreCase() throws Exception {
278         testTest("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'i')");
279     }
280 
281     /**
282      * @throws Exception if the test fails
283      */
284     @Test
285     @Alerts({"true", "$n", "", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "", "1234", "xyz"})
286     public void regExpTestNoGroupsGlobal() throws Exception {
287         testTest("1234HtmlUnitxyz", "new RegExp('HtmlUnit', 'g')");
288     }
289 
290     /**
291      * @throws Exception if the test fails
292      */
293     @Test
294     @Alerts({"true", "$n", "Html", "", "", "", "", "", "", "", "", "-", "HtmlUnit", "Html", "1234", "xyz"})
295     public void regExpTestOneGroupGlobal() throws Exception {
296         testTest("1234HtmlUnitxyz", "new RegExp('(Html)Unit', 'g')");
297     }
298 
299     /**
300      * @throws Exception if the test fails
301      */
302     @Test
303     @Alerts({"true", "$n", "Html", "", "", "", "", "", "", "", "", "-", "Html", "Html", "1234",
304              "Unit for Html; Htmlxyz"})
305     public void regExpTestOneGroupGlobalManyMatches() throws Exception {
306         testTest("1234HtmlUnit for Html; Htmlxyz", "new RegExp('(Html)', 'g')");
307     }
308 
309     /**
310      * @throws Exception if the test fails
311      */
312     @Test
313     @Alerts({"true", "$n", "Ht", "lU", "", "", "", "", "", "", "", "-", "HtmlUnit", "lU", "1234", "xyz"})
314     public void regExpTestManyGroupsGlobal() throws Exception {
315         testTest("1234HtmlUnitxyz", "new RegExp('(Ht)m(lU)nit', 'g')");
316     }
317 
318     /**
319      * @throws Exception if the test fails
320      */
321     @Test
322     @Alerts({"true", "$n", "H", "t", "m", "l", "U", "n", "i", "t", "x", "-", "HtmlUnitxy", "y", "1234", "z"})
323     public void regExpTestTooManyGroupsGlobal() throws Exception {
324         testTest("1234HtmlUnitxyz", "new RegExp('(H)(t)(m)(l)(U)(n)(i)(t)(x)(y)', 'g')");
325     }
326 }