js/src/tests/ecma_5/JSON/stringify-gap.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-gap.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,61 @@
     1.4 +// Any copyright is dedicated to the Public Domain.
     1.5 +// http://creativecommons.org/licenses/publicdomain/
     1.6 +
     1.7 +var gTestfile = 'stringify-gap.js';
     1.8 +//-----------------------------------------------------------------------------
     1.9 +var BUGNUMBER = 584909;
    1.10 +var summary =
    1.11 +  "JSON.stringify(_1, _2, numberGreaterThanOne) produces wrong output";
    1.12 +
    1.13 +print(BUGNUMBER + ": " + summary);
    1.14 +
    1.15 +/**************
    1.16 + * BEGIN TEST *
    1.17 + **************/
    1.18 +
    1.19 +var LF = "\n";
    1.20 +var GAP = "   ";
    1.21 +
    1.22 +var obj = { a: { b: [1, 2], c: { d: 3, e: 4 }, f: [], g: {}, h: [5], i: { j: 6 } } };
    1.23 +
    1.24 +var expected =
    1.25 +  '{\n' +
    1.26 +  '   "a": {\n' +
    1.27 +  '      "b": [\n' +
    1.28 +  '         1,\n' +
    1.29 +  '         2\n' +
    1.30 +  '      ],\n' +
    1.31 +  '      "c": {\n' +
    1.32 +  '         "d": 3,\n' +
    1.33 +  '         "e": 4\n' +
    1.34 +  '      },\n' +
    1.35 +  '      "f": [],\n' +
    1.36 +  '      "g": {},\n' +
    1.37 +  '      "h": [\n' +
    1.38 +  '         5\n' +
    1.39 +  '      ],\n' +
    1.40 +  '      "i": {\n' +
    1.41 +  '         "j": 6\n' +
    1.42 +  '      }\n' +
    1.43 +  '   }\n' +
    1.44 +  '}';
    1.45 +
    1.46 +assertEq(JSON.stringify(obj, null, 3), expected);
    1.47 +assertEq(JSON.stringify(obj, null, "   "), expected);
    1.48 +
    1.49 +obj = [1, 2, 3];
    1.50 +
    1.51 +String.prototype.toString = function() { return "--"; };
    1.52 +
    1.53 +assertEq(JSON.stringify(obj, null, new String("  ")), "[\n--1,\n--2,\n--3\n]");
    1.54 +
    1.55 +Number.prototype.valueOf = function() { return 0; };
    1.56 +
    1.57 +assertEq(JSON.stringify(obj, null, new Number(3)), "[1,2,3]");
    1.58 +
    1.59 +/******************************************************************************/
    1.60 +
    1.61 +if (typeof reportCompare === "function")
    1.62 +  reportCompare(true, true);
    1.63 +
    1.64 +print("All tests passed!");

mercurial