chore(lune): add time consume per test statistic

This commit is contained in:
Erica Marigold 2024-12-03 15:35:24 +00:00
parent add460c3cc
commit 392a8f2c9a
Signed by: DevComp
GPG key ID: 429EF1C337871656
2 changed files with 94 additions and 29 deletions

48
.lune/tests/channel.luau Normal file
View file

@ -0,0 +1,48 @@
--- An MPSC synchronization primitive powered by Lua upvalues which retains only
--- one value at a time.
--- ## Usage
--- ```luau
--- local send, recv = watch((nil :: any) :: string)
--- task.delay(5, send, "hello, world!")
--- task.spawn(function()
--- local value = recv()
--- print("recevied value:", value)
--- end)
--- ```
type Watch<T> = {
value: T?,
receivers: { thread },
}
--- Crates a new `Watch` channel, returning its send and receive handles.
local function chan<T>(_phantom: T): ((T) -> (), () -> T?)
local watch: Watch<T> = {
value = nil,
receivers = {},
}
local function send(value: T)
watch.value = value
for _, receiver in watch.receivers do
coroutine.resume(receiver, value)
end
end
local function recv(): T
local value = watch.value
watch.value = nil
if value == nil then
table.insert(watch.receivers, coroutine.running())
return coroutine.yield()
end
return value :: T
end
return send, recv
end
return chan

View file

@ -3,43 +3,60 @@ local stdio = require("@lune/stdio")
local frktest = require("../../lune_packages/frktest") local frktest = require("../../lune_packages/frktest")
local Reporter = frktest._reporters.lune_console_reporter local Reporter = frktest._reporters.lune_console_reporter
local STYLE = table.freeze({ local watch = require("./channel")
suite = function(name: string)
return `{stdio.style("bold")}{stdio.color("purple")}SUITE{stdio.style("reset")} {name}`
end,
report = function(name: string, state: "run" | "success" | "error" | "skip") local STYLE = table.freeze({
local state_color: stdio.Color = if state == "run" suite = function(name: string)
then "white" return `{stdio.style("bold")}{stdio.color("purple")}SUITE{stdio.style("reset")} {name}`
elseif state == "success" then "green" end,
elseif state == "error" then "red"
elseif state == "skip" then "yellow" report = function(name: string, state: "run" | "success" | "error" | "skip", elapsed: number)
else error("Invalid test state") local state_color: stdio.Color = if state == "run"
return ` {stdio.style("bold")}{stdio.color(state_color)}{if state == "skip" then "SKIP" else "TEST"}{stdio.style( then "white"
"reset" elseif state == "success" then "green"
)} {name}` elseif state == "error" then "red"
end, 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,
}) })
local ReporterExt = {} local ReporterExt = {}
function ReporterExt.init() function ReporterExt.init()
frktest.test.on_suite_enter(function(suite) frktest.test.on_suite_enter(function(suite)
print(STYLE.suite(suite.name)) print(STYLE.suite(suite.name))
end) end)
frktest.test.on_suite_leave(function() frktest.test.on_suite_leave(function()
stdio.write("\n") stdio.write("\n")
end) end)
frktest.test.on_test_leave(function(test) local send_ts, recv_ts = watch((nil :: any) :: number)
print(STYLE.report(test.name, if test.failed then "error" else "success"))
end) frktest.test.on_test_enter(function()
-- Send over some high precision timestamp when the test starts
return send_ts(os.clock())
end)
frktest.test.on_test_skipped(function(test) frktest.test.on_test_leave(function(test)
print(STYLE.report(test.name, "skip")) print(
end) STYLE.report(
test.name,
if test.failed then "error" else "success",
Reporter.init() -- Await receival of the timestamp and convert the difference to ms
(os.clock() - recv_ts()) * 1000
)
)
end)
frktest.test.on_test_skipped(function(test)
print(STYLE.report(test.name, "skip"))
end)
Reporter.init()
end end
return setmetatable(ReporterExt, { __index = Reporter }) return setmetatable(ReporterExt, { __index = Reporter })