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/octal.js'.
25   *
26   * @author Ahmed Ashour
27   */
28  @RunWith(BrowserRunner.class)
29  public class OctalTest extends WebDriverTestCase {
30  
31      /**
32       * Tests testString.match(new RegExp(testPattern)).
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
37      public void test1() throws Exception {
38          final String initialScript = "var testPattern = '\\\\101\\\\102\\\\103\\\\104\\\\105\\\\106\\\\107\\\\110"
39              + "\\\\111\\\\112\\\\113\\\\114\\\\115\\\\116\\\\117\\\\120\\\\121\\\\122\\\\123\\\\124\\\\125\\\\126"
40              + "\\\\127\\\\130\\\\131\\\\132';"
41              + "var testString = '12345ABCDEFGHIJKLMNOPQRSTUVWXYZ67890';";
42          test(initialScript, "testString.match(new RegExp(testPattern))");
43      }
44  
45      /**
46       * Tests testString.match(new RegExp(testPattern)).
47       * @throws Exception if the test fails
48       */
49      @Test
50      @Alerts("abcdefghijklmnopqrstuvwxyz")
51      public void test2() throws Exception {
52          final String initialScript = "var testPattern = '\\\\141\\\\142\\\\143\\\\144\\\\145\\\\146\\\\147\\\\150"
53              + "\\\\151\\\\152\\\\153\\\\154\\\\155\\\\156\\\\157\\\\160\\\\161\\\\162\\\\163\\\\164\\\\165\\\\166"
54              + "\\\\167\\\\170\\\\171\\\\172';"
55              + "var testString = '12345AabcdefghijklmnopqrstuvwxyzZ67890';";
56          test(initialScript, "testString.match(new RegExp(testPattern))");
57      }
58  
59      /**
60       * Tests testString.match(new RegExp(testPattern)).
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts(" !\"#$%&'()*+,-./0123")
65      public void test3() throws Exception {
66          final String initialScript = "var testPattern = '\\\\40\\\\41\\\\42\\\\43\\\\44\\\\45\\\\46\\\\47\\\\50"
67              + "\\\\51\\\\52\\\\53\\\\54\\\\55\\\\56\\\\57\\\\60\\\\61\\\\62\\\\63';"
68              + "var testString = 'abc !\"#$%&\\'()*+,-./0123ZBC';";
69          test(initialScript, "testString.match(new RegExp(testPattern))");
70      }
71  
72      /**
73       * Tests testString.match(new RegExp(testPattern)).
74       * @throws Exception if the test fails
75       */
76      @Test
77      @Alerts("456789:;<=>?@")
78      public void test4() throws Exception {
79          final String initialScript = "var testPattern = '\\\\64\\\\65\\\\66\\\\67\\\\70\\\\71\\\\72\\\\73\\\\74\\\\75"
80              + "\\\\76\\\\77\\\\100';"
81              + "var testString = '123456789:;<=>?@ABC';";
82          test(initialScript, "testString.match(new RegExp(testPattern))");
83      }
84  
85      /**
86       * Tests testString.match(new RegExp(testPattern)).
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts("{|}~")
91      public void test5() throws Exception {
92          final String initialScript = "var testPattern = '\\\\173\\\\174\\\\175\\\\176';"
93              + "var testString = '1234{|}~ABC';";
94          test(initialScript, "testString.match(new RegExp(testPattern))");
95      }
96  
97      /**
98       * Tests 'canthisbeFOUND'.match(new RegExp('[A-\\132]+')).
99       * @throws Exception if the test fails
100      */
101     @Test
102     @Alerts("FOUND")
103     public void test6() throws Exception {
104         test("'canthisbeFOUND'.match(new RegExp('[A-\\\\132]+'))");
105     }
106 
107     /**
108      * Tests 'canthisbeFOUND'.match(new RegExp('[\\141-\\172]+')).
109      * @throws Exception if the test fails
110      */
111     @Test
112     @Alerts("canthisbe")
113     public void test7() throws Exception {
114         test("'canthisbeFOUND'.match(new RegExp('[\\\\141-\\\\172]+'))");
115     }
116 
117     /**
118      * Tests 'canthisbeFOUND'.match(/[\141-\172]+/).
119      * @throws Exception if the test fails
120      */
121     @Test
122     @Alerts("canthisbe")
123     public void test8() throws Exception {
124         test("'canthisbeFOUND'.match(/[\\141-\\172]+/)");
125     }
126 
127     private void test(final String script) throws Exception {
128         test(null, script);
129     }
130 
131     private void test(final String initialScript, final String script) throws Exception {
132         String html = "<html><head>\n"
133                 + "</head><body>\n"
134                 + LOG_TEXTAREA
135                 + "<script>\n"
136                 + LOG_TEXTAREA_FUNCTION;
137         if (initialScript != null) {
138             html += initialScript + ";\n";
139         }
140         html += "  log(" + script + ");\n"
141             + "</script>\n"
142             + "</body></html>";
143         loadPageVerifyTextArea2(html);
144     }
145 }