forked from craigmj/json4lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.lua
executable file
·223 lines (195 loc) · 6.21 KB
/
tests.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
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
--[[
Some basic tests for JSON4Lua.
]]--
--- Compares two tables for being data-identical.
function compareData(a,b)
if (type(a)=='string' or type(a)=='number' or type(a)=='boolean' or type(a)=='nil') then return a==b end
-- After basic data types, we're only interested in tables
if (type(a)~='table') then return true end
-- Check that a has everything b has
for k,v in pairs(b) do
if (not compareData( a[k], v ) ) then return false end
end
for k,v in pairs(a) do
if (not compareData( v, b[k] ) ) then return false end
end
return true
end
---
-- Checks that our compareData function works properly
function testCompareData()
s = "name"
r = "name"
assert(compareData(s,r))
assert(not compareData('fred',s))
assert(not compareData(nil, s))
assert(not compareData("123",123))
assert(not compareData(false, nil))
assert(compareData(true, true))
assert(compareData({1,2,3},{1,2,3}))
assert(compareData({'one',2,'three'},{'one',2,'three'}))
assert(not compareData({'one',2,4},{4,2,'one'}))
assert(compareData({one='ichi',two='nichi',three='san'}, {three='san',two='nichi',one='ichi'}))
s = { one={1,2,3}, two={one='hitotsu',two='futatsu',three='mitsu'} }
assert(compareData(s,s))
t = { one={1,2,3}, two={one='een',two='twee',three='drie'} }
assert(not compareData(s,t))
end
testCompareData()
--
--
-- Performs some perfunctory tests on JSON module
function testJSON4Lua()
json = require('json')
if nil then
-- Test encodeString
s = [["\"
]]
r = json._encodeString(s)
assert(r=='\\"\\\\\\"\\n')
s = [["""\\\"]]
r = json._encodeString(s)
assert(r==[[\"\"\"\\\\\\\"]])
end
-- Test encode for basic strings (complicated strings)
s = [[Hello, Lua!]]
r = json.encode(s)
assert(r=='"Hello, Lua!"')
s = [["\"
]]
r = json.encode(s)
assert(r=='\"\\"\\\\\\"\\n\"')
s = [["""\\\"]]
r = json.encode(s)
assert(r==[["\"\"\"\\\\\\\""]])
-- Test encode for numeric values
s = 23
r = json.encode(s)
assert(r=='23')
s=48.123
r = json.encode(s)
assert(r=='48.123')
-- Test encode for boolean values
assert(json.encode(true)=='true')
assert(json.encode(false)=='false')
assert(json.encode(nil)=='null')
-- Test encode for arrays
s = {1,2,3}
r = json.encode(s)
assert(r=="[1,2,3]")
s = {9,9,9}
r = json.encode(s)
assert(r=="[9,9,9]")
-- Complex array test
s = { 2, 'joe', false, nil, 'hi' }
r = json.encode(s)
assert(r=='[2,"joe",false,null,"hi"]')
-- Test encode for tables
s = {Name='Craig',email='[email protected]',age=35}
r = json.encode(s)
-- NB: This test can fail because of order: need to test further once
-- decoding is supported.
-- assert(r==[[{"age":35,"Name":"Craig","email":"[email protected]"}]])
-- Test encoding tables with numeric (string) indexes
s = {}
s['1']='One'
r = json.encode(s)
-- print("r = ", r)
assert(r=='{"1":"One"}')
s['2']= {One='Uno'}
r = json.encode(s)
assert(compareData(json.decode(r), s))
-- Test decode_scanWhitespace
if nil then
s = " \n \r \t "
e = json._decode_scanWhitespace(s,1)
assert(e==string.len(s)+1)
s = " \n\r\t4"
assert(json._decode_scanWhitespace(s,1)==5)
-- Test decode_scanString
s = [["Test"]]
r,e = json._decode_scanString(s,1)
assert(r=='Test' and e==7)
s = [["This\nis a \"test"]]
r = json._decode_scanString(s,1)
assert(r=="This\nis a \"test")
s = [["Test\u00A7\\"]]
r,e = json._decode_scanString(s,1)
assert(r=="Test\xC2\xA7\\" and e==9)
-- Test decode_scanNumber
s = [[354]]
r,e = json._decode_scanNumber(s,1)
assert(r==354 and e==4)
s = [[ 4565.23 AND OTHER THINGS ]]
r,e = json._decode_scanNumber(s,2)
assert(r==4565.23 and e==9)
s = [[ -23.22 and ]]
r,e = json._decode_scanNumber(s,2)
assert(r==-23.22 and e==8)
-- Test decode_scanConstant
s = "true"
r,e = json._decode_scanConstant(s,1)
assert(r==true and e==5)
s = " false "
r,e = json._decode_scanConstant(s,3)
assert(r==false and e==8)
s = "1null6"
r,e = json._decode_scanConstant(s,2)
assert(r==nil and e==6)
-- Test decode_scanArray
s = "[1,2,3]"
r,e = json._decode_scanArray(s,1)
assert(compareData(r,{1,2,3}))
s = [[[ 1 , 3 ,5 , "Fred" , true, false, null, -23 ] ]]
r,e = json._decode_scanArray(s,1)
assert(compareData(r, {1,3,5,'Fred',true,false,nil,-23} ) )
s = "[3,5,null,7,9]"
r,e = json._decode_scanArray(s,1)
assert(compareData(r, {3,5,nil,7,9}))
s = "[3,5,null,7,9,null,null]"
r,e = json._decode_scanArray(s,1)
assert(compareData(r, {3,5,nil,7,9,nil,nil}))
end
s = [["Test\u00A7\\\""]]
r,e = json.decode(s)
assert(r=="Test\xC2\xA7\\\"", r)
-- Test decode_scanObject
s = [[ {"one":1, "two":2, "three":"three", "four":true} ]]
r,e = json.decode(s)
-- for x,y in pairs(r) do
-- print(x,y)
-- end
assert(compareData(r,{one=1,two=2,three='three',four=true}))
s = [[ { "one" : { "first":1,"second":2,"third":3}, "two":2, "three":false } ]]
r,e = json.decode(s)
assert(compareData(r, {one={first=1,second=2,third=3},two=2,three=false}))
s = [[ { "primes" : [2,3,5,7,9], "user":{"name":"craig","age":35,"programs_lua":true},
"lua_is_great":true } ]]
r,e = json.decode(s)
assert(compareData(r, {primes={2,3,5,7,9},user={name='craig',age=35,programs_lua=true},lua_is_great=true}))
-- Test json.null management
t = { 1,2,json.null,4 }
assert( json.encode(t)=="[1,2,null,4]" )
t = {x=json.null }
r = json.encode(t)
assert( json.encode(t) == '{"x":null}' )
-- Test comment decoding
s = [[ /* A comment
that spans
a few lines
*/
"test"
]]
r,e = json.decode(s)
assert(r=='test',"Comment decoding failed")
-- Per error reported by M.Hund, with incorrect decoding of string-numbered tables
s = {}
subt = {a="a",b="b",c="c"}
s['1'] = subt
s['2'] = subt
s['3'] = subt
r = json.decode('{"1":{"a":"a","b":"b","c":"c"},"2":{"a":"a","b":"b","c":"c"},"3":{"a":"a","b":"b","c":"c"}}')
assert(compareData(s, r))
end
testJSON4Lua()
print("JSON4Lua tests completed successfully")