regexp-5.js (2158B)
1 // Tests for String.prototype builtins with custom regexp symbols on 2 // the regular expression object itself. 3 4 // Custom re[@@match]. 5 function testMatch() { 6 var s = "foobar"; 7 var re = /abc.+/; 8 var count = 0; 9 for (var i = 0; i < 200; i++) { 10 s.match(re); 11 if (i === 150) { 12 re[Symbol.match] = function() { 13 count++; 14 return null; 15 }; 16 } 17 } 18 assertEq(count, 49); 19 } 20 testMatch(); 21 22 // Custom re[@@matchAll]. 23 function testMatchAll() { 24 var s = "foobar"; 25 var re = /abc.+/g; 26 var count = 0; 27 for (var i = 0; i < 200; i++) { 28 s.matchAll(re); 29 if (i === 150) { 30 re[Symbol.matchAll] = function() { 31 count++; 32 return null; 33 }; 34 } 35 } 36 assertEq(count, 49); 37 } 38 testMatchAll(); 39 40 // Custom re[@@replace] for replace. 41 function testReplace() { 42 var s = "foobar"; 43 var re = /abc.+/; 44 var count = 0; 45 for (var i = 0; i < 200; i++) { 46 s.replace(re, ""); 47 if (i === 150) { 48 re[Symbol.replace] = function() { 49 count++; 50 return ""; 51 }; 52 } 53 } 54 assertEq(count, 49); 55 } 56 testReplace(); 57 58 // Custom re[@@replace] for replaceAll. 59 function testReplaceAll() { 60 var s = "foobar"; 61 var re = /abc.+/g; 62 var count = 0; 63 for (var i = 0; i < 200; i++) { 64 s.replaceAll(re, ""); 65 if (i === 150) { 66 re[Symbol.replace] = function() { 67 count++; 68 return ""; 69 }; 70 } 71 } 72 assertEq(count, 49); 73 } 74 testReplaceAll(); 75 76 // Custom re[@@search]. 77 function testSearch() { 78 var s = "foobar"; 79 var re = /abc.+/g; 80 var count = 0; 81 for (var i = 0; i < 200; i++) { 82 s.search(re); 83 if (i === 150) { 84 re[Symbol.search] = function() { 85 count++; 86 return -1; 87 }; 88 } 89 } 90 assertEq(count, 49); 91 } 92 testSearch(); 93 94 // Custom re[@@split]. 95 function testSplit() { 96 var s = "foobar"; 97 var re = /abc.+/; 98 var count = 0; 99 for (var i = 0; i < 200; i++) { 100 s.split(re); 101 if (i === 150) { 102 re[Symbol.split] = function() { 103 count++; 104 return []; 105 }; 106 } 107 } 108 assertEq(count, 49); 109 } 110 testSplit(); 111 112 // RegExp.prototype fuse must still be intact. 113 assertEq(getFuseState().OptimizeRegExpPrototypeFuse.intact, true);