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.

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

mercurial