mock_logger.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // mock_logger.h
  3. // logger_test
  4. //
  5. // Created by Sam Jaffe on 4/2/19.
  6. //
  7. #ifndef mock_logger_h
  8. #define mock_logger_h
  9. #include <gmock/gmock.h>
  10. #include <sstream>
  11. #include "logger/detail/appender.h"
  12. #include "logger/detail/layout.h"
  13. #include "logger_impl.h"
  14. #include "logger/logpacket.h"
  15. namespace logging {
  16. inline void PrintTo(location_info const & info, std::ostream * os) {
  17. if (info.line) {
  18. (*os) << "{\"" << info.filename << "\", \"" <<
  19. info.function << "\", " << info.line << "}";
  20. } else {
  21. (*os) << "NULL";
  22. }
  23. }
  24. inline void PrintTo(logpacket const & pkt, std::ostream * os) {
  25. (*os) << "{ " << pkt.level << ", ";
  26. PrintTo(pkt.info, os);
  27. (*os) << ", \"" << pkt.logger << "\", \"" << pkt.message.str() << "\" }";
  28. }
  29. }
  30. struct StubAppender : public logging::appender {
  31. StubAppender() : threshold(logging::level::trace) { }
  32. std::ostream & stream() override { return sstream; }
  33. void flush() override {}
  34. void write(logging::logpacket const & pkt, logging::layout & lay) override {
  35. lay.format(sstream, pkt);
  36. }
  37. bool should_log(logging::level ll) const override {
  38. return ll >= threshold;
  39. }
  40. logging::level threshold;
  41. std::stringstream sstream;
  42. };
  43. struct MockAppender : public StubAppender {
  44. MockAppender() { }
  45. MOCK_METHOD0(flush, void());
  46. MOCK_METHOD2(write, void(logging::logpacket const &, logging::layout &));
  47. };
  48. struct MockLayout : public logging::layout {
  49. MOCK_CONST_METHOD2(format, void(std::ostream&, logging::logpacket const&));
  50. };
  51. struct StubLayout : public logging::layout {
  52. void format(std::ostream& os, logging::logpacket const& pkt) const {
  53. os << pkt.message.str();
  54. }
  55. };
  56. ACTION(LogMessage) {
  57. arg0 << arg1.message.str();
  58. }
  59. MATCHER_P(MessageEq, value, "") {
  60. return arg.message.str() == value;
  61. }
  62. struct LoggerTest : public testing::Test {
  63. void SetUp() override {
  64. appender.reset(new MockAppender);
  65. pimpl = std::make_shared<logging::logger_impl>();
  66. auto layout = std::make_shared<StubLayout>();
  67. auto log = std::make_shared<logging::log_appender>(appender, layout);
  68. pimpl->impls.push_back(log);
  69. using testing::_;
  70. using testing::AnyNumber;
  71. EXPECT_CALL(*appender, write(_, _)).Times(AnyNumber());
  72. EXPECT_CALL(*appender, flush()).Times(AnyNumber());
  73. }
  74. void TearDown() override {
  75. pimpl.reset();
  76. appender.reset();
  77. }
  78. std::shared_ptr<MockAppender> appender;
  79. std::shared_ptr<logging::logger_impl> pimpl;
  80. };
  81. #endif /* mock_logger_h */