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