summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/JSON/parse.js
blob: ff9dc51a79a1c5f288a96aeb94139dbe38675784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
function assertIsObject(x)
{
  assertEq(typeof x, "object");
  assertEq(x instanceof Object, true);
}

function assertIsArray(x)
{
  assertIsObject(x);
  assertEq(Array.isArray(x), true);
  assertEq(Object.getPrototypeOf(x), Array.prototype);
  assertEq(x instanceof Array, true);
  assertEq(x.constructor, Array);
}

var x;
var props;

// empty object
x = JSON.parse("{}");
assertIsObject(x);
assertEq(Object.getOwnPropertyNames(x).length, 0);

// empty array
x = JSON.parse("[]");
assertIsArray(x);
assertEq(x.length, 0);

// one element array
x = JSON.parse("[[]]");
assertIsArray(x);
assertEq(x.length, 1);
assertIsArray(x[0]);
assertEq(x[0].length, 0);

// multiple arrays
x = JSON.parse("[[],[],[]]");
assertIsArray(x);
assertEq(x.length, 3);
assertIsArray(x[0]);
assertEq(x[0].length, 0);
assertIsArray(x[1]);
assertEq(x[1].length, 0);
assertIsArray(x[2]);
assertEq(x[2].length, 0);

// array key/value
x = JSON.parse('{"foo":[]}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x);
assertEq(props.length, 1);
assertEq(props[0], "foo");
assertIsArray(x.foo);
assertEq(x.foo.length, 0);

x = JSON.parse('{"foo":[], "bar":[]}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x).sort();
assertEq(props.length, 2);
assertEq(props[0], "bar");
assertEq(props[1], "foo");
assertIsArray(x.foo);
assertEq(x.foo.length, 0);
assertIsArray(x.bar);
assertEq(x.bar.length, 0);

// nesting
x = JSON.parse('{"foo":[{}]}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x);
assertEq(props.length, 1);
assertEq(props[0], "foo");
assertIsArray(x.foo);
assertEq(x.foo.length, 1);
assertIsObject(x.foo[0]);
assertEq(Object.getOwnPropertyNames(x.foo[0]).length, 0);

x = JSON.parse('{"foo":[{"foo":[{"foo":{}}]}]}');
assertIsObject(x.foo[0].foo[0].foo);

x = JSON.parse('{"foo":[{"foo":[{"foo":[]}]}]}');
assertIsArray(x.foo[0].foo[0].foo);

// strings
x = JSON.parse('{"foo":"bar"}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x);
assertEq(props.length, 1);
assertEq(props[0], "foo");
assertEq(x.foo, "bar");

x = JSON.parse('["foo", "bar", "baz"]');
assertIsArray(x);
assertEq(x.length, 3);
assertEq(x[0], "foo");
assertEq(x[1], "bar");
assertEq(x[2], "baz");

// numbers
x = JSON.parse('{"foo":5.5, "bar":5}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x).sort();
assertEq(props.length, 2);
assertEq(props[0], "bar");
assertEq(props[1], "foo");
assertEq(x.foo, 5.5);
assertEq(x.bar, 5);

// keywords
x = JSON.parse('{"foo": true, "bar":false, "baz":null}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x).sort();
assertEq(props.length, 3);
assertEq(props[0], "bar");
assertEq(props[1], "baz");
assertEq(props[2], "foo");
assertEq(x.foo, true);
assertEq(x.bar, false);
assertEq(x.baz, null);

// short escapes
x = JSON.parse('{"foo": "\\"", "bar":"\\\\", "baz":"\\b","qux":"\\f", "quux":"\\n", "quuux":"\\r","quuuux":"\\t"}');
props = Object.getOwnPropertyNames(x).sort();
assertEq(props.length, 7);
assertEq(props[0], "bar");
assertEq(props[1], "baz");
assertEq(props[2], "foo");
assertEq(props[3], "quuuux");
assertEq(props[4], "quuux");
assertEq(props[5], "quux");
assertEq(props[6], "qux");
assertEq(x.foo, '"');
assertEq(x.bar, '\\');
assertEq(x.baz, '\b');
assertEq(x.qux, '\f');
assertEq(x.quux, "\n");
assertEq(x.quuux, "\r");
assertEq(x.quuuux, "\t");

// unicode escape
x = JSON.parse('{"foo":"hmm\\u006dmm"}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x);
assertEq(props.length, 1);
assertEq(props[0], "foo");
assertEq("hmm\u006dmm", x.foo);

x = JSON.parse('{"hmm\\u006dmm":"foo"}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x);
assertEq(props.length, 1);
assertEq(props[0], "hmmmmm");
assertEq(x.hmm\u006dmm, "foo");

// miscellaneous
x = JSON.parse('{"JSON Test Pattern pass3": {"The outermost value": "must be an object or array.","In this test": "It is an object." }}');
assertIsObject(x);
props = Object.getOwnPropertyNames(x);
assertEq(props.length, 1);
assertEq(props[0], "JSON Test Pattern pass3");
assertIsObject(x["JSON Test Pattern pass3"]);
props = Object.getOwnPropertyNames(x["JSON Test Pattern pass3"]).sort();
assertEq(props.length, 2);
assertEq(props[0], "In this test");
assertEq(props[1], "The outermost value");
assertEq(x["JSON Test Pattern pass3"]["The outermost value"],
         "must be an object or array.");
assertEq(x["JSON Test Pattern pass3"]["In this test"], "It is an object.");

/******************************************************************************/

if (typeof reportCompare === "function")
  reportCompare(true, true);

print("Tests complete");