diff options
Diffstat (limited to 'deps/v8/test/mjsunit/string-replace.js')
-rw-r--r-- | deps/v8/test/mjsunit/string-replace.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/deps/v8/test/mjsunit/string-replace.js b/deps/v8/test/mjsunit/string-replace.js index 6b022df307..de92115575 100644 --- a/deps/v8/test/mjsunit/string-replace.js +++ b/deps/v8/test/mjsunit/string-replace.js @@ -212,3 +212,64 @@ var str = 'She sells seashells by the seashore.'; var re = /sh/g; assertEquals('She sells sea$schells by the sea$schore.', str.replace(re,"$$" + 'sch')) + + +var replace_obj = { length: 0, toString: function() { return "x"; }}; +assertEquals("axc", "abc".replace(/b/, replace_obj)); +assertEquals("axc", "abc".replace(/b/g, replace_obj)); + +var search_obj = { length: 1, toString: function() { return "b"; }}; +assertEquals("axc", "abc".replace(search_obj, function() { return "x"; })); + +var side_effect_flag = false; +var replace_obj_side_effects = { + toString: function() { side_effect_flag = true; return "x" } +} +assertEquals("abc", "abc".replace(/z/g, replace_obj_side_effects)); +assertTrue(side_effect_flag); // Side effect triggers even without a match. + +var regexp99pattern = ""; +var subject = ""; +for (var i = 0; i < 99; i++) { + regexp99pattern += "(.)"; + subject += String.fromCharCode(i + 24); +} + +function testIndices99(re) { + // Test $1 .. $99 + for (var i = 1; i < 100; i++) { + assertEquals(String.fromCharCode(i + 23), + subject.replace(re, "$" + i)); + } + + // Test $01 .. $09 + for (var i = 1; i < 10; i++) { + assertEquals(String.fromCharCode(i + 23), + subject.replace(re, "$0" + i)); + } + + assertEquals("$0", subject.replace(re, "$0")); + assertEquals("$00", subject.replace(re, "$00")); + assertEquals(String.fromCharCode(10 + 23) + "0", + subject.replace(re, "$100")); +} + +testIndices99(new RegExp(regexp99pattern)); +testIndices99(new RegExp(regexp99pattern, "g")); + +var regexp59pattern = ""; +for (var i = 0; i < 59; i++) regexp59pattern += "(.)"; + +function testIndices59(re) { + // Test $60 .. $99. Captures reach up to 59. Per spec, how to deal + // with this is implementation-dependent. We interpret $60 as $6 + // followed by "0", $61 as $6, followed by "1" and so on. + var tail = subject.substr(59); + for (var i = 60; i < 100; i++) { + assertEquals(String.fromCharCode(i / 10 + 23) + (i % 10) + tail, + subject.replace(re, "$" + i)); + } +} + +testIndices59(new RegExp(regexp59pattern)); +testIndices59(new RegExp(regexp59pattern, "g")); |