add focus to testkit

This commit is contained in:
alice 2024-07-23 07:05:58 -07:00
parent a54928ee03
commit c1c3badf5b
2 changed files with 32 additions and 3 deletions

View file

@ -10,7 +10,7 @@ local getAlive = jecs.entity_index_get_alive
local ecs_pair_first = jecs.pair_first local ecs_pair_first = jecs.pair_first
local ecs_pair_second = jecs.pair_second local ecs_pair_second = jecs.pair_second
local TEST, CASE, CHECK, FINISH, SKIP = testkit.test() local TEST, CASE, CHECK, FINISH, SKIP, FOCUS = testkit.test()
local function CHECK_NO_ERR<T...>(s: string, fn: (T...) -> (), ...: T...) local function CHECK_NO_ERR<T...>(s: string, fn: (T...) -> (), ...: T...)
local ok, err: string? = pcall(fn, ...) local ok, err: string? = pcall(fn, ...)

View file

@ -1,6 +1,8 @@
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- testkit.luau -- testkit.luau
-- v0.7.3 -- v0.7.3
-- MIT License
-- Copyright (c) 2022 centau
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local disable_ansi = false local disable_ansi = false
@ -109,21 +111,34 @@ type Test = {
message: string, message: string,
trace: string, trace: string,
}?, }?,
focus: boolean,
} }
type Case = { type Case = {
name: string, name: string,
result: number, result: number,
line: number?, line: number?,
focus: boolean
} }
local PASS, FAIL, NONE, ERROR, SKIPPED = 1, 2, 3, 4, 5 local PASS, FAIL, NONE, ERROR, SKIPPED = 1, 2, 3, 4, 5
local check_for_focused = false
local skip = false local skip = false
local test: Test? local test: Test?
local tests: { Test } = {} local tests: { Test } = {}
local function output_test_result(test: Test) local function output_test_result(test: Test)
if check_for_focused then
local any_focused = test.focus
for _, case in test.cases do
any_focused = any_focused or case.focus
end
if not any_focused then return end
end
print(color.white(test.name)) print(color.white(test.name))
for _, case in test.cases do for _, case in test.cases do
@ -136,7 +151,7 @@ local function output_test_result(test: Test)
})[case.result] })[case.result]
local line = case.result == FAIL and color.red(`{case.line}:`) or "" local line = case.result == FAIL and color.red(`{case.line}:`) or ""
if check_for_focused and case.focus == false and test.focus == false then continue end
print(`{status}{WALL} {line}{color.gray(case.name)}`) print(`{status}{WALL} {line}{color.gray(case.name)}`)
end end
@ -155,6 +170,7 @@ local function CASE(name: string)
local case = { local case = {
name = name, name = name,
result = NONE, result = NONE,
focus = false
} }
test.case = case test.case = case
@ -192,6 +208,7 @@ local function TEST(name: string, fn: () -> ())
name = name, name = name,
cases = {}, cases = {},
duration = 0, duration = 0,
focus = false
} }
assert(test) assert(test)
@ -215,6 +232,17 @@ local function TEST(name: string, fn: () -> ())
test = nil test = nil
end end
local function FOCUS()
assert(test, "no active test")
check_for_focused = true
if test.case then
test.case.focus = true
else
test.focus = true
end
end
local function FINISH(): boolean local function FINISH(): boolean
local success = true local success = true
local total_cases = 0 local total_cases = 0
@ -252,6 +280,7 @@ local function FINISH(): boolean
) )
) )
check_for_focused = false
return success, table.clear(tests) return success, table.clear(tests)
end end
@ -467,7 +496,7 @@ end
return { return {
test = function() test = function()
return TEST, CASE, CHECK, FINISH, SKIP return TEST, CASE, CHECK, FINISH, SKIP, FOCUS
end, end,
benchmark = function() benchmark = function()