js/src/tests/ecma_5/JSON/parse-reviver.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_reviver.js
michael@0 2
michael@0 3 function doubler(k, v)
michael@0 4 {
michael@0 5 assertEq(typeof k, "string");
michael@0 6
michael@0 7 if (typeof v == "number")
michael@0 8 return 2 * v;
michael@0 9
michael@0 10 return v;
michael@0 11 }
michael@0 12
michael@0 13 var x = JSON.parse('{"a":5,"b":6}', doubler);
michael@0 14 assertEq(x.hasOwnProperty('a'), true);
michael@0 15 assertEq(x.hasOwnProperty('b'), true);
michael@0 16 assertEq(x.a, 10);
michael@0 17 assertEq(x.b, 12);
michael@0 18
michael@0 19 x = JSON.parse('[3, 4, 5]', doubler);
michael@0 20 assertEq(x[0], 6);
michael@0 21 assertEq(x[1], 8);
michael@0 22 assertEq(x[2], 10);
michael@0 23
michael@0 24 // make sure reviver isn't called after a failed parse
michael@0 25 var called = false;
michael@0 26 function dontCallMe(k, v)
michael@0 27 {
michael@0 28 called = true;
michael@0 29 }
michael@0 30
michael@0 31 try
michael@0 32 {
michael@0 33 JSON.parse('{{{{{{{}}}}', dontCallMe);
michael@0 34 throw new Error("didn't throw?");
michael@0 35 }
michael@0 36 catch (e)
michael@0 37 {
michael@0 38 assertEq(e instanceof SyntaxError, true, "wrong exception: " + e);
michael@0 39 }
michael@0 40 assertEq(called, false);
michael@0 41
michael@0 42 /******************************************************************************/
michael@0 43
michael@0 44 if (typeof reportCompare === "function")
michael@0 45 reportCompare(true, true);
michael@0 46
michael@0 47 print("Tests complete");

mercurial