2024-03-24 23:52:24 +00:00
|
|
|
/**
|
2024-06-28 12:34:14 +00:00
|
|
|
* @file tests/unit/test_file_handler.cpp
|
2024-03-24 23:52:24 +00:00
|
|
|
* @brief Test src/file_handler.*.
|
|
|
|
*/
|
|
|
|
#include <src/file_handler.h>
|
|
|
|
|
2024-08-22 20:48:24 +00:00
|
|
|
#include "../tests_common.h"
|
2024-03-24 23:52:24 +00:00
|
|
|
|
2024-08-22 20:48:24 +00:00
|
|
|
struct FileHandlerParentDirectoryTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
2024-06-17 18:03:42 +00:00
|
|
|
|
|
|
|
TEST_P(FileHandlerParentDirectoryTest, Run) {
|
|
|
|
auto [input, expected] = GetParam();
|
|
|
|
EXPECT_EQ(file_handler::get_parent_directory(input), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
|
|
FileHandlerTests,
|
|
|
|
FileHandlerParentDirectoryTest,
|
2024-08-22 20:48:24 +00:00
|
|
|
testing::Values(
|
2024-06-17 18:03:42 +00:00
|
|
|
std::make_tuple("/path/to/file.txt", "/path/to"),
|
|
|
|
std::make_tuple("/path/to/directory", "/path/to"),
|
|
|
|
std::make_tuple("/path/to/directory/", "/path/to")));
|
|
|
|
|
2024-08-22 20:48:24 +00:00
|
|
|
struct FileHandlerMakeDirectoryTest: testing::TestWithParam<std::tuple<std::string, bool, bool>> {};
|
2024-06-17 18:03:42 +00:00
|
|
|
|
|
|
|
TEST_P(FileHandlerMakeDirectoryTest, Run) {
|
|
|
|
auto [input, expected, remove] = GetParam();
|
|
|
|
const std::string test_dir = platf::appdata().string() + "/tests/path/";
|
|
|
|
input = test_dir + input;
|
|
|
|
|
|
|
|
EXPECT_EQ(file_handler::make_directory(input), expected);
|
|
|
|
EXPECT_TRUE(std::filesystem::exists(input));
|
|
|
|
|
|
|
|
// remove test directory
|
|
|
|
if (remove) {
|
|
|
|
std::filesystem::remove_all(test_dir);
|
|
|
|
EXPECT_FALSE(std::filesystem::exists(test_dir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
|
|
FileHandlerTests,
|
|
|
|
FileHandlerMakeDirectoryTest,
|
2024-08-22 20:48:24 +00:00
|
|
|
testing::Values(
|
2024-06-17 18:03:42 +00:00
|
|
|
std::make_tuple("dir_123", true, false),
|
|
|
|
std::make_tuple("dir_123", true, true),
|
|
|
|
std::make_tuple("dir_123/abc", true, false),
|
|
|
|
std::make_tuple("dir_123/abc", true, true)));
|
|
|
|
|
2024-08-22 20:48:24 +00:00
|
|
|
struct FileHandlerTests: testing::TestWithParam<std::tuple<int, std::string>> {};
|
2024-05-12 17:13:20 +00:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
|
|
TestFiles,
|
|
|
|
FileHandlerTests,
|
2024-08-22 20:48:24 +00:00
|
|
|
testing::Values(
|
2024-05-12 17:13:20 +00:00
|
|
|
std::make_tuple(0, ""), // empty file
|
|
|
|
std::make_tuple(1, "a"), // single character
|
|
|
|
std::make_tuple(2, "Mr. Blue Sky - Electric Light Orchestra"), // single line
|
|
|
|
std::make_tuple(3, R"(
|
|
|
|
Morning! Today's forecast calls for blue skies
|
|
|
|
The sun is shining in the sky
|
|
|
|
There ain't a cloud in sight
|
|
|
|
It's stopped raining
|
|
|
|
Everybody's in the play
|
|
|
|
And don't you know, it's a beautiful new day
|
|
|
|
Hey, hey, hey!
|
|
|
|
Running down the avenue
|
|
|
|
See how the sun shines brightly in the city
|
|
|
|
All the streets where once was pity
|
|
|
|
Mr. Blue Sky is living here today!
|
|
|
|
Hey, hey, hey!
|
|
|
|
)") // multi-line
|
|
|
|
));
|
|
|
|
|
|
|
|
TEST_P(FileHandlerTests, WriteFileTest) {
|
|
|
|
auto [fileNum, content] = GetParam();
|
|
|
|
std::string fileName = "write_file_test_" + std::to_string(fileNum) + ".txt";
|
|
|
|
EXPECT_EQ(file_handler::write_file(fileName.c_str(), content), 0);
|
2024-03-24 23:52:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 17:13:20 +00:00
|
|
|
TEST_P(FileHandlerTests, ReadFileTest) {
|
|
|
|
auto [fileNum, content] = GetParam();
|
|
|
|
std::string fileName = "write_file_test_" + std::to_string(fileNum) + ".txt";
|
|
|
|
EXPECT_EQ(file_handler::read_file(fileName.c_str()), content);
|
|
|
|
}
|
2024-03-24 23:52:24 +00:00
|
|
|
|
2024-05-12 17:13:20 +00:00
|
|
|
TEST(FileHandlerTests, ReadMissingFileTest) {
|
2024-03-24 23:52:24 +00:00
|
|
|
// read missing file
|
|
|
|
EXPECT_EQ(file_handler::read_file("non-existing-file.txt"), "");
|
|
|
|
}
|