|
1 /* |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/licenses/publicdomain/ |
|
4 */ |
|
5 |
|
6 var BUGNUMBER = 587366; |
|
7 var summary = "String.prototype.replace with non-regexp searchValue"; |
|
8 |
|
9 print(BUGNUMBER + ": " + summary); |
|
10 |
|
11 /************** |
|
12 * BEGIN TEST * |
|
13 **************/ |
|
14 |
|
15 /* |
|
16 * Check that regexp statics are preserved across the whole test. |
|
17 * If the engine is trying to cheat by turning stuff into regexps, |
|
18 * we should catch it! |
|
19 */ |
|
20 /(a|(b)|c)+/.exec('abcabc'); |
|
21 var before = { |
|
22 "source" : RegExp.source, |
|
23 "$`": RegExp.leftContext, |
|
24 "$'": RegExp.rightContext, |
|
25 "$&": RegExp.lastMatch, |
|
26 "$1": RegExp.$1, |
|
27 "$2": RegExp.$2 |
|
28 }; |
|
29 |
|
30 var text = 'I once was lost but now am found.'; |
|
31 var searchValue = 'found'; |
|
32 var replaceValue; |
|
33 |
|
34 /* Lambda substitution. */ |
|
35 replaceValue = function(matchStr, matchStart, textStr) { |
|
36 assertEq(matchStr, searchValue); |
|
37 assertEq(matchStart, 27); |
|
38 assertEq(textStr, text); |
|
39 return 'not watching that show anymore'; |
|
40 } |
|
41 var result = text.replace(searchValue, replaceValue); |
|
42 assertEq(result, 'I once was lost but now am not watching that show anymore.'); |
|
43 |
|
44 /* Dollar substitution. */ |
|
45 replaceValue = "...wait, where was I again? And where is all my $$$$$$? Oh right, $`$&$'" + |
|
46 " But with no $$$$$$"; /* Note the dot is not replaced and trails the end. */ |
|
47 result = text.replace(searchValue, replaceValue); |
|
48 assertEq(result, 'I once was lost but now am ...wait, where was I again?' + |
|
49 ' And where is all my $$$? Oh right, I once was lost but now am found.' + |
|
50 ' But with no $$$.'); |
|
51 |
|
52 /* Missing capture group dollar substitution. */ |
|
53 replaceValue = "$1$&$2$'$3"; |
|
54 result = text.replace(searchValue, replaceValue); |
|
55 assertEq(result, 'I once was lost but now am $1found$2.$3.'); |
|
56 |
|
57 /* Check RegExp statics haven't been mutated. */ |
|
58 for (var ident in before) |
|
59 assertEq(RegExp[ident], before[ident]); |
|
60 |
|
61 /******************************************************************************/ |
|
62 |
|
63 if (typeof reportCompare === "function") |
|
64 reportCompare(true, true); |
|
65 |
|
66 print("All tests passed!"); |