Skip to content

Commit e5111d6

Browse files
Tests for LogAndAbortTracer
1 parent ecb51d7 commit e5111d6

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

services/tracer/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ target_link_libraries(services.tracer PUBLIC
1717
target_sources(services.tracer PRIVATE
1818
GlobalTracer.cpp
1919
GlobalTracer.hpp
20+
LogAndAbortTracer.cpp
21+
LogAndAbortTracer.hpp
2022
StreamWriterOnSerialCommunication.cpp
2123
StreamWriterOnSerialCommunication.hpp
2224
StreamWriterOnSynchronousSerialCommunication.cpp
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "services/util/LogAndAbortTracer.hpp"
1+
#include "services/tracer/LogAndAbortTracer.hpp"
22
#include "services/tracer/TracerAdapterPrintf.hpp"
33

44
namespace services

services/tracer/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ target_link_libraries(services.tracer_test PUBLIC
1111
)
1212

1313
target_sources(services.tracer_test PRIVATE
14+
TestLogAndAbortTracer.cpp
1415
TestStreamWriterOnSerialCommunication.cpp
1516
TestStreamWriterOnSynchronousSerialCommunication.cpp
1617
TestTracer.cpp
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "services/tracer/LogAndAbortTracer.hpp"
2+
#include "services/tracer/TracerOnIoOutputInfrastructure.hpp"
3+
#include "gmock/gmock.h"
4+
#include "gtest/gtest.h"
5+
6+
namespace
7+
{
8+
struct StdOutTracer
9+
{
10+
// Basically TracerOnIoOutputInfrastructure, but does not set global tracer
11+
12+
infra::IoOutputStreamWriter writer;
13+
infra::TextOutputStream::WithErrorPolicy stream{ writer };
14+
services::TracerWithDateTime tracer{ stream };
15+
};
16+
17+
class LogAndAbortTracerTest
18+
: public testing::Test
19+
{
20+
public:
21+
StdOutTracer tracer;
22+
};
23+
}
24+
25+
TEST_F(LogAndAbortTracerTest, log_and_abort_without_registered_tracer_doesnt_call_tracer)
26+
{
27+
testing::internal::CaptureStdout();
28+
// Calling hook directly to avoid aborting the test process.
29+
infra::ExecuteLogAndAbortHook("speak %s and enter", "elf");
30+
std::string output = testing::internal::GetCapturedStdout();
31+
32+
EXPECT_THAT(output, testing::HasSubstr(""));
33+
}
34+
35+
TEST_F(LogAndAbortTracerTest, log_and_abort_with_registered_tracer_calls_tracer)
36+
{
37+
services::RegisterLogAndAbortTracerProvider([this]() -> services::Tracer&
38+
{
39+
return tracer.tracer;
40+
});
41+
42+
testing::internal::CaptureStdout();
43+
// Calling hook directly to avoid aborting the test process.
44+
infra::ExecuteLogAndAbortHook("speak %s and enter", "mellon");
45+
std::string output = testing::internal::GetCapturedStdout();
46+
47+
EXPECT_THAT(output, testing::HasSubstr("speak mellon and enter"));
48+
}

services/util/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ target_sources(services.util PRIVATE
5151
GpioPinInverted.hpp
5252
I2cMultipleAccess.cpp
5353
I2cMultipleAccess.hpp
54-
LogAndAbortTracer.cpp
55-
LogAndAbortTracer.hpp
5654
LowPowerSerialCommunication.cpp
5755
LowPowerSerialCommunication.hpp
5856
LowPowerSpiMaster.cpp

0 commit comments

Comments
 (0)