Skip to content

Commit c0d2a5d

Browse files
committed
✅ Improve log FATAL tests
1 parent d40c4de commit c0d2a5d

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

test/log/log.cpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@
77
#include <catch2/catch_test_macros.hpp>
88

99
#include <iterator>
10+
#include <optional>
1011
#include <string>
1112
#include <string_view>
1213

1314
namespace {
1415
bool panicked{};
16+
std::string_view expected_why{};
17+
std::optional<int> expected_arg{};
1518

1619
struct injected_handler {
17-
template <stdx::ct_string Why, typename... Ts>
18-
static auto panic(Ts &&...) noexcept -> void {
19-
static_assert(std::string_view{Why}.starts_with("Hello"));
20+
template <stdx::ct_string Why, typename... Args>
21+
static auto panic(Args &&...args) noexcept -> void {
22+
constexpr auto s = std::string_view{Why};
23+
CAPTURE(s);
24+
CHECK(s.ends_with(expected_why));
2025
panicked = true;
26+
if (expected_arg) {
27+
CHECK(sizeof...(Args) == 1);
28+
if constexpr (sizeof...(Args) == 1) {
29+
CHECK(*expected_arg == (args, ...));
30+
}
31+
}
2132
}
2233
};
2334

@@ -31,34 +42,57 @@ inline auto logging::config<> =
3142
template <> inline auto stdx::panic_handler<> = injected_handler{};
3243

3344
TEST_CASE("CIB_FATAL logs the string", "[log]") {
45+
expected_why = {};
46+
expected_arg.reset();
47+
buffer.clear();
48+
3449
CIB_FATAL("Hello");
3550
CAPTURE(buffer);
36-
CHECK(buffer.substr(buffer.size() - std::size("Hello")) == "Hello\n");
51+
CHECK(buffer.find("Hello") != std::string::npos);
3752
}
3853

3954
TEST_CASE("CIB_FATAL respects the log module", "[log]") {
55+
expected_why = {};
56+
expected_arg.reset();
57+
buffer.clear();
58+
4059
CIB_LOG_MODULE("test");
4160
CIB_FATAL("Hello");
4261
CAPTURE(buffer);
43-
CHECK(buffer.substr(buffer.size() - std::size("FATAL [test]: Hello")) ==
44-
"FATAL [test]: Hello\n");
62+
CHECK(buffer.find("FATAL [test]: Hello") != std::string::npos);
4563
}
4664

4765
TEST_CASE("CIB_FATAL calls compile-time panic", "[log]") {
66+
expected_why = "Hello";
67+
expected_arg.reset();
68+
buffer.clear();
4869
panicked = false;
70+
4971
CIB_FATAL("Hello");
5072
CHECK(panicked);
5173
}
5274

5375
TEST_CASE("CIB_FATAL pre-formats arguments passed to panic", "[log]") {
76+
expected_why = "Hello 42";
77+
expected_arg.reset();
78+
buffer.clear();
5479
panicked = false;
55-
CIB_FATAL("{}", "Hello"_sc);
80+
81+
CIB_FATAL("{} {}", "Hello"_sc, sc::int_<42>);
82+
CAPTURE(buffer);
83+
CHECK(buffer.find("Hello 42") != std::string::npos);
5684
CHECK(panicked);
5785
}
5886

5987
TEST_CASE("CIB_FATAL can format stack arguments", "[log]") {
88+
expected_why = "Hello {}";
89+
expected_arg = 42;
90+
buffer.clear();
6091
panicked = false;
92+
6193
auto x = 42;
6294
CIB_FATAL("Hello {}", x);
95+
CAPTURE(buffer);
96+
CHECK(buffer.find("Hello 42") != std::string::npos);
6397
CHECK(panicked);
6498
}

0 commit comments

Comments
 (0)