michael@0: // Ported from dom/src/json/test/unit/test_replacer.js michael@0: michael@0: /** michael@0: * These return* functions are used by the michael@0: * replacer tests taken from bug 512447 michael@0: */ michael@0: function returnObjectFor1(k, v) michael@0: { michael@0: if (k == "1") michael@0: return {}; michael@0: return v; michael@0: } michael@0: function returnArrayFor1(k, v) michael@0: { michael@0: if (k == "1") michael@0: return []; michael@0: return v; michael@0: } michael@0: function returnNullFor1(k, v) michael@0: { michael@0: if (k == "1") michael@0: return null; michael@0: return v; michael@0: } michael@0: function returnStringForUndefined(k, v) michael@0: { michael@0: if (v === undefined) michael@0: return "undefined value"; michael@0: return v; michael@0: } michael@0: var cycleObject = {}; cycleObject.cycle = cycleObject; michael@0: function returnCycleObjectFor1(k, v) michael@0: { michael@0: if (k == "1") michael@0: return cycleObject; michael@0: return v; michael@0: } michael@0: var array = [0, 1, 2]; array[3] = array; michael@0: function returnCycleArrayFor1(k, v) michael@0: { michael@0: if (k == "1") michael@0: return array; michael@0: return v; michael@0: } michael@0: michael@0: // BEGIN TEST michael@0: var x; michael@0: michael@0: x = JSON.stringify({ key: 2 }, michael@0: function(k,v) { return k ? undefined : v; }); michael@0: assertEq(x, "{}"); michael@0: michael@0: x = JSON.stringify(["hmm", "hmm"], michael@0: function(k,v) { return k !== "" ? undefined : v; }); michael@0: assertEq(x, "[null,null]"); michael@0: michael@0: var foo = ["hmm"]; michael@0: function censor(k, v) michael@0: { michael@0: if (v !== foo) michael@0: return "XXX"; michael@0: return v; michael@0: } michael@0: x = JSON.stringify(foo, censor); michael@0: assertEq(x, '["XXX"]'); michael@0: michael@0: foo = ["bar", ["baz"], "qux"]; michael@0: x = JSON.stringify(foo, censor); michael@0: assertEq(x, '["XXX","XXX","XXX"]'); michael@0: michael@0: function censor2(k, v) michael@0: { michael@0: if (typeof(v) == "string") michael@0: return "XXX"; michael@0: return v; michael@0: } michael@0: michael@0: foo = ["bar", ["baz"], "qux"]; michael@0: x = JSON.stringify(foo, censor2); michael@0: assertEq(x, '["XXX",["XXX"],"XXX"]'); michael@0: michael@0: foo = { bar: 42, qux: 42, quux: 42 }; michael@0: x = JSON.stringify(foo, ["bar"]); michael@0: assertEq(x, '{"bar":42}'); michael@0: michael@0: foo = {bar: {bar: 42, schmoo:[]}, qux: 42, quux: 42}; michael@0: x = JSON.stringify(foo, ["bar", "schmoo"]); michael@0: assertEq(x, '{"bar":{"bar":42,"schmoo":[]}}'); michael@0: michael@0: x = JSON.stringify(foo, null, ""); michael@0: assertEq(x, '{"bar":{"bar":42,"schmoo":[]},"qux":42,"quux":42}'); michael@0: michael@0: x = JSON.stringify(foo, null, " "); michael@0: assertEq(x, '{\n "bar": {\n "bar": 42,\n "schmoo": []\n },\n "qux": 42,\n "quux": 42\n}'); michael@0: michael@0: foo = {bar:{bar:{}}} michael@0: x = JSON.stringify(foo, null, " "); michael@0: assertEq(x, '{\n "bar": {\n "bar": {}\n }\n}'); michael@0: michael@0: x = JSON.stringify({ x: 1, arr: [1] }, michael@0: function (k,v) { return typeof v === 'number' ? 3 : v; }); michael@0: assertEq(x, '{"x":3,"arr":[3]}'); michael@0: michael@0: foo = ['e']; michael@0: x = JSON.stringify(foo, null, '\t'); michael@0: assertEq(x, '[\n\t"e"\n]'); michael@0: michael@0: foo = {0:0, 1:1, 2:2, 3:undefined}; michael@0: x = JSON.stringify(foo, returnObjectFor1); michael@0: assertEq(x, '{"0":0,"1":{},"2":2}'); michael@0: michael@0: x = JSON.stringify(foo, returnArrayFor1); michael@0: assertEq(x, '{"0":0,"1":[],"2":2}'); michael@0: michael@0: x = JSON.stringify(foo, returnNullFor1); michael@0: assertEq(x, '{"0":0,"1":null,"2":2}'); michael@0: michael@0: x = JSON.stringify(foo, returnStringForUndefined); michael@0: assertEq(x, '{"0":0,"1":1,"2":2,"3":"undefined value"}'); michael@0: michael@0: try michael@0: { michael@0: JSON.stringify(foo, returnCycleObjectFor1); michael@0: throw new Error("no error thrown"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); michael@0: } michael@0: michael@0: try michael@0: { michael@0: JSON.stringify(foo, returnCycleArrayFor1); michael@0: throw new Error("no error thrown"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); michael@0: } michael@0: michael@0: foo = [0, 1, 2, undefined]; michael@0: try michael@0: { michael@0: JSON.stringify(foo, returnCycleObjectFor1); michael@0: throw new Error("no error thrown"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); michael@0: } michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("Tests complete");