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/octal.js'.
23   *
24   * @author Ahmed Ashour
25   */
26  public class OctalTest 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 = "var testPattern = '\\\\101\\\\102\\\\103\\\\104\\\\105\\\\106\\\\107\\\\110"
36              + "\\\\111\\\\112\\\\113\\\\114\\\\115\\\\116\\\\117\\\\120\\\\121\\\\122\\\\123\\\\124\\\\125\\\\126"
37              + "\\\\127\\\\130\\\\131\\\\132';"
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 = '\\\\141\\\\142\\\\143\\\\144\\\\145\\\\146\\\\147\\\\150"
50              + "\\\\151\\\\152\\\\153\\\\154\\\\155\\\\156\\\\157\\\\160\\\\161\\\\162\\\\163\\\\164\\\\165\\\\166"
51              + "\\\\167\\\\170\\\\171\\\\172';"
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 = '\\\\40\\\\41\\\\42\\\\43\\\\44\\\\45\\\\46\\\\47\\\\50"
64              + "\\\\51\\\\52\\\\53\\\\54\\\\55\\\\56\\\\57\\\\60\\\\61\\\\62\\\\63';"
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 = '\\\\64\\\\65\\\\66\\\\67\\\\70\\\\71\\\\72\\\\73\\\\74\\\\75"
77              + "\\\\76\\\\77\\\\100';"
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 = '\\\\173\\\\174\\\\175\\\\176';"
90              + "var testString = '1234{|}~ABC';";
91          test(initialScript, "testString.match(new RegExp(testPattern))");
92      }
93  
94      /**
95       * Tests 'canthisbeFOUND'.match(new RegExp('[A-\\132]+')).
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-\\\\132]+'))");
102     }
103 
104     /**
105      * Tests 'canthisbeFOUND'.match(new RegExp('[\\141-\\172]+')).
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('[\\\\141-\\\\172]+'))");
112     }
113 
114     /**
115      * Tests 'canthisbeFOUND'.match(/[\141-\172]+/).
116      * @throws Exception if the test fails
117      */
118     @Test
119     @Alerts("canthisbe")
120     public void test8() throws Exception {
121         test("'canthisbeFOUND'.match(/[\\141-\\172]+/)");
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 }