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