Debian Codegraph @

tests/spec/s-xregexp-natives.js

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
•1 const XRegExp = require("../../src/index.js");
2 const { repeat, REGEX_DATA, hasNativeU, hasNativeY, hasStrictMode } = require("../helpers/h.js");
3
4 describe('When overridden, RegExp.prototype.exec()', function() {
5
6 beforeEach(function() {
7 XRegExp.install('natives');
8 });
9
10 // This is broken in old Firefox (tested in v2.0; it works in v8+), but not for any fault of
11 // XRegExp. Uncomment this test if future XRegExp fixes it for old Firefox.
12 /*it('should type convert an undefined argument to a string', function() {
13 expect(/undefined/.exec()).toEqualMatch(['undefined']);
14 });*/
15
16 /*
17 * NOTE: These specs should mirror those for RegExp.prototype.test, nonglobal
18 * String.prototype.match, XRegExp.exec, and XRegExp.test as closely as possible.
19 */
20
21 // TODO: Copy/update specs from XRegExp.exec here
22
23 /*
24 * The following specs:
25 * - Have no corresponding specs for XRegExp.exec and XRegExp.test.
26 * - Are mirrored by RegExp.prototype.test and nonglobal String.prototype.match.
27 */
28
29 it('should ignore lastIndex and set the search start position at 0 for a nonglobal regex', function() {
30 var regex = /x/;
31
32 regex.lastIndex = 4;
33 expect(regex.exec('123x5')).toEqualMatch(['x']);
34 });
35
36 /*
37 * The following specs:
38 * - Have no corresponding specs for XRegExp.exec, XRegExp.test, and nonglobal
39 * String.prototype.match.
40 * - Are mirrored by RegExp.prototype.test.
41 */
42
43 it('should use lastIndex to set the search start position for a global regex', function() {
44 var regex = /x/g;
45
46 regex.lastIndex = 4;
47 expect(regex.exec('123x5')).toBeNull();
48
49 regex.lastIndex = 2;
50 expect(regex.exec('123x5')).toEqualMatch(['x']);
51 });
52
53 it('should type convert lastIndex when setting the search start position', function() {
54 var regex = /x/g;
55
56 regex.lastIndex = '3';
57 expect(regex.exec('123x5')).toEqualMatch(['x']);
58
59 regex.lastIndex = '4';
60 expect(regex.exec('123x5')).toBeNull();
61 });
62
63 /*
64 * The following specs:
65 * - Have no corresponding specs for RegExp.prototype.test and XRegExp.test.
66 * - Are mirrored by XRegExp.exec and nonglobal String.prototype.match.
67 */
68
•69 // xdescribe('provides extensions:', function() {
70 // // TODO: Copy/update named capture specs from XRegExp.exec here
71 // });
72
73 });
74
75 describe('When overridden, RegExp.prototype.test()', function() {
76
77 beforeEach(function() {
78 XRegExp.install('natives');
79 });
80
81 /*
82 * NOTE: These specs should mirror those for RegExp.prototype.exec, nonglobal
83 * String.prototype.match, XRegExp.test, and XRegExp.exec as closely as possible.
84 */
85
86 // TODO: Copy/update specs from XRegExp.test here
87
88 /*
89 * The following specs:
90 * - Have no corresponding specs for XRegExp.test and XRegExp.exec.
91 * - Are mirrored by RegExp.prototype.exec and nonglobal String.prototype.match.
92 */
93
94 it('should ignore lastIndex and set the search start position at 0 for a nonglobal regex', function() {
95 var regex = /x/;
96
97 regex.lastIndex = 4;
98 expect(regex.test('123x5')).toBe(true);
99 });
100
101 /*
102 * The following specs:
103 * - Have no corresponding specs for XRegExp.test, XRegExp.exec, and nonglobal
104 * String.prototype.match.
105 * - Are mirrored by RegExp.prototype.exec.
106 */
107
108 it('should use lastIndex to set the search start position for a global regex', function() {
109 var regex = /x/g;
110
111 regex.lastIndex = 4;
112 expect(regex.test('123x5')).toBe(false);
113
114 regex.lastIndex = 2;
115 expect(regex.test('123x5')).toBe(true);
116 });
117
118 it('should type convert lastIndex when setting the search start position', function() {
119 var regex = /x/g;
120
121 regex.lastIndex = '3';
122 expect(regex.test('123x5')).toBe(true);
123
124 regex.lastIndex = '4';
125 expect(regex.test('123x5')).toBe(false);
126 });
127
128 // NOTE: The fixed RegExp.prototype.test does not provide any extensions to native handling
129
130 });
131
132 describe('When overridden, String.prototype.match()', function() {
133
134 beforeEach(function() {
135 XRegExp.install('natives');
136 });
137
138 describe('with a global regex', function() {
139
140 it('should return an array with all matches', function() {
141 expect('a bc'.match(/(\w)/g)).toEqualMatch(['a', 'b', 'c']);
142 });
143
144 it('should return null if no match is found', function() {
145 expect('a bc'.match(/x/g)).toBeNull();
146 });
147
148 it('should reset lastIndex to 0 when a match is found', function() {
149 var regex = /x/g;
150 regex.lastIndex = 1;
151 '123x5'.match(regex);
152
153 expect(regex.lastIndex).toBe(0);
154 });
155
156 it('should reset lastIndex to 0 when no match is found', function() {
157 var regex = /x/g;
158 regex.lastIndex = 1;
159 '123'.match(regex);
160
161 expect(regex.lastIndex).toBe(0);
162 });
163
164 it('should start the search at the beginning of the string, ignoring lastIndex', function() {
165 var regex = /x/g;
166 regex.lastIndex = 4;
167
168 expect('123x5'.match(regex)).toBeTruthy();
169 });
170
171 it('should convert any nonstring context to a string (except null and undefined)', function() {
172 expect(String.prototype.match.call(11, /1/g)).toEqualMatch(['1', '1']);
173 });
174
175 it('should throw an exception when called on null or undefined context, if strict mode is supported', function() {
176 [null, undefined].forEach(function(value) {
177 // This doesn't work the same when strict mode isn't supported, because the match
178 // method will be called with the global object (window) as its context, rather
179 // than null or undefined
180 if (hasStrictMode) {
181 expect(function() {String.prototype.match.call(value, /x/g);}).toThrowError(TypeError);
182 } else {
183 // Keep the assertion count consistent cross-browser
184 expect(hasStrictMode).toBe(false);
185 }
186 });
187 });
188
189 });
190
191 describe('with a nonglobal regex', function() {
192
193 /*
194 * NOTE: These specs should mirror those for RegExp.prototype.exec, RegExp.prototype.test,
195 * XRegExp.exec, and XRegExp.test as closely as possible.
196 */
197
198 // TODO: Copy/update specs from RegExp.prototype.exec here
199
200 /*
201 * The following specs:
202 * - Have no corresponding specs for RegExp.prototype.exec, RegExp.prototype.test,
203 * XRegExp.test, and XRegExp.exec.
204 */
205
206 it('should convert any provided non RegExp object to a RegExp', function() {
207 // These don't error because, per the spec, the values are passed through new RegExp()
208 // before being used as the context object for the (fixed) RegExp.prototype.exec
209 var tests = [
210 {str: '12', regex: '^(1)', result: ['1', '1']},
211 // This would throw if the string was converted to an XRegExp rather than RegExp
212 {str: '\x01', regex: '\\1', result: ['\x01']},
213 // The converted value '[object Object]' creates a character class
214 {str: '[obj]', regex: {}, result: ['o']},
215 {str: 'null', regex: null, result: ['null']}
216 ];
217
218 tests.forEach(function(test) {
219 expect(test.str.match(test.regex)).toEqualMatch(test.result);
220 });
221
222 // The native RegExp with an explicit undefined pattern incorrectly creates /undefined/
223 // in Firefox 3 and 3.6 (but not v4)
224 //expect('undefined'.match()).toEqualMatch(['']);
225 //expect('undefined'.match(undefined)).toEqualMatch(['']);
226 });
227
228 it('should throw an exception when called on null or undefined context, if strict mode is supported', function() {
229 [null, undefined].forEach(function(value) {
230 // This doesn't work the same when strict mode isn't supported, because the match
231 // method will be called with the global object (window) as its context, rather
232 // than null or undefined
233 if (hasStrictMode) {
234 expect(function() {String.prototype.match.call(value, /x/);}).toThrowError(TypeError);
235 } else {
236 // Keep the assertion count consistent cross-browser
237 expect(hasStrictMode).toBe(false);
238 }
239 });
240 });
241
242 /*
243 * The following specs:
244 * - Have no corresponding specs for RegExp.prototype.test and XRegExp.test.
245 * - Are mirrored by RegExp.prototype.exec and XRegExp.exec.
246 */
247
•248 // xdescribe('provides extensions:', function() {
249 // // TODO: Copy/update specs from RegExp.prototype.exec here
250 // });
251
252 });
253
254 });
255
256 describe('When overridden, String.prototype.replace()', function() {
257
258 beforeEach(function() {
259 XRegExp.install('natives');
260 });
261
262 /*
263 * NOTE: These specs should mirror those for XRegExp.replace as closely as possible.
264 */
265
266 it('should replace the first match only when given a nonglobal regex', function() {
267 expect('aaa'.replace(/a/, 'b')).toBe('baa');
268 });
269
270 it('should replace all matches when given a global regex', function() {
271 expect('aaa'.replace(/a/g, 'b')).toBe('bbb');
272 });
273
274 it('should replace the first match only when given a string as the search pattern', function() {
275 expect('aaa'.replace('a', 'b')).toBe('baa');
276 });
277
278 it('should not type convert a string search pattern to a regex', function() {
279 expect('aaa'.replace('a(a)', 'b')).toBe('aaa');
280 expect('a(a)a'.replace('a(a)', 'b')).toBe('ba');
281 });
282
283 it('should handle single-digit backreference $1 in the replacement string', function() {
284 expect('aaa'.replace(/a(a)/, '$1b')).toBe('aba');
285
286 // Backreference to a nonparticipating capturing group
287 expect('test'.replace(/t|(e)/g, '$1')).toBe('es');
288 });
289
290 it('should handle double-digit backreferences $01, $10, and $99 in the replacement string', function() {
291 expect('aaa'.replace(/a(a)/, '$01b')).toBe('aba');
292 expect('aaa'.replace(new RegExp('a' + repeat('()', 9) + '(a)'), '$10b')).toBe('aba');
293 expect('aaa'.replace(new RegExp('a' + repeat('()', 98) + '(a)'), '$99b')).toBe('aba');
294 });
295
296 it('should end backreferences in the replacement string after two digits', function() {
297 expect('aaa'.replace(new RegExp('a' + repeat('()', 99) + '(a)'), '$100b')).toBe('0ba');
298 });
299
300 // NOTE: IE < 9 incorrectly treats all occurrences of $ as literal text when performing a
301 // replacement based on a search value that is not a regex. XRegExp restores the special
302 // meaning of $$, $&, etc. for all replacements.
303
304 it('should handle backreference $& in the replacement string', function() {
305 expect('aaa'.replace(/aa/, '$&b')).toBe('aaba');
306 expect('aaa'.replace('aa', '$&b')).toBe('aaba');
307 });
308
309 it("should handle right context token $' in the replacement string", function() {
310 expect('aaa'.replace(/aa/, "$'b")).toBe('aba');
311 expect('aaa'.replace('aa', "$'b")).toBe('aba');
312 });
313
314 it('should handle left context token $` in the replacement string', function() {
315 expect('xaaa'.replace(/aa/, '$`b')).toBe('xxba');
316 expect('xaaa'.replace('aa', '$`b')).toBe('xxba');
317 });
318
319 it('should handle token $$ in the replacement string', function() {
320 expect('aaa'.replace(/aa/, '$$b')).toBe('$ba');
321 expect('aaa'.replace('aa', '$$b')).toBe('$ba');
322 });
323
324 it('should allow a function to generate the replacement', function() {
325 expect('aaa'.replace(/a/, function() {return 'b';})).toBe('baa');
326 expect('aaa'.replace(/a/g, function() {return 'b';})).toBe('bbb');
327 expect('aaa'.replace('a', function() {return 'b';})).toBe('baa');
328 });
329
330 it('should allow using backreferences with replacement functions', function() {
331 expect('aaa'.replace(/aa/, function($0) {return $0 + 'b';})).toBe('aaba');
332 expect('aaa'.replace(/a(a)/, function($0, $1) {return $1 + 'b';})).toBe('aba');
333 expect('aaa'.replace('aa', function($0) {return $0 + 'b';})).toBe('aaba');
334 });
335
336 // Firefox (last tested v14.0.1) provides empty strings instead of undefined, but not for any
337 // fault of XRegExp
338 /*it('should provide undefined within replacement functions for nonparticipating capturing groups', function() {
339 expect('y'.replace(/(x)?y/, function($0, $1) {return String($1);})).toBe('undefined');
340 expect('y'.replace(/(x)?y/, function($0, $1) {return $1;})).toBe('undefined');
341 });*/
342
343 it('should not substitute tokens returned by replacement functions', function() {
344 // Regex search...
345 expect('aaa'.replace(/a(a)/, function($0, $1) {return '$1';})).toBe('$1a');
346 expect('aaa'.replace(/a/, function() {return '$&';})).toBe('$&aa');
347
348 // String search...
349 // This is broken in Safari (tested in Safari 5.1.2/7534.52.7), but not for any fault of
350 // XRegExp. Uncomment this test if future XRegExp fixes it for Safari.
351 //expect('aaa'.replace('a', function() {return '$&';})).toBe('$&aa');
352 });
353
354 it('should allow using the match position within replacement functions', function() {
355 expect('xaaa'.replace(/a/, function($0, pos) {return '' + pos;})).toBe('x1aa');
356 expect('xaaa'.replace(/a/g, function($0, pos) {return '' + pos;})).toBe('x123');
357 expect('xaaa'.replace(/(a)/g, function($0, $1, pos) {return '' + pos;})).toBe('x123');
358 expect('xaaa'.replace('a', function($0, pos) {return '' + pos;})).toBe('x1aa');
359 });
360
361 it('should allow using the source string within replacement functions', function() {
362 expect('xaaa'.replace(/a/, function($0, pos, str) {return str;})).toBe('xxaaaaa');
363 expect('xaaa'.replace(/(a)/, function($0, $1, pos, str) {return str;})).toBe('xxaaaaa');
364 expect('xaaa'.replace('a', function($0, pos, str) {return str;})).toBe('xxaaaaa');
365 });
366
367 it('should return string as the typeof the last argument in replacement functions', function() {
368 // NOTE: This tests a fix for IE < 9, which doesn't get this correct natively
369
370 expect('100'.replace(/0/, function($0, pos, str) {return typeof str;})).toBe('1string0');
371 expect(new String('100').replace(/0/, function($0, pos, str) {return typeof str;})).toBe('1string0');
372 expect(String.prototype.replace.call(100, /0/, function($0, pos, str) {return typeof str;})).toBe('1string0');
373 });
374
375 it('should handle nonstring context when using a replacement text token that references the subject text', function() {
376 expect(String.prototype.replace.call(0, /^/, '$`')).toBe('0');
377 });
378
379 it('should not modify the lastIndex of a nonglobal regex', function() {
380 var regex = /x/;
381
382 '123x567'.replace(regex, '_');
383 expect(regex.lastIndex).toBe(0);
384
385 regex.lastIndex = 1;
386 '123x567'.replace(regex, '_');
387 expect(regex.lastIndex).toBe(1);
388
389 'nomatch'.replace(regex, '_');
390 expect(regex.lastIndex).toBe(1);
391 });
392
393 it('should reset the lastIndex of a global regex to 0', function() {
394 var regex = /x/g;
395
396 regex.lastIndex = 1;
397 '123x567'.replace(regex, '_');
398 expect(regex.lastIndex).toBe(0);
399
400 regex.lastIndex = 1;
401 'nomatch'.replace(regex, '_');
402 expect(regex.lastIndex).toBe(0);
403 });
404
405 it('should ignore lastIndex when setting the search start position', function() {
406 [/x/, /x/g].forEach(function(regex) {
407 regex.lastIndex = 5;
408 expect('123x567'.replace(regex, '_')).toBe('123_567');
409 });
410 });
411
412 it('should update lastIndex during replacement iterations', function() {
413 var regex = /x/g;
414 var interimLastIndex = 0;
415 '1x2'.replace(regex, function() {
416 interimLastIndex = regex.lastIndex;
417 });
418
419 expect(interimLastIndex).toBe(2);
420 });
421
422 it('should convert any provided nonstring search to a string', function() {
423 var values = [
424 {target: '10x10', search: 10, replacement: 'x', expected: 'xx10'},
425 {target: 'xaaa,ba,b', search: ['a','b'], replacement: 'x', expected: 'xaaxa,b'},
426 {target: 'undefined', search: undefined, replacement: 'x', expected: 'x'}
427 ];
428
429 values.forEach(function(value) {
430 expect(value.target.replace(value.search, value.replacement)).toBe(value.expected);
431 });
432
433 // Implicit undefined search and replacement
434 expect('undefined'.replace()).toBe('undefined');
435 });
436
437 it('should convert any provided nonstring/nonfunction replacement to a string', function() {
438 var values = [
439 {target: 'xaaa', search: /a/g, replacement: 1.1, expected: 'x1.11.11.1'},
440 {target: 'xaaa', search: /a/g, replacement: ['a','b'], expected: 'xa,ba,ba,b'},
441 {target: 'x', search: /x/, replacement: /x/, expected: '/x/'},
442 {target: 'xaaa', search: /a/, replacement: undefined, expected: 'xundefinedaa'}
443 ];
444
445 values.forEach(function(value) {
446 expect(value.target.replace(value.search, value.replacement)).toBe(value.expected);
447 });
448
449 // Implicit undefined replacement
450 expect('xaaa'.replace(/a/)).toBe('xundefinedaa');
451 });
452
453 it('should convert any nonstring context to a string (except null and undefined)', function() {
454 var values = [
455 100,
456 {},
457 true,
458 false,
459 NaN,
460 ['a']
461 ];
462
463 values.forEach(function(value) {
464 expect(String.prototype.replace.call(value, /^/, 'x')).toBe('x' + value);
465 });
466 });
467
468 it('should throw an exception when called on null or undefined context, if strict mode is supported', function() {
469 [null, undefined].forEach(function(value) {
470 // This doesn't work the same when strict mode isn't supported, because the replace
471 // method will be called with the global object (window) as its context, rather
472 // than null or undefined
473 if (hasStrictMode) {
474 expect(function() {String.prototype.replace.call(value, /^/, '');}).toThrowError(TypeError);
475 } else {
476 // Keep the assertion count consistent cross-browser
477 expect(hasStrictMode).toBe(false);
478 }
479 });
480 });
481
482 describe('provides extensions:', function() {
483
484 it('should allow accessing named backreferences in a callback function as properties of the first argument', function() {
485 expect('abc'.replace(XRegExp('(?<name>.).'), function(match) {
486 return ':' + match.name + ':';
487 })).toBe(':a:c');
488 });
489
490 describe('supports new replacement text syntax:', function() {
491
492 describe('backreference $0', function() {
493
494 it('should work like $&', function() {
495 expect('xaaa'.replace(/aa/, '$0b')).toBe('xaaba');
496 expect('xaaa'.replace('aa', '$0b')).toBe('xaaba');
497 });
498
499 it('should be allowed as $00', function() {
500 expect('xaaa'.replace(/aa/, '$00b')).toBe('xaaba');
501 });
502
503 it('should end after two zeros', function() {
504 expect('xaaa'.replace(/aa/, '$000b')).toBe('xaa0ba');
505 expect('xaaa'.replace(/aa/, '$001b')).toBe('xaa1ba');
506 });
507
508 });
509
510 describe('named backreferences', function() {
511
512 it('should return the named backreference', function() {
513 expect('test'.replace(XRegExp('(?<test>t)', 'g'), ':${test}:')).toBe(':t:es:t:');
514
515 // Backreference to a nonparticipating capturing group
516 expect('test'.replace(XRegExp('t|(?<test>t)', 'g'), ':${test}:')).toBe('::es::');
517 });
518
519 it('should throw an exception for backreferences to unknown group names', function() {
520 expect(function() {'test'.replace(XRegExp('(?<test>t)', 'g'), ':${x}:');}).toThrowError(SyntaxError);
521 });
522
523 });
524
525 describe('explicit numbered backreferences', function() {
526
527 it('should return the numbered backreference', function() {
528 expect('test'.replace(/(.)./g, '${1}')).toBe('ts');
529
530 // Backreference to a nonparticipating capturing group
531 expect('test'.replace(/t|(e)/g, '${1}')).toBe('es');
532 });
533
534 it('should allow leading zeros', function() {
535 expect('test'.replace(/(.)./g, '${01}')).toBe('ts');
536 expect('test'.replace(/(.)./g, '${001}')).toBe('ts');
537 });
538
539 it('should return named backreferences by number', function() {
540 expect('test'.replace(XRegExp('(?<name>.).', 'g'), '${1}')).toBe('ts');
541 });
542
543 it('should separate numbered backreferences from following literal digits', function() {
544 expect('test'.replace(new RegExp('(.).', 'g'), '${1}0')).toBe('t0s0');
545 expect('test'.replace(new RegExp('(.).' + repeat('()', 9), 'g'), '${1}0')).toBe('t0s0');
546 });
547
548 it('should throw an exception for backreferences to unknown group numbers', function() {
549 expect(function() {'test'.replace(/t/, '${1}');}).toThrowError(SyntaxError);
550 expect(function() {'test'.replace(/(t)/, '${2}');}).toThrowError(SyntaxError);
551 });
552
553 it('should allow ${0} to refer to the entire match', function() {
554 expect('test'.replace(/../g, '${0}:')).toBe('te:st:');
555 expect('test'.replace(/../g, '${00}:')).toBe('te:st:');
556 expect('test'.replace(/../g, '${000}:')).toBe('te:st:');
557 });
558
559 it('should support backreferences 100 and greater, if the browser does natively', function() {
560 // IE < 9 doesn't allow backreferences greater than \99 *within* a regex, but
561 // XRegExp still allows backreferences to groups 100+ within replacement text
562 try {
563 // Regex with 1,000 capturing groups. This fails in Firefox 4-6 (but not v3.6
564 // or v7+) with `InternalError: regular expression too complex`
565 var lottaGroups = new RegExp([
566 '^(a)\\1', repeat('()', 8),
567 '(b)\\10', repeat('()', 89),
568 '(c)', repeat('()', 899),
569 '(d)$'
570 ].join(''));
571
572 expect('aabbcd'.replace(lottaGroups, '${0} ${01} ${001} ${0001} ${1} ${10} ${100} ${1000}')).toBe('aabbcd a a a a b c d');
573 // For comparison...
574 expect('aabbcd'.replace(lottaGroups, '$0 $01 $001 $0001 $1 $10 $100 $1000')).toBe('aabbcd a aabbcd1 aabbcd01 a b b0 b00');
575 } catch (err) {
576 // Keep the assertion count consistent cross-browser
577 expect(true).toBe(true);
578 expect(true).toBe(true);
579 }
580 });
581
582 });
583
584 describe('strict error handling', function() {
585
586 it('should throw an exception for backreferences to unknown group numbers', function() {
587 expect(function() {'xaaa'.replace(/aa/, '$1b');}).toThrowError(SyntaxError);
588 expect(function() {'xaaa'.replace(/aa/, '$01b');}).toThrowError(SyntaxError);
589 expect(function() {'xaaa'.replace(/a(a)/, '$2b');}).toThrowError(SyntaxError);
590 expect(function() {'xa(a)a'.replace('a(a)', '$1b');}).toThrowError(SyntaxError);
591 });
592
593 });
594
595 });
596
597 });
598
599 });
600
•601
•602 // describe('When overridden, String.prototype.split()', function() {
603
•604 // beforeEach(function() {
605 // XRegExp.install('natives');
606 // });
607
•608 // /*
609 // * NOTE: These specs should mirror those for XRegExp.split as closely as possible.
610 // */
611
•612 // // TODO: Copy/update specs from XRegExp.split here
613
•614 // // NOTE: The fixed String.prototype.split does not provide any extensions to native handling
615
•616 // });
•617