1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/String/15.5.4.11-01.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,66 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/licenses/publicdomain/ 1.7 + */ 1.8 + 1.9 +var BUGNUMBER = 587366; 1.10 +var summary = "String.prototype.replace with non-regexp searchValue"; 1.11 + 1.12 +print(BUGNUMBER + ": " + summary); 1.13 + 1.14 +/************** 1.15 + * BEGIN TEST * 1.16 + **************/ 1.17 + 1.18 +/* 1.19 + * Check that regexp statics are preserved across the whole test. 1.20 + * If the engine is trying to cheat by turning stuff into regexps, 1.21 + * we should catch it! 1.22 + */ 1.23 +/(a|(b)|c)+/.exec('abcabc'); 1.24 +var before = { 1.25 + "source" : RegExp.source, 1.26 + "$`": RegExp.leftContext, 1.27 + "$'": RegExp.rightContext, 1.28 + "$&": RegExp.lastMatch, 1.29 + "$1": RegExp.$1, 1.30 + "$2": RegExp.$2 1.31 +}; 1.32 + 1.33 +var text = 'I once was lost but now am found.'; 1.34 +var searchValue = 'found'; 1.35 +var replaceValue; 1.36 + 1.37 +/* Lambda substitution. */ 1.38 +replaceValue = function(matchStr, matchStart, textStr) { 1.39 + assertEq(matchStr, searchValue); 1.40 + assertEq(matchStart, 27); 1.41 + assertEq(textStr, text); 1.42 + return 'not watching that show anymore'; 1.43 +} 1.44 +var result = text.replace(searchValue, replaceValue); 1.45 +assertEq(result, 'I once was lost but now am not watching that show anymore.'); 1.46 + 1.47 +/* Dollar substitution. */ 1.48 +replaceValue = "...wait, where was I again? And where is all my $$$$$$? Oh right, $`$&$'" + 1.49 + " But with no $$$$$$"; /* Note the dot is not replaced and trails the end. */ 1.50 +result = text.replace(searchValue, replaceValue); 1.51 +assertEq(result, 'I once was lost but now am ...wait, where was I again?' + 1.52 + ' And where is all my $$$? Oh right, I once was lost but now am found.' + 1.53 + ' But with no $$$.'); 1.54 + 1.55 +/* Missing capture group dollar substitution. */ 1.56 +replaceValue = "$1$&$2$'$3"; 1.57 +result = text.replace(searchValue, replaceValue); 1.58 +assertEq(result, 'I once was lost but now am $1found$2.$3.'); 1.59 + 1.60 +/* Check RegExp statics haven't been mutated. */ 1.61 +for (var ident in before) 1.62 + assertEq(RegExp[ident], before[ident]); 1.63 + 1.64 +/******************************************************************************/ 1.65 + 1.66 +if (typeof reportCompare === "function") 1.67 + reportCompare(true, true); 1.68 + 1.69 +print("All tests passed!");