7
7
#include < catch2/catch_test_macros.hpp>
8
8
9
9
#include < iterator>
10
+ #include < optional>
10
11
#include < string>
11
12
#include < string_view>
12
13
13
14
namespace {
14
15
bool panicked{};
16
+ std::string_view expected_why{};
17
+ std::optional<int > expected_arg{};
15
18
16
19
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));
20
25
panicked = true ;
26
+ if (expected_arg) {
27
+ CHECK (sizeof ...(Args) == 1 );
28
+ if constexpr (sizeof ...(Args) == 1 ) {
29
+ CHECK (*expected_arg == (args, ...));
30
+ }
31
+ }
21
32
}
22
33
};
23
34
24
35
std::string buffer{};
36
+
37
+ auto reset_test_state () {
38
+ panicked = false ;
39
+ expected_why = {};
40
+ expected_arg.reset ();
41
+ buffer.clear ();
42
+ }
25
43
} // namespace
26
44
27
45
template <>
@@ -31,34 +49,48 @@ inline auto logging::config<> =
31
49
template <> inline auto stdx::panic_handler<> = injected_handler{};
32
50
33
51
TEST_CASE (" CIB_FATAL logs the string" , " [log]" ) {
52
+ reset_test_state ();
53
+
34
54
CIB_FATAL (" Hello" );
35
55
CAPTURE (buffer);
36
- CHECK (buffer.substr (buffer. size () - std::size ( " Hello" )) == " Hello \n " );
56
+ CHECK (buffer.find ( " Hello" ) != std::string::npos );
37
57
}
38
58
39
59
TEST_CASE (" CIB_FATAL respects the log module" , " [log]" ) {
60
+ reset_test_state ();
61
+
40
62
CIB_LOG_MODULE (" test" );
41
63
CIB_FATAL (" Hello" );
42
64
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);
45
66
}
46
67
47
68
TEST_CASE (" CIB_FATAL calls compile-time panic" , " [log]" ) {
48
- panicked = false ;
69
+ reset_test_state ();
70
+ expected_why = " Hello" ;
71
+
49
72
CIB_FATAL (" Hello" );
50
73
CHECK (panicked);
51
74
}
52
75
53
76
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);
56
83
CHECK (panicked);
57
84
}
58
85
59
86
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
+
61
91
auto x = 42 ;
62
92
CIB_FATAL (" Hello {}" , x);
93
+ CAPTURE (buffer);
94
+ CHECK (buffer.find (" Hello 42" ) != std::string::npos);
63
95
CHECK (panicked);
64
96
}
0 commit comments