2015-10-17 15:49:54 +02:00
|
|
|
#pragma once
|
|
|
|
#ifndef MESSMER_CPPUTILS_TEST_LOGGING_TESTUTILS_LOGGINGTEST_H
|
|
|
|
#define MESSMER_CPPUTILS_TEST_LOGGING_TESTUTILS_LOGGINGTEST_H
|
|
|
|
|
2016-02-10 00:58:03 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
2016-02-11 12:53:42 +01:00
|
|
|
#include "cpp-utils/logging/logging.h"
|
2015-10-17 15:49:54 +02:00
|
|
|
|
|
|
|
class MockLogger final {
|
|
|
|
public:
|
|
|
|
MockLogger():
|
|
|
|
_capturedLogData(),
|
|
|
|
_sink(std::make_shared<spdlog::sinks::ostream_sink<std::mutex>>(_capturedLogData, true)),
|
2015-10-17 16:42:56 +02:00
|
|
|
_logger(spdlog::create("MockLogger", {_sink})) {
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~MockLogger() {
|
|
|
|
spdlog::drop("MockLogger");
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<spdlog::logger> get() {
|
|
|
|
return _logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string capturedLog() const {
|
|
|
|
return _capturedLogData.str();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::ostringstream _capturedLogData;
|
|
|
|
std::shared_ptr<spdlog::sinks::ostream_sink<std::mutex>> _sink;
|
|
|
|
std::shared_ptr<spdlog::logger> _logger;
|
|
|
|
};
|
|
|
|
|
|
|
|
class LoggingTest: public ::testing::Test {
|
|
|
|
public:
|
2015-10-17 20:15:20 +02:00
|
|
|
LoggingTest(): mockLogger() {}
|
|
|
|
|
2015-10-17 16:49:58 +02:00
|
|
|
std::string captureStderr(std::function<void()> func) {
|
|
|
|
testing::internal::CaptureStderr();
|
2015-10-17 15:49:54 +02:00
|
|
|
func();
|
2015-10-17 16:49:58 +02:00
|
|
|
return testing::internal::GetCapturedStderr();
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~LoggingTest() {
|
|
|
|
cpputils::logging::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
MockLogger mockLogger;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|