示例工程 26-testing-and-lint
26-testing-and-lint/01-harness-basics/main.lua
-- @runtimes lua54 luajit
--- 演示:表驱动测试的核心结构(不依赖第三方框架)。
local M = {}
local function add(a, b)
return a + b
end
local CASES = {
{ 1, 2, 3 },
{ -1, 1, 0 },
{ 0, 0, 0 },
}
--- @return table { passed = integer, total = integer }
function M.run(log)
log = log or print
local passed = 0
for _, case in ipairs(CASES) do
local actual = add(case[1], case[2])
if actual == case[3] then
passed = passed + 1
else
log(("FAIL %d+%d expected %d got %d"):format(case[1], case[2], case[3], actual))
end
end
log(("passed=%d/%d"):format(passed, #CASES))
return { passed = passed, total = #CASES }
end
if not ... then
M.run()
end
return M