js/src/tests/ecma_5/JSON/parse.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_wrappers.js and
     2 // dom/src/json/test/unit/test_decode.js
     4 function assertIsObject(x)
     5 {
     6   assertEq(typeof x, "object");
     7   assertEq(x instanceof Object, true);
     8 }
    10 function assertIsArray(x)
    11 {
    12   assertIsObject(x);
    13   assertEq(Array.isArray(x), true);
    14   assertEq(Object.getPrototypeOf(x), Array.prototype);
    15   assertEq(x instanceof Array, true);
    16   assertEq(x.constructor, Array);
    17 }
    19 var x;
    20 var props;
    22 // empty object
    23 x = JSON.parse("{}");
    24 assertIsObject(x);
    25 assertEq(Object.getOwnPropertyNames(x).length, 0);
    27 // empty array
    28 x = JSON.parse("[]");
    29 assertIsArray(x);
    30 assertEq(x.length, 0);
    32 // one element array
    33 x = JSON.parse("[[]]");
    34 assertIsArray(x);
    35 assertEq(x.length, 1);
    36 assertIsArray(x[0]);
    37 assertEq(x[0].length, 0);
    39 // multiple arrays
    40 x = JSON.parse("[[],[],[]]");
    41 assertIsArray(x);
    42 assertEq(x.length, 3);
    43 assertIsArray(x[0]);
    44 assertEq(x[0].length, 0);
    45 assertIsArray(x[1]);
    46 assertEq(x[1].length, 0);
    47 assertIsArray(x[2]);
    48 assertEq(x[2].length, 0);
    50 // array key/value
    51 x = JSON.parse('{"foo":[]}');
    52 assertIsObject(x);
    53 props = Object.getOwnPropertyNames(x);
    54 assertEq(props.length, 1);
    55 assertEq(props[0], "foo");
    56 assertIsArray(x.foo);
    57 assertEq(x.foo.length, 0);
    59 x = JSON.parse('{"foo":[], "bar":[]}');
    60 assertIsObject(x);
    61 props = Object.getOwnPropertyNames(x).sort();
    62 assertEq(props.length, 2);
    63 assertEq(props[0], "bar");
    64 assertEq(props[1], "foo");
    65 assertIsArray(x.foo);
    66 assertEq(x.foo.length, 0);
    67 assertIsArray(x.bar);
    68 assertEq(x.bar.length, 0);
    70 // nesting
    71 x = JSON.parse('{"foo":[{}]}');
    72 assertIsObject(x);
    73 props = Object.getOwnPropertyNames(x);
    74 assertEq(props.length, 1);
    75 assertEq(props[0], "foo");
    76 assertIsArray(x.foo);
    77 assertEq(x.foo.length, 1);
    78 assertIsObject(x.foo[0]);
    79 assertEq(Object.getOwnPropertyNames(x.foo[0]).length, 0);
    81 x = JSON.parse('{"foo":[{"foo":[{"foo":{}}]}]}');
    82 assertIsObject(x.foo[0].foo[0].foo);
    84 x = JSON.parse('{"foo":[{"foo":[{"foo":[]}]}]}');
    85 assertIsArray(x.foo[0].foo[0].foo);
    87 // strings
    88 x = JSON.parse('{"foo":"bar"}');
    89 assertIsObject(x);
    90 props = Object.getOwnPropertyNames(x);
    91 assertEq(props.length, 1);
    92 assertEq(props[0], "foo");
    93 assertEq(x.foo, "bar");
    95 x = JSON.parse('["foo", "bar", "baz"]');
    96 assertIsArray(x);
    97 assertEq(x.length, 3);
    98 assertEq(x[0], "foo");
    99 assertEq(x[1], "bar");
   100 assertEq(x[2], "baz");
   102 // numbers
   103 x = JSON.parse('{"foo":5.5, "bar":5}');
   104 assertIsObject(x);
   105 props = Object.getOwnPropertyNames(x).sort();
   106 assertEq(props.length, 2);
   107 assertEq(props[0], "bar");
   108 assertEq(props[1], "foo");
   109 assertEq(x.foo, 5.5);
   110 assertEq(x.bar, 5);
   112 // keywords
   113 x = JSON.parse('{"foo": true, "bar":false, "baz":null}');
   114 assertIsObject(x);
   115 props = Object.getOwnPropertyNames(x).sort();
   116 assertEq(props.length, 3);
   117 assertEq(props[0], "bar");
   118 assertEq(props[1], "baz");
   119 assertEq(props[2], "foo");
   120 assertEq(x.foo, true);
   121 assertEq(x.bar, false);
   122 assertEq(x.baz, null);
   124 // short escapes
   125 x = JSON.parse('{"foo": "\\"", "bar":"\\\\", "baz":"\\b","qux":"\\f", "quux":"\\n", "quuux":"\\r","quuuux":"\\t"}');
   126 props = Object.getOwnPropertyNames(x).sort();
   127 assertEq(props.length, 7);
   128 assertEq(props[0], "bar");
   129 assertEq(props[1], "baz");
   130 assertEq(props[2], "foo");
   131 assertEq(props[3], "quuuux");
   132 assertEq(props[4], "quuux");
   133 assertEq(props[5], "quux");
   134 assertEq(props[6], "qux");
   135 assertEq(x.foo, '"');
   136 assertEq(x.bar, '\\');
   137 assertEq(x.baz, '\b');
   138 assertEq(x.qux, '\f');
   139 assertEq(x.quux, "\n");
   140 assertEq(x.quuux, "\r");
   141 assertEq(x.quuuux, "\t");
   143 // unicode escape
   144 x = JSON.parse('{"foo":"hmm\\u006dmm"}');
   145 assertIsObject(x);
   146 props = Object.getOwnPropertyNames(x);
   147 assertEq(props.length, 1);
   148 assertEq(props[0], "foo");
   149 assertEq("hmm\u006dmm", x.foo);
   151 x = JSON.parse('{"hmm\\u006dmm":"foo"}');
   152 assertIsObject(x);
   153 props = Object.getOwnPropertyNames(x);
   154 assertEq(props.length, 1);
   155 assertEq(props[0], "hmmmmm");
   156 assertEq(x.hmm\u006dmm, "foo");
   158 // miscellaneous
   159 x = JSON.parse('{"JSON Test Pattern pass3": {"The outermost value": "must be an object or array.","In this test": "It is an object." }}');
   160 assertIsObject(x);
   161 props = Object.getOwnPropertyNames(x);
   162 assertEq(props.length, 1);
   163 assertEq(props[0], "JSON Test Pattern pass3");
   164 assertIsObject(x["JSON Test Pattern pass3"]);
   165 props = Object.getOwnPropertyNames(x["JSON Test Pattern pass3"]).sort();
   166 assertEq(props.length, 2);
   167 assertEq(props[0], "In this test");
   168 assertEq(props[1], "The outermost value");
   169 assertEq(x["JSON Test Pattern pass3"]["The outermost value"],
   170          "must be an object or array.");
   171 assertEq(x["JSON Test Pattern pass3"]["In this test"], "It is an object.");
   173 /******************************************************************************/
   175 if (typeof reportCompare === "function")
   176   reportCompare(true, true);
   178 print("Tests complete");

mercurial