1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include "testutils/LoggingTest.h"
#include <regex>
/*
* Contains test cases for the following logging interface:
* LOG(INFO, "My log message)"
*/
using namespace cpputils::logging;
using std::string;
// Disable the next tests for MSVC debug builds since writing to stderr doesn't seem to work well there
#if !defined(_MSC_VER) || NDEBUG
TEST_F(LoggingTest, DefaultLoggerIsStderr) {
const string output = captureStderr([]{
LOG(INFO, "My log message");
cpputils::logging::flush();
});
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(output, MatchesRegex(".*\\[Log\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(output, std::regex(".*\\[Log\\].*\\[info\\].*My log message.*")));
}
TEST_F(LoggingTest, SetLogger_NewLoggerIsUsed) {
setLogger(spdlog::stderr_logger_mt("MyTestLog2"));
const string output = captureStderr([]{
LOG(INFO, "My log message");
cpputils::logging::flush();
});
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(output, MatchesRegex(".*\\[MyTestLog2\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(output, std::regex(".*\\[MyTestLog2\\].*\\[info\\].*My log message.*")));
}
#endif
TEST_F(LoggingTest, SetNonStderrLogger_LogsToNewLogger) {
setLogger(mockLogger.get());
logger()->info("My log message");
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(output, MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")));
}
TEST_F(LoggingTest, SetNonStderrLogger_DoesNotLogToStderr) {
setLogger(mockLogger.get());
const string output = captureStderr([] {
logger()->info("My log message");
cpputils::logging::flush();
});
EXPECT_EQ("", output);
}
TEST_F(LoggingTest, InfoLog) {
setLogger(mockLogger.get());
LOG(INFO, "My log message");
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")));
}
TEST_F(LoggingTest, WarningLog) {
setLogger(mockLogger.get());
LOG(WARN, "My log message");
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[warning\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[warning\\].*My log message.*")));
}
TEST_F(LoggingTest, DebugLog) {
setLevel(DEBUG);
setLogger(mockLogger.get());
LOG(DEBUG, "My log message");
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[debug\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[debug\\].*My log message.*")));
}
TEST_F(LoggingTest, ErrorLog) {
setLogger(mockLogger.get());
LOG(ERR, "My log message");
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[error\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[error\\].*My log message.*")));
}
void logAndExit(const string &message) {
LOG(INFO, message);
cpputils::logging::flush();
exit(1);
}
// fork() only forks the main thread. This test ensures that logging doesn't depend on threads that suddenly aren't
// there anymore after a fork().
TEST_F(LoggingTest, LoggingAlsoWorksAfterFork) {
testing::FLAGS_gtest_death_test_style = "threadsafe";
setLogger(spdlog::stderr_logger_mt("StderrLogger"));
EXPECT_EXIT(
logAndExit("My log message"),
::testing::ExitedWithCode(1),
"My log message"
);
}
TEST_F(LoggingTest, MessageIsConstChar) {
setLogger(mockLogger.get());
LOG(INFO, "My log message");
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")));
}
TEST_F(LoggingTest, MessageIsString) {
setLogger(mockLogger.get());
const string msg = "My log message";
LOG(INFO, msg);
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")));
}
TEST_F(LoggingTest, FormatWithStringPlaceholder) {
setLogger(mockLogger.get());
const string str = "placeholder";
LOG(INFO, "My log message: {}", str);
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*")));
}
TEST_F(LoggingTest, FormatWithConstCharPlaceholder) {
setLogger(mockLogger.get());
LOG(INFO, "My log message: {}", "placeholder");
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*")));
}
TEST_F(LoggingTest, FormatWithIntPlaceholder) {
setLogger(mockLogger.get());
LOG(INFO, "My log message: {}", 4);
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4.*")));
}
TEST_F(LoggingTest, FormatWithMultiplePlaceholders) {
setLogger(mockLogger.get());
LOG(INFO, "My log message: {}, {}, {}", 4, "then", true);
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
//EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4, then, true.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4, then, true.*")));
}
|