Implement testkit FOCUS() (#87)

* add focus to testkit

* show amount of focus cases passed
This commit is contained in:
alicesaidhi 2024-09-17 03:18:52 +02:00 committed by GitHub
parent 3276566ea5
commit 2b1122e073
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 3 deletions

View file

@ -12,7 +12,7 @@ local ecs_pair_first = jecs.pair_first
local ecs_pair_second = jecs.pair_second
local world_new = jecs.World.new
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 ok, err: string? = pcall(fn, ...)

View file

@ -1,6 +1,8 @@
--------------------------------------------------------------------------------
-- testkit.luau
-- v0.7.3
-- MIT License
-- Copyright (c) 2022 centau
--------------------------------------------------------------------------------
local disable_ansi = false
@ -109,21 +111,34 @@ type Test = {
message: string,
trace: string,
}?,
focus: boolean,
}
type Case = {
name: string,
result: number,
line: number?,
focus: boolean
}
local PASS, FAIL, NONE, ERROR, SKIPPED = 1, 2, 3, 4, 5
local check_for_focused = false
local skip = false
local test: Test?
local tests: { 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))
for _, case in test.cases do
@ -136,7 +151,7 @@ local function output_test_result(test: Test)
})[case.result]
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)}`)
end
@ -155,6 +170,7 @@ local function CASE(name: string)
local case = {
name = name,
result = NONE,
focus = false
}
test.case = case
@ -192,6 +208,7 @@ local function TEST(name: string, fn: () -> ())
name = name,
cases = {},
duration = 0,
focus = false
}
assert(test)
@ -215,17 +232,36 @@ local function TEST(name: string, fn: () -> ())
test = nil
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 success = true
local total_cases = 0
local passed_cases = 0
local passed_focus_cases = 0
local total_focus_cases = 0
local duration = 0
for _, test in tests do
duration += test.duration
for _, case in test.cases do
total_cases += 1
if case.focus or test.focus then
total_focus_cases += 1
end
if case.result == PASS or case.result == NONE or case.result == SKIPPED then
if case.focus or test.focus then
passed_focus_cases += 1
end
passed_cases += 1
else
success = false
@ -243,6 +279,11 @@ local function FINISH(): boolean
)
)
)
if check_for_focused then
print(
color.gray(`{passed_focus_cases}/{total_focus_cases} focused test cases passed`)
)
end
local fails = total_cases - passed_cases
@ -252,6 +293,7 @@ local function FINISH(): boolean
)
)
check_for_focused = false
return success, table.clear(tests)
end
@ -467,7 +509,7 @@ end
return {
test = function()
return TEST, CASE, CHECK, FINISH, SKIP
return TEST, CASE, CHECK, FINISH, SKIP, FOCUS
end,
benchmark = function()