scripts/.lune/tests/reporter.luau

70 lines
1.7 KiB
Text
Raw Normal View History

2024-12-08 17:42:05 +00:00
local stdio = require("@lune/stdio")
local frktest = require("../../lune_packages/frktest")
local Reporter = frktest._reporters.lune_console_reporter
local watch = require("../lib/channel")
local STYLE = table.freeze({
2024-12-08 18:30:38 +00:00
suite = function(name: string)
return `{stdio.style("bold")}{stdio.color("purple")}SUITE{stdio.style(
"reset"
)} {name}`
end,
report = function(
name: string,
state: "success" | "error" | "skip",
elapsed: number
)
local state_color: stdio.Color = if state == "success"
then "green"
elseif state == "error" then "red"
elseif state == "skip" then "yellow"
else error("Invalid test state")
return ` {stdio.style("bold")}{stdio.color(state_color)}{if state
== "skip"
then "SKIP"
else "TEST"}{stdio.style("reset")} {name} [{stdio.style("dim")}{string.format(
"%.2fms",
elapsed
)}{stdio.style("reset")}]`
end,
2024-12-08 17:42:05 +00:00
})
local ReporterExt = {}
function ReporterExt.init()
2024-12-08 18:30:38 +00:00
frktest.test.on_suite_enter(function(suite)
print(STYLE.suite(suite.name))
end)
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
frktest.test.on_suite_leave(function()
stdio.write("\n")
end)
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
local send_ts, recv_ts = watch((nil :: any) :: number)
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
frktest.test.on_test_enter(function()
-- Send over some high precision timestamp when the test starts
return send_ts(os.clock())
end)
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
frktest.test.on_test_leave(function(test)
print(STYLE.report(
test.name,
if test.failed then "error" else "success",
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
-- Await receival of the timestamp and convert the difference to ms
(os.clock() - recv_ts()) * 1000
))
end)
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
frktest.test.on_test_skipped(function(test)
print(STYLE.report(test.name, "skip", 0))
end)
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
Reporter.init()
2024-12-08 17:42:05 +00:00
end
return setmetatable(ReporterExt, { __index = Reporter })