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.mozilla.js1_2;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests originally in '/js/src/tests/js1_2/regexp/compile.js'.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  public class CompileTest extends WebDriverTestCase {
28  
29      /**
30       * Tests '234X456X7890'.match(regularExpression).
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts("456X7890")
35      public void test1() throws Exception {
36          final String initialScript = "var regularExpression = new RegExp();\n"
37              + "regularExpression.compile('[0-9]{3}x[0-9]{4}', 'i');";
38          test(initialScript, "'234X456X7890'.match(regularExpression)");
39      }
40  
41      /**
42       * Tests regularExpression.source.
43       * @throws Exception if the test fails
44       */
45      @Test
46      @Alerts("[0-9]{3}x[0-9]{4}")
47      public void test2() throws Exception {
48          final String initialScript = "var regularExpression = new RegExp();\n"
49              + "regularExpression.compile('[0-9]{3}x[0-9]{4}', 'i');";
50          test(initialScript, "regularExpression.source");
51      }
52  
53      /**
54       * Tests regularExpression.global.
55       * @throws Exception if the test fails
56       */
57      @Test
58      @Alerts("false")
59      public void test3() throws Exception {
60          final String initialScript = "var regularExpression = new RegExp();\n"
61              + "regularExpression.compile('[0-9]{3}x[0-9]{4}', 'i');";
62          test(initialScript, "regularExpression.global");
63      }
64  
65      /**
66       * Tests regularExpression.ignoreCase.
67       * @throws Exception if the test fails
68       */
69      @Test
70      @Alerts("true")
71      public void test4() throws Exception {
72          final String initialScript = "var regularExpression = new RegExp();\n"
73              + "regularExpression.compile('[0-9]{3}x[0-9]{4}', 'i');";
74          test(initialScript, "regularExpression.ignoreCase");
75      }
76  
77      /**
78       * Tests '234X456X7890'.match(regularExpression).
79       * @throws Exception if the test fails
80       */
81      @Test
82      @Alerts("234X456")
83      public void test5() throws Exception {
84          final String initialScript = "var regularExpression = new RegExp();\n"
85              + "regularExpression.compile('[0-9]{3}X[0-9]{3}', 'g')";
86          test(initialScript, "'234X456X7890'.match(regularExpression)");
87      }
88  
89      /**
90       * Tests regularExpression.source.
91       * @throws Exception if the test fails
92       */
93      @Test
94      @Alerts("[0-9]{3}X[0-9]{3}")
95      public void test6() throws Exception {
96          final String initialScript = "var regularExpression = new RegExp();\n"
97              + "regularExpression.compile('[0-9]{3}X[0-9]{3}', 'g')";
98          test(initialScript, "regularExpression.source");
99      }
100 
101     /**
102      * Tests regularExpression.global.
103      * @throws Exception if the test fails
104      */
105     @Test
106     @Alerts("true")
107     public void test7() throws Exception {
108         final String initialScript = "var regularExpression = new RegExp();\n"
109             + "regularExpression.compile('[0-9]{3}X[0-9]{3}', 'g')";
110         test(initialScript, "regularExpression.global");
111     }
112 
113     /**
114      * Tests regularExpression.ignoreCase.
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts("false")
119     public void test8() throws Exception {
120         final String initialScript = "var regularExpression = new RegExp();\n"
121             + "regularExpression.compile('[0-9]{3}X[0-9]{3}', 'g')";
122         test(initialScript, "regularExpression.ignoreCase");
123     }
124 
125     private void test(final String initialScript, final String script) throws Exception {
126         String html = "<html><head><script>\n"
127                         + LOG_TITLE_FUNCTION;
128         if (initialScript != null) {
129             html += initialScript + ";\n";
130         }
131         html += "  log(" + script + ");\n"
132             + "</script></head><body>\n"
133             + "</body></html>";
134         loadPageVerifyTitle2(html);
135     }
136 }