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 // Test special cases of replacement parts longer than 1<<11.
    167 var longstring = "xyzzy";
    168 longstring = longstring + longstring;
    169 longstring = longstring + longstring;
    170 longstring = longstring + longstring;
    171 longstring = longstring + longstring;
    172 longstring = longstring + longstring;
    173 longstring = longstring + longstring;
    174 longstring = longstring + longstring;
    175 longstring = longstring + longstring;
    176 longstring = longstring + longstring;
    177 longstring = longstring + longstring;
    178 longstring = longstring + longstring;
    179 // longstring.length == 5 << 11
    180 
    181 replaceTest(longstring + longstring, 
    182             "<" + longstring + ">", /<(.*)>/g, "$1$1");
    183