js/src/tests/ecma_5/JSON/stringify.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.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +// Ported from dom/src/json/test/unit/test_wrappers.js
     1.5 +
     1.6 +function assertStringify(v, expect)
     1.7 +{
     1.8 +  assertEq(JSON.stringify(v), expect);
     1.9 +}
    1.10 +
    1.11 +assertStringify({}, "{}");
    1.12 +assertStringify([], "[]");
    1.13 +assertStringify({"foo":"bar"}, '{"foo":"bar"}');
    1.14 +assertStringify({"null":null}, '{"null":null}');
    1.15 +assertStringify({"five":5}, '{"five":5}');
    1.16 +assertStringify({"five":5, "six":6}, '{"five":5,"six":6}');
    1.17 +assertStringify({"x":{"y":"z"}}, '{"x":{"y":"z"}}');
    1.18 +assertStringify({"w":{"x":{"y":"z"}}}, '{"w":{"x":{"y":"z"}}}');
    1.19 +assertStringify([1,2,3], '[1,2,3]');
    1.20 +assertStringify({"w":{"x":{"y":[1,2,3]}}}, '{"w":{"x":{"y":[1,2,3]}}}');
    1.21 +assertStringify({"false":false}, '{"false":false}');
    1.22 +assertStringify({"true":true}, '{"true":true}');
    1.23 +assertStringify({"child has two members": {"this":"one", 2:"and this one"}},
    1.24 +                '{"child has two members":{"2":"and this one","this":"one"}}');
    1.25 +assertStringify({"x":{"a":"b","c":{"y":"z"},"f":"g"}},
    1.26 +                '{"x":{"a":"b","c":{"y":"z"},"f":"g"}}');
    1.27 +assertStringify({"x":[1,{"y":"z"},3]}, '{"x":[1,{"y":"z"},3]}');
    1.28 +assertStringify([new String("hmm")], '["hmm"]');
    1.29 +assertStringify([new Boolean(true)], '[true]');
    1.30 +assertStringify([new Number(42)], '[42]');
    1.31 +assertStringify([new Date(Date.UTC(1978, 8, 13, 12, 24, 34, 23))],
    1.32 +                '["1978-09-13T12:24:34.023Z"]');
    1.33 +assertStringify([1,,3], '[1,null,3]');
    1.34 +assertStringify({"mm\"mm":"hmm"}, '{"mm\\\"mm":"hmm"}');
    1.35 +assertStringify({"mm\"mm\"mm":"hmm"}, '{"mm\\\"mm\\\"mm":"hmm"}');
    1.36 +assertStringify({'"':"hmm"}, '{"\\\"":"hmm"}');
    1.37 +assertStringify({'\\':"hmm"}, '{"\\\\":"hmm"}');
    1.38 +assertStringify({'mmm\\mmm':"hmm"}, '{"mmm\\\\mmm":"hmm"}');
    1.39 +assertStringify({'mmm\\mmm\\mmm':"hmm"}, '{"mmm\\\\mmm\\\\mmm":"hmm"}');
    1.40 +assertStringify({"mm\u000bmm":"hmm"}, '{"mm\\u000bmm":"hmm"}');
    1.41 +assertStringify({"mm\u0000mm":"hmm"}, '{"mm\\u0000mm":"hmm"}');
    1.42 +
    1.43 +var x = {"free":"variable"};
    1.44 +assertStringify(x, '{"free":"variable"}');
    1.45 +assertStringify({"y":x}, '{"y":{"free":"variable"}}');
    1.46 +
    1.47 +// array prop
    1.48 +assertStringify({ a: [1,2,3] }, '{"a":[1,2,3]}');
    1.49 +
    1.50 +assertStringify({"y": { foo: function(hmm) { return hmm; } } }, '{"y":{}}');
    1.51 +
    1.52 +// test toJSON
    1.53 +var hmm = { toJSON: function() { return {"foo":"bar"} } };
    1.54 +assertStringify({"hmm":hmm}, '{"hmm":{"foo":"bar"}}');
    1.55 +assertStringify(hmm, '{"foo":"bar"}'); // on the root
    1.56 +
    1.57 +// toJSON on prototype
    1.58 +var Y = function() {
    1.59 +  this.not = "there?";
    1.60 +  this.d = "e";
    1.61 +};
    1.62 +Y.prototype = {
    1.63 +  not: "there?",
    1.64 +  toJSON: function() { return {"foo":"bar"}}
    1.65 +};
    1.66 +var y = new Y();
    1.67 +assertStringify(y.toJSON(), '{"foo":"bar"}');
    1.68 +assertStringify(y, '{"foo":"bar"}');
    1.69 +
    1.70 +// return undefined from toJSON
    1.71 +assertStringify({"hmm": { toJSON: function() { return; } } }, '{}');
    1.72 +
    1.73 +// array with named prop
    1.74 +var x = new Array();
    1.75 +x[0] = 1;
    1.76 +x.foo = "bar";
    1.77 +assertStringify(x, '[1]');
    1.78 +
    1.79 +// prototype
    1.80 +var X = function() { this.a = "b" };
    1.81 +X.prototype = { c: "d" };
    1.82 +assertStringify(new X(), '{"a":"b"}');
    1.83 +
    1.84 +/******************************************************************************/
    1.85 +
    1.86 +if (typeof reportCompare === "function")
    1.87 +  reportCompare(true, true);
    1.88 +
    1.89 +print("Tests complete");

mercurial