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/hexadecimal.js'.
23   *
24   * @author Ahmed Ashour
25   */
26  public class HexadecimalTest extends WebDriverTestCase {
27  
28      /**
29       * Tests testString.match(new RegExp(testPattern)).
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
34      public void test1() throws Exception {
35          final String initialScript = "testPattern = '\\\\x41\\\\x42\\\\x43\\\\x44\\\\x45\\\\x46\\\\x47\\\\x48\\\\x49"
36              + "\\\\x4A\\\\x4B\\\\x4C\\\\x4D\\\\x4E\\\\x4F\\\\x50\\\\x51\\\\x52\\\\x53\\\\x54\\\\x55\\\\x56\\\\x57"
37              + "\\\\x58\\\\x59\\\\x5A';"
38              + "var testString = '12345ABCDEFGHIJKLMNOPQRSTUVWXYZ67890';";
39          test(initialScript, "testString.match(new RegExp(testPattern))");
40      }
41  
42      /**
43       * Tests testString.match(new RegExp(testPattern)).
44       * @throws Exception if the test fails
45       */
46      @Test
47      @Alerts("abcdefghijklmnopqrstuvwxyz")
48      public void test2() throws Exception {
49          final String initialScript = "var testPattern = '\\\\x61\\\\x62\\\\x63\\\\x64\\\\x65\\\\x66\\\\x67\\\\x68"
50              + "\\\\x69\\\\x6A\\\\x6B\\\\x6C\\\\x6D\\\\x6E\\\\x6F\\\\x70\\\\x71\\\\x72\\\\x73\\\\x74\\\\x75\\\\x76"
51              + "\\\\x77\\\\x78\\\\x79\\\\x7A';"
52              + "var testString = '12345AabcdefghijklmnopqrstuvwxyzZ67890';";
53          test(initialScript, "testString.match(new RegExp(testPattern))");
54      }
55  
56      /**
57       * Tests testString.match(new RegExp(testPattern)).
58       * @throws Exception if the test fails
59       */
60      @Test
61      @Alerts(" !\"#$%&'()*+,-./0123")
62      public void test3() throws Exception {
63          final String initialScript = "var testPattern = '\\\\x20\\\\x21\\\\x22\\\\x23\\\\x24\\\\x25\\\\x26\\\\x27"
64              + "\\\\x28\\\\x29\\\\x2A\\\\x2B\\\\x2C\\\\x2D\\\\x2E\\\\x2F\\\\x30\\\\x31\\\\x32\\\\x33';"
65              + "var testString = 'abc !\"#$%&\\'()*+,-./0123ZBC';";
66          test(initialScript, "testString.match(new RegExp(testPattern))");
67      }
68  
69      /**
70       * Tests testString.match(new RegExp(testPattern)).
71       * @throws Exception if the test fails
72       */
73      @Test
74      @Alerts("456789:;<=>?@")
75      public void test4() throws Exception {
76          final String initialScript = "var testPattern = '\\\\x34\\\\x35\\\\x36\\\\x37\\\\x38\\\\x39\\\\x3A\\\\x3B"
77              + "\\\\x3C\\\\x3D\\\\x3E\\\\x3F\\\\x40';"
78              + "var testString = '123456789:;<=>?@ABC';";
79          test(initialScript, "testString.match(new RegExp(testPattern))");
80      }
81  
82      /**
83       * Tests testString.match(new RegExp(testPattern)).
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts("{|}~")
88      public void test5() throws Exception {
89          final String initialScript = "var testPattern = '\\\\x7B\\\\x7C\\\\x7D\\\\x7E';"
90              + "var testString = '1234{|}~ABC';";
91          test(initialScript, "testString.match(new RegExp(testPattern))");
92      }
93  
94      /**
95       * Tests 'canthisbeFOUND'.match(new RegExp('[A-\\x5A]+')).
96       * @throws Exception if the test fails
97       */
98      @Test
99      @Alerts("FOUND")
100     public void test6() throws Exception {
101         test("'canthisbeFOUND'.match(new RegExp('[A-\\\\x5A]+'))");
102     }
103 
104     /**
105      * Tests 'canthisbeFOUND'.match(new RegExp('[\\x61-\\x7A]+')).
106      * @throws Exception if the test fails
107      */
108     @Test
109     @Alerts("canthisbe")
110     public void test7() throws Exception {
111         test("'canthisbeFOUND'.match(new RegExp('[\\\\x61-\\\\x7A]+'))");
112     }
113 
114     /**
115      * Tests 'canthisbeFOUND'.match(/[\x61-\x7A]+/).
116      * @throws Exception if the test fails
117      */
118     @Test
119     @Alerts("canthisbe")
120     public void test8() throws Exception {
121         test("'canthisbeFOUND'.match(/[\\x61-\\x7A]+/)");
122     }
123 
124     private void test(final String script) throws Exception {
125         test(null, script);
126     }
127 
128     private void test(final String initialScript, final String script) throws Exception {
129         String html = "<html><head>\n"
130                 + "</head><body>\n"
131                 + LOG_TEXTAREA
132                 + "<script>\n"
133                 + LOG_TEXTAREA_FUNCTION;
134         if (initialScript != null) {
135             html += initialScript + ";\n";
136         }
137         html += "  log(" + script + ");\n"
138             + "</script>\n"
139             + "</body></html>";
140         loadPageVerifyTextArea2(html);
141     }
142 }