michael@0: // Ported from dom/src/json/test/unit/test_wrappers.js michael@0: michael@0: function assertStringify(v, expect) michael@0: { michael@0: assertEq(JSON.stringify(v), expect); michael@0: } michael@0: michael@0: assertStringify({}, "{}"); michael@0: assertStringify([], "[]"); michael@0: assertStringify({"foo":"bar"}, '{"foo":"bar"}'); michael@0: assertStringify({"null":null}, '{"null":null}'); michael@0: assertStringify({"five":5}, '{"five":5}'); michael@0: assertStringify({"five":5, "six":6}, '{"five":5,"six":6}'); michael@0: assertStringify({"x":{"y":"z"}}, '{"x":{"y":"z"}}'); michael@0: assertStringify({"w":{"x":{"y":"z"}}}, '{"w":{"x":{"y":"z"}}}'); michael@0: assertStringify([1,2,3], '[1,2,3]'); michael@0: assertStringify({"w":{"x":{"y":[1,2,3]}}}, '{"w":{"x":{"y":[1,2,3]}}}'); michael@0: assertStringify({"false":false}, '{"false":false}'); michael@0: assertStringify({"true":true}, '{"true":true}'); michael@0: assertStringify({"child has two members": {"this":"one", 2:"and this one"}}, michael@0: '{"child has two members":{"2":"and this one","this":"one"}}'); michael@0: assertStringify({"x":{"a":"b","c":{"y":"z"},"f":"g"}}, michael@0: '{"x":{"a":"b","c":{"y":"z"},"f":"g"}}'); michael@0: assertStringify({"x":[1,{"y":"z"},3]}, '{"x":[1,{"y":"z"},3]}'); michael@0: assertStringify([new String("hmm")], '["hmm"]'); michael@0: assertStringify([new Boolean(true)], '[true]'); michael@0: assertStringify([new Number(42)], '[42]'); michael@0: assertStringify([new Date(Date.UTC(1978, 8, 13, 12, 24, 34, 23))], michael@0: '["1978-09-13T12:24:34.023Z"]'); michael@0: assertStringify([1,,3], '[1,null,3]'); michael@0: assertStringify({"mm\"mm":"hmm"}, '{"mm\\\"mm":"hmm"}'); michael@0: assertStringify({"mm\"mm\"mm":"hmm"}, '{"mm\\\"mm\\\"mm":"hmm"}'); michael@0: assertStringify({'"':"hmm"}, '{"\\\"":"hmm"}'); michael@0: assertStringify({'\\':"hmm"}, '{"\\\\":"hmm"}'); michael@0: assertStringify({'mmm\\mmm':"hmm"}, '{"mmm\\\\mmm":"hmm"}'); michael@0: assertStringify({'mmm\\mmm\\mmm':"hmm"}, '{"mmm\\\\mmm\\\\mmm":"hmm"}'); michael@0: assertStringify({"mm\u000bmm":"hmm"}, '{"mm\\u000bmm":"hmm"}'); michael@0: assertStringify({"mm\u0000mm":"hmm"}, '{"mm\\u0000mm":"hmm"}'); michael@0: michael@0: var x = {"free":"variable"}; michael@0: assertStringify(x, '{"free":"variable"}'); michael@0: assertStringify({"y":x}, '{"y":{"free":"variable"}}'); michael@0: michael@0: // array prop michael@0: assertStringify({ a: [1,2,3] }, '{"a":[1,2,3]}'); michael@0: michael@0: assertStringify({"y": { foo: function(hmm) { return hmm; } } }, '{"y":{}}'); michael@0: michael@0: // test toJSON michael@0: var hmm = { toJSON: function() { return {"foo":"bar"} } }; michael@0: assertStringify({"hmm":hmm}, '{"hmm":{"foo":"bar"}}'); michael@0: assertStringify(hmm, '{"foo":"bar"}'); // on the root michael@0: michael@0: // toJSON on prototype michael@0: var Y = function() { michael@0: this.not = "there?"; michael@0: this.d = "e"; michael@0: }; michael@0: Y.prototype = { michael@0: not: "there?", michael@0: toJSON: function() { return {"foo":"bar"}} michael@0: }; michael@0: var y = new Y(); michael@0: assertStringify(y.toJSON(), '{"foo":"bar"}'); michael@0: assertStringify(y, '{"foo":"bar"}'); michael@0: michael@0: // return undefined from toJSON michael@0: assertStringify({"hmm": { toJSON: function() { return; } } }, '{}'); michael@0: michael@0: // array with named prop michael@0: var x = new Array(); michael@0: x[0] = 1; michael@0: x.foo = "bar"; michael@0: assertStringify(x, '[1]'); michael@0: michael@0: // prototype michael@0: var X = function() { this.a = "b" }; michael@0: X.prototype = { c: "d" }; michael@0: assertStringify(new X(), '{"a":"b"}'); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("Tests complete");