-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
executable file
·134 lines (116 loc) · 2.62 KB
/
test.lua
File metadata and controls
executable file
·134 lines (116 loc) · 2.62 KB
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
#!/usr/bin/env lua
-- colors
local ERROR = "\27[38;5;196m"
local SUCCESS = "\27[38;5;46m"
local BOLD = "\27[1m"
local RESET = "\27[0m"
-- helpers
---Runs an oly command
---@param cmd string[]
---@param opts {silent: boolean?}?
---@return {success: boolean, lines: string[]?}?
local function oly(cmd, opts)
local uv = require("luv")
opts = opts or {}
local stdout = uv.new_pipe(false)
local stderr = uv.new_pipe(false)
local output = {}
local exit_code = nil
local handle
handle = uv.spawn("build/Debug/oly", {
args = cmd,
stdio = { nil, stdout, stderr },
}, function(code, signal)
exit_code = code
stdout:read_stop()
stderr:read_stop()
stdout:close()
stderr:close()
handle:close()
end)
if not handle then
io.write("uv.spawn failed !")
return nil
end
if not opts.silent then
stdout:read_start(function(err, data)
assert(not err, err)
if data then table.insert(output, data) end
end)
else
stdout:read_start(function() end)
end
stderr:read_start(function(err, data)
assert(not err, err)
end)
uv.run()
local lines = {}
if not opts.silent then
local full_output = table.concat(output)
for line in full_output:gmatch("[^\n]+") do
table.insert(lines, line)
end
end
return {
success = exit_code == 0,
lines = opts.silent and nil or lines,
}
end
---@param lines string[]
local function list_failed(lines)
local first = true
for _, pb in ipairs(lines) do
if first then
first = false
else
io.write(", ")
end
io.write(ERROR .. pb .. RESET)
end
end
-- tests
local tests = {
{
name = "problem retrieval",
test = function()
local failed = {}
for _, lang in ipairs({ "latex", "typst" }) do
local handle = oly({ "list", "--filter-lang", "--lang", lang })
if not handle then return 2 end
for _, pb in ipairs(handle.lines) do
local show = oly({ "show", pb, "--lang", lang, "--color", "never" }, { silent = true })
if not show or not show.success then
table.insert(failed, pb)
end
end
end
if #failed > 0 then
list_failed(failed)
io.write(" failed !\n")
return 1
end
end,
},
{
name = "problem naming",
test = function()
local handle = oly({ "list", "--test" })
if not handle then return 2 end
if #handle.lines > 0 then
io.write("Problem names not matching their source:\n")
list_failed(handle.lines)
return 1
end
end,
},
} --[[@as table<{name: string, test: fun():nil|integer}>]]
-- run tests
for i, test in ipairs(tests) do
print(BOLD .. "Testing " .. test.name .. "..." .. RESET)
if test.test() == nil then
print(SUCCESS .. "All good !" .. RESET)
end
if i ~= #tests then
print()
end
end