Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit 33402bf8f218ac0cf874aff36de66e7683fefbf2 parent 32f2b0cde0b748c9802ee6e236993be2f19146f5 Author: sentomk <sentomk@proton.me> Date: Tue, 13 Oct 2026 20:25:28 +0800 test: print only failures by default and report the golden hash in the message The failed checks of a test case are kept and printed under its name, so a failure reads as a block: the case, then its messages. Passing cases are listed only with --verbose, which the builder passes for `kumoko -v test`; --color colors the words ok and FAIL. Output is flushed after every case, so that nothing is lost when the next case crashes. The golden test used to print the actual hash on its own line, which ended up above the name of the case. The hash and the expected one are now in the message of the failure. Diffstat:
| M | tests/check.h | | | 89 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- |
| M | tests/e2e/golden_test.cpp | | | 9 | ++++++--- |
2 files changed, 69 insertions(+), 29 deletions(-)
diff --git a/tests/check.h b/tests/check.h @@ -11,8 +11,12 @@ /// CHECK_EQ(1 + 1, 2); /// } /// @endcode -/// A failed check is reported and the test case goes on, so one run shows every +/// 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. #pragma once #include <cstddef> @@ -41,6 +45,20 @@ inline int& failed_checks() { return count; } +/// @brief The messages of the failed checks of the test case that is running, +/// kept to be printed under its name. +inline std::string& failure_report() { + static std::string text; + return text; +} + +/// @brief Records a failed check. +inline void fail(const char* file, int line, const std::string& message) { + failure_report() += " " + std::string(file) + ":" + std::to_string(line) + + ": " + message + "\n"; + ++failed_checks(); +} + /// @brief Adds a test case to cases() when a global of this type is created. struct Registrar { Registrar(const char* name, void (*run)()) { cases().push_back({name, run}); } @@ -66,45 +84,64 @@ std::string show(const T& value) { static ::kumo_test::Registrar kumo_test_registrar_##name(#name, name); \ static void name() -/// @brief Reports a failure, with the line, if @p condition is false. -#define CHECK(condition) \ - do { \ - if (!(condition)) { \ - std::printf(" %s:%d: CHECK(%s) failed\n", __FILE__, __LINE__, \ - #condition); \ - ++::kumo_test::failed_checks(); \ - } \ +/// @brief Records a failure, with the line, if @p condition is false. +#define CHECK(condition) \ + do { \ + if (!(condition)) { \ + ::kumo_test::fail(__FILE__, __LINE__, "CHECK(" #condition ") failed"); \ + } \ } while (0) -/// @brief Reports a failure, with both values, if @p actual != @p expected. +/// @brief Records a failure, with both values, if @p actual != @p expected. /// /// Both arguments are evaluated once. -#define CHECK_EQ(actual, expected) \ - do { \ - const auto& kumo_test_actual = (actual); \ - const auto& kumo_test_expected = (expected); \ - if (!(kumo_test_actual == kumo_test_expected)) { \ - std::printf(" %s:%d: CHECK_EQ(%s, %s) failed: %s != %s\n", __FILE__, \ - __LINE__, #actual, #expected, \ - ::kumo_test::show(kumo_test_actual).c_str(), \ - ::kumo_test::show(kumo_test_expected).c_str()); \ - ++::kumo_test::failed_checks(); \ - } \ +#define CHECK_EQ(actual, expected) \ + do { \ + const auto& kumo_test_actual = (actual); \ + const auto& kumo_test_expected = (expected); \ + if (!(kumo_test_actual == kumo_test_expected)) { \ + ::kumo_test::fail( \ + __FILE__, __LINE__, \ + std::string("CHECK_EQ(") + #actual + ", " + #expected + \ + ") failed: " + ::kumo_test::show(kumo_test_actual) + \ + " != " + ::kumo_test::show(kumo_test_expected)); \ + } \ } while (0) -/// @brief Runs every registered test case and reports the result. -int main() { +/// @brief Runs every registered test case and reports the failures. +/// +/// @param argc Argument count. +/// @param argv `--verbose` (or `-v`) lists the passing cases too; `--color` +/// colors the words `ok` and `FAIL`. +int main(int argc, char** argv) { + bool verbose = false; + bool color = 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; + } + const auto paint = [color](const char* code, const char* text) { + return color ? std::string("\033[") + code + "m" + text + "\033[0m" + : std::string(text); + }; + int failed_cases = 0; for (const kumo_test::Case& test : kumo_test::cases()) { kumo_test::failed_checks() = 0; + kumo_test::failure_report().clear(); test.run(); if (kumo_test::failed_checks() == 0) { - std::printf(" ok %s\n", test.name); + if (verbose) + std::printf(" %s %s\n", paint("32", "ok ").c_str(), test.name); } else { - std::printf(" FAIL %s (%d checks failed)\n", test.name, - kumo_test::failed_checks()); ++failed_cases; + std::printf(" %s %s\n%s", paint("31", "FAIL").c_str(), test.name, + kumo_test::failure_report().c_str()); } + // Flushed after every case, so that nothing is lost if the next one + // crashes. + std::fflush(stdout); } return failed_cases == 0 ? 0 : 1; } diff --git a/tests/e2e/golden_test.cpp b/tests/e2e/golden_test.cpp @@ -13,7 +13,7 @@ using kumo_test::kDemoWidth; // The hash of the demo picture. It must be the same on every platform, so a // change here is only right if the picture was changed on purpose. Then run // `kumoko test golden`, look at the picture in build/golden_demo.tga, and take -// the new value from the "actual hash" line it prints. +// the new value from the failure message. constexpr uint64_t kExpectedHash = 0x635d63193c43fe8aull; TEST(demo_scene_has_the_expected_hash) { @@ -25,9 +25,12 @@ TEST(demo_scene_has_the_expected_hash) { const uint64_t hash = kumo::hash_pixels(frame); if (hash != kExpectedHash) { - std::printf(" actual hash: %016" PRIx64 "\n", hash); + char message[96]; + std::snprintf(message, sizeof(message), + "hash is %016" PRIx64 ", expected %016" PRIx64, hash, + kExpectedHash); + kumo_test::fail(__FILE__, __LINE__, message); } - CHECK(hash == kExpectedHash); } TEST(drawing_the_scene_twice_gives_the_same_picture) {
