From 61640f143ddc0cf67f782a125eefe68804a2cedf Mon Sep 17 00:00:00 2001 From: FourierTransformer Date: Mon, 11 May 2026 19:48:00 -0500 Subject: [PATCH] filtering! --- build/tested.lua | 6 ++++++ build/tested/main.lua | 20 ++++++++++++++++++-- build/tested/results/tap.lua | 2 +- build/tested/results/terminal.lua | 5 ++++- build/tested/types.lua | 1 + docs/cli.md | 6 +++++- docs/index.md | 9 ++++----- src/tested.tl | 6 ++++++ src/tested/main.tl | 22 +++++++++++++++++++--- src/tested/results/tap.tl | 2 +- src/tested/results/terminal.tl | 5 ++++- src/tested/types.tl | 3 ++- 12 files changed, 71 insertions(+), 16 deletions(-) diff --git a/build/tested.lua b/build/tested.lua index f0ce143..169c994 100644 --- a/build/tested.lua +++ b/build/tested.lua @@ -166,6 +166,12 @@ function tested:run(filename, options) test_results.tests[i].time = 0 test_results.counts.skipped = test_results.counts.skipped + 1 + elseif options and options.filter ~= nil and not string.find(test.name, options.filter) then + test_results.tests[i].result = "CONDITIONAL_SKIP" + test_results.tests[i].message = "Test name does not match filter pattern '" .. options.filter .. "'" + test_results.tests[i].time = 0 + test_results.counts.skipped = test_results.counts.skipped + 1 + elseif test.options.run_when ~= nil and test.options.run_when == false then test_results.tests[i].result = "CONDITIONAL_SKIP" test_results.tests[i].message = "Condition in `tested.conditional_skip` returned false. Skipping test." diff --git a/build/tested/main.lua b/build/tested/main.lua index b8f1f31..fdf17d0 100644 --- a/build/tested/main.lua +++ b/build/tested/main.lua @@ -8,7 +8,7 @@ local TestRunner, run_parallel_tests = test_runner[1], test_runner[2] local logging = require("tested.libs.logging") local logger = logging.get_logger("tested.main") -local TESTED_VERSION = "tested v0.1.0" +local TESTED_VERSION = "tested v0.2.0" @@ -55,6 +55,7 @@ local cli_to_display = { + local function parse_args() @@ -65,6 +66,8 @@ local function parse_args() parser:flag("-r --random"): description("Randomize the order of the tests (default: not-set)"): default(false) + parser:option("-F --filter"): + description("Only run tests whose name matches this Lua pattern (default: not-set)") parser:option("-s --show"): description("What test results to display (default: '-s fail -s exception -s unknown')"): choices({ "all", "valid", "invalid", "skip", "pass", "fail", "exception", "unknown", "expected", "unexpected" }): @@ -126,6 +129,12 @@ local function validate_args(args) if info.mode == "directory" then table.insert(args.test_directories, path) end if info.mode == "file" then table.insert(args.test_files, path) end end + if args.filter then + local ok, err = pcall(string.find, "", args.filter) + if not ok then + error("Invalid --filter pattern '" .. args.filter .. "': " .. tostring(err), 0) + end + end end local function load_result_formatter(args) @@ -255,6 +264,7 @@ local function run_tests(formatter, args, test_files) local options = { random = args.random, coverage = args.coverage, + filter = args.filter, } local display_results = function(test_output) @@ -295,8 +305,14 @@ local function main() local test_files = get_all_test_files(args) if #test_files == 0 then error("Unable to find any tests to run in: " .. table.concat(args.paths, ", "), 0) end + local header_comments = {} + if args.filter ~= nil then + table.insert(header_comments, "Filtering tests with pattern: '" .. args.filter .. "'") + end + + + formatter.header(TESTED_VERSION, args.paths, header_comments) - formatter.header(TESTED_VERSION, args.paths) local runner_output = run_tests(formatter, args, test_files) formatter.summary(runner_output) diff --git a/build/tested/results/tap.lua b/build/tested/results/tap.lua index bc69818..6cf03da 100644 --- a/build/tested/results/tap.lua +++ b/build/tested/results/tap.lua @@ -6,7 +6,7 @@ local tap = {} tap.allow_filtering = false tap.format = "tap" -function tap.header(_version_info, _filepaths) +function tap.header(_version_info, _filepaths, _comments) print("TAP version 14") end diff --git a/build/tested/results/terminal.lua b/build/tested/results/terminal.lua index 9041e6a..ced29fc 100644 --- a/build/tested/results/terminal.lua +++ b/build/tested/results/terminal.lua @@ -41,8 +41,11 @@ terminal.allow_filtering = true terminal.colors = colors -function terminal.header(version_info, filepaths) +function terminal.header(version_info, filepaths, comments) print(colors("%{bright}" .. version_info .. " " .. table.concat(filepaths, " "))) + for _, comment in ipairs(comments) do + print(comment) + end print() end diff --git a/build/tested/types.lua b/build/tested/types.lua index b4fecf9..0dac245 100644 --- a/build/tested/types.lua +++ b/build/tested/types.lua @@ -145,6 +145,7 @@ local types = {} + return types diff --git a/docs/cli.md b/docs/cli.md index 7d06539..8cd20c6 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -16,6 +16,8 @@ The `-c` flag will enable [luacov](https://github.com/lunarmodules/luacov), and ## `tested -r/--random` The `-r` will randomize the order of tests _within_ a test file. +## `tested -F/--filter` +Will only run tests that match the Lua pattern that applies to the **test name** using `string.find`. Useful for debugging specific tests. ## `tested -s/--show` By default the `tested` output shows the problematic test results, that likely need to be addressed: `fail`, `exception`, `unknown`, and `unexpected` but allows filtering to display other results. @@ -53,7 +55,7 @@ Specify the number of threads `tested` should use. If set to `0`, will not use a ``` Usage: tested ([-f {terminal,plain,tap}] | [-z ]) - [-h] [-c] [-r] + [-h] [-c] [-r] [-F ] [-s {all,valid,invalid,skip,pass,fail,exception,unknown,expected,unexpected}] [-n ] [-x ] [-d {DEBUG,INFO,WARNING}] [--version] [] ... @@ -67,6 +69,8 @@ Options: -h, --help Show this help message and exit. -c, --coverage Enable code coverage - will generate luacov.stats.out (default: not-set) -r, --random Randomize the order of the tests (default: not-set) + -F , Only run tests whose name matches this Lua pattern (default: not-set) + --filter -s {all,valid,invalid,skip,pass,fail,exception,unknown,expected,unexpected}, --show {all,valid,invalid,skip,pass,fail,exception,unknown,expected,unexpected} What test results to display (default: '-s fail -s exception -s unknown') diff --git a/docs/index.md b/docs/index.md index 02c779d..92365ed 100644 --- a/docs/index.md +++ b/docs/index.md @@ -68,8 +68,6 @@ Test files should be placed in a folder called `tests`, and the `tested` command Fully Tested! ``` -Check out the [Unit Testing](./unit-testing.md) page for more information on how `tested` works! and after, if you're using Teal, be sure to check out the [Teal Support](./teal-support.md) page! - ## Quickstart ### Folder setup @@ -113,7 +111,7 @@ return tested After the `tested` command loads up a test file, it goes through and finds all the various tests defined in the file (in this case there are two) and adds them to a list to be executed. In the example above, that `print` statement will execute before either tests. -It does this so tests can be [shuffled](#cli-quick-reference), [skipped](#skipping-tests), or to [only](#only-tests) run a specific test within a test file! +It does this so tests can be [shuffled](#cli-quick-reference), skipped, or to only run a specific test within a test file! ### Asserts The basic assert is composed of four parts: @@ -128,7 +126,7 @@ The basic assert is composed of four parts: The `given` and `should` are optional strings that get displayed in the output to help you identify which specific assert has failed. The idea behind them is to be able to look at the testing output and know _exactly_ what and how something has failed. If your test references multiple files, placing a filename in given can be incredibly useful. Since some tests are more obvious than others (based on a test name), they are not required and can be omitted. -The `expected` and `actual` take in the expected and actual values. There are a couple of other asserts builtin to `tested`, including one for [exceptions](#testing-exceptions), [truthy, and falsy](#truthyfalsy-tests)! +The `expected` and `actual` take in the expected and actual values. There are a couple of other asserts builtin to `tested`, including one for [exceptions](./unit-testing.md#testing-exceptions), [truthy, and falsy](./unit-testing.md#truthyfalsy-tests)! ### CLI Quick Reference @@ -136,7 +134,8 @@ There are a couple CLI commands that are good to know when you get started: - `tested -c` or `--coverage` will enable luacov code coverage and generate a `luacov.stats.out` file - `tested -r` or `--random` will randomize the order of tests _within a test file_. -- `tested -s` or `--show` supports displaying different status of tests. By default `tested` shows tests which require followup (so `fail`, `exception`, and `invalid`) +- `tested -F ` or `--filter ` will filter tests based on a `string.find` pattern. It can just be the test name, a couple words from the test name (in order), or a full on Lua pattern! +- `tested -s