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.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22
23
24
25
26
27
28
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
43
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
54
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
65
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
76
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
87
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
98
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
109
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
123
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 }