-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.lua
190 lines (170 loc) · 6.28 KB
/
test.lua
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
print "1..5"
local tap = require("tap")
local yajl = require("yajl")
local ok = tap.ok
function main()
test_simple()
test_issue_13()
null_at_end_of_array()
null_object_value()
weird_numbers()
number_in_string()
test_generator()
test_to_value()
end
function to_value(string)
local result
local stack = {
function(val) result = val end
}
local obj_key
local events = {
value = function(_, val)
stack[#stack](val)
end,
open_array = function()
local arr = {}
local idx = 1
stack[#stack](arr)
table.insert(stack, function(val)
arr[idx] = val
idx = idx + 1
end)
end,
open_object = function()
local obj = {}
stack[#stack](obj)
table.insert(stack, function(val)
obj[obj_key] = val
end)
end,
object_key = function(_, val)
obj_key = val
end,
close = function()
stack[#stack] = nil
end,
}
yajl.parser({ events = events })(string)
return result
end
function test_issue_13()
local custom = { __gen_json = function(self, gen)
gen:string("custom json serializer")
end
}
setmetatable(custom, custom)
local val = {
func = function() end,
float = 1.5,
integer = 5,
string = "hello",
empty = {}, -- defaults to an empty array.
["false"] = false,
custom = custom,
ostr_key = { key = "value" },
obool_key = { [true] = true },
onull_key = { [yajl.null] = yajl.null },
}
-- Round trip it:
local got = yajl.to_value(yajl.to_string(val))
ok(string.find(got.func, "^function: "), "expect 'function: .*', got '" .. tostring(got.func) .. "'")
ok(got.float == 1.5, "expect 1.5, got " .. tostring(got.float))
ok(got.integer == 5, "expect 5, got " .. tostring(got.integer))
ok(got.string == "hello", "expect 'hello', got '" .. tostring(got.string) .. "'")
ok(type(got.empty) == "table" and #got.empty == 0, "expect empty table, got " .. type(got.empty) .. " len=" .. #got.empty)
ok(got["false"] == false, "expected false, got " .. tostring(got["false"]))
ok(got.custom == "custom json serializer", "expected custom json serializer to run, got " .. tostring(got.custom))
ok(type(got.ostr_key) == "table" and got.ostr_key.key == "value", "got " .. tostring(got.ostr_key))
ok(type(got.obool_key) == "table" and got.obool_key["true"] == true, "got " .. tostring(got.obool_key))
ok(type(got.onull_key) == "table" and got.onull_key["null"] == yajl.null, "got " .. tostring(got.onull_key))
end
function test_to_value()
local json = '[["float",1.5,"integer",5,"string","hello","empty",[],"false",false,"custom","custom json serializer","ostr_key",{"key":"value"},"obool_key",{"true":true},"onull_key",{"null":null}],10,10.3,10.3,"a string",null,false,true]'
local val = yajl.to_value(json)
local got = yajl.to_string(val);
ok(got == json,
"yajl.to_value(" .. json .. ") -> " .. got)
end
function test_generator()
local strings = {}
local generator = yajl.generator {
printer = function(string)
table.insert(strings, string)
end
}
local custom = { __gen_json = function(self, gen)
gen:string("custom json serializer")
end
}
setmetatable(custom, custom)
generator:open_array()
generator:value({ "float", 1.5,
"integer", 5,
"string", "hello",
"empty", {}, -- defaults to an empty array.
"false", false,
"custom", custom,
"ostr_key", { key = "value" },
"obool_key", { [true] = true },
"onull_key", { [yajl.null] = yajl.null },
})
generator:integer(10)
generator:double(10.3)
generator:number(10.3)
generator:string("a string")
generator:null()
generator:boolean(false)
generator:boolean(true)
generator:open_object()
generator:close()
generator:close()
local expect = '[["float",1.5,"integer",5,"string","hello","empty",[],"false",false,"custom","custom json serializer","ostr_key",{"key":"value"},"obool_key",{"true":true},"onull_key",{"null":null}],10,10.300000000000000711,10.3,"a string",null,false,true,{}]'
local got = table.concat(strings)
ok(expect == got, expect .. " == " .. got)
end
function test_simple()
-- Thanks to fab13n@github for this bug report:
-- https://github.com/brimworks/lua-yajl/issues/8
assert(yajl.to_value(yajl.to_string(0)) == 0)
local expect =
'['..
'"float",1.5,'..
'"integer",5,'..
'"true",true,'..
'"false",false,'..
'"null",null,'..
'"string","hello",'..
'"array",[1,2],'..
'"object",{"key":"value"}'..
']'
-- Input to to_value matches the output of to_string:
local got = yajl.to_string(to_value(expect))
ok(expect == got, expect .. " == " .. tostring(got))
end
function null_at_end_of_array()
local expect = '["something",null]'
local got = yajl.to_string(to_value(expect))
ok(expect == got, expect .. " == " .. tostring(got))
end
function null_object_value()
local expect = '{"something":null}'
local got = yajl.to_string(to_value(expect))
ok(expect == got, expect .. " == " .. tostring(got))
end
function weird_numbers()
-- See note in README about how we are broken when it comes to big numbers.
local expect = '[1e+666,-1e+666,-0]'
local got = yajl.to_string(to_value(expect))
ok(expect == got, expect .. " == " .. tostring(got))
local nan = 0/0
got = yajl.to_string { nan };
ok("[-0]" == got, "NaN converts to -0 (got: " .. got .. ")")
end
function number_in_string()
-- Thanks to agentzh for this bug report!
local expect = '{"2":"two"}'
local got = yajl.to_string {["2"]="two"}
ok(expect == got, expect .. " == " .. tostring(got))
end
main()