// // logger_test.cxx // logger // // Created by Sam Jaffe on 3/31/19. // #include #include "logger/logger.h" #include "logger/logger_impl.h" namespace logging { void PrintTo(location_info const & info, std::ostream * os) { if (info.line) { (*os) << "{\"" << info.filename << "\", \"" << info.function << "\", " << info.line << "}"; } else { (*os) << "NULL"; } } void PrintTo(logpacket const & pkt, std::ostream * os) { (*os) << "{ " << pkt.level << ", "; PrintTo(pkt.info, os); (*os) << ", \"" << pkt.logger << "\", \"" << pkt.message << "\" }"; } } struct MockLoggerImpl : public logging::logger_impl { MockLoggerImpl() { SetLogLevel(logging::LTRACE); } void SetLogLevel(logging::log_level ll) { min_log_level = ll; } MOCK_METHOD0(flush, void()); MOCK_METHOD1(write, void(logging::logpacket const &)); MOCK_METHOD2(write, void(logging::log_level, std::string const &)); }; struct LoggerTest : public testing::Test { void SetUp() override; void TearDown() override; std::shared_ptr pimpl; }; using namespace logging; struct t_logger : public logger { t_logger(std::string const & name, std::shared_ptr impl) : logger(name, impl) {} }; void LoggerTest::SetUp() { pimpl.reset(new MockLoggerImpl); using testing::AnyNumber; EXPECT_CALL(*pimpl, flush()).Times(AnyNumber()); } void LoggerTest::TearDown() { pimpl.reset(); } TEST_F(LoggerTest, LogsWithBraceFmtCode) { using testing::Field; EXPECT_CALL(*pimpl, write(Field(&logpacket::message, "5"))).Times(1); t_logger("", pimpl).log(LERROR, "{}", 5); } TEST_F(LoggerTest, DoesNotLogAboveLevel) { using testing::_; pimpl->SetLogLevel(LFATAL); EXPECT_CALL(*pimpl, write(_)).Times(0); t_logger("", pimpl).log(LERROR, "{}", 5); } TEST_F(LoggerTest, LogCurlyBraceLiteralByDoubling) { using testing::Field; EXPECT_CALL(*pimpl, write(Field(&logpacket::message, "{}"))).Times(1); t_logger("", pimpl).log(LERROR, "{{}}", 5); }