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/digit.js'.
25   *
26   * @author Ahmed Ashour
27   * @author Frank Danek
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class DigitTest extends WebDriverTestCase {
32  
33      private static final String NON_DIGITS = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
34          + "\\f\\n\\r\\t\\v~`!@#$%^&*()-+={[}]|\\\\:;\\'<,>./? \"";
35  
36      private static final String NON_DIGITS_EXPECTED = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
37          + "\f\\n\\r\t\u000B~`!@#$%^&*()-+={[}]|\\:;\'<,>./? \"";
38  
39      private static final String DIGITS = "1234567890";
40  
41      /**
42       * Tests digits.match(new RegExp('\\d+')).
43       * @throws Exception if the test fails
44       */
45      @Test
46      @Alerts(DIGITS)
47      public void test1() throws Exception {
48          final String initialScript = "var digits = '" + DIGITS + "'";
49          test(initialScript, "digits.match(new RegExp('\\\\d+'))");
50      }
51  
52      /**
53       * Tests non_digits.match(new RegExp('\\D+')).
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts(NON_DIGITS_EXPECTED)
58      public void test2() throws Exception {
59          final String initialScript = "var non_digits = '" + NON_DIGITS + "'";
60          test(initialScript, "non_digits.match(new RegExp('\\\\D+'))");
61      }
62  
63      /**
64       * Tests non_digits.match(new RegExp('\\d')).
65       * @throws Exception if the test fails
66       */
67      @Test
68      @Alerts("null")
69      public void test3() throws Exception {
70          final String initialScript = "var non_digits = '" + NON_DIGITS + "'";
71          test(initialScript, "non_digits.match(new RegExp('\\\\d'))");
72      }
73  
74      /**
75       * Tests digits.match(new RegExp('\\D')).
76       * @throws Exception if the test fails
77       */
78      @Test
79      @Alerts("null")
80      public void test4() throws Exception {
81          final String initialScript = "var digits = '" + DIGITS + "'";
82          test(initialScript, "digits.match(new RegExp('\\\\D'))");
83      }
84  
85      /**
86       * Tests s.match(new RegExp('\\d+')).
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts(DIGITS)
91      public void test5() throws Exception {
92          final String initialScript = "var s = '" + NON_DIGITS + DIGITS + "'";
93          test(initialScript, "s.match(new RegExp('\\\\d+'))");
94      }
95  
96      /**
97       * Tests s.match(new RegExp('\\D+')).
98       * @throws Exception if the test fails
99       */
100     @Test
101     @Alerts(NON_DIGITS_EXPECTED)
102     public void test6() throws Exception {
103         final String initialScript = "var s = '" + DIGITS + NON_DIGITS + "'";
104         test(initialScript, "s.match(new RegExp('\\\\D+'))");
105     }
106 
107     /**
108      * Tests s.match(new RegExp('\\d')).
109      * @throws Exception if the test fails
110      */
111     @Test
112     public void test7() throws Exception {
113         for (int i = 0; i < DIGITS.length(); i++) {
114             final String initialScript = "var s = 'ab" + DIGITS.charAt(i) + "cd'";
115             setExpectedAlerts(String.valueOf(DIGITS.charAt(i)));
116             test(initialScript, "s.match(new RegExp('\\\\d'))");
117             test(initialScript, "s.match(/\\d/)");
118         }
119     }
120 
121     /**
122      * Tests s.match(new RegExp('\\D')).
123      * @throws Exception if the test fails
124      */
125     @Test
126     public void test8() throws Exception {
127         for (int i = 0; i < NON_DIGITS.length() - 1; i++) {
128             final char ch = NON_DIGITS.charAt(i);
129             String expected = String.valueOf(ch);
130             String input = expected;
131             switch (ch) {
132                 case '\\':
133                     input = "\\" + ch;
134                     break;
135 
136                 case '\'':
137                     input = "\\" + ch;
138                     break;
139 
140                 case 'f':
141                     expected = "\f";
142                     input = "\\" + ch;
143                     break;
144 
145                 case 'n':
146                     expected = "\\n";
147                     input = "\\" + ch;
148                     break;
149 
150                 case 'r':
151                     expected = "\\r";
152                     input = "\\" + ch;
153                     break;
154 
155                 case 't':
156                     expected = "\t";
157                     input = "\\" + ch;
158                     break;
159 
160                 case 'v':
161                     expected = "\u000B";
162                     input = "\\" + ch;
163                     break;
164 
165                 default:
166             }
167 
168             setExpectedAlerts(expected);
169 
170             final String initialScript = "var s = '12" + input + "34'";
171             test(initialScript, "s.match(new RegExp('\\\\D'))");
172             test(initialScript, "s.match(/\\D/)");
173         }
174     }
175 
176     private void test(final String initialScript, final String script) throws Exception {
177         String html = "<html><head>\n"
178                 + "</head><body>"
179                 + LOG_TEXTAREA
180                 + "<script>\n"
181                 + LOG_TEXTAREA_FUNCTION;
182         if (initialScript != null) {
183             html += initialScript + ";\n";
184         }
185         html += "  var txt = '' + " + script + ";\n"
186             + "  txt = txt.replace('\\r', '\\\\r');\n"
187             + "  txt = txt.replace('\\n', '\\\\n');\n"
188             + "  log(txt);\n"
189             + "</script>\n"
190             + "</body></html>";
191         loadPageVerifyTextArea2(html);
192     }
193 }