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 @RunWith(BrowserRunner.class)
30 public class AlphanumericTest extends WebDriverTestCase {
31
32 private static final String NON_ALPHANUMERIC = "~`!@#$%^&*()-+={[}]|\\\\:;\\'<,>./?\\f\\n\\r\\t \"\\v";
33 private static final String ALPHANUMERIC = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
34
35
36
37
38
39 @Test
40 @Alerts(ALPHANUMERIC)
41 public void test1() throws Exception {
42 test("'" + ALPHANUMERIC + "'.match(new RegExp('\\\\w+'))", false);
43 }
44
45
46
47
48
49 @Test
50 @Alerts("7E-60-21-40-23-24-25-5E-26-2A-28-29-2D-2B-3D-7B-5B-7D-5D-7C-5C-"
51 + "3A-3B-27-3C-2C-3E-2E-2F-3F-C-A-D-9-20-22-B-")
52 public void test2() throws Exception {
53 test("'" + NON_ALPHANUMERIC + "'.match(new RegExp('\\\\W+'))", true);
54 }
55
56
57
58
59
60 @Test
61 @Alerts("null")
62 public void test3() throws Exception {
63 test("'" + NON_ALPHANUMERIC + "'.match(new RegExp('\\\\w+'))", false);
64 }
65
66
67
68
69
70 @Test
71 @Alerts("null")
72 public void test4() throws Exception {
73 test("'" + ALPHANUMERIC + "'.match(new RegExp('\\\\W+'))", false);
74 }
75
76
77
78
79
80 @Test
81 @Alerts(ALPHANUMERIC)
82 public void test5() throws Exception {
83 test("'" + NON_ALPHANUMERIC + ALPHANUMERIC + "'.match(new RegExp('\\\\w+'))", false);
84 }
85
86
87
88
89
90 @Test
91 @Alerts("7E-60-21-40-23-24-25-5E-26-2A-28-29-2D-2B-3D-7B-5B-7D-5D-7C-5C-"
92 + "3A-3B-27-3C-2C-3E-2E-2F-3F-C-A-D-9-20-22-B-")
93 public void test6() throws Exception {
94 test("'" + ALPHANUMERIC + NON_ALPHANUMERIC + "'.match(new RegExp('\\\\W+'))", true);
95 }
96
97
98
99
100
101 @Test
102 @Alerts(ALPHANUMERIC)
103 public void test7() throws Exception {
104 test("'" + ALPHANUMERIC + NON_ALPHANUMERIC + "'.match(/\\w+/)", false);
105 }
106
107
108
109
110
111 @Test
112 @Alerts("7E-60-21-40-23-24-25-5E-26-2A-28-29-2D-2B-3D-7B-5B-7D-5D-7C-5C-"
113 + "3A-3B-27-3C-2C-3E-2E-2F-3F-C-A-D-9-20-22-B-")
114 public void test8() throws Exception {
115 test("'" + ALPHANUMERIC + NON_ALPHANUMERIC + "'.match(/\\W+/)", true);
116 }
117
118
119
120
121
122 @Test
123 @Alerts("abcd*&^%$$,abcd,%$$")
124 public void test9() throws Exception {
125 test("'" + "abcd*&^%$$" + "'.match(/(\\w+)...(\\W+)/)", false);
126 }
127
128
129
130
131
132 @Test
133 public void test10() throws Exception {
134 for (int i = 0; i < ALPHANUMERIC.length(); i++) {
135 setExpectedAlerts(String.valueOf(ALPHANUMERIC.charAt(i)));
136 test("'" + "#$" + ALPHANUMERIC.charAt(i) + "%^" + "'.match(new RegExp('\\\\w'))", false);
137 }
138 }
139
140
141
142
143
144 @Test
145 @Alerts("\n")
146 public void test11() throws Exception {
147 final String[] exp = getExpectedAlerts();
148 for (int i = 0; i < NON_ALPHANUMERIC.length() - 1; i++) {
149 final char ch = NON_ALPHANUMERIC.charAt(i);
150 String expected = String.valueOf(ch);
151 String input = expected;
152 switch (ch) {
153 case '\\':
154 input = "\\" + ch;
155 break;
156
157 case '\'':
158 input = "\\" + ch;
159 break;
160
161 case 'f':
162 expected = "\f";
163 input = "\\" + ch;
164 break;
165
166 case 'n':
167 expected = "\n";
168 input = "\\" + ch;
169 break;
170
171 case 'r':
172 expected = exp[0];
173 input = "\\" + ch;
174 break;
175
176 case 't':
177 expected = "\t";
178 input = "\\" + ch;
179 break;
180
181 case 'v':
182 expected = "\u000B";
183 input = "\\" + ch;
184 break;
185
186 default:
187 }
188
189 setExpectedAlerts(expected);
190
191 final String s = "sd" + input + String.valueOf((i + 10) * (i + 10) - 2 * (i + 10));
192 test("'" + s + "'.match(new RegExp('\\\\W'))", false);
193 }
194 }
195
196 private void test(final String script, final boolean charCode) throws Exception {
197 String html
198 = "<html><head><script>\n";
199 if (charCode) {
200 html += " var string = " + script + ".toString();\n"
201 + " var output = '';\n"
202 + " for (var i = 0; i < string.length; i++) {\n"
203 + " output += string.charCodeAt(i).toString(16).toUpperCase() + '-';\n"
204 + " }\n"
205 + " alert(output);\n";
206 }
207 else {
208 html += " alert(" + script + ");\n";
209 }
210 html += "</script></head><body>\n"
211 + "</body></html>";
212
213 loadPageWithAlerts2(html);
214 }
215 }