js/src/tests/ecma_5/String/15.5.4.11-01.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/licenses/publicdomain/
     4  */
     6 var BUGNUMBER = 587366;
     7 var summary = "String.prototype.replace with non-regexp searchValue";
     9 print(BUGNUMBER + ": " + summary);
    11 /**************
    12  * BEGIN TEST *
    13  **************/
    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 };
    30 var text = 'I once was lost but now am found.';
    31 var searchValue = 'found';
    32 var replaceValue;
    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.');
    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 $$$.');
    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.');
    57 /* Check RegExp statics haven't been mutated. */
    58 for (var ident in before)
    59     assertEq(RegExp[ident], before[ident]);
    61 /******************************************************************************/
    63 if (typeof reportCompare === "function")
    64   reportCompare(true, true);
    66 print("All tests passed!");

mercurial