Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2009 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 /**
     29  * @fileoverview Test String.prototype.replace
     30  */
     31 
     32 function replaceTest(result, subject, pattern, replacement) {
     33   var name =
     34     "\"" + subject + "\".replace(" + pattern + ", " + replacement + ")";
     35   assertEquals(result, subject.replace(pattern, replacement), name);
     36 }
     37 
     38 
     39 var short = "xaxbxcx";
     40 
     41 replaceTest("axbxcx", short, "x", "");
     42 replaceTest("axbxcx", short, /x/, "");
     43 replaceTest("abc", short, /x/g, "");
     44 
     45 replaceTest("xaxxcx", short, "b", "");
     46 replaceTest("xaxxcx", short, /b/, "");
     47 replaceTest("xaxxcx", short, /b/g, "");
     48 
     49 
     50 replaceTest("[]axbxcx", short, "x", "[]");
     51 replaceTest("[]axbxcx", short, /x/, "[]");
     52 replaceTest("[]a[]b[]c[]", short, /x/g, "[]");
     53 
     54 replaceTest("xax[]xcx", short, "b", "[]");
     55 replaceTest("xax[]xcx", short, /b/, "[]");
     56 replaceTest("xax[]xcx", short, /b/g, "[]");
     57 
     58 
     59 replaceTest("[$]axbxcx", short, "x", "[$$]");
     60 replaceTest("[$]axbxcx", short, /x/, "[$$]");
     61 replaceTest("[$]a[$]b[$]c[$]", short, /x/g, "[$$]");
     62 
     63 replaceTest("xax[$]xcx", short, "b", "[$$]");
     64 replaceTest("xax[$]xcx", short, /b/, "[$$]");
     65 replaceTest("xax[$]xcx", short, /b/g, "[$$]");
     66 
     67 
     68 replaceTest("[]axbxcx", short, "x", "[$`]");
     69 replaceTest("[]axbxcx", short, /x/, "[$`]");
     70 replaceTest("[]a[xa]b[xaxb]c[xaxbxc]", short, /x/g, "[$`]");
     71 
     72 replaceTest("xax[xax]xcx", short, "b", "[$`]");
     73 replaceTest("xax[xax]xcx", short, /b/, "[$`]");
     74 replaceTest("xax[xax]xcx", short, /b/g, "[$`]");
     75 
     76 
     77 replaceTest("[x]axbxcx", short, "x", "[$&]");
     78 replaceTest("[x]axbxcx", short, /x/, "[$&]");
     79 replaceTest("[x]a[x]b[x]c[x]", short, /x/g, "[$&]");
     80 
     81 replaceTest("xax[b]xcx", short, "b", "[$&]");
     82 replaceTest("xax[b]xcx", short, /b/, "[$&]");
     83 replaceTest("xax[b]xcx", short, /b/g, "[$&]");
     84 
     85 
     86 replaceTest("[axbxcx]axbxcx", short, "x", "[$']");
     87 replaceTest("[axbxcx]axbxcx", short, /x/, "[$']");
     88 replaceTest("[axbxcx]a[bxcx]b[cx]c[]", short, /x/g, "[$']");
     89 
     90 replaceTest("xax[xcx]xcx", short, "b", "[$']");
     91 replaceTest("xax[xcx]xcx", short, /b/, "[$']");
     92 replaceTest("xax[xcx]xcx", short, /b/g, "[$']");
     93 
     94 
     95 replaceTest("[$1]axbxcx", short, "x", "[$1]");
     96 replaceTest("[$1]axbxcx", short, /x/, "[$1]");
     97 replaceTest("[]axbxcx", short, /x()/, "[$1]");
     98 replaceTest("[$1]a[$1]b[$1]c[$1]", short, /x/g, "[$1]");
     99 replaceTest("[]a[]b[]c[]", short, /x()/g, "[$1]");
    100 
    101 replaceTest("xax[$1]xcx", short, "b", "[$1]");
    102 replaceTest("xax[$1]xcx", short, /b/, "[$1]");
    103 replaceTest("xax[]xcx", short, /b()/, "[$1]");
    104 replaceTest("xax[$1]xcx", short, /b/g, "[$1]");
    105 replaceTest("xax[]xcx", short, /b()/g, "[$1]");
    106 
    107 // Bug 317 look-alikes. If "$e" has no meaning, the "$" must be retained.
    108 replaceTest("xax$excx", short, "b", "$e");
    109 replaceTest("xax$excx", short, /b/, "$e");
    110 replaceTest("xax$excx", short, /b/g, "$e");
    111 
    112 replaceTest("xaxe$xcx", short, "b", "e$");
    113 replaceTest("xaxe$xcx", short, /b/, "e$");
    114 replaceTest("xaxe$xcx", short, /b/g, "e$");
    115 
    116 
    117 replaceTest("[$$$1$$a1abb1bb0$002$3$03][$$$1$$b1bcc1cc0$002$3$03]c",
    118             "abc", /(.)(?=(.))/g, "[$$$$$$1$$$$$11$01$2$21$02$020$002$3$03]");
    119 
    120 // Replace with functions.
    121 
    122 
    123 var ctr = 0;
    124 replaceTest("0axbxcx", short, "x", function r(m, i, s) {
    125   assertEquals(3, arguments.length, "replace('x',func) func-args");
    126   assertEquals("x", m, "replace('x',func(m,..))");
    127   assertEquals(0, i, "replace('x',func(..,i,..))");
    128   assertEquals(short, s, "replace('x',func(..,s))");
    129   return String(ctr++);
    130 });
    131 assertEquals(1, ctr, "replace('x',func) num-match");
    132 
    133 ctr = 0;
    134 replaceTest("0axbxcx", short, /x/, function r(m, i, s) {
    135   assertEquals(3, arguments.length, "replace(/x/,func) func-args");
    136   assertEquals("x", m, "replace(/x/,func(m,..))");
    137   assertEquals(0, i, "replace(/x/,func(..,i,..))");
    138   assertEquals(short, s, "replace(/x/,func(..,s))");
    139   return String(ctr++);
    140 });
    141 assertEquals(1, ctr, "replace(/x/,func) num-match");
    142 
    143 ctr = 0;
    144 replaceTest("0a1b2c3", short, /x/g, function r(m, i, s) {
    145   assertEquals(3, arguments.length, "replace(/x/g,func) func-args");
    146   assertEquals("x", m, "replace(/x/g,func(m,..))");
    147   assertEquals(ctr * 2, i, "replace(/x/g,func(..,i,.))");
    148   assertEquals(short, s, "replace(/x/g,func(..,s))");
    149   return String(ctr++);
    150 });
    151 assertEquals(4, ctr, "replace(/x/g,func) num-match");
    152 
    153 ctr = 0;
    154 replaceTest("0a1b2cx", short, /(x)(?=(.))/g, function r(m, c1, c2, i, s) {
    155   assertEquals(5, arguments.length, "replace(/(x)(?=(.))/g,func) func-args");
    156   assertEquals("x", m, "replace(/(x)(?=(.))/g,func(m,..))");
    157   assertEquals("x", c1, "replace(/(x)(?=(.))/g,func(..,c1,..))");
    158   assertEquals(["a","b","c"][ctr], c2, "replace(/(x)(?=(.))/g,func(..,c2,..))");
    159   assertEquals(ctr * 2, i, "replace(/(x)(?=(.))/g,func(..,i,..))");
    160   assertEquals(short, s, "replace(/(x)(?=(.))/g,func(..,s))");
    161   return String(ctr++);
    162 });
    163 assertEquals(3, ctr, "replace(/x/g,func) num-match");
    164 
    165 
    166 replaceTest("ABCD", "abcd", /(.)/g, function r(m, c1, i, s) {
    167   assertEquals("d", RegExp.lastMatch);
    168   assertEquals("d", RegExp.$1);
    169   assertEquals("abc", RegExp.leftContext);
    170   return m.toUpperCase();
    171 });
    172 
    173 
    174 var long = "";
    175 while (long.length < 0x2000) {
    176   long += String.fromCharCode(65 + Math.random() * 26);
    177 }
    178 
    179 for (var i = 0; i < 3; i++) {
    180   replaceTest(long.toLowerCase(), long, /(..)/g, function r(m, c1, i, s) {
    181     var expected = long.substring(0x1ffe, 0x2000);
    182     assertEquals(expected, RegExp.lastMatch);
    183     assertEquals(expected, RegExp.$1);
    184     assertEquals(long.substring(0, 0x1ffe), RegExp.leftContext);
    185     return m.toLowerCase();
    186   });
    187 }
    188 
    189 
    190 // Test special cases of replacement parts longer than 1<<11.
    191 var longstring = "xyzzy";
    192 longstring = longstring + longstring;
    193 longstring = longstring + longstring;
    194 longstring = longstring + longstring;
    195 longstring = longstring + longstring;
    196 longstring = longstring + longstring;
    197 longstring = longstring + longstring;
    198 longstring = longstring + longstring;
    199 longstring = longstring + longstring;
    200 longstring = longstring + longstring;
    201 longstring = longstring + longstring;
    202 longstring = longstring + longstring;
    203 // longstring.length == 5 << 11
    204 
    205 replaceTest(longstring + longstring,
    206             "<" + longstring + ">", /<(.*)>/g, "$1$1");
    207 
    208 replaceTest("string 42", "string x", /x/g, function() { return 42; });
    209 replaceTest("string 42", "string x", /x/, function() { return 42; });
    210 replaceTest("string 42", "string x", /[xy]/g, function() { return 42; });
    211 replaceTest("string 42", "string x", /[xy]/, function() { return 42; });
    212 replaceTest("string true", "string x", /x/g, function() { return true; });
    213 replaceTest("string null", "string x", /x/g, function() { return null; });
    214 replaceTest("string undefined", "string x", /x/g, function() { return undefined; });
    215 
    216 replaceTest("aundefinedbundefinedcundefined",
    217             "abc", /(.)|(.)/g, function(m, m1, m2, i, s) { return m1+m2; });
    218 
    219 // Test nested calls to replace, including that it sets RegExp.$& correctly.
    220 
    221 var str = 'She sells seashells by the seashore.';
    222 var re = /sh/g;
    223 assertEquals('She sells sea$schells by the sea$schore.',
    224              str.replace(re,"$$" + 'sch'))
    225 
    226 
    227 var replace_obj = { length: 0, toString: function() { return "x"; }};
    228 assertEquals("axc", "abc".replace(/b/, replace_obj));
    229 assertEquals("axc", "abc".replace(/b/g, replace_obj));
    230 
    231 var search_obj = { length: 1, toString: function() { return "b"; }};
    232 assertEquals("axc", "abc".replace(search_obj, function() { return "x"; }));
    233 
    234 var side_effect_flag = false;
    235 var replace_obj_side_effects = {
    236     toString: function() { side_effect_flag = true; return "x" }
    237 }
    238 assertEquals("abc", "abc".replace(/z/g, replace_obj_side_effects));
    239 assertTrue(side_effect_flag);  // Side effect triggers even without a match.
    240 
    241 var regexp99pattern = "";
    242 var subject = "";
    243 for (var i = 0; i < 99; i++) {
    244   regexp99pattern += "(.)";
    245   subject += String.fromCharCode(i + 24);
    246 }
    247 
    248 function testIndices99(re) {
    249   // Test $1 .. $99
    250   for (var i = 1; i < 100; i++) {
    251     assertEquals(String.fromCharCode(i + 23),
    252                  subject.replace(re, "$" + i));
    253   }
    254 
    255   // Test $01 .. $09
    256   for (var i = 1; i < 10; i++) {
    257     assertEquals(String.fromCharCode(i + 23),
    258                  subject.replace(re, "$0" + i));
    259   }
    260 
    261   assertEquals("$0", subject.replace(re, "$0"));
    262   assertEquals("$00", subject.replace(re, "$00"));
    263   assertEquals(String.fromCharCode(10 + 23) + "0",
    264                subject.replace(re, "$100"));
    265 }
    266 
    267 testIndices99(new RegExp(regexp99pattern));
    268 testIndices99(new RegExp(regexp99pattern, "g"));
    269 
    270 var regexp59pattern = "";
    271 for (var i = 0; i < 59; i++) regexp59pattern += "(.)";
    272 
    273 function testIndices59(re) {
    274   // Test $60 .. $99.  Captures reach up to 59.  Per spec, how to deal
    275   // with this is implementation-dependent. We interpret $60 as $6
    276   // followed by "0", $61 as $6, followed by "1" and so on.
    277   var tail = subject.substr(59);
    278   for (var i = 60; i < 100; i++) {
    279     assertEquals(String.fromCharCode(i / 10 + 23) + (i % 10) + tail,
    280                  subject.replace(re, "$" + i));
    281   }
    282 }
    283 
    284 testIndices59(new RegExp(regexp59pattern));
    285 testIndices59(new RegExp(regexp59pattern, "g"));
    286