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/alphanumeric.js'.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class AlphanumericTest extends WebDriverTestCase {
29  
30      private static final String NON_ALPHANUMERIC = "~`!@#$%^&*()-+={[}]|\\\\:;\\'<,>./?\\f\\n\\r\\t \"\\v";
31      private static final String ALPHANUMERIC     = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
32  
33      /**
34       * Be sure all alphanumerics are matched by \w.
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts(ALPHANUMERIC)
39      public void test1() throws Exception {
40          test("'" + ALPHANUMERIC + "'.match(new RegExp('\\\\w+'))", false);
41      }
42  
43      /**
44       * Be sure all non-alphanumerics are matched by \W.
45       * @throws Exception if the test fails
46       */
47      @Test
48      @Alerts("7E-60-21-40-23-24-25-5E-26-2A-28-29-2D-2B-3D-7B-5B-7D-5D-7C-5C-"
49                  + "3A-3B-27-3C-2C-3E-2E-2F-3F-C-A-D-9-20-22-B-")
50      public void test2() throws Exception {
51          test("'" + NON_ALPHANUMERIC + "'.match(new RegExp('\\\\W+'))", true);
52      }
53  
54      /**
55       * Be sure all non-alphanumerics are not matched by \w.
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts("null")
60      public void test3() throws Exception {
61          test("'" + NON_ALPHANUMERIC + "'.match(new RegExp('\\\\w+'))", false);
62      }
63  
64      /**
65       * Be sure all alphanumerics are not matched by \W.
66       * @throws Exception if the test fails
67       */
68      @Test
69      @Alerts("null")
70      public void test4() throws Exception {
71          test("'" + ALPHANUMERIC + "'.match(new RegExp('\\\\W+'))", false);
72      }
73  
74      /**
75       * Be sure all alphanumerics are matched by \w.
76       * @throws Exception if the test fails
77       */
78      @Test
79      @Alerts(ALPHANUMERIC)
80      public void test5() throws Exception {
81          test("'" + NON_ALPHANUMERIC + ALPHANUMERIC + "'.match(new RegExp('\\\\w+'))", false);
82      }
83  
84      /**
85       * Be sure all non-alphanumerics are matched by \W.
86       * @throws Exception if the test fails
87       */
88      @Test
89      @Alerts("7E-60-21-40-23-24-25-5E-26-2A-28-29-2D-2B-3D-7B-5B-7D-5D-7C-5C-"
90                  + "3A-3B-27-3C-2C-3E-2E-2F-3F-C-A-D-9-20-22-B-")
91      public void test6() throws Exception {
92          test("'" + ALPHANUMERIC + NON_ALPHANUMERIC + "'.match(new RegExp('\\\\W+'))", true);
93      }
94  
95      /**
96       * Be sure all alphanumerics are matched by \w (using literals).
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts(ALPHANUMERIC)
101     public void test7() throws Exception {
102         test("'" + ALPHANUMERIC + NON_ALPHANUMERIC + "'.match(/\\w+/)", false);
103     }
104 
105     /**
106      * Be sure all non-alphanumerics are matched by \W (using literals).
107      * @throws Exception if the test fails
108      */
109     @Test
110     @Alerts("7E-60-21-40-23-24-25-5E-26-2A-28-29-2D-2B-3D-7B-5B-7D-5D-7C-5C-"
111                 + "3A-3B-27-3C-2C-3E-2E-2F-3F-C-A-D-9-20-22-B-")
112     public void test8() throws Exception {
113         test("'" + ALPHANUMERIC + NON_ALPHANUMERIC + "'.match(/\\W+/)", true);
114     }
115 
116     /**
117      * Be sure the following test behaves consistently.
118      * @throws Exception if the test fails
119      */
120     @Test
121     @Alerts("abcd*&^%$$,abcd,%$$")
122     public void test9() throws Exception {
123         test("'" + "abcd*&^%$$" + "'.match(/(\\w+)...(\\W+)/)", false);
124     }
125 
126     /**
127      * Be sure all alphanumeric characters match individually.
128      * @throws Exception if the test fails
129      */
130     @Test
131     public void test10() throws Exception {
132         for (int i = 0; i < ALPHANUMERIC.length(); i++) {
133             setExpectedAlerts(String.valueOf(ALPHANUMERIC.charAt(i)));
134             test("'" + "#$" + ALPHANUMERIC.charAt(i) + "%^" + "'.match(new RegExp('\\\\w'))", false);
135         }
136     }
137 
138     /**
139      * Be sure all non_alphanumeric characters match individually.
140      * @throws Exception if the test fails
141      */
142     @Test
143     @Alerts("\n")
144     public void test11() throws Exception {
145         final String[] exp = getExpectedAlerts();
146         for (int i = 0; i < NON_ALPHANUMERIC.length() - 1; i++) {
147             final char ch = NON_ALPHANUMERIC.charAt(i);
148             String expected = String.valueOf(ch);
149             String input = expected;
150             switch (ch) {
151                 case '\\':
152                     input = "\\" + ch;
153                     break;
154 
155                 case '\'':
156                     input = "\\" + ch;
157                     break;
158 
159                 case 'f':
160                     expected = "\f";
161                     input = "\\" + ch;
162                     break;
163 
164                 case 'n':
165                     expected = "\n";
166                     input = "\\" + ch;
167                     break;
168 
169                 case 'r':
170                     expected = exp[0];
171                     input = "\\" + ch;
172                     break;
173 
174                 case 't':
175                     expected = "\t";
176                     input = "\\" + ch;
177                     break;
178 
179                 case 'v':
180                     expected = "\u000B";
181                     input = "\\" + ch;
182                     break;
183 
184                 default:
185             }
186 
187             setExpectedAlerts(expected);
188 
189             final String s = "sd" + input + String.valueOf((i + 10) * (i + 10) - 2 * (i + 10));
190             test("'" + s + "'.match(new RegExp('\\\\W'))", false);
191         }
192     }
193 
194     private void test(final String script, final boolean charCode) throws Exception {
195         String html
196             = "<html><head><script>\n";
197         if (charCode) {
198             html += "  var string = " + script + ".toString();\n"
199                 + "  var output = '';\n"
200                 + "  for (var i = 0; i < string.length; i++) {\n"
201                 + "    output += string.charCodeAt(i).toString(16).toUpperCase() + '-';\n"
202                 + "  }\n"
203                 + "  alert(output);\n";
204         }
205         else {
206             html += "  alert(" + script + ");\n";
207         }
208         html += "</script></head><body>\n"
209             + "</body></html>";
210 
211         loadPageWithAlerts2(html);
212     }
213 }