Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 30af7b196ad6db646af367e39859050386f58fcf
parent 4232b4e96fecd20e965fe6f3f78f5358e4b3d47b
Author: sentomk <sentomk@proton.me>
Date:   Wed, 14 Oct 2026 16:25:09 +0800

feat: list every test case as it runs and rewrite the running one in place

A test program now lists each of its cases with the result and the time as it
finishes, and the builder prefixes the list with the name of the program. On
a terminal the case that is running is shown on the last line as "running",
which is replaced by its result when it ends; in a pipe or a file the line is
printed once, when the case is done. A crash leaves the line of the case that
was running, so it says which one it was.

The messages of the failed checks are kept and printed after the list, under
the name of each failed case, so that they do not break it up.

The builder tells the program whether it may color and rewrite its line
(--color, --live) only when its own output is a terminal, which is also what
decides its colors. -q or --quiet restores the compact output: one line per
test program and the failures only. -v is now just about commands.

Diffstat:
Mkumo_builder.cpp | 33++++++++++++++++++++++++---------
Mtests/check.h | 107++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
2 files changed, 108 insertions(+), 32 deletions(-)

diff --git a/kumo_builder.cpp b/kumo_builder.cpp @@ -24,7 +24,8 @@ /// # contains `name` if one is given /// ./kumoko clean # delete all build output /// ./kumoko help # show the usage; also -h and --help -/// ./kumoko -v ... # print full commands, list every test case +/// ./kumoko -v ... # print full commands +/// ./kumoko -q test # one line per test program, not per test case /// @endcode /// /// Output is colored when it goes to a terminal; set `NO_COLOR` to turn that @@ -143,10 +144,13 @@ static const fs::path CONFIG_FILE = "kumo.toml"; /// @brief Whether messages are colored. Set once by init_terminal(). static bool color_enabled = false; -/// @brief Whether commands are printed in full and every test case is listed. -/// Set by `-v` or `--verbose`. +/// @brief Whether commands are printed in full. Set by `-v` or `--verbose`. static bool verbose = false; +/// @brief Whether `test` shows one line per test program instead of one per +/// test case. Set by `-q` or `--quiet`. +static bool quiet = false; + /// @brief The colors a message can have. enum class Style { Plain, Dim, Red, Green, Cyan }; @@ -1294,7 +1298,8 @@ Commands: help Show this text Options: - -v, --verbose Print full commands, and list every test case + -v, --verbose Print full commands + -q, --quiet With test: one line per test program, not one per test case -h, --help Show this text Run it from the project root; its settings are in kumo.toml. Output is colored @@ -1329,6 +1334,8 @@ static Args parse_args(int argc, char** argv) { std::exit(0); } else if (arg == "-v" || arg == "--verbose") { verbose = true; + } else if (arg == "-q" || arg == "--quiet") { + quiet = true; } else if (arg.size() > 1 && arg[0] == '-') { mistake("unknown option '" + arg + "'"); } else { @@ -1554,15 +1561,21 @@ int main(int argc, char** argv) { const fs::path exe = test_exe(cfg, test->src); if (!link_if_needed(cfg, test_objects, exe)) return 1; - // The test program is told whether to color and whether to list its cases. + // The test program lists its cases as it runs them. It is told whether it + // may color its output and rewrite its current line, which a terminal + // allows and a pipe or a file does not, and whether to list only failures. const std::string name = test_name(cfg, test->src); fs::path program = exe; program.make_preferred(); Cmd command = {program.string()}; - if (color_enabled) command.push_back("--color"); - if (verbose) { - command.push_back("--verbose"); - std::printf("%s\n", paint(Style::Dim, "running " + name).c_str()); + if (color_enabled) { + command.push_back("--color"); + command.push_back("--live"); + } + if (quiet) { + command.push_back("--quiet"); + } else { + log_step(Style::Cyan, "test", name); } const Clock::time_point test_began = Clock::now(); @@ -1573,6 +1586,8 @@ int main(int argc, char** argv) { // that is not success (a signal, another status) is a crash. const bool crashed = !status.ok() && !(status.signal == 0 && status.code == 1); + // A crash leaves the line of the test case that was running unfinished. + if (crashed && !quiet) std::printf("\n"); if (status.ok()) { std::printf("%s %s %s\n", paint(Style::Green, "PASS ").c_str(), pad(name, name_width).c_str(), diff --git a/tests/check.h b/tests/check.h @@ -14,11 +14,16 @@ /// A failed check is recorded and the test case goes on, so one run shows every /// problem. The program exits with status 1 if any check failed. /// -/// By default only the failures are printed, each under the name of its test -/// case. The builder passes `--verbose` to list every test case, and `--color` -/// when the output goes to a terminal. +/// Every test case is listed with its result and its time as it finishes. The +/// builder passes `--color` and `--live` when the output goes to a terminal; +/// then the case that is running is shown on the last line, as `running`, and +/// that line is rewritten when the case ends. A failed case is listed again at +/// the end with the messages of its failed checks. `--quiet` lists nothing but +/// the failures. #pragma once +#include <algorithm> +#include <chrono> #include <cstddef> #include <cstdio> #include <string> @@ -76,6 +81,30 @@ std::string show(const T& value) { } } +/// @brief Pads @p text with spaces on the right up to @p width characters. +inline std::string pad(const std::string& text, size_t width) { + return text.size() >= width ? text + : text + std::string(width - text.size(), ' '); +} + +/// @brief Pads @p text with spaces on the left up to @p width characters. +inline std::string pad_left(const std::string& text, size_t width) { + return text.size() >= width ? text + : std::string(width - text.size(), ' ') + text; +} + +/// @brief Formats the time a test case took: `<1 ms`, `12 ms`, `1.50 s`. +inline std::string format_time(std::chrono::steady_clock::duration time) { + const auto us = + std::chrono::duration_cast<std::chrono::microseconds>(time).count(); + if (us < 1000) return "<1 ms"; + if (us < 1000000) return std::to_string(us / 1000) + " ms"; + char buffer[32]; + std::snprintf(buffer, sizeof(buffer), "%.2f s", + static_cast<double>(us) / 1e6); + return buffer; +} + } // namespace kumo_test /// @brief Defines a test case and registers it; the body follows the macro. @@ -108,40 +137,72 @@ std::string show(const T& value) { } \ } while (0) -/// @brief Runs every registered test case and reports the failures. +/// @brief Runs every registered test case, listing each one as it goes. /// /// @param argc Argument count. -/// @param argv `--verbose` (or `-v`) lists the passing cases too; `--color` -/// colors the words `ok` and `FAIL`. +/// @param argv `--color` colors the words `ok` and `FAIL`; `--live` shows the +/// running case on the last line and rewrites it when it ends; +/// `--quiet` lists only the failures. int main(int argc, char** argv) { - bool verbose = false; bool color = false; + bool live = false; + bool quiet = false; for (int i = 1; i < argc; ++i) { const std::string arg = argv[i]; - if (arg == "-v" || arg == "--verbose") verbose = true; if (arg == "--color") color = true; + if (arg == "--live") live = true; + if (arg == "-q" || arg == "--quiet") quiet = true; } - const auto paint = [color](const char* code, const char* text) { - return color ? std::string("\033[") + code + "m" + text + "\033[0m" - : std::string(text); + const auto paint = [color](const char* code, const std::string& text) { + return color ? std::string("\033[") + code + "m" + text + "\033[0m" : text; }; - int failed_cases = 0; + size_t width = 0; + for (const kumo_test::Case& test : kumo_test::cases()) { + width = std::max(width, std::string(test.name).size()); + } + + struct Failure { + std::string name; + std::string report; + }; + std::vector<Failure> failures; for (const kumo_test::Case& test : kumo_test::cases()) { kumo_test::failed_checks() = 0; kumo_test::failure_report().clear(); + + // The name goes out before the case runs, and is flushed, so that if the + // case crashes the last line says which one it was. + const std::string label = " " + kumo_test::pad(test.name, width) + " "; + if (!quiet) { + std::printf("%s%s", label.c_str(), + live ? paint("2", "running").c_str() : ""); + std::fflush(stdout); + } + + const auto began = std::chrono::steady_clock::now(); test.run(); - if (kumo_test::failed_checks() == 0) { - if (verbose) - std::printf(" %s %s\n", paint("32", "ok ").c_str(), test.name); - } else { - ++failed_cases; - std::printf(" %s %s\n%s", paint("31", "FAIL").c_str(), test.name, - kumo_test::failure_report().c_str()); + const std::string took = + kumo_test::format_time(std::chrono::steady_clock::now() - began); + const bool failed = kumo_test::failed_checks() != 0; + + if (!quiet) { + // On a terminal, erase "running" and write the result in its place. + if (live) std::printf("\r\033[2K%s", label.c_str()); + std::printf( + "%s %s\n", + failed ? paint("31", "FAIL").c_str() : paint("32", "ok ").c_str(), + paint("2", kumo_test::pad_left(took, 6)).c_str()); + std::fflush(stdout); } - // Flushed after every case, so that nothing is lost if the next one - // crashes. - std::fflush(stdout); + if (failed) failures.push_back({test.name, kumo_test::failure_report()}); + } + + // The messages come after the list, so that they do not break it up. + if (!failures.empty() && !quiet) std::printf("\n"); + for (const Failure& failure : failures) { + std::printf(" %s %s\n%s", paint("31", "FAIL").c_str(), + failure.name.c_str(), failure.report.c_str()); } - return failed_cases == 0 ? 0 : 1; + return failures.empty() ? 0 : 1; }