This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest.lua
executable file
·233 lines (208 loc) · 8.57 KB
/
unittest.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
224
225
226
227
228
229
230
231
232
233
local TkJson = require('source/TkJson')
local TkDriver = require('source/TkDriver')
local function DecodeLiteral()
TkDriver.TestDecode(TkJson.null, 'null')
TkDriver.TestDecode(true, 'true')
TkDriver.TestDecode(false, 'false')
end
local function DecodeIllegalLiteral()
TkDriver.TestError(TkJson.ErrorCode.ExpectValue, 1, 1, '')
TkDriver.TestError(TkJson.ErrorCode.ExpectValue, 1, 2, ' ')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, 'nul')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, '?')
TkDriver.TestError(TkJson.ErrorCode.RootNotSingular, 1, 5, 'null x')
end
local function DecodeNumber()
TkDriver.TestDecode(0.0, '0')
TkDriver.TestDecode(0.0, '-0')
TkDriver.TestDecode(0.0, '-0.0')
TkDriver.TestDecode(1.0, '1')
TkDriver.TestDecode(-1.0, '-1')
TkDriver.TestDecode(1.5, '1.5')
TkDriver.TestDecode(-1.5, '-1.5')
TkDriver.TestDecode(3.1416, '3.1416')
TkDriver.TestDecode(1E10, '1E10')
TkDriver.TestDecode(1e10, '1e10')
TkDriver.TestDecode(1E+10, '1E+10')
TkDriver.TestDecode(1E-10, '1E-10')
TkDriver.TestDecode(-1E10, '-1E10')
TkDriver.TestDecode(-1e10, '-1e10')
TkDriver.TestDecode(-1E+10, '-1E+10')
TkDriver.TestDecode(-1E-10, '-1E-10')
TkDriver.TestDecode(1.234E+10, '1.234E+10')
TkDriver.TestDecode(1.234E-10, '1.234E-10')
TkDriver.TestDecode(0.0, '1e-10000')
TkDriver.TestDecode(1.0000000000000002, '1.0000000000000002')
TkDriver.TestDecode( 4.9406564584124654e-324, '4.9406564584124654e-324')
TkDriver.TestDecode(-4.9406564584124654e-324, '-4.9406564584124654e-324')
TkDriver.TestDecode( 2.2250738585072009e-308, '2.2250738585072009e-308')
TkDriver.TestDecode(-2.2250738585072009e-308, '-2.2250738585072009e-308')
TkDriver.TestDecode( 2.2250738585072014e-308, '2.2250738585072014e-308')
TkDriver.TestDecode(-2.2250738585072014e-308, '-2.2250738585072014e-308')
TkDriver.TestDecode( 1.7976931348623157e+308, '1.7976931348623157e+308')
TkDriver.TestDecode(-1.7976931348623157e+308, '-1.7976931348623157e+308')
end
local function DecodeIllegalNumber()
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, '+0')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, '+1')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, '.123')
-- TkDriver.TestError(TkJson.ErrorCode.InvalidValue, '1.')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, 'INF')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, 'inf')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, 'NAN')
TkDriver.TestError(TkJson.ErrorCode.InvalidValue, 1, 1, 'nan')
TkDriver.TestError(TkJson.ErrorCode.NumberTooBig, 1, 6, '1e309')
TkDriver.TestError(TkJson.ErrorCode.NumberTooBig, 1, 7, '-1e309')
-- TkDriver.TestError(TkJson.ErrorCode.RootNotSingular, '0123')
TkDriver.TestError(TkJson.ErrorCode.RootNotSingular, 1, 1, '0x0')
TkDriver.TestError(TkJson.ErrorCode.RootNotSingular, 1, 1, '0x123')
end
local function DecodeString()
TkDriver.TestDecode('', '\"\"')
TkDriver.TestDecode('Hello', '\"Hello\"')
TkDriver.TestDecode('Hello\nWorld', '\"Hello\\nWorld\"')
TkDriver.TestDecode('\" \\ / \b \f \n \r \t', '\"\\\" \\\\ \\/ \\b \\f \\n \\r \\t\"')
TkDriver.TestDecode('Hello\0World', '\"Hello\\u0000World\"')
TkDriver.TestDecode('\x24', '\"\\u0024\"')
TkDriver.TestDecode('\xC2\xA2', '\"\\u00A2\"')
TkDriver.TestDecode('\xE2\x82\xAC', '\"\\u20AC\"')
TkDriver.TestDecode('\xF0\x9D\x84\x9E', '\"\\uD834\\uDD1E\"')
TkDriver.TestDecode('\xF0\x9D\x84\x9E', '\"\\ud834\\udd1e\"')
end
local function DecodeIllegalString()
TkDriver.TestError(TkJson.ErrorCode.MissQuotationMark, 1, 2, '\"')
TkDriver.TestError(TkJson.ErrorCode.MissQuotationMark, 1, 5, '\"abc')
TkDriver.TestError(TkJson.ErrorCode.InvalidStringEscape, 1, 3, '\"\\v\"')
TkDriver.TestError(TkJson.ErrorCode.InvalidStringEscape, 1, 3, '\"\\;\"')
TkDriver.TestError(TkJson.ErrorCode.InvalidStringEscape, 1, 3, '\"\\0\"')
TkDriver.TestError(TkJson.ErrorCode.InvalidStringEscape, 1, 3, '\"\\x12\"')
TkDriver.TestError(TkJson.ErrorCode.InvalidStringChar, 1, 3, '\"\x01\"')
TkDriver.TestError(TkJson.ErrorCode.InvalidStringChar, 1, 3, '\"\x1F\"')
end
local function DecodeArray()
TkDriver.TestDecode(
{ TkJson.null, false, true, 123, 'abc', __length = 5 },
'[ null , false , true , 123 , \"abc\" ]'
)
TkDriver.TestDecode({ __length = 0 }, '[ ]')
TkDriver.TestDecode(
{
{ __length = 0 },
{ 0, __length = 1 },
{ 0, 1, __length = 2 },
{ 0, 1, 2, __length = 3 },
__length = 4
},
'[ [ ] , [ 0 ] , [ 0 , 1 ] , [ 0 , 1 , 2 ] ]'
)
end
local function DecodeIllegalArray()
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrSquareBracket, 1, 3, '[1')
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrSquareBracket, 1, 3, '[1}')
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrSquareBracket, 1, 4, '[1 2')
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrSquareBracket, 1, 4, '[[]')
end
local function DecodeObject()
TkDriver.TestDecode({}, '{}')
TkDriver.TestDecode(
{
['n'] = TkJson.null,
['f'] = false,
['t'] = true,
['i'] = 123,
['s'] = 'abc',
['a'] = { 1, 2, 3, __length = 3 },
['o'] = { ['1'] = 1, ['2'] = 2, ['3'] = 3 }
},
[===[
{
"n": null,
"f": false,
"t": true,
"i": 123,
"s": "abc",
"a": [ 1, 2, 3 ],
"o": { "1": 1, "2": 2, "3": 3 }
}
]===]
)
end
local function DecodeIllegalObject()
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 2, '{:1,')
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 2, '{1:1,')
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 2, '{true:1,')
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 2, '{false:1,')
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 2, '{null:1,')
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 2, '{[]:1,')
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 2, '{{}:1,')
TkDriver.TestError(TkJson.ErrorCode.MissKey, 1, 8, '{\"a\":1,')
TkDriver.TestError(TkJson.ErrorCode.MissColon, 1, 5, '{\"a\"}')
TkDriver.TestError(TkJson.ErrorCode.MissColon, 1, 5, '{\"a\",\"b\"}')
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrCurlyBracket, 1, 7, '{\"a\":1')
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrCurlyBracket, 1, 7, '{\"a\":1]')
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrCurlyBracket, 1, 8, '{\"a\":1 \"b\"')
TkDriver.TestError(TkJson.ErrorCode.MissCommaOrCurlyBracket, 1, 8, '{\"a\":{}')
end
local function TestDecode()
DecodeLiteral()
DecodeIllegalLiteral()
DecodeNumber()
DecodeIllegalNumber()
-- DecodeString()
-- DecodeIllegalString()
-- DecodeArray()
-- DecodeIllegalArray()
-- DecodeObject()
-- DecodeIllegalObject()
end
local TestEncodeLiteral = function()
TkDriver.testRoundTrip('null')
TkDriver.testRoundTrip('true')
TkDriver.testRoundTrip('false')
end
local TestEncodeNumber = function()
TkDriver.testRoundTrip('0')
-- TkDriver.testRoundTrip('-0')
TkDriver.testRoundTrip('1')
TkDriver.testRoundTrip('-1')
TkDriver.testRoundTrip('1.5')
TkDriver.testRoundTrip('-1.5')
TkDriver.testRoundTrip('3.25')
TkDriver.testRoundTrip('1e+20')
TkDriver.testRoundTrip('1.234e+20')
TkDriver.testRoundTrip('1.234e-20')
TkDriver.testRoundTrip('1.0000000000000002')
TkDriver.testRoundTrip('4.9406564584124654e-324')
TkDriver.testRoundTrip('-4.9406564584124654e-324')
TkDriver.testRoundTrip('2.2250738585072009e-308')
TkDriver.testRoundTrip('-2.2250738585072009e-308')
TkDriver.testRoundTrip('2.2250738585072014e-308')
TkDriver.testRoundTrip('-2.2250738585072014e-308')
TkDriver.testRoundTrip('1.7976931348623157e+308')
TkDriver.testRoundTrip('-1.7976931348623157e+308')
end
local TestEncodeString = function()
TkDriver.testRoundTrip("\"\"");
TkDriver.testRoundTrip("\"Hello\"");
TkDriver.testRoundTrip("\"Hello\\nWorld\"");
TkDriver.testRoundTrip("\"\\\" \\\\ / \\b \\f \\n \\r \\t\"");
TkDriver.testRoundTrip("\"Hello\\u0000World\"");
end
local TestEncodeArray = function()
TkDriver.testRoundTrip('[]')
TkDriver.testRoundTrip('[null,false,true,123,\"abc\",[1,2,3]]')
end
local TestEncodeObject = function()
TkDriver.testRoundTrip('{}')
-- TkDriver.testRoundTrip('{\"n\":null,\"f\":false,\"t\":true,\"i\":123,\"s\":\"abc\",\"a\":[1,2,3],\"o\":{\"1\":1,\"2\":2,\"3\":3}}')
end
local TestEncoder = function()
TestEncodeLiteral()
TestEncodeNumber()
TestEncodeString()
TestEncodeArray()
TestEncodeObject()
end
TestDecode()
-- TestEncoder()
print('> All Tests Passed!')