Skip to content

Commit aff754e

Browse files
committed
🎨 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`.
1 parent ba9dd15 commit aff754e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

include/log/log.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ template <stdx::ct_string Fmt, typename Env, typename F, typename L>
129129
logging::detail::panic<MSG, cib_log_env_t>( \
130130
__FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__)
131131

132-
#define CIB_ASSERT(expr) \
133-
((expr) ? void(0) : CIB_FATAL("Assertion failure: " #expr))
132+
#define CIB_ASSERT(expr, ...) \
133+
((expr) \
134+
? void(0) \
135+
: CIB_FATAL("Assertion failure: " #expr __VA_OPT__(, ) __VA_ARGS__))
134136

135137
namespace logging {
136138
template <typename Env, typename... Ts> static auto log_version() -> void {

test/log/log.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,24 @@ TEST_CASE("CIB_FATAL passes extra arguments to panic", "[log]") {
110110
CHECK(buffer.find("Hello 42") != std::string::npos);
111111
CHECK(panicked);
112112
}
113+
114+
TEST_CASE("CIB_ASSERT is equivalent to CIB_FATAL on failure", "[log]") {
115+
reset_test_state();
116+
expected_why = "Assertion failure: true == false";
117+
118+
CIB_ASSERT(true == false);
119+
CAPTURE(buffer);
120+
CHECK(buffer.find(expected_why) != std::string::npos);
121+
CHECK(panicked);
122+
}
123+
124+
TEST_CASE("CIB_ASSERT passes arguments to panic", "[log]") {
125+
reset_test_state();
126+
expected_why = "Assertion failure: true == false";
127+
expected_args = std::make_tuple(stdx::make_tuple(), 42);
128+
129+
CIB_ASSERT(true == false, 42);
130+
CAPTURE(buffer);
131+
CHECK(buffer.find(expected_why) != std::string::npos);
132+
CHECK(panicked);
133+
}

0 commit comments

Comments
 (0)