Skip to content

Commit f80b666

Browse files
authored
Merge pull request #707 from elbeno/more-log-tests
✅ Improve log FATAL tests
2 parents 6f952f1 + 47a2712 commit f80b666

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

test/log/log.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,39 @@
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

2435
std::string buffer{};
36+
37+
auto reset_test_state() {
38+
panicked = false;
39+
expected_why = {};
40+
expected_arg.reset();
41+
buffer.clear();
42+
}
2543
} // namespace
2644

2745
template <>
@@ -31,34 +49,48 @@ inline auto logging::config<> =
3149
template <> inline auto stdx::panic_handler<> = injected_handler{};
3250

3351
TEST_CASE("CIB_FATAL logs the string", "[log]") {
52+
reset_test_state();
53+
3454
CIB_FATAL("Hello");
3555
CAPTURE(buffer);
36-
CHECK(buffer.substr(buffer.size() - std::size("Hello")) == "Hello\n");
56+
CHECK(buffer.find("Hello") != std::string::npos);
3757
}
3858

3959
TEST_CASE("CIB_FATAL respects the log module", "[log]") {
60+
reset_test_state();
61+
4062
CIB_LOG_MODULE("test");
4163
CIB_FATAL("Hello");
4264
CAPTURE(buffer);
43-
CHECK(buffer.substr(buffer.size() - std::size("FATAL [test]: Hello")) ==
44-
"FATAL [test]: Hello\n");
65+
CHECK(buffer.find("FATAL [test]: Hello") != std::string::npos);
4566
}
4667

4768
TEST_CASE("CIB_FATAL calls compile-time panic", "[log]") {
48-
panicked = false;
69+
reset_test_state();
70+
expected_why = "Hello";
71+
4972
CIB_FATAL("Hello");
5073
CHECK(panicked);
5174
}
5275

5376
TEST_CASE("CIB_FATAL pre-formats arguments passed to panic", "[log]") {
54-
panicked = false;
55-
CIB_FATAL("{}", "Hello"_sc);
77+
reset_test_state();
78+
expected_why = "Hello 42";
79+
80+
CIB_FATAL("{} {}", "Hello"_sc, sc::int_<42>);
81+
CAPTURE(buffer);
82+
CHECK(buffer.find("Hello 42") != std::string::npos);
5683
CHECK(panicked);
5784
}
5885

5986
TEST_CASE("CIB_FATAL can format stack arguments", "[log]") {
60-
panicked = false;
87+
reset_test_state();
88+
expected_why = "Hello {}";
89+
expected_arg = 42;
90+
6191
auto x = 42;
6292
CIB_FATAL("Hello {}", x);
93+
CAPTURE(buffer);
94+
CHECK(buffer.find("Hello 42") != std::string::npos);
6395
CHECK(panicked);
6496
}

0 commit comments

Comments
 (0)