This commit is contained in:
KjellKod 2013-08-18 23:17:04 -06:00
parent f4c939dc8a
commit bb778adf25
2 changed files with 82 additions and 83 deletions

View File

@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <iostream>
#include "testing_helpers.h"
#include "g2log.h"
#include "g2logworker.h"
@ -9,16 +10,16 @@
using namespace std;
namespace {
g2LogWorker* oldworker = nullptr;
}
ScopedCout::ScopedCout(std::stringstream* buffer)
: _old_cout(std::cout.rdbuf()) {
cout.rdbuf(buffer->rdbuf());
namespace testing_helpers {
bool removeFile(std::string path_to_file) {
return (0 == std::remove(path_to_file.c_str()));
}
ScopedCout::~ScopedCout() {
cout.rdbuf(_old_cout);
}
RestoreLogger::RestoreLogger(std::string directory)
: logger_(g2LogWorker::createWithNoSink()) {
@ -28,7 +29,7 @@ RestoreLogger::RestoreLogger(std::string directory)
oldworker = g2::shutDownLogging();
initializeLogging(logger_.get());
internal::changeFatalInitHandlerForUnitTesting();
LOG(INFO) << "Restore logger test ";
auto filename = filehandler->call(&g2FileSink::fileName);
if (!filename.valid()) ADD_FAILURE();
log_file_ = filename.get();
@ -38,7 +39,7 @@ RestoreLogger::~RestoreLogger() {
reset();
g2::shutDownLogging();
if (nullptr != oldworker) g2::initializeLogging(oldworker);
if (0 != remove(log_file_.c_str()))
if (!removeFile(log_file_))
ADD_FAILURE();
}
@ -46,17 +47,6 @@ void RestoreLogger::reset() {
logger_.reset();
}
namespace testing_helper__cleaner {
bool removeFile(std::string path_to_file) {
return (0 == std::remove(path_to_file.c_str()));
}
}
size_t LogFileCleaner::size() {
return logs_to_clean_.size();
}
@ -64,9 +54,9 @@ size_t LogFileCleaner::size() {
LogFileCleaner::~LogFileCleaner() {
std::lock_guard<std::mutex> lock(g_mutex);
{
for (std::string p : logs_to_clean_) {
if (false == testing_helper__cleaner::removeFile(p)) {
ADD_FAILURE() << "UNABLE to remove: " << p.c_str() << std::endl;
for (const auto& file : logs_to_clean_) {
if (!removeFile(file)) {
ADD_FAILURE() << "UNABLE to remove: " << file << std::endl;
}
}
logs_to_clean_.clear();
@ -80,3 +70,5 @@ void LogFileCleaner::addLogToClean(std::string path_to_log) {
logs_to_clean_.push_back(path_to_log);
}
}
} // testing_helpers

View File

@ -5,36 +5,47 @@
* Created on July 13, 2013, 4:46 PM
*/
#ifndef TEST_HELPER__RESTORE_LOGGER_H
#define TEST_HELPER__RESTORE_LOGGER_H
#pragma once
#include <memory>
#include <string>
#include <iostream>
#include <mutex>
#include <algorithm>
#include "g2logworker.h"
// After initializing ScopedCout all std::couts is redirected to the buffer
// Example:
// stringstream buffer;
// ScopedCout guard(&buffer);
// cout << "Hello World";
// ASSERT_STREQ(buffer.str().c_str(), "Hello World");
namespace testing_helpers {
class ScopedCout {
bool removeFile(std::string path_to_file);
/** After initializing ScopedCout all std::couts is redirected to the buffer
@verbatim
Example:
stringstream buffer;
ScopedCout guard(&buffer);
cout << "Hello World";
ASSERT_STREQ(buffer.str().c_str(), "Hello World"); */
class ScopedOut {
std::ostream& _out_type;
std::streambuf* _old_cout;
public:
explicit ScopedCout(std::stringstream* buffer);
~ScopedCout();
explicit ScopedOut(std::ostream& out_type, std::stringstream* buffer)
: _out_type(out_type)
, _old_cout(_out_type.rdbuf()) {
_out_type.rdbuf(buffer->rdbuf());
}
virtual ~ScopedOut() {
_out_type.rdbuf(_old_cout);
}
};
namespace testing_helper__cleaner {
bool removeFile(std::string path_to_file);
}
class LogFileCleaner // RAII cluttering files cleanup
/// RAII cluttering files cleanup
class LogFileCleaner
{
private:
std::vector<std::string> logs_to_clean_;
@ -50,14 +61,12 @@ public:
// RAII temporarily replace of logger
// and restoration of original logger at scope end
/** RAII temporarily replace of logger
* and restoration of original logger at scope end*/
struct RestoreLogger {
explicit RestoreLogger(std::string directory);
~RestoreLogger();
void reset();
std::unique_ptr<g2LogWorker> logger_;
template<typename Call, typename ... Args >
@ -66,14 +75,12 @@ struct RestoreLogger {
return func();
}
std::string logFile() { return log_file_; }
private:
std::string log_file_;
};
} // testing_helpers
#endif /* TEST_HELPER__RESTORE_LOGGER_H */