diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/JSON/stringify-replacer.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | js/src/tests/non262/JSON/stringify-replacer.js | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/js/src/tests/non262/JSON/stringify-replacer.js b/js/src/tests/non262/JSON/stringify-replacer.js new file mode 100644 index 0000000000..37497fd348 --- /dev/null +++ b/js/src/tests/non262/JSON/stringify-replacer.js @@ -0,0 +1,155 @@ +/** + * These return* functions are used by the + * replacer tests taken from bug 512447 + */ +function returnObjectFor1(k, v) +{ + if (k == "1") + return {}; + return v; +} +function returnArrayFor1(k, v) +{ + if (k == "1") + return []; + return v; +} +function returnNullFor1(k, v) +{ + if (k == "1") + return null; + return v; +} +function returnStringForUndefined(k, v) +{ + if (v === undefined) + return "undefined value"; + return v; +} +var cycleObject = {}; cycleObject.cycle = cycleObject; +function returnCycleObjectFor1(k, v) +{ + if (k == "1") + return cycleObject; + return v; +} +var array = [0, 1, 2]; array[3] = array; +function returnCycleArrayFor1(k, v) +{ + if (k == "1") + return array; + return v; +} + +// BEGIN TEST +var x; + +x = JSON.stringify({ key: 2 }, + function(k,v) { return k ? undefined : v; }); +assertEq(x, "{}"); + +x = JSON.stringify(["hmm", "hmm"], + function(k,v) { return k !== "" ? undefined : v; }); +assertEq(x, "[null,null]"); + +var foo = ["hmm"]; +function censor(k, v) +{ + if (v !== foo) + return "XXX"; + return v; +} +x = JSON.stringify(foo, censor); +assertEq(x, '["XXX"]'); + +foo = ["bar", ["baz"], "qux"]; +x = JSON.stringify(foo, censor); +assertEq(x, '["XXX","XXX","XXX"]'); + +function censor2(k, v) +{ + if (typeof(v) == "string") + return "XXX"; + return v; +} + +foo = ["bar", ["baz"], "qux"]; +x = JSON.stringify(foo, censor2); +assertEq(x, '["XXX",["XXX"],"XXX"]'); + +foo = { bar: 42, qux: 42, quux: 42 }; +x = JSON.stringify(foo, ["bar"]); +assertEq(x, '{"bar":42}'); + +foo = {bar: {bar: 42, schmoo:[]}, qux: 42, quux: 42}; +x = JSON.stringify(foo, ["bar", "schmoo"]); +assertEq(x, '{"bar":{"bar":42,"schmoo":[]}}'); + +x = JSON.stringify(foo, null, ""); +assertEq(x, '{"bar":{"bar":42,"schmoo":[]},"qux":42,"quux":42}'); + +x = JSON.stringify(foo, null, " "); +assertEq(x, '{\n "bar": {\n "bar": 42,\n "schmoo": []\n },\n "qux": 42,\n "quux": 42\n}'); + +foo = {bar:{bar:{}}} +x = JSON.stringify(foo, null, " "); +assertEq(x, '{\n "bar": {\n "bar": {}\n }\n}'); + +x = JSON.stringify({ x: 1, arr: [1] }, + function (k,v) { return typeof v === 'number' ? 3 : v; }); +assertEq(x, '{"x":3,"arr":[3]}'); + +foo = ['e']; +x = JSON.stringify(foo, null, '\t'); +assertEq(x, '[\n\t"e"\n]'); + +foo = {0:0, 1:1, 2:2, 3:undefined}; +x = JSON.stringify(foo, returnObjectFor1); +assertEq(x, '{"0":0,"1":{},"2":2}'); + +x = JSON.stringify(foo, returnArrayFor1); +assertEq(x, '{"0":0,"1":[],"2":2}'); + +x = JSON.stringify(foo, returnNullFor1); +assertEq(x, '{"0":0,"1":null,"2":2}'); + +x = JSON.stringify(foo, returnStringForUndefined); +assertEq(x, '{"0":0,"1":1,"2":2,"3":"undefined value"}'); + +try +{ + JSON.stringify(foo, returnCycleObjectFor1); + throw new Error("no error thrown"); +} +catch (e) +{ + assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); +} + +try +{ + JSON.stringify(foo, returnCycleArrayFor1); + throw new Error("no error thrown"); +} +catch (e) +{ + assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); +} + +foo = [0, 1, 2, undefined]; +try +{ + JSON.stringify(foo, returnCycleObjectFor1); + throw new Error("no error thrown"); +} +catch (e) +{ + assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e); +} + +/******************************************************************************/ + +if (typeof reportCompare === "function") + reportCompare(true, true); + +print("Tests complete"); |