Commit 3d420a3b authored by gabime's avatar gabime

tests refactoring

parent 6feaa29c
...@@ -7,15 +7,6 @@ using namespace spdlog::details; ...@@ -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()]]") ...@@ -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);
} }
......
...@@ -3,39 +3,6 @@ ...@@ -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]]")
{ {
......
...@@ -9,3 +9,37 @@ void prepare_logdir() ...@@ -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();
}
#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);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment