tests refactoring

This commit is contained in:
gabime 2015-12-13 14:59:48 +02:00
parent 6feaa29c62
commit 3d420a3bcf
4 changed files with 46 additions and 43 deletions

View File

@ -7,15 +7,6 @@ using namespace spdlog::details;
static const std::string filename = "logs/file_helper_test.txt"; static const std::string filename = "logs/file_helper_test.txt";
size_t filesize2(const std::string& filename)
{
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
if (!ifs)
throw std::runtime_error("Failed open file ");
return (size_t)ifs.tellg();
}
static void write_with_helper(file_helper &helper, size_t howmany) static void write_with_helper(file_helper &helper, size_t howmany)
{ {
log_msg msg; log_msg msg;
@ -45,7 +36,7 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]")
write_with_helper(helper, expected_size); write_with_helper(helper, expected_size);
REQUIRE(helper.size() == expected_size); REQUIRE(helper.size() == expected_size);
} }
REQUIRE(filesize2(filename) == expected_size); REQUIRE(filesize(filename) == expected_size);
} }

View File

@ -3,39 +3,6 @@
*/ */
#include "includes.h" #include "includes.h"
static std::string file_contents(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
return std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
static std::size_t count_lines(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
std::string line;
size_t counter = 0;
while(std::getline(ifs, line))
counter++;
return counter;
}
std::ifstream::pos_type filesize(const std::string& filename)
{
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
if (!ifs)
throw std::runtime_error("Failed open file ");
return ifs.tellg();
}
TEST_CASE("simple_file_logger", "[simple_logger]]") TEST_CASE("simple_file_logger", "[simple_logger]]")
{ {

View File

@ -9,3 +9,37 @@ void prepare_logdir()
auto rv = system("rm -f logs/*"); auto rv = system("rm -f logs/*");
#endif #endif
} }
std::string file_contents(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
return std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
std::size_t count_lines(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
std::string line;
size_t counter = 0;
while(std::getline(ifs, line))
counter++;
return counter;
}
std::ifstream::pos_type filesize(const std::string& filename)
{
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
if (!ifs)
throw std::runtime_error("Failed open file ");
return ifs.tellg();
}

View File

@ -1,4 +1,15 @@
#pragma once #pragma once
#include <string>
#include<cstddef>
std::size_t count_lines(const std::string& filename);
void prepare_logdir(); void prepare_logdir();
std::string file_contents(const std::string& filename);
std::size_t count_lines(const std::string& filename);
std::ifstream::pos_type filesize(const std::string& filename);