computable-pandoc/scripts/test.lua

108 lines
2.9 KiB
Lua

-- Makes tests
-- Adds Lua custom extensions
require "src.extensions"
require "scripts.make_dist"
-- Variables
local filter = "dist/lin.lua"
local verbose = ""
local trace = ""
local files = {}
local cmds = {
["unix"] = {
["pandoc"] = "pandoc",
["clear"] = "clear",
["ls"] = "ls",
},
["win"] = {
["pandoc"] = "pandoc.exe",
["clear"] = "cls",
["ls"] = "dir",
},
}
-- Prints help
local function help()
local cmd = " pandoc lua script/test.lua"
print(table.concat({
"Usage:",
cmd .. " [OPTIONS] [FILES]",
"Options",
" -V --verbose Prints verbose debbuging output",
" -t --trace Prints diagnostic output tracing parser progress",
" -h --help Prints this help",
"Examples:",
cmd .. " Tests for 'tests/*.*",
cmd .. " -V Verbose tests for 'tests/*.*'",
cmd .. " -t Tests and tracing for 'tests/*.*'",
cmd .. " tests/fail* Tests for 'tests/fail*'",
cmd .. " -Vt tests/fail* Verbose tests and tracing for 'tests/fail*'",
cmd .. " -h Display this help",
}, "\n"))
os.exit()
end
-- Gets command according to OS
local function getcmd(str)
local system = (os.isunix() and "unix" or "win")
return cmds[system][str]
end
-- Obtains result as "pass" | "fail" | "diff" (AST doesn't match)
local function getresult(file, f, v, t)
local tmp, name = "tmp.json", pandoc.path.filename(file)
local json = pandoc.path.join({"tests", "asts", name .. ".json"})
local ok, out = io.try(getcmd("pandoc"), f, v, t, "-t json", "-o", tmp, file)
local json1, json2 = tmp:read_text(), json:read_text()
os.remove("tmp.json")
if (ok and json2 == nil) or (ok and json1 == json2) then
return "pass", out:strip()
elseif ok and json1 ~= json2 then
return "diff", out:strip()
else
return "fail", out:strip()
end
end
-- Parses args
for _, a in ipairs(arg) do
if a:match("^-.*h.*") then
help()
elseif a == "-V" or a == "--verbose" then
verbose = "--verbose"
elseif a == "-t" or a == "--trace" then
trace = "--trace"
elseif a == "-Vt" or a == "-tV" then
verbose = "--verbose"
trace = "--trace"
elseif a:match("^-") == nil then
table.insert(files, pandoc.path.normalize(a))
end
end
-- Default files
if #files == 0 then
for file in io.popen(getcmd("ls") .. " tests"):lines() do
if file ~= "asts" then
table.insert(files, pandoc.path.join({"tests", file}))
end
end
end
-- Clears terminal
os.execute(getcmd("clear"))
print "🐾 Starting tests"
-- Does tests
for _, file in ipairs(files) do
local expectation = pandoc.path.filename(file):gsub("%W.+$", "")
local result, output = getresult(file, "-L", filter, verbose, trace)
print("⚗️ " .. file .. ":")
print(" Expect: " .. expectation)
print(" Result: " .. result)
if #verbose > 0 or #trace > 0 then
print(output)
end
end