Skip to content

Commit 2ad0e8c

Browse files
Make LogAndAbort tests a little more robust
1 parent c7b1123 commit 2ad0e8c

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
#include "infra/util/LogAndAbort.hpp"
22
#include "gmock/gmock.h"
33
#include "gtest/gtest.h"
4+
#include <array>
5+
#include <cstdio>
46

57
namespace
68
{
7-
class Listener
9+
void PrintLogAndAbortToStdout(const char* format, va_list* args)
810
{
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+
}
1915
}
2016

21-
TEST_F(LogAndAbortTest, log_and_abort_aborts)
17+
TEST(LogAndAbortTest, log_and_abort_aborts)
2218
{
2319
EXPECT_DEATH(LOG_AND_ABORT("Fly you fools!"), "");
2420
}
2521

26-
TEST_F(LogAndAbortTest, log_and_abort_no_handler_does_nothing)
22+
TEST(LogAndAbortTest, log_and_abort_no_handler_does_nothing)
2723
{
24+
testing::internal::CaptureStdout();
2825
// Manually calling hook to avoid aborting the test
2926
infra::ExecuteLogAndAbortHook("fool of a took");
3027
infra::ExecuteLogAndAbortHook("speak %s and enter", "friend");
28+
std::string output = testing::internal::GetCapturedStdout();
29+
30+
EXPECT_THAT(output.empty(), true);
3131
}
3232

33-
TEST_F(LogAndAbortTest, log_and_abort_with_handler_calls_handler)
33+
TEST(LogAndAbortTest, log_and_abort_with_handler_calls_handler)
3434
{
3535
infra::RegisterLogAndAbortHook([&](auto format, auto args)
3636
{
37-
listener.LogAndAbortHook(format, *args);
37+
PrintLogAndAbortToStdout(format, args);
3838
});
39-
EXPECT_CALL(listener, LogAndAbortHook(testing::_, testing::_)).Times(1);
4039

40+
testing::internal::CaptureStdout();
4141
// Manually calling hook to avoid aborting the test
4242
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"));
4346
}

services/tracer/test/TestLogAndAbortTracer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ TEST_F(LogAndAbortTracerTest, log_and_abort_without_registered_tracer_doesnt_cal
2929
infra::ExecuteLogAndAbortHook("speak %s and enter", "elf");
3030
std::string output = testing::internal::GetCapturedStdout();
3131

32-
EXPECT_THAT(output, testing::HasSubstr(""));
32+
EXPECT_THAT(output.empty(), true);
3333
}
3434

3535
TEST_F(LogAndAbortTracerTest, log_and_abort_with_registered_tracer_calls_tracer)
@@ -41,8 +41,8 @@ TEST_F(LogAndAbortTracerTest, log_and_abort_with_registered_tracer_calls_tracer)
4141

4242
testing::internal::CaptureStdout();
4343
// Calling hook directly to avoid aborting the test process.
44-
infra::ExecuteLogAndAbortHook("speak %s and enter", "mellon");
44+
infra::ExecuteLogAndAbortHook("speak %s and enter", "friend");
4545
std::string output = testing::internal::GetCapturedStdout();
4646

47-
EXPECT_THAT(output, testing::HasSubstr("speak mellon and enter"));
47+
EXPECT_THAT(output, testing::HasSubstr("speak friend and enter"));
4848
}

0 commit comments

Comments
 (0)