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