js/src/tests/ecma_5/JSON/stringify-replacer-with-array-indexes.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/JSON/stringify-replacer-with-array-indexes.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,56 @@
     1.4 +// Any copyright is dedicated to the Public Domain.
     1.5 +// http://creativecommons.org/licenses/publicdomain/
     1.6 +
     1.7 +var gTestfile = 'stringify-replacer-with-array-indexes.js';
     1.8 +//-----------------------------------------------------------------------------
     1.9 +var BUGNUMBER = 584909;
    1.10 +var summary =
    1.11 +  "Call the replacer function for array elements with stringified indexes";
    1.12 +
    1.13 +print(BUGNUMBER + ": " + summary);
    1.14 +
    1.15 +/**************
    1.16 + * BEGIN TEST *
    1.17 + **************/
    1.18 +
    1.19 +var arr = [0, 1, 2, 3, 4];
    1.20 +
    1.21 +var seenTopmost = false;
    1.22 +var index = 0;
    1.23 +function replacer()
    1.24 +{
    1.25 +  assertEq(arguments.length, 2);
    1.26 +
    1.27 +  var key = arguments[0], value = arguments[1];
    1.28 +
    1.29 +  // Topmost array: ignore replacer call.
    1.30 +  if (key === "")
    1.31 +  {
    1.32 +    assertEq(seenTopmost, false);
    1.33 +    seenTopmost = true;
    1.34 +    return value;
    1.35 +  }
    1.36 +
    1.37 +  assertEq(seenTopmost, true);
    1.38 +
    1.39 +  assertEq(typeof key, "string");
    1.40 +  assertEq(key === index, false);
    1.41 +  assertEq(key === index + "", true);
    1.42 +
    1.43 +  assertEq(value, index);
    1.44 +
    1.45 +  index++;
    1.46 +
    1.47 +  assertEq(this, arr);
    1.48 +
    1.49 +  return value;
    1.50 +}
    1.51 +
    1.52 +assertEq(JSON.stringify(arr, replacer), '[0,1,2,3,4]');
    1.53 +
    1.54 +/******************************************************************************/
    1.55 +
    1.56 +if (typeof reportCompare === "function")
    1.57 +  reportCompare(true, true);
    1.58 +
    1.59 +print("Tests complete");

mercurial