Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 // Ported from dom/src/json/test/unit/test_wrappers.js
3 function assertStringify(v, expect)
4 {
5 assertEq(JSON.stringify(v), expect);
6 }
8 assertStringify({}, "{}");
9 assertStringify([], "[]");
10 assertStringify({"foo":"bar"}, '{"foo":"bar"}');
11 assertStringify({"null":null}, '{"null":null}');
12 assertStringify({"five":5}, '{"five":5}');
13 assertStringify({"five":5, "six":6}, '{"five":5,"six":6}');
14 assertStringify({"x":{"y":"z"}}, '{"x":{"y":"z"}}');
15 assertStringify({"w":{"x":{"y":"z"}}}, '{"w":{"x":{"y":"z"}}}');
16 assertStringify([1,2,3], '[1,2,3]');
17 assertStringify({"w":{"x":{"y":[1,2,3]}}}, '{"w":{"x":{"y":[1,2,3]}}}');
18 assertStringify({"false":false}, '{"false":false}');
19 assertStringify({"true":true}, '{"true":true}');
20 assertStringify({"child has two members": {"this":"one", 2:"and this one"}},
21 '{"child has two members":{"2":"and this one","this":"one"}}');
22 assertStringify({"x":{"a":"b","c":{"y":"z"},"f":"g"}},
23 '{"x":{"a":"b","c":{"y":"z"},"f":"g"}}');
24 assertStringify({"x":[1,{"y":"z"},3]}, '{"x":[1,{"y":"z"},3]}');
25 assertStringify([new String("hmm")], '["hmm"]');
26 assertStringify([new Boolean(true)], '[true]');
27 assertStringify([new Number(42)], '[42]');
28 assertStringify([new Date(Date.UTC(1978, 8, 13, 12, 24, 34, 23))],
29 '["1978-09-13T12:24:34.023Z"]');
30 assertStringify([1,,3], '[1,null,3]');
31 assertStringify({"mm\"mm":"hmm"}, '{"mm\\\"mm":"hmm"}');
32 assertStringify({"mm\"mm\"mm":"hmm"}, '{"mm\\\"mm\\\"mm":"hmm"}');
33 assertStringify({'"':"hmm"}, '{"\\\"":"hmm"}');
34 assertStringify({'\\':"hmm"}, '{"\\\\":"hmm"}');
35 assertStringify({'mmm\\mmm':"hmm"}, '{"mmm\\\\mmm":"hmm"}');
36 assertStringify({'mmm\\mmm\\mmm':"hmm"}, '{"mmm\\\\mmm\\\\mmm":"hmm"}');
37 assertStringify({"mm\u000bmm":"hmm"}, '{"mm\\u000bmm":"hmm"}');
38 assertStringify({"mm\u0000mm":"hmm"}, '{"mm\\u0000mm":"hmm"}');
40 var x = {"free":"variable"};
41 assertStringify(x, '{"free":"variable"}');
42 assertStringify({"y":x}, '{"y":{"free":"variable"}}');
44 // array prop
45 assertStringify({ a: [1,2,3] }, '{"a":[1,2,3]}');
47 assertStringify({"y": { foo: function(hmm) { return hmm; } } }, '{"y":{}}');
49 // test toJSON
50 var hmm = { toJSON: function() { return {"foo":"bar"} } };
51 assertStringify({"hmm":hmm}, '{"hmm":{"foo":"bar"}}');
52 assertStringify(hmm, '{"foo":"bar"}'); // on the root
54 // toJSON on prototype
55 var Y = function() {
56 this.not = "there?";
57 this.d = "e";
58 };
59 Y.prototype = {
60 not: "there?",
61 toJSON: function() { return {"foo":"bar"}}
62 };
63 var y = new Y();
64 assertStringify(y.toJSON(), '{"foo":"bar"}');
65 assertStringify(y, '{"foo":"bar"}');
67 // return undefined from toJSON
68 assertStringify({"hmm": { toJSON: function() { return; } } }, '{}');
70 // array with named prop
71 var x = new Array();
72 x[0] = 1;
73 x.foo = "bar";
74 assertStringify(x, '[1]');
76 // prototype
77 var X = function() { this.a = "b" };
78 X.prototype = { c: "d" };
79 assertStringify(new X(), '{"a":"b"}');
81 /******************************************************************************/
83 if (typeof reportCompare === "function")
84 reportCompare(true, true);
86 print("Tests complete");