1
2
3
4
5
6
7
8
9
10
11
12
13
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
23
24
25
26
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
40
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
51
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
62
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
73
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
84
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
95
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
106
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
120
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 }