From c1c3badf5bbc70d0f433c714b58cc186053863ec Mon Sep 17 00:00:00 2001 From: alice <166900055+alicesaidhi@users.noreply.github.com> Date: Tue, 23 Jul 2024 07:05:58 -0700 Subject: [PATCH] add focus to testkit --- test/tests.luau | 2 +- testkit.luau | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/test/tests.luau b/test/tests.luau index 31f2614..d1ff63a 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -10,7 +10,7 @@ local getAlive = jecs.entity_index_get_alive local ecs_pair_first = jecs.pair_first 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(s: string, fn: (T...) -> (), ...: T...) local ok, err: string? = pcall(fn, ...) diff --git a/testkit.luau b/testkit.luau index f54b1e8..3c7d29a 100644 --- a/testkit.luau +++ b/testkit.luau @@ -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,6 +232,17 @@ 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 @@ -252,6 +280,7 @@ local function FINISH(): boolean ) ) + check_for_focused = false return success, table.clear(tests) end @@ -467,7 +496,7 @@ end return { test = function() - return TEST, CASE, CHECK, FINISH, SKIP + return TEST, CASE, CHECK, FINISH, SKIP, FOCUS end, benchmark = function()