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