29
29
class LoggerTest : public ::testing::Test
32
std::streambuf *old_stdout_buf;
33
std::stringstream stdout_buffer;
34
std::streambuf *old_stderr_buf;
35
std::stringstream stderr_buffer;
37
virtual void SetUp() override
39
// Trap stdout to verify tests.
40
old_stdout_buf = std::cout.rdbuf();
41
std::cout.rdbuf(stdout_buffer.rdbuf());
43
// Trap stderr to verify tests.
44
old_stderr_buf = std::cerr.rdbuf();
45
std::cerr.rdbuf(stderr_buffer.rdbuf());
48
virtual void TearDown() override
50
std::cout.rdbuf(old_stdout_buf);
51
std::cerr.rdbuf(old_stderr_buf);
34
_handler_id = g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
36
std::cerr << "Set up handler: " << _handler_id << std::endl;
41
if (_handler_id != 0) {
42
g_log_remove_handler(G_LOG_DOMAIN, _handler_id);
52
static void _log_handler(const gchar*, GLogLevelFlags,
53
const gchar* message, gpointer gthis)
55
static_cast<LoggerTest*>(gthis)->log_message(message);
58
void log_message(const char* message)
60
std::cerr << "Message: " << message << std::endl;
65
std::ostringstream _stream;
55
TEST_F(LoggerTest, testEndingSpace)
57
warn() << "This ends with a space ";
60
69
TEST_F(LoggerTest, testDebug)
62
debug() << "Some data about things happening.";
71
auto expected = "Some data about things happening.";
73
ASSERT_STREQ(expected, str().c_str());
76
TEST_F(LoggerTest, testInfo)
78
auto expected = "Some info.";
80
ASSERT_STREQ(expected, str().c_str());
65
83
TEST_F(LoggerTest, testWarning)
67
warn() << "This shouldn't normally happen.";
85
auto expected = "This shouldn't normally happen.";
87
ASSERT_STREQ(expected, str().c_str());
70
90
TEST_F(LoggerTest, testCritical)
72
critical() << "Critical fail.";
92
auto expected = "Critical fail.";
93
critical() << expected;
94
ASSERT_STREQ(expected, str().c_str());
75
97
TEST_F(LoggerTest, testError)
77
ASSERT_EXIT({error() << "Error crash.";},
78
::testing::KilledBySignal(SIGTRAP), "Error crash.");
81
TEST_F(LoggerTest, testStreamLevelDebug)
83
debug() << "Level:" << Logger::Level::DEBUG;
86
TEST_F(LoggerTest, testStreamLevelWarning)
88
debug() << "Level:" << Logger::Level::WARNING;
91
TEST_F(LoggerTest, testStreamLevelCritical)
93
debug() << "Level:" << Logger::Level::CRITICAL;
96
TEST_F(LoggerTest, testStreamLevelERROR)
98
debug() << "Level:" << Logger::Level::ERROR;
101
TEST_F(LoggerTest, testStreamLevelUnknown)
103
debug() << "Level:" << Logger::Level::UNKNOWN;
106
TEST_F(LoggerTest, testStreamBool)
108
debug() << "Bool:" << true;
111
TEST_F(LoggerTest, testStreamPointer)
113
debug() << "Pointer:" << this;
116
TEST_F(LoggerTest, testStreamNullPointer)
118
debug() << "Pointer:" << nullptr;
121
TEST_F(LoggerTest, testStreamInt)
123
debug() << "Count:" << 42;
126
TEST_F(LoggerTest, testStreamShort)
128
debug() << "Count:" << (short)42;
131
TEST_F(LoggerTest, testStreamLong)
133
debug() << "Count:" << (long)42;
136
TEST_F(LoggerTest, testStreamLongLong)
138
debug() << "Count" << (long long)42;
141
TEST_F(LoggerTest, testStreamUnsignedInt)
143
debug() << "Unsigned:" << (unsigned int)42;
146
TEST_F(LoggerTest, testStreamUnsignedShort)
148
debug() << "Unsigned:" << (unsigned short)42;
151
TEST_F(LoggerTest, testStreamUnsignedLong)
153
debug() << "Unsigned Long:" << (unsigned long)42;
156
TEST_F(LoggerTest, testStreamUnsignedLongLong)
158
debug() << "Unsigned long long:" << (unsigned long long)42;
161
TEST_F(LoggerTest, testStreamFloat)
163
debug() << "Float:" << 2.2f;
166
TEST_F(LoggerTest, testStreamDouble)
168
debug() << "Double:" << (double)2.2f;
99
auto expected = "Error crash.";
100
ASSERT_EXIT({error() << expected;},
101
::testing::KilledBySignal(SIGTRAP), expected);
102
ASSERT_STREQ(expected, str().c_str());