-
Notifications
You must be signed in to change notification settings - Fork 9
/
json_test.go
183 lines (147 loc) · 4.29 KB
/
json_test.go
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
176
177
178
179
180
181
182
183
package otto
import (
"testing"
)
func BenchmarkJSON_parse(b *testing.B) {
vm := New()
for i := 0; i < b.N; i++ {
vm.Run(`JSON.parse("1")`)
vm.Run(`JSON.parse("[1,2,3]")`)
vm.Run(`JSON.parse('{"a":{"x":100,"y":110},"b":[10,20,30],"c":"zazazaza"}')`)
vm.Run(`JSON.parse("[1,2,3]", function(k, v) { return undefined })`)
}
}
func TestJSON_parse(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
JSON.parse("1");
`, 1)
test(`
JSON.parse("null");
`, "null") // TODO Can we make this nil?
test(`
var abc = JSON.parse('"a\uFFFFbc"');
[ abc[0], abc[2], abc[3], abc.length ];
`, "a,b,c,4")
test(`
JSON.parse("[1, 2, 3]");
`, "1,2,3")
test(`
JSON.parse('{ "abc": 1, "def":2 }').abc;
`, 1)
test(`
JSON.parse('{ "abc": { "x": 100, "y": 110 }, "def": [ 10, 20 ,30 ], "ghi": "zazazaza" }').def;
`, "10,20,30")
test(`raise:
JSON.parse("12\t\r\n 34");
`, "SyntaxError: invalid character '3' after top-level value")
test(`
JSON.parse("[1, 2, 3]", function() { return undefined });
`, "undefined")
test(`raise:
JSON.parse("");
`, "SyntaxError: unexpected end of JSON input")
test(`raise:
JSON.parse("[1, 2, 3");
`, "SyntaxError: unexpected end of JSON input")
test(`raise:
JSON.parse("[1, 2, ; abc=10");
`, "SyntaxError: invalid character ';' looking for beginning of value")
test(`raise:
JSON.parse("[1, 2, function(){}]");
`, "SyntaxError: invalid character 'u' in literal false (expecting 'a')")
})
}
func TestJSON_stringify(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
JSON.stringify(function(){});
`, "undefined")
test(`
JSON.stringify(new Boolean(false));
`, "false")
test(`
JSON.stringify({a1: {b1: [1,2,3,4], b2: {c1: 1, c2: 2}}, a2: 'a2'}, null, -5);
`, `{"a1":{"b1":[1,2,3,4],"b2":{"c1":1,"c2":2}},"a2":"a2"}`)
test(`
JSON.stringify(undefined);
`, "undefined")
test(`
JSON.stringify(1);
`, "1")
test(`
JSON.stringify("abc def");
`, "\"abc def\"")
test(`
JSON.stringify(3.14159);
`, "3.14159")
test(`
JSON.stringify([]);
`, "[]")
test(`
JSON.stringify([1, 2, 3]);
`, "[1,2,3]")
test(`
JSON.stringify([true, false, null]);
`, "[true,false,null]")
test(`
JSON.stringify({
abc: { x: 100, y: 110 },
def: [ 10, 20, 30 ],
ghi: "zazazaza"
});
`, `{"abc":{"x":100,"y":110},"def":[10,20,30],"ghi":"zazazaza"}`)
test(`
JSON.stringify([
'e',
{pluribus: 'unum'}
], null, '\t');
`, "[\n\t\"e\",\n\t{\n\t\t\"pluribus\": \"unum\"\n\t}\n]")
test(`
JSON.stringify(new Date(0));
`, `"1970-01-01T00:00:00.000Z"`)
test(`
JSON.stringify([ new Date(0) ], function(key, value){
return this[key] instanceof Date ? 'Date(' + this[key] + ')' : value
});
`, `["Date(Thu, 01 Jan 1970 00:00:00 UTC)"]`)
test(`
JSON.stringify({
abc: 1,
def: 2,
ghi: 3
}, ['abc','def']);
`, `{"abc":1,"def":2}`)
test(`raise:
var abc = {
def: null
};
abc.def = abc;
JSON.stringify(abc)
`, "TypeError: Converting circular structure to JSON")
test(`raise:
var abc= [ null ];
abc[0] = abc;
JSON.stringify(abc);
`, "TypeError: Converting circular structure to JSON")
test(`raise:
var abc = {
def: {}
};
abc.def.ghi = abc;
JSON.stringify(abc)
`, "TypeError: Converting circular structure to JSON")
test(`
var ghi = { "pi": 3.14159 };
var abc = {
def: {}
};
abc.ghi = ghi;
abc.def.ghi = ghi;
JSON.stringify(abc);
`, `{"def":{"ghi":{"pi":3.14159}},"ghi":{"pi":3.14159}}`)
})
}