|
1 // Ported from dom/src/json/test/unit/test_wrappers.js and |
|
2 // dom/src/json/test/unit/test_decode.js |
|
3 |
|
4 function assertIsObject(x) |
|
5 { |
|
6 assertEq(typeof x, "object"); |
|
7 assertEq(x instanceof Object, true); |
|
8 } |
|
9 |
|
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 } |
|
18 |
|
19 var x; |
|
20 var props; |
|
21 |
|
22 // empty object |
|
23 x = JSON.parse("{}"); |
|
24 assertIsObject(x); |
|
25 assertEq(Object.getOwnPropertyNames(x).length, 0); |
|
26 |
|
27 // empty array |
|
28 x = JSON.parse("[]"); |
|
29 assertIsArray(x); |
|
30 assertEq(x.length, 0); |
|
31 |
|
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); |
|
38 |
|
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); |
|
49 |
|
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); |
|
58 |
|
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); |
|
69 |
|
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); |
|
80 |
|
81 x = JSON.parse('{"foo":[{"foo":[{"foo":{}}]}]}'); |
|
82 assertIsObject(x.foo[0].foo[0].foo); |
|
83 |
|
84 x = JSON.parse('{"foo":[{"foo":[{"foo":[]}]}]}'); |
|
85 assertIsArray(x.foo[0].foo[0].foo); |
|
86 |
|
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"); |
|
94 |
|
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"); |
|
101 |
|
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); |
|
111 |
|
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); |
|
123 |
|
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"); |
|
142 |
|
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); |
|
150 |
|
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"); |
|
157 |
|
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."); |
|
172 |
|
173 /******************************************************************************/ |
|
174 |
|
175 if (typeof reportCompare === "function") |
|
176 reportCompare(true, true); |
|
177 |
|
178 print("Tests complete"); |