logger_test.cxx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // logger_test.cxx
  3. // logger
  4. //
  5. // Created by Sam Jaffe on 3/31/19.
  6. //
  7. #include "logger_test_obj.h"
  8. #include "logger/logger.h"
  9. using namespace logging;
  10. namespace {
  11. struct t_logger : public logger {
  12. t_logger(std::string const & name, std::shared_ptr<logger_impl> impl)
  13. : logger(name, impl) {}
  14. };
  15. }
  16. TEST_F(LoggerTest, FlushesOnClose) {
  17. EXPECT_CALL(*appender, flush()).Times(1);
  18. t_logger("", pimpl);
  19. }
  20. TEST_F(LoggerTest, FlushesOnFlushCall) {
  21. EXPECT_CALL(*appender, flush()).Times(2);
  22. t_logger("", pimpl).flush();
  23. }
  24. TEST_F(LoggerTest, LogsWithBraceFmtCode) {
  25. using testing::_;
  26. EXPECT_CALL(*appender, write(MessageEq("5"), _)).Times(1);
  27. t_logger("", pimpl).log(level::error, "{}", 5);
  28. }
  29. TEST_F(LoggerTest, DoesNotLogAboveLevel) {
  30. using testing::_;
  31. pimpl->min_log_level = level::fatal;
  32. EXPECT_CALL(*appender, write(_, _)).Times(0);
  33. t_logger("", pimpl).log(level::error, "{}", 5);
  34. }
  35. TEST_F(LoggerTest, LogCurlyBraceLiteralByDoubling) {
  36. using testing::_;
  37. EXPECT_CALL(*appender, write(MessageEq("{}"), _)).Times(1);
  38. t_logger("", pimpl).log(level::error, "{{}}", 5);
  39. }
  40. MATCHER_P3(LocationInfoNear, file, line, function, "") {
  41. return !strcmp(arg.info.filename, file) &&
  42. !strcmp(arg.info.function, function) &&
  43. std::abs(arg.info.line - line) < 5;
  44. }
  45. TEST_F(LoggerTest, LogMacroInjectsLocationInfo) {
  46. using testing::_;
  47. auto near_this_line = LocationInfoNear(__FILE__, __LINE__, __FUNCTION__);
  48. EXPECT_CALL(*appender, write(near_this_line, _)).Times(1);
  49. t_logger log("", pimpl);
  50. log_message(log, error, "{}", 5);
  51. }