js/src/tests/ecma_5/JSON/parse.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/JSON/parse.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,178 @@
     1.4 +// Ported from dom/src/json/test/unit/test_wrappers.js and
     1.5 +// dom/src/json/test/unit/test_decode.js
     1.6 +
     1.7 +function assertIsObject(x)
     1.8 +{
     1.9 +  assertEq(typeof x, "object");
    1.10 +  assertEq(x instanceof Object, true);
    1.11 +}
    1.12 +
    1.13 +function assertIsArray(x)
    1.14 +{
    1.15 +  assertIsObject(x);
    1.16 +  assertEq(Array.isArray(x), true);
    1.17 +  assertEq(Object.getPrototypeOf(x), Array.prototype);
    1.18 +  assertEq(x instanceof Array, true);
    1.19 +  assertEq(x.constructor, Array);
    1.20 +}
    1.21 +
    1.22 +var x;
    1.23 +var props;
    1.24 +
    1.25 +// empty object
    1.26 +x = JSON.parse("{}");
    1.27 +assertIsObject(x);
    1.28 +assertEq(Object.getOwnPropertyNames(x).length, 0);
    1.29 +
    1.30 +// empty array
    1.31 +x = JSON.parse("[]");
    1.32 +assertIsArray(x);
    1.33 +assertEq(x.length, 0);
    1.34 +
    1.35 +// one element array
    1.36 +x = JSON.parse("[[]]");
    1.37 +assertIsArray(x);
    1.38 +assertEq(x.length, 1);
    1.39 +assertIsArray(x[0]);
    1.40 +assertEq(x[0].length, 0);
    1.41 +
    1.42 +// multiple arrays
    1.43 +x = JSON.parse("[[],[],[]]");
    1.44 +assertIsArray(x);
    1.45 +assertEq(x.length, 3);
    1.46 +assertIsArray(x[0]);
    1.47 +assertEq(x[0].length, 0);
    1.48 +assertIsArray(x[1]);
    1.49 +assertEq(x[1].length, 0);
    1.50 +assertIsArray(x[2]);
    1.51 +assertEq(x[2].length, 0);
    1.52 +
    1.53 +// array key/value
    1.54 +x = JSON.parse('{"foo":[]}');
    1.55 +assertIsObject(x);
    1.56 +props = Object.getOwnPropertyNames(x);
    1.57 +assertEq(props.length, 1);
    1.58 +assertEq(props[0], "foo");
    1.59 +assertIsArray(x.foo);
    1.60 +assertEq(x.foo.length, 0);
    1.61 +
    1.62 +x = JSON.parse('{"foo":[], "bar":[]}');
    1.63 +assertIsObject(x);
    1.64 +props = Object.getOwnPropertyNames(x).sort();
    1.65 +assertEq(props.length, 2);
    1.66 +assertEq(props[0], "bar");
    1.67 +assertEq(props[1], "foo");
    1.68 +assertIsArray(x.foo);
    1.69 +assertEq(x.foo.length, 0);
    1.70 +assertIsArray(x.bar);
    1.71 +assertEq(x.bar.length, 0);
    1.72 +
    1.73 +// nesting
    1.74 +x = JSON.parse('{"foo":[{}]}');
    1.75 +assertIsObject(x);
    1.76 +props = Object.getOwnPropertyNames(x);
    1.77 +assertEq(props.length, 1);
    1.78 +assertEq(props[0], "foo");
    1.79 +assertIsArray(x.foo);
    1.80 +assertEq(x.foo.length, 1);
    1.81 +assertIsObject(x.foo[0]);
    1.82 +assertEq(Object.getOwnPropertyNames(x.foo[0]).length, 0);
    1.83 +
    1.84 +x = JSON.parse('{"foo":[{"foo":[{"foo":{}}]}]}');
    1.85 +assertIsObject(x.foo[0].foo[0].foo);
    1.86 +
    1.87 +x = JSON.parse('{"foo":[{"foo":[{"foo":[]}]}]}');
    1.88 +assertIsArray(x.foo[0].foo[0].foo);
    1.89 +
    1.90 +// strings
    1.91 +x = JSON.parse('{"foo":"bar"}');
    1.92 +assertIsObject(x);
    1.93 +props = Object.getOwnPropertyNames(x);
    1.94 +assertEq(props.length, 1);
    1.95 +assertEq(props[0], "foo");
    1.96 +assertEq(x.foo, "bar");
    1.97 +
    1.98 +x = JSON.parse('["foo", "bar", "baz"]');
    1.99 +assertIsArray(x);
   1.100 +assertEq(x.length, 3);
   1.101 +assertEq(x[0], "foo");
   1.102 +assertEq(x[1], "bar");
   1.103 +assertEq(x[2], "baz");
   1.104 +
   1.105 +// numbers
   1.106 +x = JSON.parse('{"foo":5.5, "bar":5}');
   1.107 +assertIsObject(x);
   1.108 +props = Object.getOwnPropertyNames(x).sort();
   1.109 +assertEq(props.length, 2);
   1.110 +assertEq(props[0], "bar");
   1.111 +assertEq(props[1], "foo");
   1.112 +assertEq(x.foo, 5.5);
   1.113 +assertEq(x.bar, 5);
   1.114 +
   1.115 +// keywords
   1.116 +x = JSON.parse('{"foo": true, "bar":false, "baz":null}');
   1.117 +assertIsObject(x);
   1.118 +props = Object.getOwnPropertyNames(x).sort();
   1.119 +assertEq(props.length, 3);
   1.120 +assertEq(props[0], "bar");
   1.121 +assertEq(props[1], "baz");
   1.122 +assertEq(props[2], "foo");
   1.123 +assertEq(x.foo, true);
   1.124 +assertEq(x.bar, false);
   1.125 +assertEq(x.baz, null);
   1.126 +
   1.127 +// short escapes
   1.128 +x = JSON.parse('{"foo": "\\"", "bar":"\\\\", "baz":"\\b","qux":"\\f", "quux":"\\n", "quuux":"\\r","quuuux":"\\t"}');
   1.129 +props = Object.getOwnPropertyNames(x).sort();
   1.130 +assertEq(props.length, 7);
   1.131 +assertEq(props[0], "bar");
   1.132 +assertEq(props[1], "baz");
   1.133 +assertEq(props[2], "foo");
   1.134 +assertEq(props[3], "quuuux");
   1.135 +assertEq(props[4], "quuux");
   1.136 +assertEq(props[5], "quux");
   1.137 +assertEq(props[6], "qux");
   1.138 +assertEq(x.foo, '"');
   1.139 +assertEq(x.bar, '\\');
   1.140 +assertEq(x.baz, '\b');
   1.141 +assertEq(x.qux, '\f');
   1.142 +assertEq(x.quux, "\n");
   1.143 +assertEq(x.quuux, "\r");
   1.144 +assertEq(x.quuuux, "\t");
   1.145 +
   1.146 +// unicode escape
   1.147 +x = JSON.parse('{"foo":"hmm\\u006dmm"}');
   1.148 +assertIsObject(x);
   1.149 +props = Object.getOwnPropertyNames(x);
   1.150 +assertEq(props.length, 1);
   1.151 +assertEq(props[0], "foo");
   1.152 +assertEq("hmm\u006dmm", x.foo);
   1.153 +
   1.154 +x = JSON.parse('{"hmm\\u006dmm":"foo"}');
   1.155 +assertIsObject(x);
   1.156 +props = Object.getOwnPropertyNames(x);
   1.157 +assertEq(props.length, 1);
   1.158 +assertEq(props[0], "hmmmmm");
   1.159 +assertEq(x.hmm\u006dmm, "foo");
   1.160 +
   1.161 +// miscellaneous
   1.162 +x = JSON.parse('{"JSON Test Pattern pass3": {"The outermost value": "must be an object or array.","In this test": "It is an object." }}');
   1.163 +assertIsObject(x);
   1.164 +props = Object.getOwnPropertyNames(x);
   1.165 +assertEq(props.length, 1);
   1.166 +assertEq(props[0], "JSON Test Pattern pass3");
   1.167 +assertIsObject(x["JSON Test Pattern pass3"]);
   1.168 +props = Object.getOwnPropertyNames(x["JSON Test Pattern pass3"]).sort();
   1.169 +assertEq(props.length, 2);
   1.170 +assertEq(props[0], "In this test");
   1.171 +assertEq(props[1], "The outermost value");
   1.172 +assertEq(x["JSON Test Pattern pass3"]["The outermost value"],
   1.173 +         "must be an object or array.");
   1.174 +assertEq(x["JSON Test Pattern pass3"]["In this test"], "It is an object.");
   1.175 +
   1.176 +/******************************************************************************/
   1.177 +
   1.178 +if (typeof reportCompare === "function")
   1.179 +  reportCompare(true, true);
   1.180 +
   1.181 +print("Tests complete");

mercurial