2015-10-17 15:49:54 +02:00
|
|
|
#include "testutils/LoggingTest.h"
|
|
|
|
|
|
|
|
using namespace cpputils::logging;
|
|
|
|
using std::string;
|
|
|
|
using testing::MatchesRegex;
|
|
|
|
|
|
|
|
class LoggingLevelTest: public LoggingTest {
|
|
|
|
public:
|
|
|
|
void EXPECT_DEBUG_LOG_ENABLED() {
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(DEBUG, "My log message");
|
2015-10-17 15:49:54 +02:00
|
|
|
EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[debug\\].*My log message.*"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2015-10-17 15:49:54 +02:00
|
|
|
EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2015-10-17 15:49:54 +02:00
|
|
|
EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[warning\\].*My log message.*"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(ERROR, "My log message");
|
2015-10-17 15:49:54 +02:00
|
|
|
EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[error\\].*My log message.*"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EXPECT_ERROR_LOG_DISABLED() {
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(ERROR, "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(ERROR);
|
|
|
|
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(ERROR);
|
|
|
|
EXPECT_DEBUG_LOG_DISABLED();
|
|
|
|
EXPECT_INFO_LOG_DISABLED();
|
|
|
|
EXPECT_WARNING_LOG_DISABLED();
|
|
|
|
EXPECT_ERROR_LOG_ENABLED();
|
|
|
|
}
|