-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
88 lines (73 loc) · 1.86 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
local Luck = require"luck"
local template = [[
return {
enum = OR(0, 1, 127, 128),
region = LIST(NUM, OR(STRING, BOOL)), -- the first item must be NUM, rest must be STRING or BOOL
kk = OR(
{ bar = TABLE(OR("momo", "ejoy"), RANGE(1,200)) },
"hello",
123,
{foo1 = {foo2 = ANY}}
),
start_time = OR(PATTERN("([%d]+)-([%d]+)-([%d]+)"), PATTERN("([%d]+)-([%d]+)-([%d]+) ([%d]+):([%d]+):([%d]+)")),
mok = OR(4, NIL),
title = STRING,
count = RANGE(0),
flag = BOOL,
num = NUM,
}
]]
local cfgs = {
{
enum = 127,
region = {0,"foo", true},
kk = {bar = {momo = 199}},
start_time = "1988-06-07",
mok = 4,
title = "correct one",
count = 10000,
flag = false,
num = -1,
},
{
enum = 300, -- wrong enum
region = {0,0,127, true, "foo"},
start_time = "1988-06-07",
kk = {bar = {momo = 199}},
mok = 4,
title = "correct one",
count = 10000,
flag = false,
num = -1,
},
{
enum = 0,
region = {"foo", 127},
start_time = "1988-06-07",
kk = {bar = {tencent = 199}}, -- wrong key
-- no mok
title = "correct one",
count = 0,
flag = false,
num = -1,
},
{
enum = 128,
region = {true,true},
start_time = "1988-06-07",
kk = {foo1 = {foo2 = "this can be anything"}},
-- no mok
title = "correct one",
count = -2, -- wrong range
flag = false,
num = -1,
},
}
function main()
local checker = Luck.new(Luck.load_chunk(template))
for i,cfg in ipairs(cfgs) do
local ok, err = checker:check(cfg)
print(i, ok, err)
end
end
main()