From aff754e6657f4ad7c5b44fd035f37ffdf9ed1da6 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Mon, 31 Mar 2025 17:12:11 -0600 Subject: [PATCH] :art: Pass extra args through `CIB_ASSERT` Problem: - `CIB_FATAL` can pass extra args through to `panic` but `CIB_ASSERT` cannot. Solution: - Allow extra args passed through `CIB_ASSERT`. --- include/log/log.hpp | 6 ++++-- test/log/log.cpp | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/log/log.hpp b/include/log/log.hpp index 77678290..fc2959f0 100644 --- a/include/log/log.hpp +++ b/include/log/log.hpp @@ -129,8 +129,10 @@ template logging::detail::panic( \ __FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__) -#define CIB_ASSERT(expr) \ - ((expr) ? void(0) : CIB_FATAL("Assertion failure: " #expr)) +#define CIB_ASSERT(expr, ...) \ + ((expr) \ + ? void(0) \ + : CIB_FATAL("Assertion failure: " #expr __VA_OPT__(, ) __VA_ARGS__)) namespace logging { template static auto log_version() -> void { diff --git a/test/log/log.cpp b/test/log/log.cpp index c28a2459..f451c73f 100644 --- a/test/log/log.cpp +++ b/test/log/log.cpp @@ -110,3 +110,24 @@ TEST_CASE("CIB_FATAL passes extra arguments to panic", "[log]") { CHECK(buffer.find("Hello 42") != std::string::npos); CHECK(panicked); } + +TEST_CASE("CIB_ASSERT is equivalent to CIB_FATAL on failure", "[log]") { + reset_test_state(); + expected_why = "Assertion failure: true == false"; + + CIB_ASSERT(true == false); + CAPTURE(buffer); + CHECK(buffer.find(expected_why) != std::string::npos); + CHECK(panicked); +} + +TEST_CASE("CIB_ASSERT passes arguments to panic", "[log]") { + reset_test_state(); + expected_why = "Assertion failure: true == false"; + expected_args = std::make_tuple(stdx::make_tuple(), 42); + + CIB_ASSERT(true == false, 42); + CAPTURE(buffer); + CHECK(buffer.find(expected_why) != std::string::npos); + CHECK(panicked); +}