js/src/tests/ecma_5/JSON/stringify-replacer.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Ported from dom/src/json/test/unit/test_replacer.js
michael@0 2
michael@0 3 /**
michael@0 4 * These return* functions are used by the
michael@0 5 * replacer tests taken from bug 512447
michael@0 6 */
michael@0 7 function returnObjectFor1(k, v)
michael@0 8 {
michael@0 9 if (k == "1")
michael@0 10 return {};
michael@0 11 return v;
michael@0 12 }
michael@0 13 function returnArrayFor1(k, v)
michael@0 14 {
michael@0 15 if (k == "1")
michael@0 16 return [];
michael@0 17 return v;
michael@0 18 }
michael@0 19 function returnNullFor1(k, v)
michael@0 20 {
michael@0 21 if (k == "1")
michael@0 22 return null;
michael@0 23 return v;
michael@0 24 }
michael@0 25 function returnStringForUndefined(k, v)
michael@0 26 {
michael@0 27 if (v === undefined)
michael@0 28 return "undefined value";
michael@0 29 return v;
michael@0 30 }
michael@0 31 var cycleObject = {}; cycleObject.cycle = cycleObject;
michael@0 32 function returnCycleObjectFor1(k, v)
michael@0 33 {
michael@0 34 if (k == "1")
michael@0 35 return cycleObject;
michael@0 36 return v;
michael@0 37 }
michael@0 38 var array = [0, 1, 2]; array[3] = array;
michael@0 39 function returnCycleArrayFor1(k, v)
michael@0 40 {
michael@0 41 if (k == "1")
michael@0 42 return array;
michael@0 43 return v;
michael@0 44 }
michael@0 45
michael@0 46 // BEGIN TEST
michael@0 47 var x;
michael@0 48
michael@0 49 x = JSON.stringify({ key: 2 },
michael@0 50 function(k,v) { return k ? undefined : v; });
michael@0 51 assertEq(x, "{}");
michael@0 52
michael@0 53 x = JSON.stringify(["hmm", "hmm"],
michael@0 54 function(k,v) { return k !== "" ? undefined : v; });
michael@0 55 assertEq(x, "[null,null]");
michael@0 56
michael@0 57 var foo = ["hmm"];
michael@0 58 function censor(k, v)
michael@0 59 {
michael@0 60 if (v !== foo)
michael@0 61 return "XXX";
michael@0 62 return v;
michael@0 63 }
michael@0 64 x = JSON.stringify(foo, censor);
michael@0 65 assertEq(x, '["XXX"]');
michael@0 66
michael@0 67 foo = ["bar", ["baz"], "qux"];
michael@0 68 x = JSON.stringify(foo, censor);
michael@0 69 assertEq(x, '["XXX","XXX","XXX"]');
michael@0 70
michael@0 71 function censor2(k, v)
michael@0 72 {
michael@0 73 if (typeof(v) == "string")
michael@0 74 return "XXX";
michael@0 75 return v;
michael@0 76 }
michael@0 77
michael@0 78 foo = ["bar", ["baz"], "qux"];
michael@0 79 x = JSON.stringify(foo, censor2);
michael@0 80 assertEq(x, '["XXX",["XXX"],"XXX"]');
michael@0 81
michael@0 82 foo = { bar: 42, qux: 42, quux: 42 };
michael@0 83 x = JSON.stringify(foo, ["bar"]);
michael@0 84 assertEq(x, '{"bar":42}');
michael@0 85
michael@0 86 foo = {bar: {bar: 42, schmoo:[]}, qux: 42, quux: 42};
michael@0 87 x = JSON.stringify(foo, ["bar", "schmoo"]);
michael@0 88 assertEq(x, '{"bar":{"bar":42,"schmoo":[]}}');
michael@0 89
michael@0 90 x = JSON.stringify(foo, null, "");
michael@0 91 assertEq(x, '{"bar":{"bar":42,"schmoo":[]},"qux":42,"quux":42}');
michael@0 92
michael@0 93 x = JSON.stringify(foo, null, " ");
michael@0 94 assertEq(x, '{\n "bar": {\n "bar": 42,\n "schmoo": []\n },\n "qux": 42,\n "quux": 42\n}');
michael@0 95
michael@0 96 foo = {bar:{bar:{}}}
michael@0 97 x = JSON.stringify(foo, null, " ");
michael@0 98 assertEq(x, '{\n "bar": {\n "bar": {}\n }\n}');
michael@0 99
michael@0 100 x = JSON.stringify({ x: 1, arr: [1] },
michael@0 101 function (k,v) { return typeof v === 'number' ? 3 : v; });
michael@0 102 assertEq(x, '{"x":3,"arr":[3]}');
michael@0 103
michael@0 104 foo = ['e'];
michael@0 105 x = JSON.stringify(foo, null, '\t');
michael@0 106 assertEq(x, '[\n\t"e"\n]');
michael@0 107
michael@0 108 foo = {0:0, 1:1, 2:2, 3:undefined};
michael@0 109 x = JSON.stringify(foo, returnObjectFor1);
michael@0 110 assertEq(x, '{"0":0,"1":{},"2":2}');
michael@0 111
michael@0 112 x = JSON.stringify(foo, returnArrayFor1);
michael@0 113 assertEq(x, '{"0":0,"1":[],"2":2}');
michael@0 114
michael@0 115 x = JSON.stringify(foo, returnNullFor1);
michael@0 116 assertEq(x, '{"0":0,"1":null,"2":2}');
michael@0 117
michael@0 118 x = JSON.stringify(foo, returnStringForUndefined);
michael@0 119 assertEq(x, '{"0":0,"1":1,"2":2,"3":"undefined value"}');
michael@0 120
michael@0 121 try
michael@0 122 {
michael@0 123 JSON.stringify(foo, returnCycleObjectFor1);
michael@0 124 throw new Error("no error thrown");
michael@0 125 }
michael@0 126 catch (e)
michael@0 127 {
michael@0 128 assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e);
michael@0 129 }
michael@0 130
michael@0 131 try
michael@0 132 {
michael@0 133 JSON.stringify(foo, returnCycleArrayFor1);
michael@0 134 throw new Error("no error thrown");
michael@0 135 }
michael@0 136 catch (e)
michael@0 137 {
michael@0 138 assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e);
michael@0 139 }
michael@0 140
michael@0 141 foo = [0, 1, 2, undefined];
michael@0 142 try
michael@0 143 {
michael@0 144 JSON.stringify(foo, returnCycleObjectFor1);
michael@0 145 throw new Error("no error thrown");
michael@0 146 }
michael@0 147 catch (e)
michael@0 148 {
michael@0 149 assertEq(e instanceof TypeError, true, "no TypeError thrown: " + e);
michael@0 150 }
michael@0 151
michael@0 152 /******************************************************************************/
michael@0 153
michael@0 154 if (typeof reportCompare === "function")
michael@0 155 reportCompare(true, true);
michael@0 156
michael@0 157 print("Tests complete");

mercurial