|
1 | 1 | #include "infra/util/LogAndAbort.hpp" |
2 | 2 | #include "gmock/gmock.h" |
3 | 3 | #include "gtest/gtest.h" |
| 4 | +#include <array> |
| 5 | +#include <cstdio> |
4 | 6 |
|
5 | 7 | namespace |
6 | 8 | { |
7 | | - class Listener |
| 9 | + void PrintLogAndAbortToStdout(const char* format, va_list* args) |
8 | 10 | { |
9 | | - public: |
10 | | - MOCK_METHOD(void, LogAndAbortHook, (const char* message, va_list args), ()); |
11 | | - }; |
12 | | - |
13 | | - class LogAndAbortTest |
14 | | - : public testing::Test |
15 | | - { |
16 | | - public: |
17 | | - Listener listener; |
18 | | - }; |
| 11 | + std::array<char, 256> buffer; |
| 12 | + vsnprintf(buffer.data(), buffer.size(), format, *args); |
| 13 | + std::cout << buffer.data(); |
| 14 | + } |
19 | 15 | } |
20 | 16 |
|
21 | | -TEST_F(LogAndAbortTest, log_and_abort_aborts) |
| 17 | +TEST(LogAndAbortTest, log_and_abort_aborts) |
22 | 18 | { |
23 | 19 | EXPECT_DEATH(LOG_AND_ABORT("Fly you fools!"), ""); |
24 | 20 | } |
25 | 21 |
|
26 | | -TEST_F(LogAndAbortTest, log_and_abort_no_handler_does_nothing) |
| 22 | +TEST(LogAndAbortTest, log_and_abort_no_handler_does_nothing) |
27 | 23 | { |
| 24 | + testing::internal::CaptureStdout(); |
28 | 25 | // Manually calling hook to avoid aborting the test |
29 | 26 | infra::ExecuteLogAndAbortHook("fool of a took"); |
30 | 27 | infra::ExecuteLogAndAbortHook("speak %s and enter", "friend"); |
| 28 | + std::string output = testing::internal::GetCapturedStdout(); |
| 29 | + |
| 30 | + EXPECT_THAT(output.empty(), true); |
31 | 31 | } |
32 | 32 |
|
33 | | -TEST_F(LogAndAbortTest, log_and_abort_with_handler_calls_handler) |
| 33 | +TEST(LogAndAbortTest, log_and_abort_with_handler_calls_handler) |
34 | 34 | { |
35 | 35 | infra::RegisterLogAndAbortHook([&](auto format, auto args) |
36 | 36 | { |
37 | | - listener.LogAndAbortHook(format, *args); |
| 37 | + PrintLogAndAbortToStdout(format, args); |
38 | 38 | }); |
39 | | - EXPECT_CALL(listener, LogAndAbortHook(testing::_, testing::_)).Times(1); |
40 | 39 |
|
| 40 | + testing::internal::CaptureStdout(); |
41 | 41 | // Manually calling hook to avoid aborting the test |
42 | 42 | infra::ExecuteLogAndAbortHook("speak %s and enter", "friend"); |
| 43 | + std::string output = testing::internal::GetCapturedStdout(); |
| 44 | + |
| 45 | + EXPECT_THAT(output, testing::HasSubstr("speak friend and enter")); |
43 | 46 | } |
0 commit comments