From 47a271228f5d0cd4b13c4f2e214119f32006cf48 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Thu, 13 Mar 2025 16:26:39 -0600 Subject: [PATCH] :white_check_mark: Improve log FATAL tests --- test/log/log.cpp | 52 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/test/log/log.cpp b/test/log/log.cpp index b388b463..082f31c3 100644 --- a/test/log/log.cpp +++ b/test/log/log.cpp @@ -7,21 +7,39 @@ #include #include +#include #include #include namespace { bool panicked{}; +std::string_view expected_why{}; +std::optional expected_arg{}; struct injected_handler { - template - static auto panic(Ts &&...) noexcept -> void { - static_assert(std::string_view{Why}.starts_with("Hello")); + template + static auto panic(Args &&...args) noexcept -> void { + constexpr auto s = std::string_view{Why}; + CAPTURE(s); + CHECK(s.ends_with(expected_why)); panicked = true; + if (expected_arg) { + CHECK(sizeof...(Args) == 1); + if constexpr (sizeof...(Args) == 1) { + CHECK(*expected_arg == (args, ...)); + } + } } }; std::string buffer{}; + +auto reset_test_state() { + panicked = false; + expected_why = {}; + expected_arg.reset(); + buffer.clear(); +} } // namespace template <> @@ -31,34 +49,48 @@ inline auto logging::config<> = template <> inline auto stdx::panic_handler<> = injected_handler{}; TEST_CASE("CIB_FATAL logs the string", "[log]") { + reset_test_state(); + CIB_FATAL("Hello"); CAPTURE(buffer); - CHECK(buffer.substr(buffer.size() - std::size("Hello")) == "Hello\n"); + CHECK(buffer.find("Hello") != std::string::npos); } TEST_CASE("CIB_FATAL respects the log module", "[log]") { + reset_test_state(); + CIB_LOG_MODULE("test"); CIB_FATAL("Hello"); CAPTURE(buffer); - CHECK(buffer.substr(buffer.size() - std::size("FATAL [test]: Hello")) == - "FATAL [test]: Hello\n"); + CHECK(buffer.find("FATAL [test]: Hello") != std::string::npos); } TEST_CASE("CIB_FATAL calls compile-time panic", "[log]") { - panicked = false; + reset_test_state(); + expected_why = "Hello"; + CIB_FATAL("Hello"); CHECK(panicked); } TEST_CASE("CIB_FATAL pre-formats arguments passed to panic", "[log]") { - panicked = false; - CIB_FATAL("{}", "Hello"_sc); + reset_test_state(); + expected_why = "Hello 42"; + + CIB_FATAL("{} {}", "Hello"_sc, sc::int_<42>); + CAPTURE(buffer); + CHECK(buffer.find("Hello 42") != std::string::npos); CHECK(panicked); } TEST_CASE("CIB_FATAL can format stack arguments", "[log]") { - panicked = false; + reset_test_state(); + expected_why = "Hello {}"; + expected_arg = 42; + auto x = 42; CIB_FATAL("Hello {}", x); + CAPTURE(buffer); + CHECK(buffer.find("Hello 42") != std::string::npos); CHECK(panicked); }