-
Notifications
You must be signed in to change notification settings - Fork 1
/
val_test.lua
498 lines (452 loc) · 12 KB
/
val_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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
local yaml = require 'yaml'
yaml.cfg{ encode_use_tostring = true }
local ffi = require'ffi'
local val = require 'val'
local testno = 0
local failed = 0
local function ok (r, name)
testno = testno+1
name = name or "test"
if r then
print(string.format("ok %d - %s", testno, name))
else
failed = failed + 1
print(string.format("not ok %d - %s", testno, name))
print(string.format("#\tError: %s",e))
end
end
local function is (v1, v2, name)
testno = testno+1
name = name or "test"
if v1 == v2 then
print(string.format("ok %d - %s", testno, name))
else
failed = failed + 1
print(string.format("not ok %d - %s", testno, name))
print(string.format("#\tError: %s is not %s", v1, v2))
end
end
local function summary()
print(string.format("%d..%d",testno,testno))
if failed > 0 then
print(string.format("# Looks like you failed %d test of %d.",failed,testno))
end
end
local function v_test_ok(validator, struct, name)
testno = testno+1
name = name or "test"
local r,e = pcall(validator,struct)
if r then
print(string.format("ok %d - %s", testno, name))
else
failed = failed + 1
print(string.format("not ok %d - %s", testno, name))
print(string.format("#\tError: %s",e))
end
end
local function v_test_not_ok(validator, struct, kind, name)
testno = testno+1
name = name or "test"
local r,e = pcall(validator,struct)
if r then
failed = failed + 1
print(string.format("not ok %d - %s", testno, name))
print(string.format("#\tError: test is passing"))
else
if type(kind) == 'function' then
if kind(e) then
print(string.format("ok %d - %s", testno, name))
else
failed = failed + 1
print(string.format("not ok %d - %s", testno, name))
print(string.format("#\tError is wrong: %s",e))
end
end
end
end
--[[
synonyms
types + cdata
-- TODO:
non-empty-string
any-number
callable
toboolean
nested structures
custom error
]]
for _,cons in pairs({
{true, ""},
{true, "+"},
{true, "req "},
{true, "required "},
{true, {"req"}},
{true, {"required"}},
{false, "?"},
{false, "opt "},
{false, "optional "},
{false, "maybe "},
{false, {"opt"}},
{false, {"optional"}},
{false, {"maybe"}},
}) do
local required, typecheck = unpack(cons)
for _,test in pairs({
{ "number", { -2^31, -1, 0, 1, 0.5, 2/3, 2^31 }, { "","test", 1LL, 1ULL, {}, function() end, true, false } },
{ "string", { "", "test" }, { 1, 1LL, 1ULL, {}, function() end, true, false } },
{ "table", { {}, {x=1} }, { "", "test", 1, 1LL, 1ULL, function() end, true, false } },
{ "function", { function() end }, { "", "test", 1, 1LL, 1ULL, {}, true, false } },
{ "boolean", { true, false }, { "", "test", 1, 1LL, 1ULL, {}, function() end } },
{ "cdata", { 1LL, 1ULL }, { "", 1, {}, function() end } },
{ "ctype<uint64_t>", { 1ULL }, { "",1, 1LL, {}, function() end } },
{ "ctype<int64_t>", { 1LL }, { "",1, 1ULL, {}, function() end } },
}) do
local typename, positive, negative = unpack(test)
local constraint;
local constraint_name;
if type(typecheck) == 'table' then
if #typecheck == 1 then
local method = unpack(typecheck)
constraint_name = "val."..method.."("..typename..")"
constraint = val[method](typename)
-- elseif #typecheck == 2 then
-- local label,checkf = unpack(typecheck)
-- constraint_name = label.."()"
-- constraint = checkf
else
error("GTFO")
end
else
constraint = typecheck..typename
constraint_name = constraint
end
for _,is_nested in pairs({true,false}) do
local positives = {}
local negatives = {}
local n_cons
local n_cons_name
if is_nested then
for _,v in pairs(positive) do
table.insert(positives, { inner = v })
end
for _,v in pairs(negative) do
table.insert(negatives, { inner = v })
end
n_cons = { inner = constraint }
n_cons_name = "nested "..constraint_name
else
positives = positive
negatives = negative
n_cons = constraint
n_cons_name = "common "..constraint_name
end
local vv = val({ test = n_cons });
for _,value in pairs(positives) do
local real_value
if is_nested then
real_value = value.inner
else
real_value = value
end
v_test_ok(
vv,
{ test = value },
"constraint '"..n_cons_name.."' positive with '"..tostring(real_value).."'"
)
end
if required then
v_test_not_ok(
vv,
{},
function(e) return e:match("missing") end,
"constraint '"..n_cons_name.."' negative: missing"
)
else
v_test_ok(
vv,
is_nested and { test = {} } or {},
"constraint '"..n_cons_name.."' positive missing"
)
end
for _,value in pairs(negatives) do
local real_value
if is_nested then
real_value = value.inner
else
real_value = value
end
v_test_not_ok(
vv,
{ test = value },
function(e)
return e:match("invalid")
and e:match(typename)
and e:match(type(real_value) == 'cdata' and tostring(ffi.typeof(real_value)) or type(real_value))
end,
"constraint '"..n_cons_name.."' negative: invalid with '"..tostring(real_value).."'"
)
end
end
end
end
local dummy = function (v) return v end
local noret = function (v) return end
local custom_err = function (v) error{'wrong', 'myerrormessage'} end
v_test_not_ok(
val(
{ test = custom_err }
),
{ test = 1 },
function(e)
-- print("#",e)
return e:match("wrong") and e:match("myerrormessage")
end,
"required function negative: custom error"
)
v_test_not_ok(
val(
{ test = noret }
),
{ test = 1 },
function(e)
return e:match("invalid") and e:match("after 1 cast")
end,
"required function negative: noreturn"
)
v_test_ok(
val(
{ test = { inner = dummy } }
),
{ test = { inner = "test" }},
"required function nested positive"
)
v_test_not_ok(
val(
{ test = { inner = custom_err } }
),
{ test = { inner = "test" }},
function(e)
return e:match("test%.inner") and e:match("wrong") and e:match("myerrormessage")
end,
"required function nested negative: custom error"
)
v_test_not_ok(
val(
{ test = { inner = noret }}
),
{ test = { inner = "test" }},
"required function nested negative: noreturn"
)
v_test_ok(
val(
{ test = { inner = val.req(dummy, 'string', dummy, 'string') }}
),
{ test = { inner = "test" }},
"required function nested positive: cascade"
)
v_test_not_ok(
val(
{ test = { inner = val.req(dummy, 'string', noret) }}
),
{ test = { inner = "test" }},
function(e)
return e:match("test%.inner") and e:match("invalid") and e:match("after 2 cast")
end,
"required function nested negative: cascade noreturn"
)
v_test_ok(
val(
{ test = { inner = val.req(dummy, 'string', function (v) return 1 end, 'number') }}
),
{ test = { inner = "test" }},
"required function nested positive: cascade with changing type"
)
v_test_not_ok(
val(
{ test = { inner = val.req(dummy, 'string', function (v) return {} end, 'number') }}
),
{ test = { inner = "test" }},
function(e)
return e:match("test%.inner") and e:match("invalid") and e:match("after 2 cast")
end,
"required function nested negative: cascade with changing type"
)
v_test_ok(
val(
{ test = val.opt('table', { nest_str = '+string', nest_num = '+number' }) }
),
{},
"optional nested table positive: missing table"
)
v_test_ok(
val(
{ test = val.opt('table', { nest_str = '+string', nest_num = '+number' }) }
),
{ test = { nest_str = 'str', nest_num = 1 } },
"optional nested table positive: correct table"
)
v_test_not_ok(
val(
{ test = val.opt('table', { nest_str = '+string', nest_num = '+number' }) }
),
{ test = { nest_str = 'str', nest_num = 'str' } },
function(e)
return e:match("test%.nest_num") and e:match("invalid") and e:match("expected number")
end,
"optional nested table negative: invalid value"
)
v_test_not_ok(
val(
{ test = val.opt('table', { nest_str = '+string', nest_num = '+number' }) }
),
{ test = { nest_str = 'str' } },
function(e)
return e:match("test%.nest_num") and e:match("missing")
end,
"optional nested table negative: missing value"
)
local test_table = {}
v_test_ok(
val(
{ test = val.opt(tostring) }
),
test_table,
"optional casts positive"
)
ok(test_table.test == nil, "no rudimentary leftovers")
local json = require 'json'
local custom_handler = function(key_check, value_check, error_check, name)
name = name or "custom handler test"
return function (k, v, e)
if type(key_check) == 'function' then
ok(key_check(k), string.format("'%s' correct key in custom handler", name))
else
is(k, key_check, string.format("'%s' correct key in custom handler", name))
end
if type(value_check) == 'function' then
ok(value_check(v), string.format("'%s' correct value in custom handler", name))
else
is(v, value_check, string.format("'%s' correct value in custom handler", name))
end
if type(error_check) == 'function' then
ok(error_check(e), string.format("'%s' correct error in custom handler", name))
else
is(e[1], error_check[1], string.format("'%s' correct error reason in custom handler", name))
is(e[2], error_check[2], string.format("'%s' correct error message in custom handler", name))
end
error(e)
end
end
local function custom_handler_test (schema, to_test, key_check, value_check, err_check, name)
local vv = val(schema, {
handler = custom_handler(key_check, value_check, err_check, name)
})
local r, e = pcall(vv, to_test)
if r then
ok(false, string.format("'%s' did not raise", name))
end
end
custom_handler_test(
{ a = 'string' },
{ a = 1 },
'args.a',
1,
function(e)
return e[1] == 'invalid' and e[2]:match('got number') and e[2]:match('expected string')
end,
"custom handler for normal errors"
)
custom_handler_test(
{ inner = { a = 'string' } },
{ inner = { a = 1 } },
'args.inner.a',
1,
function(e)
return e[1] == 'invalid' and e[2]:match('got number') and e[2]:match('expected string')
end,
"custom handler for normal errors, nested"
)
custom_handler_test(
{ a = custom_err },
{ a = 1 },
'args.a',
1,
function(e)
return e[1] == 'wrong' and e[2]:match('myerrormessage')
end,
"custom handler for custom errors"
)
custom_handler_test(
{ inner = { a = custom_err } },
{ inner = { a = 1 } },
'args.inner.a',
1,
function(e)
return e[1] == 'wrong' and e[2]:match('myerrormessage')
end,
"custom handler for custom errors: nested"
)
local v = val.idator({
ull = 'ctype<uint64_t>';
string = '?string';
number = val.required(
'number',
function(v, name)
if v < 0 then
error{ 'CustomInvalid', name..' must be positive'; shit="happens" }
end
return v
end,
'number'
);
number2 = val.required(
'number',
function(v,name) return ffi.typeof('uint64_t')(v) --[[return v]] end,
'ctype<uint64_t>',
function(v,name) return tostring(v) end,
'string'
);
nested = {
string = val.req('string');
number = val.opt('number');
};
['x-req-id'] = 'string';
},{
handler = function(name,value,err)
error(box.tuple.new{ 500, { Code=err[1]; ArgumentName=name; ArgumentValue=value; Debug = err[2] } })
end;
})
local r,e = pcall(v, {
string = "test";
number = 1;
number2 = 1;
ull = 1ull;
nested = {
string = 'qwe';
number = 1;
};
['x-req-id'] = 'ksdiluf';
})
ok(r, "complex validator positive")
local r,e = pcall(v, {
string = "test";
number = -1;
number2 = 1;
ull = 1ull;
nested = {
string = 'qwe';
number = 1;
};
['x-req-id'] = 'ksdiluf';
})
ok(not r, "complex validator negative")
if not r then
local status, err = e[1], e[2]
is(status, 500, "complex validator correct custom error 1")
is(err.Code, 'CustomInvalid', "complex validator correct custom error 2")
is(err.Debug, 'args.number must be positive', "complex validator correct custom error 3")
is(err.ArgumentName, 'args.number', "complex validator correct custom error 4")
is(err.ArgumentValue, -1, "complex validator correct custom error 5")
end
summary()
do return end