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"
|
2017-02-04 20:03:20 +01:00
|
|
|
#include <spdlog/sinks/ostream_sink.h>
|
2015-10-17 15:49:54 +02:00
|
|
|
|
|
|
|
class MockLogger final {
|
|
|
|
public:
|
|
|
|
MockLogger():
|
|
|
|
_capturedLogData(),
|
2019-06-02 06:05:26 +02:00
|
|
|
_logger(spdlog::create<spdlog::sinks::ostream_sink_mt>("MockLogger", _capturedLogData, true)) {
|
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::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
|