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.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.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/js/src/tests/non262/JSON/stringify.js b/js/src/tests/non262/JSON/stringify.js new file mode 100644 index 0000000000..3b21474206 --- /dev/null +++ b/js/src/tests/non262/JSON/stringify.js @@ -0,0 +1,87 @@ +function assertStringify(v, expect) +{ + assertEq(JSON.stringify(v), expect); +} + +assertStringify({}, "{}"); +assertStringify([], "[]"); +assertStringify({"foo":"bar"}, '{"foo":"bar"}'); +assertStringify({"null":null}, '{"null":null}'); +assertStringify({"five":5}, '{"five":5}'); +assertStringify({"five":5, "six":6}, '{"five":5,"six":6}'); +assertStringify({"x":{"y":"z"}}, '{"x":{"y":"z"}}'); +assertStringify({"w":{"x":{"y":"z"}}}, '{"w":{"x":{"y":"z"}}}'); +assertStringify([1,2,3], '[1,2,3]'); +assertStringify({"w":{"x":{"y":[1,2,3]}}}, '{"w":{"x":{"y":[1,2,3]}}}'); +assertStringify({"false":false}, '{"false":false}'); +assertStringify({"true":true}, '{"true":true}'); +assertStringify({"child has two members": {"this":"one", 2:"and this one"}}, + '{"child has two members":{"2":"and this one","this":"one"}}'); +assertStringify({"x":{"a":"b","c":{"y":"z"},"f":"g"}}, + '{"x":{"a":"b","c":{"y":"z"},"f":"g"}}'); +assertStringify({"x":[1,{"y":"z"},3]}, '{"x":[1,{"y":"z"},3]}'); +assertStringify([new String("hmm")], '["hmm"]'); +assertStringify([new Boolean(true)], '[true]'); +assertStringify([new Number(42)], '[42]'); +assertStringify([new Date(Date.UTC(1978, 8, 13, 12, 24, 34, 23))], + '["1978-09-13T12:24:34.023Z"]'); +assertStringify([1,,3], '[1,null,3]'); +assertStringify({"mm\"mm":"hmm"}, '{"mm\\\"mm":"hmm"}'); +assertStringify({"mm\"mm\"mm":"hmm"}, '{"mm\\\"mm\\\"mm":"hmm"}'); +assertStringify({'"':"hmm"}, '{"\\\"":"hmm"}'); +assertStringify({'\\':"hmm"}, '{"\\\\":"hmm"}'); +assertStringify({'mmm\\mmm':"hmm"}, '{"mmm\\\\mmm":"hmm"}'); +assertStringify({'mmm\\mmm\\mmm':"hmm"}, '{"mmm\\\\mmm\\\\mmm":"hmm"}'); +assertStringify({"mm\u000bmm":"hmm"}, '{"mm\\u000bmm":"hmm"}'); +assertStringify({"mm\u0000mm":"hmm"}, '{"mm\\u0000mm":"hmm"}'); +assertStringify({"\u0000\u000b":""}, '{"\\u0000\\u000b":""}'); +assertStringify({"\u000b\ufdfd":"hmm"}, '{"\\u000b\ufdfd":"hmm"}'); +assertStringify({"\u000b\ufdfd":"h\xfc\ufdfdm"}, '{"\\u000b\ufdfd":"h\xfc\ufdfdm"}'); + +var x = {"free":"variable"}; +assertStringify(x, '{"free":"variable"}'); +assertStringify({"y":x}, '{"y":{"free":"variable"}}'); + +// array prop +assertStringify({ a: [1,2,3] }, '{"a":[1,2,3]}'); + +assertStringify({"y": { foo: function(hmm) { return hmm; } } }, '{"y":{}}'); + +// test toJSON +var hmm = { toJSON: function() { return {"foo":"bar"} } }; +assertStringify({"hmm":hmm}, '{"hmm":{"foo":"bar"}}'); +assertStringify(hmm, '{"foo":"bar"}'); // on the root + +// toJSON on prototype +var Y = function() { + this.not = "there?"; + this.d = "e"; +}; +Y.prototype = { + not: "there?", + toJSON: function() { return {"foo":"bar"}} +}; +var y = new Y(); +assertStringify(y.toJSON(), '{"foo":"bar"}'); +assertStringify(y, '{"foo":"bar"}'); + +// return undefined from toJSON +assertStringify({"hmm": { toJSON: function() { return; } } }, '{}'); + +// array with named prop +var x = new Array(); +x[0] = 1; +x.foo = "bar"; +assertStringify(x, '[1]'); + +// prototype +var X = function() { this.a = "b" }; +X.prototype = { c: "d" }; +assertStringify(new X(), '{"a":"b"}'); + +/******************************************************************************/ + +if (typeof reportCompare === "function") + reportCompare(true, true); + +print("Tests complete"); |