libcryfs/test/cpp-utils/logging/LoggingLevelTest.cpp

137 lines
4.5 KiB
C++
Raw Normal View History

2015-10-17 15:49:54 +02:00
#include "testutils/LoggingTest.h"
2018-07-09 04:34:08 +02:00
#include <regex>
2015-10-17 15:49:54 +02:00
using namespace cpputils::logging;
using std::string;
class LoggingLevelTest: public LoggingTest {
public:
void EXPECT_DEBUG_LOG_ENABLED() {
2017-02-04 20:03:20 +01:00
LOG(DEBUG, "My log message");
2018-07-09 04:34:08 +02:00
// 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.*")));
2015-10-17 15:49:54 +02:00
}
void EXPECT_DEBUG_LOG_DISABLED() {
2017-02-04 20:03:20 +01:00
LOG(DEBUG, "My log message");
2015-10-17 15:49:54 +02:00
EXPECT_EQ("", mockLogger.capturedLog());
}
void EXPECT_INFO_LOG_ENABLED() {
2017-02-04 20:03:20 +01:00
LOG(INFO, "My log message");
2018-07-09 04:34:08 +02:00
// 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.*")));
2015-10-17 15:49:54 +02:00
}
void EXPECT_INFO_LOG_DISABLED() {
2017-02-04 20:03:20 +01:00
LOG(INFO, "My log message");
2015-10-17 15:49:54 +02:00
EXPECT_EQ("", mockLogger.capturedLog());
}
void EXPECT_WARNING_LOG_ENABLED() {
2017-02-04 20:03:20 +01:00
LOG(WARN, "My log message");
2018-07-09 04:34:08 +02:00
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[warning\\].*My log message.*")));
// 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.*"));
2015-10-17 15:49:54 +02:00
}
void EXPECT_WARNING_LOG_DISABLED() {
2017-02-04 20:03:20 +01:00
LOG(WARN, "My log message");
2015-10-17 15:49:54 +02:00
EXPECT_EQ("", mockLogger.capturedLog());
}
void EXPECT_ERROR_LOG_ENABLED() {
LOG(ERR, "My log message");
2018-07-09 04:34:08 +02:00
// 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.*")));
2015-10-17 15:49:54 +02:00
}
void EXPECT_ERROR_LOG_DISABLED() {
LOG(ERR, "My log message");
2015-10-17 15:49:54 +02:00
EXPECT_EQ("", mockLogger.capturedLog());
}
};
TEST_F(LoggingLevelTest, DefaultLevelIsInfo) {
setLogger(mockLogger.get());
EXPECT_DEBUG_LOG_DISABLED();
EXPECT_INFO_LOG_ENABLED();
EXPECT_WARNING_LOG_ENABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, DEBUG_SetBeforeSettingLogger) {
setLevel(DEBUG);
setLogger(mockLogger.get());
EXPECT_DEBUG_LOG_ENABLED();
EXPECT_INFO_LOG_ENABLED();
EXPECT_WARNING_LOG_ENABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, DEBUG_SetAfterSettingLogger) {
setLogger(mockLogger.get());
setLevel(DEBUG);
EXPECT_DEBUG_LOG_ENABLED();
EXPECT_INFO_LOG_ENABLED();
EXPECT_WARNING_LOG_ENABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, INFO_SetBeforeSettingLogger) {
setLevel(INFO);
setLogger(mockLogger.get());
EXPECT_DEBUG_LOG_DISABLED();
EXPECT_INFO_LOG_ENABLED();
EXPECT_WARNING_LOG_ENABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, INFO_SetAfterSettingLogger) {
setLogger(mockLogger.get());
setLevel(INFO);
EXPECT_DEBUG_LOG_DISABLED();
EXPECT_INFO_LOG_ENABLED();
EXPECT_WARNING_LOG_ENABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, WARNING_SetBeforeSettingLogger) {
setLevel(WARN);
setLogger(mockLogger.get());
EXPECT_DEBUG_LOG_DISABLED();
EXPECT_INFO_LOG_DISABLED();
EXPECT_WARNING_LOG_ENABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, WARNING_SetAfterSettingLogger) {
setLogger(mockLogger.get());
setLevel(WARN);
EXPECT_DEBUG_LOG_DISABLED();
EXPECT_INFO_LOG_DISABLED();
EXPECT_WARNING_LOG_ENABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, ERROR_SetBeforeSettingLogger) {
setLevel(ERR);
2015-10-17 15:49:54 +02:00
setLogger(mockLogger.get());
EXPECT_DEBUG_LOG_DISABLED();
EXPECT_INFO_LOG_DISABLED();
EXPECT_WARNING_LOG_DISABLED();
EXPECT_ERROR_LOG_ENABLED();
}
TEST_F(LoggingLevelTest, ERROR_SetAfterSettingLogger) {
setLogger(mockLogger.get());
setLevel(ERR);
2015-10-17 15:49:54 +02:00
EXPECT_DEBUG_LOG_DISABLED();
EXPECT_INFO_LOG_DISABLED();
EXPECT_WARNING_LOG_DISABLED();
EXPECT_ERROR_LOG_ENABLED();
}