forked from rxi/json.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
301 lines (264 loc) · 6.99 KB
/
test.lua
File metadata and controls
301 lines (264 loc) · 6.99 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
-- I was having a heck of a time figuring out how to make the filepaths play nice, so I just moved
-- the tests up here; hopefully I can figure this out before trying to benchmark things
local json = require("coroutinejson")
pprint = require("pprint")
local fmt = string.format
local function test(name, func)
xpcall(function()
func()
print( fmt("[pass] %s", name) )
end, function(err)
print( fmt("[fail] %s : %s", name, err) )
end)
end
local function equal(a, b)
-- Handle table
if type(a) == "table" and type(b) == "table" then
for k in pairs(a) do
if not equal(a[k], b[k]) then
return false
end
end
for k in pairs(b) do
if not equal(b[k], a[k]) then
return false
end
end
return true
end
-- Handle scalar
return a == b
end
local function u(tbl) -- mark a table as unfinished
tbl[json.unfinished] = true
return tbl
end
test("numbers", function()
local t = {
[ "123.456" ] = 123.456,
[ "-123" ] = -123,
[ "-567.765" ] = -567.765,
[ "12.3" ] = 12.3,
[ "0" ] = 0,
[ "0.10000000012" ] = 0.10000000012,
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( res == v, fmt("expected '%s', got '%s'", k, res) )
local res = json.encode(v)
assert( res == k, fmt("expected '%s', got '%s'", v, res) )
end
assert( json.decode("13e2") == 13e2 )
assert( json.decode("13E+2") == 13e2 )
assert( json.decode("13e-2") == 13e-2 )
end)
test("literals", function()
assert( json.decode("true") == true )
assert( json.encode(true) == "true" )
assert( json.decode("false") == false )
assert( json.encode(false) == "false" )
assert( json.decode("null") == nil )
assert( json.encode(nil) == "null")
end)
test("strings", function()
local s = ""
assert( s == json.decode( json.encode(s) ) )
local s = "\\"
assert( s == json.decode( json.encode(s) ) )
local s = "Hello world"
assert( s == json.decode( json.encode(s) ) )
local s = "\0 \13 \27"
assert( s == json.decode( json.encode(s) ) )
local s = "\0\r\n\8"
assert( s == json.decode( json.encode(s) ) )
end)
test("unicode", function()
local s = "こんにちは世界"
assert( s == json.decode( json.encode(s) ) )
end)
test("arrays", function()
local t = { "cat", "dog", "owl" }
assert( equal( t, json.decode( json.encode(t) ) ) )
end)
test("objects", function()
local t = { x = 10, y = 20, z = 30 }
assert( equal( t, json.decode( json.encode(t) ) ) )
end)
--test("strict decode", function()
-- local t = {
-- '{x : 1}',
-- '{x : hello}',
-- "{'x' : 1}",
-- '{"x" : nil}',
-- '{"x" : 0x10}',
-- '{"x" : 001}',
-- '{"x" : .1}',
-- '{"x" : 1, }',
-- '[1, 2, 3, ]',
-- }
-- for i, v in ipairs(t) do
-- local status = pcall(json.decode, v)
-- assert( not status, fmt("'%s' was parsed without error", v) )
-- end
--end)
test("decode invalid", function()
local t = {
'',
' ',
'{',
'[',
'{"x" : ',
'{"x" : 1',
'{"x" : z }',
'{"x" : 123z }',
'{x : 123 }',
'{10 : 123 }',
'{]',
'[}',
'"a',
'10 xx',
'{}123'
}
for i, v in ipairs(t) do
local status = pcall(json.decode, v)
assert( not status, fmt("'%s' was parsed without error", v) )
end
end)
test("decode invalid string", function()
local t = {
[["\z"]],
[["\1"]],
[["\u000z"]],
[["\ud83d\ude0q"]],
'"x\ny"',
'"x\0y"',
}
for i, v in ipairs(t) do
local status, err = pcall(json.decode, v)
assert( not status, fmt("'%s' was parsed without error", v) )
end
end)
test("decode escape", function()
local t = {
[ [["\u263a"]] ] = '☺',
[ [["\ud83d\ude02"]] ] = '😂',
[ [["\r\n\t\\\""]] ] = '\r\n\t\\"',
[ [["\\"]] ] = '\\',
[ [["\\\\"]] ] = '\\\\',
[ [["\/"]] ] = '/',
[ [["\\u \u263a"]] ] = [[\u ☺]],
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( res == v, fmt("expected '%s', got '%s'", v, res) )
end
end)
test("decode empty", function()
local t = {
[ '[]' ] = {},
[ '{}' ] = {},
[ '""' ] = "",
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( equal(res, v), fmt("'%s' did not equal expected", k) )
end
end)
test("decode collection", function()
local t = {
[ '[1, 2, 3, 4, 5, 6]' ] = {1, 2, 3, 4, 5, 6},
[ '[1, 2, 3, "hello"]' ] = {1, 2, 3, "hello"},
[ '{ "name": "test", "id": 231 }' ] = {name = "test", id = 231},
[ '{"x":1,"y":2,"z":[1,2,3]}' ] = {x = 1, y = 2, z = {1, 2, 3}},
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( equal(res, v), fmt("'%s' did not equal expected", k) )
end
end)
test("decode via decoder.resume", function()
local t = {
['[1, 2, 3, 4, 5, 6]'] = {
u{},
u{1},
u{1, 2},
u{1, 2, 3},
u{1, 2, 3, 4},
u{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5, 6}},
['{ "name": "test", "id": 231 }'] = {
u{},
u{name = "test"},
{name = "test", id = 231}
}
}
for k, v in pairs(t) do
local decoder = json.decoder(k)
local length = #v
local complete, res
for progress, value in ipairs(v) do
assert( not complete, fmt("'%s' completed early", k))
complete, res = decoder:resume(k)
assert( equal(res, v[progress]), fmt("'%s' did not equal expected on iteration '%d'", k, progress))
end
assert( complete, fmt("'%s' did not complete", k))
end
end)
test("decode via decoder.work", function()
local t = {}
do
local items = {}
math.randomseed(38912)
for k=1, 100 do items[k] = math.random(999) end
t['['.. table.concat(items, ',') ..']'] = items
end
for k, v in pairs(t) do
local decoder = json.decoder(k)
local out
for i=1,#v//2 do
assert(not decoder:work(2), 'returned early')
end
out = decoder:work(1) -- one extra iteration to mark the table as finished
pprint(out)
pprint(v)
assert(equal(out, v), 'incorrect result')
end
end)
test("encode invalid", function()
local t = {
{ [1000] = "b" },
{ [ function() end ] = 12 },
{ nil, 2, 3, 4 },
{ x = 10, [1] = 2 },
{ [1] = "a", [3] = "b" },
{ x = 10, [4] = 5 },
}
for i, v in ipairs(t) do
local status, res = pcall(json.encode, v)
assert( not status, fmt("encoding idx %d did not result in an error", i) )
end
end)
test("encode invalid number", function()
local t = {
math.huge, -- inf
-math.huge, -- -inf
math.huge * 0, -- NaN
}
for i, v in ipairs(t) do
local status, res = pcall(json.encode, v)
assert( not status, fmt("encoding '%s' did not result in an error", v) )
end
end)
test("encode escape", function()
local t = {
[ '"x"' ] = [["\"x\""]],
[ 'x\ny' ] = [["x\ny"]],
[ 'x\0y' ] = [["x\u0000y"]],
[ 'x\27y' ] = [["x\u001by"]],
[ '\r\n\t\\"' ] = [["\r\n\t\\\""]],
}
for k, v in pairs(t) do
local res = json.encode(k)
assert( res == v, fmt("'%s' was not escaped properly", k) )
end
end)