| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * @fileoverview Test String.prototype.replace | 29 * @fileoverview Test String.prototype.replace |
| 30 */ | 30 */ |
| 31 | 31 |
| 32 function replaceTest(result, subject, pattern, replacement) { | 32 function replaceTest(result, subject, pattern, replacement) { |
| 33 var name = | 33 var name = |
| 34 "\"" + subject + "\".replace(" + pattern + ", " + replacement + ")"; | 34 "\"" + subject + "\".replace(" + pattern + ", " + replacement + ")"; |
| 35 assertEquals(result, subject.replace(pattern, replacement), name); | 35 assertEquals(result, subject.replace(pattern, replacement), name); |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 var short = "xaxbxcx"; | 39 var short = "xaxbxcx"; |
| 40 | 40 |
| 41 replaceTest("axbxcx", short, "x", ""); | 41 replaceTest("axbxcx", short, "x", ""); |
| 42 replaceTest("axbxcx", short, /x/, ""); | 42 replaceTest("axbxcx", short, /x/, ""); |
| 43 replaceTest("abc", short, /x/g, ""); | 43 replaceTest("abc", short, /x/g, ""); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // Bug 317 look-alikes. If "$e" has no meaning, the "$" must be retained. | 107 // Bug 317 look-alikes. If "$e" has no meaning, the "$" must be retained. |
| 108 replaceTest("xax$excx", short, "b", "$e"); | 108 replaceTest("xax$excx", short, "b", "$e"); |
| 109 replaceTest("xax$excx", short, /b/, "$e"); | 109 replaceTest("xax$excx", short, /b/, "$e"); |
| 110 replaceTest("xax$excx", short, /b/g, "$e"); | 110 replaceTest("xax$excx", short, /b/g, "$e"); |
| 111 | 111 |
| 112 replaceTest("xaxe$xcx", short, "b", "e$"); | 112 replaceTest("xaxe$xcx", short, "b", "e$"); |
| 113 replaceTest("xaxe$xcx", short, /b/, "e$"); | 113 replaceTest("xaxe$xcx", short, /b/, "e$"); |
| 114 replaceTest("xaxe$xcx", short, /b/g, "e$"); | 114 replaceTest("xaxe$xcx", short, /b/g, "e$"); |
| 115 | 115 |
| 116 | 116 |
| 117 replaceTest("[$$$1$$a1abb1bb0$002$3$03][$$$1$$b1bcc1cc0$002$3$03]c", | 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]"); | 118 "abc", /(.)(?=(.))/g, "[$$$$$$1$$$$$11$01$2$21$02$020$002$3$03]"); |
| 119 | 119 |
| 120 // Replace with functions. | 120 // Replace with functions. |
| 121 | 121 |
| 122 | 122 |
| 123 var ctr = 0; | 123 var ctr = 0; |
| 124 replaceTest("0axbxcx", short, "x", function r(m, i, s) { | 124 replaceTest("0axbxcx", short, "x", function r(m, i, s) { |
| 125 assertEquals(3, arguments.length, "replace('x',func) func-args"); | 125 assertEquals(3, arguments.length, "replace('x',func) func-args"); |
| 126 assertEquals("x", m, "replace('x',func(m,..))"); | 126 assertEquals("x", m, "replace('x',func(m,..))"); |
| 127 assertEquals(0, i, "replace('x',func(..,i,..))"); | 127 assertEquals(0, i, "replace('x',func(..,i,..))"); |
| 128 assertEquals(short, s, "replace('x',func(..,s))"); | 128 assertEquals(short, s, "replace('x',func(..,s))"); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 "<" + longstring + ">", /<(.*)>/g, "$1$1"); | 182 "<" + longstring + ">", /<(.*)>/g, "$1$1"); |
| 183 | 183 |
| 184 replaceTest("string 42", "string x", /x/g, function() { return 42; }); | 184 replaceTest("string 42", "string x", /x/g, function() { return 42; }); |
| 185 replaceTest("string 42", "string x", /x/, function() { return 42; }); | 185 replaceTest("string 42", "string x", /x/, function() { return 42; }); |
| 186 replaceTest("string 42", "string x", /[xy]/g, function() { return 42; }); | 186 replaceTest("string 42", "string x", /[xy]/g, function() { return 42; }); |
| 187 replaceTest("string 42", "string x", /[xy]/, function() { return 42; }); | 187 replaceTest("string 42", "string x", /[xy]/, function() { return 42; }); |
| 188 replaceTest("string true", "string x", /x/g, function() { return true; }); | 188 replaceTest("string true", "string x", /x/g, function() { return true; }); |
| 189 replaceTest("string null", "string x", /x/g, function() { return null; }); | 189 replaceTest("string null", "string x", /x/g, function() { return null; }); |
| 190 replaceTest("string undefined", "string x", /x/g, function() { return undefined;
}); | 190 replaceTest("string undefined", "string x", /x/g, function() { return undefined;
}); |
| 191 | 191 |
| 192 replaceTest("aundefinedbundefinedcundefined", | 192 replaceTest("aundefinedbundefinedcundefined", |
| 193 "abc", /(.)|(.)/g, function(m, m1, m2, i, s) { return m1+m2; }); | 193 "abc", /(.)|(.)/g, function(m, m1, m2, i, s) { return m1+m2; }); |
| 194 |
| 195 // Test nested calls to replace, including that it sets RegExp.$& correctly. |
| 196 |
| 197 function replacer(m,i,s) { |
| 198 assertEquals(m,RegExp['$&']); |
| 199 return "[" + RegExp['$&'] + "-" |
| 200 + m.replace(/./g,"$&$&") + "-" |
| 201 + m.replace(/./g,function() { return RegExp['$&']; }) |
| 202 + "-" + RegExp['$&'] + "]"; |
| 203 } |
| 204 |
| 205 replaceTest("[ab-aabb-ab-b][az-aazz-az-z]", |
| 206 "abaz", /a./g, replacer); |
| 207 |
| 208 replaceTest("[ab-aabb-ab-b][az-aazz-az-z]", |
| 209 "abaz", /a(.)/g, replacer); |
| OLD | NEW |