|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * |
|
8 * Date: 12 December 2003 |
|
9 * SUMMARY: Testing regexps with unescaped braces |
|
10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=228087 |
|
11 * |
|
12 * Note: unbalanced, unescaped braces are not permitted by ECMA-262 Ed.3, |
|
13 * but we decided to follow Perl and IE and allow this for compatibility. |
|
14 * |
|
15 * See http://bugzilla.mozilla.org/show_bug.cgi?id=188206 and its testcase. |
|
16 * See http://bugzilla.mozilla.org/show_bug.cgi?id=223273 and its testcase. |
|
17 */ |
|
18 //----------------------------------------------------------------------------- |
|
19 var i = 0; |
|
20 var BUGNUMBER = 228087; |
|
21 var summary = 'Testing regexps with unescaped braces'; |
|
22 var status = ''; |
|
23 var statusmessages = new Array(); |
|
24 var pattern = ''; |
|
25 var patterns = new Array(); |
|
26 var string = ''; |
|
27 var strings = new Array(); |
|
28 var actualmatch = ''; |
|
29 var actualmatches = new Array(); |
|
30 var expectedmatch = ''; |
|
31 var expectedmatches = new Array(); |
|
32 var e; |
|
33 |
|
34 |
|
35 string = 'foo {1} foo {2} foo'; |
|
36 |
|
37 // try an example with the braces escaped |
|
38 status = inSection(1); |
|
39 try |
|
40 { |
|
41 pattern = new RegExp('\{1.*\}', 'g'); |
|
42 actualmatch = string.match(pattern); |
|
43 } |
|
44 catch (e) |
|
45 { |
|
46 pattern = 'error'; |
|
47 actualmatch = ''; |
|
48 } |
|
49 expectedmatch = Array('{1} foo {2}'); |
|
50 addThis(); |
|
51 |
|
52 // just like Section 1, without the braces being escaped |
|
53 status = inSection(2); |
|
54 try |
|
55 { |
|
56 pattern = new RegExp('{1.*}', 'g'); |
|
57 actualmatch = string.match(pattern); |
|
58 } |
|
59 catch (e) |
|
60 { |
|
61 pattern = 'error'; |
|
62 actualmatch = ''; |
|
63 } |
|
64 expectedmatch = Array('{1} foo {2}'); |
|
65 addThis(); |
|
66 |
|
67 // try an example with the braces escaped |
|
68 status = inSection(3); |
|
69 try |
|
70 { |
|
71 pattern = new RegExp('\{1[.!\}]*\}', 'g'); |
|
72 actualmatch = string.match(pattern); |
|
73 } |
|
74 catch (e) |
|
75 { |
|
76 pattern = 'error'; |
|
77 actualmatch = ''; |
|
78 } |
|
79 expectedmatch = Array('{1}'); |
|
80 addThis(); |
|
81 |
|
82 // just like Section 3, without the braces being escaped |
|
83 status = inSection(4); |
|
84 try |
|
85 { |
|
86 pattern = new RegExp('{1[.!}]*}', 'g'); |
|
87 actualmatch = string.match(pattern); |
|
88 } |
|
89 catch (e) |
|
90 { |
|
91 pattern = 'error'; |
|
92 actualmatch = ''; |
|
93 } |
|
94 expectedmatch = Array('{1}'); |
|
95 addThis(); |
|
96 |
|
97 |
|
98 string = 'abccccc{3 }c{ 3}c{3, }c{3 ,}c{3 ,4}c{3, 4}c{3,4 }de'; |
|
99 |
|
100 // use braces in a normal quantifier construct |
|
101 status = inSection(5); |
|
102 try |
|
103 { |
|
104 pattern = new RegExp('c{3}'); |
|
105 actualmatch = string.match(pattern); |
|
106 } |
|
107 catch (e) |
|
108 { |
|
109 pattern = 'error'; |
|
110 actualmatch = ''; |
|
111 } |
|
112 expectedmatch = Array('ccc'); |
|
113 addThis(); |
|
114 |
|
115 // now disrupt the quantifer - the braces should now be interpreted literally |
|
116 status = inSection(6); |
|
117 try |
|
118 { |
|
119 pattern = new RegExp('c{3 }'); |
|
120 actualmatch = string.match(pattern); |
|
121 } |
|
122 catch (e) |
|
123 { |
|
124 pattern = 'error'; |
|
125 actualmatch = ''; |
|
126 } |
|
127 expectedmatch = Array('c{3 }'); |
|
128 addThis(); |
|
129 |
|
130 status = inSection(7); |
|
131 try |
|
132 { |
|
133 pattern = new RegExp('c{3.}'); |
|
134 actualmatch = string.match(pattern); |
|
135 } |
|
136 catch (e) |
|
137 { |
|
138 pattern = 'error'; |
|
139 actualmatch = ''; |
|
140 } |
|
141 expectedmatch = Array('c{3 }'); |
|
142 addThis(); |
|
143 |
|
144 status = inSection(8); |
|
145 try |
|
146 { |
|
147 // need to escape the \ in \s since |
|
148 // this has been converted to a constructor call |
|
149 // instead of a literal regexp |
|
150 pattern = new RegExp('c{3\\s}'); |
|
151 actualmatch = string.match(pattern); |
|
152 } |
|
153 catch (e) |
|
154 { |
|
155 pattern = 'error'; |
|
156 actualmatch = ''; |
|
157 } |
|
158 expectedmatch = Array('c{3 }'); |
|
159 addThis(); |
|
160 |
|
161 status = inSection(9); |
|
162 try |
|
163 { |
|
164 pattern = new RegExp('c{3[ ]}'); |
|
165 actualmatch = string.match(pattern); |
|
166 } |
|
167 catch (e) |
|
168 { |
|
169 pattern = 'error'; |
|
170 actualmatch = ''; |
|
171 } |
|
172 expectedmatch = Array('c{3 }'); |
|
173 addThis(); |
|
174 |
|
175 status = inSection(10); |
|
176 try |
|
177 { |
|
178 pattern = new RegExp('c{ 3}'); |
|
179 actualmatch = string.match(pattern); |
|
180 } |
|
181 catch (e) |
|
182 { |
|
183 pattern = 'error'; |
|
184 actualmatch = ''; |
|
185 } |
|
186 expectedmatch = Array('c{ 3}'); |
|
187 addThis(); |
|
188 |
|
189 // using braces in a normal quantifier construct again |
|
190 status = inSection(11); |
|
191 try |
|
192 { |
|
193 pattern = new RegExp('c{3,}'); |
|
194 actualmatch = string.match(pattern); |
|
195 } |
|
196 catch (e) |
|
197 { |
|
198 pattern = 'error'; |
|
199 actualmatch = ''; |
|
200 } |
|
201 expectedmatch = Array('ccccc'); |
|
202 addThis(); |
|
203 |
|
204 // now disrupt it - the braces should now be interpreted literally |
|
205 status = inSection(12); |
|
206 try |
|
207 { |
|
208 pattern = new RegExp('c{3, }'); |
|
209 actualmatch = string.match(pattern); |
|
210 } |
|
211 catch (e) |
|
212 { |
|
213 pattern = 'error'; |
|
214 actualmatch = ''; |
|
215 } |
|
216 expectedmatch = Array('c{3, }'); |
|
217 addThis(); |
|
218 |
|
219 status = inSection(13); |
|
220 try |
|
221 { |
|
222 pattern = new RegExp('c{3 ,}'); |
|
223 actualmatch = string.match(pattern); |
|
224 } |
|
225 catch (e) |
|
226 { |
|
227 pattern = 'error'; |
|
228 actualmatch = ''; |
|
229 } |
|
230 expectedmatch = Array('c{3 ,}'); |
|
231 addThis(); |
|
232 |
|
233 // using braces in a normal quantifier construct again |
|
234 status = inSection(14); |
|
235 try |
|
236 { |
|
237 pattern = new RegExp('c{3,4}'); |
|
238 actualmatch = string.match(pattern); |
|
239 } |
|
240 catch (e) |
|
241 { |
|
242 pattern = 'error'; |
|
243 actualmatch = ''; |
|
244 } |
|
245 expectedmatch = Array('cccc'); |
|
246 addThis(); |
|
247 |
|
248 // now disrupt it - the braces should now be interpreted literally |
|
249 status = inSection(15); |
|
250 try |
|
251 { |
|
252 pattern = new RegExp('c{3 ,4}'); |
|
253 actualmatch = string.match(pattern); |
|
254 } |
|
255 catch (e) |
|
256 { |
|
257 pattern = 'error'; |
|
258 actualmatch = ''; |
|
259 } |
|
260 expectedmatch = Array('c{3 ,4}'); |
|
261 addThis(); |
|
262 |
|
263 status = inSection(16); |
|
264 try |
|
265 { |
|
266 pattern = new RegExp('c{3, 4}'); |
|
267 actualmatch = string.match(pattern); |
|
268 } |
|
269 catch (e) |
|
270 { |
|
271 pattern = 'error'; |
|
272 actualmatch = ''; |
|
273 } |
|
274 expectedmatch = Array('c{3, 4}'); |
|
275 addThis(); |
|
276 |
|
277 status = inSection(17); |
|
278 try |
|
279 { |
|
280 pattern = new RegExp('c{3,4 }'); |
|
281 actualmatch = string.match(pattern); |
|
282 } |
|
283 catch (e) |
|
284 { |
|
285 pattern = 'error'; |
|
286 actualmatch = ''; |
|
287 } |
|
288 expectedmatch = Array('c{3,4 }'); |
|
289 addThis(); |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 //------------------------------------------------------------------------------------------------- |
|
295 test(); |
|
296 //------------------------------------------------------------------------------------------------- |
|
297 |
|
298 |
|
299 |
|
300 function addThis() |
|
301 { |
|
302 statusmessages[i] = status; |
|
303 patterns[i] = pattern; |
|
304 strings[i] = string; |
|
305 actualmatches[i] = actualmatch; |
|
306 expectedmatches[i] = expectedmatch; |
|
307 i++; |
|
308 } |
|
309 |
|
310 |
|
311 function test() |
|
312 { |
|
313 enterFunc ('test'); |
|
314 printBugNumber(BUGNUMBER); |
|
315 printStatus (summary); |
|
316 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); |
|
317 exitFunc ('test'); |
|
318 } |