Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / string-replace.js
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
184 replaceTest("string 42", "string x", /x/g, function() { return 42; });
185 replaceTest("string 42", "string x", /x/, function() { return 42; });
186 replaceTest("string 42", "string x", /[xy]/g, function() { return 42; });
187 replaceTest("string 42", "string x", /[xy]/, function() { return 42; });
188 replaceTest("string true", "string x", /x/g, function() { return true; });
189 replaceTest("string null", "string x", /x/g, function() { return null; });
190 replaceTest("string undefined", "string x", /x/g, function() { return undefined; });
191
192 replaceTest("aundefinedbundefinedcundefined",
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);
210
211 var str = 'She sells seashells by the seashore.';
212 var re = /sh/g;
213 assertEquals('She sells sea$schells by the sea$schore.',
214              str.replace(re,"$$" + 'sch'))
215
216
217 var replace_obj = { length: 0, toString: function() { return "x"; }};
218 assertEquals("axc", "abc".replace(/b/, replace_obj));
219 assertEquals("axc", "abc".replace(/b/g, replace_obj));
220
221 var search_obj = { length: 1, toString: function() { return "b"; }};
222 assertEquals("axc", "abc".replace(search_obj, function() { return "x"; }));
223
224 var side_effect_flag = false;
225 var replace_obj_side_effects = {
226     toString: function() { side_effect_flag = true; return "x" }
227 }
228 assertEquals("abc", "abc".replace(/z/g, replace_obj_side_effects));
229 assertTrue(side_effect_flag);  // Side effect triggers even without a match.
230
231 var regexp99pattern = "";
232 var subject = "";
233 for (var i = 0; i < 99; i++) {
234   regexp99pattern += "(.)";
235   subject += String.fromCharCode(i + 24);
236 }
237
238 function testIndices99(re) {
239   // Test $1 .. $99
240   for (var i = 1; i < 100; i++) {
241     assertEquals(String.fromCharCode(i + 23),
242                  subject.replace(re, "$" + i));
243   }
244
245   // Test $01 .. $09
246   for (var i = 1; i < 10; i++) {
247     assertEquals(String.fromCharCode(i + 23),
248                  subject.replace(re, "$0" + i));
249   }
250
251   assertEquals("$0", subject.replace(re, "$0"));
252   assertEquals("$00", subject.replace(re, "$00"));
253   assertEquals(String.fromCharCode(10 + 23) + "0",
254                subject.replace(re, "$100"));
255 }
256
257 testIndices99(new RegExp(regexp99pattern));
258 testIndices99(new RegExp(regexp99pattern, "g"));
259
260 var regexp59pattern = "";
261 for (var i = 0; i < 59; i++) regexp59pattern += "(.)";
262
263 function testIndices59(re) {
264   // Test $60 .. $99.  Captures reach up to 59.  Per spec, how to deal
265   // with this is implementation-dependent. We interpret $60 as $6
266   // followed by "0", $61 as $6, followed by "1" and so on.
267   var tail = subject.substr(59);
268   for (var i = 60; i < 100; i++) {
269     assertEquals(String.fromCharCode(i / 10 + 23) + (i % 10) + tail,
270                  subject.replace(re, "$" + i));
271   }
272 }
273
274 testIndices59(new RegExp(regexp59pattern));
275 testIndices59(new RegExp(regexp59pattern, "g"));