This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.lua
116 lines (108 loc) · 5.07 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
local _sf = string.format
local commandfmt = [[./match '%s' '%s']]
local printfmt = [[%s : "%s", "%s", %s]]
local _x = function(s)
return s:gsub('\\', '\\\\'):gsub('\n', '\\n'):gsub('\r', '\\r'):gsub('\t', '\\t')
end
local OK = "OK"
local NOK = "NOK"
local exit_status = true
local test_tre = function(expected, pattern, sample)
local result
local pipe = io.popen(string.format(commandfmt, pattern, sample))
pipe:read('*all')
local _, exit, match = pipe:close()
pattern, sample = _x(pattern), _x(sample)
if exit == "exit" then
if (match == 1 and expected == OK) or (match == 0 and expected == NOK) then
result = "PASS"
else
result = "FAIL"
exit_status = false
end
print(_sf(printfmt, result, pattern, sample, expected))
else
exit_status = false
print(_sf(printfmt, "ERROR", pattern, sample, expected))
print(_sf("\t%s code:%s", exit, match))
end
end
local test_vector =
{
{ OK, "\\d", "5" },
{ OK, "\\w+", "hej" },
{ OK, "\\s", "\t \n" },
{ NOK, "\\S", "\t \n" },
{ OK, "[\\s]", "\t \n" },
{ NOK, "[\\S]", "\t \n" },
{ NOK, "\\D", "5" },
{ NOK, "\\W+", "hej" },
{ OK, "[0-9]+", "12345" },
{ OK, "\\D", "hej" },
{ NOK, "\\d", "hej" },
{ OK, "[^\\w]", "\\" },
{ OK, "[\\W]", "\\" },
{ NOK, "[\\w]", "\\" },
{ OK, "[^\\d]", "d" },
{ NOK, "[\\d]", "d" },
{ NOK, "[^\\D]", "d" },
{ OK, "[\\D]", "d" },
{ OK, "^.*\\\\.*$", "c:\\Tools" },
{ OK, "^[\\+-]*[\\d]+$", "+27" },
{ OK, "[abc]", "1c2" },
{ NOK, "[abc]", "1C2" },
{ OK, "[1-5]+", "0123456789" },
{ OK, "[.2]", "1C2" },
{ OK, "a*$", "Xaa" },
{ OK, "a*$", "Xaa" },
{ OK, "[a-h]+", "abcdefghxxx" },
{ NOK, "[a-h]+", "ABCDEFGH" },
{ OK, "[A-H]+", "ABCDEFGH" },
{ NOK, "[A-H]+", "abcdefgh" },
{ OK, "[^\\s]+", "abc def" },
{ OK, "[^fc]+", "abc def" },
{ OK, "[^d\\sf]+", "abc def" },
{ OK, "\n", "abc\ndef" },
{ OK, "b.\\s*\n", "aa\r\nbb\r\ncc\r\n\r\n" },
{ OK, ".*c", "abcabc" },
{ OK, ".+c", "abcabc" },
{ OK, "[b-z].*", "ab" },
{ OK, "b[k-z]*", "ab" },
{ NOK, "[0-9]", " - " },
{ OK, "[^0-9]", " - " },
{ OK, "0|", "0|" },
{ NOK, "\\d\\d:\\d\\d:\\d\\d", "0s:00:00" },
{ NOK, "\\d\\d:\\d\\d:\\d\\d", "000:00" },
{ NOK, "\\d\\d:\\d\\d:\\d\\d", "00:0000" },
{ NOK, "\\d\\d:\\d\\d:\\d\\d", "100:0:00" },
{ NOK, "\\d\\d:\\d\\d:\\d\\d", "00:100:00" },
{ NOK, "\\d\\d:\\d\\d:\\d\\d", "0:00:100" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "0:0:0" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "0:00:0" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "0:0:00" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "00:0:0" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "00:00:0" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "00:0:00" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "0:00:00" },
{ OK, "\\d\\d?:\\d\\d?:\\d\\d?", "00:00:00" },
{ OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world !" },
{ OK, "[Hh]ello [Ww]orld\\s*[!]?", "hello world !" },
{ OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello World !" },
{ OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world! " },
{ OK, "[Hh]ello [Ww]orld\\s*[!]?", "Hello world !" },
{ OK, "[Hh]ello [Ww]orld\\s*[!]?", "hello World !" },
{ NOK, "\\d\\d?:\\d\\d?:\\d\\d?", "a:0" },
--[[]]
{ OK, "[^\\w][^-1-4]", ")T" },
{ OK, "[^\\w][^-1-4]", ")^" },
{ OK, "[^\\w][^-1-4]", "*)" },
{ OK, "[^\\w][^-1-4]", "!." },
{ OK, "[^\\w][^-1-4]", " x" },
{ OK, "[^\\w][^-1-4]", "$b" },
--[[]]
{ OK, ".?bar", "real_bar" },
{ NOK, ".?bar", "real_foo" },
{ NOK, "X?Y", "Z" },
}
for i, v in pairs(test_vector) do test_tre(table.unpack(v)) end
os.exit(exit_status)