Merge pull request #675 from lodle/feature647

Converted boost tests into gtests #647
master
Mark Chandler 2013-10-28 15:35:49 -07:00
commit 5f5647286b
26 changed files with 504 additions and 449 deletions

View File

@ -264,7 +264,7 @@ function(install_data_directories target_path)
endforeach()
endfunction()
macro(LinkWithGTest target)
macro(link_with_gtest target)
if(WITH_GTEST)
if(MSVC11)
add_definitions(-D_VARIADIC_MAX=10)
@ -289,4 +289,4 @@ MACRO(setup_precompiled_header PrecompiledHeader PrecompiledSource SourcesVar)
# Add precompiled header to SourcesVar
LIST(APPEND ${SourcesVar} ${PrecompiledSource})
ENDIF(MSVC)
ENDMACRO(setup_precompiled_header)
ENDMACRO(setup_precompiled_header)

View File

@ -99,6 +99,10 @@ endif()
add_subdirectory(shared/usercore)
add_subdirectory(shared/webcore)
if(WITH_GTEST)
add_subdirectory(shared/unittests)
endif()
###############################################################################
# build the client
###############################################################################
@ -125,12 +129,6 @@ if(BUILD_TOOLS)
add_subdirectory(tools/mcf_util)
endif()
###############################################################################
# build the tests
###############################################################################
add_subdirectory(tests)
###############################################################################
# some pre building or installing actions
###############################################################################

View File

@ -45,5 +45,5 @@ if(WIN32)
SetSharedRuntime(mcfcore)
endif()
LinkWithGTest(mcfcore)
link_with_gtest(mcfcore)
install_library(mcfcore)

View File

@ -181,5 +181,5 @@ if(WIN32)
SetSharedRuntime(uicore)
endif()
LinkWithGTest(uicore)
link_with_gtest(uicore)
install_library(uicore)

View File

@ -35,6 +35,8 @@ extern "C" CEXPORT UICoreI* GetInterface();
#include "util/UtilLinux.h"
#endif
#include "SharedObjectLoader.h"
#ifdef WIN32
#include <shlobj.h>
@ -94,6 +96,16 @@ public:
m_bExitCodeSet = false;
m_iExitCode = 0;
#if defined(DEBUG) && defined(WITH_GTEST)
#ifdef WIN32
m_hUnitTest.load("unittest.dll");
#else
m_hUnitTest.load("unittest.so");
#endif
#endif
}
~UICore()
@ -258,6 +270,12 @@ public:
int runUnitTests(int argc, char** argv)
{
#ifdef WITH_GTEST
#ifdef WIN32
m_hUnitTest.load("unittest.dll");
#else
m_hUnitTest.load("unittest.so");
#endif
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
#else
@ -275,6 +293,8 @@ private:
bool m_bExitCodeSet;
int32 m_iExitCode;
SharedObjectLoader m_hUnitTest;
#ifndef WIN32
wxSingleInstanceChecker* m_pChecker;
#endif

View File

@ -0,0 +1,27 @@
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/code
${Boost_INCLUDE_DIR}
)
file(GLOB Sources code/util/gcBuff_test.cpp
code/util/MD5_test.cpp
code/util_fs/util_fs_copyFile.cpp
code/util_fs/util_fs_copyFolder.cpp
code/util_fs/util_fs_getAllFiles.cpp
code/util_fs/util_fs_getAllFolders.cpp
code/util_string/util_string_sanitizeFilePath.cpp
)
#code/util/UtilLinux_test.cpp
add_library(unittest SHARED ${Sources})
target_link_libraries(unittest
util
util_fs
)
link_with_gtest(unittest)
install_library(unittest "Shared")

View File

@ -0,0 +1,27 @@
#include <gtest/gtest.h>
#include "Common.h"
#include "util/MD5Progressive.h"
#define EMPTY_MD5 "d41d8cd98f00b204e9800998ecf8427e"
static const char TEST_CSTRING[] = "this is a little test";
#define TEST_CSTRING_SIZE sizeof(TEST_CSTRING)
#define TEST_MD5_RESULT "419d085dcebc10a7e6d7115216e9820d"
namespace UnitTest
{
TEST(MD5Progressive, cons)
{
MD5Progressive md5;
ASSERT_STREQ(md5.finish().c_str(), EMPTY_MD5);
}
TEST(MD5Progressive, update)
{
MD5Progressive md5;
md5.update(TEST_CSTRING, TEST_CSTRING_SIZE - 1);
ASSERT_STREQ(md5.finish().c_str(), TEST_MD5_RESULT);
}
}

View File

@ -0,0 +1,61 @@
#include <gtest/gtest.h>
#include "Common.h"
#include "util/gcBuff.h"
#define TEST_SIZE 20
#define TEST_CSTRING "this is a test!"
#define TEST_CSTRING_SIZE sizeof(TEST_CSTRING)
const char TEST_STATIC_CSTRING[] = TEST_CSTRING;
namespace UnitTest
{
TEST(gcBuff, constructor)
{
//construct our objects
gcBuff buf1(TEST_SIZE);
gcBuff buf2(TEST_CSTRING, TEST_CSTRING_SIZE);
ASSERT_EQ(buf1.size(), TEST_SIZE);
ASSERT_EQ(buf2.size(), TEST_CSTRING_SIZE);
ASSERT_STREQ(buf2.c_ptr(), TEST_STATIC_CSTRING);
}
TEST(gcBuff, copy_constructor)
{
gcBuff buf_ori(TEST_CSTRING, TEST_CSTRING_SIZE);
gcBuff buf_copy_ref(buf_ori);
gcBuff buf_copy_ptr(&buf_ori);
ASSERT_STREQ(buf_copy_ref.c_ptr(), buf_ori.c_ptr());
ASSERT_EQ(buf_copy_ref.size(), buf_ori.size());
ASSERT_STREQ(buf_copy_ptr.c_ptr(), buf_ori.c_ptr());
ASSERT_EQ(buf_copy_ptr.size(), buf_ori.size());
}
TEST(gcBuff, cpy)
{
gcBuff buf(TEST_SIZE);
buf.cpy(TEST_CSTRING, TEST_CSTRING_SIZE);
ASSERT_EQ(buf.size(), TEST_SIZE);
ASSERT_STREQ(buf.c_ptr(), TEST_CSTRING);
}
TEST(gcBuff, charArr_cast)
{
gcBuff buf(TEST_CSTRING, TEST_CSTRING_SIZE);
ASSERT_STREQ(static_cast<char*>(buf), TEST_CSTRING);
}
TEST(gcBuff, element_access_operator)
{
gcBuff buf(TEST_STATIC_CSTRING, TEST_CSTRING_SIZE);
for (uint32 i = 0; i < TEST_CSTRING_SIZE; i++)
ASSERT_EQ(buf[i], TEST_STATIC_CSTRING[i]);
}
}

View File

@ -0,0 +1,101 @@
#ifndef TEST_DIR
#error you have to define TEST_DIR first
#endif
#include "Common.h"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
namespace UnitTest
{
static const char* UNICODE_EXAMPLE_FILE = "ʨʬжװᇪ";
#define ASSERT_EQ_FILES(/* const fs::path& */ file1, /* const fs::path& */ file2) \
{ \
fs::ifstream file1_stream(file1); \
fs::ifstream file2_stream(file2); \
\
std::string file1_string; \
std::string file2_string; \
\
for (std::string file1_string_buf; std::getline(file1_stream, file1_string_buf);) \
file1_string += file1_string_buf; \
for (std::string file2_string_buf; std::getline(file2_stream, file2_string_buf);) \
file2_string += file2_string_buf; \
\
ASSERT_STREQ(file1_string.c_str(), file2_string.c_str()); \
\
file1_stream.close(); \
file2_stream.close(); \
}
class FSTestFixture : public ::testing::Test
{
public:
void SetUp() override
{
createTestDirectory();
}
void TearDown() override
{
deleteTestDirectory();
}
protected:
void createTestDirectory()
{
fs::create_directories(getTestDirectory());
fillWithTestData();
}
void deleteTestDirectory()
{
fs::remove_all(getTestDirectory());
}
const fs::path& getTestDirectory()
{
static const fs::path TEST_DIR_ROOT = fs::current_path() / "unit_test" / "util_fs" / TEST_DIR;
return TEST_DIR_ROOT;
}
/*
* this will create the following directory tree:
* ./0/0
* ./0/1.txt
* ./0/2.png
* ./0/UNICODE_EXAMPLE_FILE
*/
void fillWithTestData()
{
const fs::path &testDir = getTestDirectory();
fs::create_directories(testDir);
std::vector<std::string> firstLevel;
firstLevel.push_back("0");
std::vector<std::string> secondLevel;
secondLevel.push_back("0");
secondLevel.push_back("1.txt");
secondLevel.push_back("2.png");
secondLevel.push_back(UNICODE_EXAMPLE_FILE);
for (const std::string& i : firstLevel)
{
fs::create_directory(testDir / i);
for (const std::string& j : secondLevel)
{
fs::path newFilePath = testDir / i / j;
fs::ofstream newFileStream(newFilePath);
newFileStream << "this is a test file" << std::endl;
newFileStream.close();
}
}
}
};
}

View File

@ -0,0 +1,40 @@
#include <gtest/gtest.h>
// interface: void copyFile(Path src, Path dest);
// void copyFile(std::string src, std::string dest)
// set up test env for util_fs testing
#define TEST_DIR "copyFile"
#include "util_fs/testFunctions.cpp"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
#define SRC (getTestDirectory()/"0"/"1.txt")
#define DES1 (getTestDirectory()/"0"/"jsdakl1")
#define DES2 (getTestDirectory()/"0"/"jsdakl2")
namespace UnitTest
{
TEST_F(FSTestFixture, copyFile_Path)
{
Path src(SRC.string(), "", false);
Path des(DES1.string(), "", false);
ASSERT_TRUE(!fs::exists(DES1));
copyFile(src, des);
ASSERT_TRUE(fs::exists(DES1));
ASSERT_EQ_FILES(SRC, DES1);
}
TEST_F(FSTestFixture, copyFile_string)
{
ASSERT_TRUE(!fs::exists(DES2));
copyFile(SRC.string(), DES2.string());
ASSERT_TRUE(fs::exists(DES2));
ASSERT_EQ_FILES(SRC, DES2);
}
}

View File

@ -0,0 +1,98 @@
#include <gtest/gtest.h>
// interface: void copyFolder(Path src, Path dest, std::vector<std::string> *vIgnoreList = NULL, bool copyOverExisting = true);
// void copyFolder(std::string src, std::string dest, std::vector<std::string> *vIgnoreList = NULL, bool copyOverExisting = true)
// set up test env for util_fs testing
#define TEST_DIR "copyFolder"
#include "util_fs/testFunctions.cpp"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
#define SRC (getTestDirectory()/"0")
#define DES1 (getTestDirectory()/"1")
#define DES2 (getTestDirectory()/"2")
#define DES3 (getTestDirectory()/"3")
#define DES4 (getTestDirectory()/"4")
#define SRC5 (getTestDirectory()/"S5")
#define DES5 (getTestDirectory()/"D5")
namespace UnitTest
{
TEST_F(FSTestFixture, copyFolder_Path_NULL_default)
{
Path src(SRC.string(), "", false);
Path des(DES1.string(), "", false);
ASSERT_TRUE(!fs::exists(DES1));
copyFolder(src, des);
ASSERT_TRUE(fs::exists(DES1));
ASSERT_EQ_FILES(SRC / "0", DES1 / "0");
ASSERT_EQ_FILES(SRC / "1.txt", DES1 / "1.txt");
ASSERT_EQ_FILES(SRC / "2.png", DES1 / "2.png");
ASSERT_EQ_FILES(SRC / UNICODE_EXAMPLE_FILE, DES1 / UNICODE_EXAMPLE_FILE);
}
TEST_F(FSTestFixture, copyFolder_Path_vector_default)
{
Path src(SRC.string(), "", false);
Path des(DES2.string(), "", false);
std::vector<std::string> ignoreList;
ignoreList.push_back("1.txt");
ASSERT_TRUE(!fs::exists(DES2));
copyFolder(src, des, &ignoreList);
ASSERT_TRUE(fs::exists(DES2));
ASSERT_EQ_FILES(SRC / "0", DES2 / "0");
ASSERT_TRUE(!fs::exists(DES2 / "1.txt"));
ASSERT_EQ_FILES(SRC / "2.png", DES2 / "2.png");
ASSERT_EQ_FILES(SRC / UNICODE_EXAMPLE_FILE, DES2 / UNICODE_EXAMPLE_FILE);
}
TEST_F(FSTestFixture, copyFolder_string_NULL_default)
{
ASSERT_TRUE(!fs::exists(DES3));
copyFolder(SRC.string(), DES3.string());
ASSERT_TRUE(fs::exists(DES3));
ASSERT_EQ_FILES(SRC / "0", DES3 / "0");
ASSERT_EQ_FILES(SRC / "1.txt", DES3 / "1.txt");
ASSERT_EQ_FILES(SRC / "2.png", DES3 / "2.png");
ASSERT_EQ_FILES(SRC / UNICODE_EXAMPLE_FILE, DES3 / UNICODE_EXAMPLE_FILE);
}
TEST_F(FSTestFixture, copyFolder_string_vector_default)
{
std::vector<std::string> ignoreList;
ignoreList.push_back("1.txt");
ASSERT_TRUE(!fs::exists(DES4));
copyFolder(SRC.string(), DES4.string(), &ignoreList);
ASSERT_TRUE(fs::exists(DES4));
ASSERT_EQ_FILES(SRC / "0", DES4 / "0");
ASSERT_TRUE(!fs::exists(DES4 / "1.txt"));
ASSERT_EQ_FILES(SRC / "2.png", DES4 / "2.png");
ASSERT_EQ_FILES(SRC / UNICODE_EXAMPLE_FILE, DES4 / UNICODE_EXAMPLE_FILE);
}
TEST_F(FSTestFixture, copyFolder_string_rec)
{
// here we copy a directory with files into a directory ...
copyFolder(SRC.string(), (SRC5 / "0").string());
ASSERT_TRUE(!fs::exists(DES5));
// ... so we can copy the directory with the directory with files ;)
copyFolder(SRC5.string(), DES5.string());
ASSERT_TRUE(fs::exists(DES5));
ASSERT_TRUE(fs::exists(DES5 / "0"));
ASSERT_EQ_FILES(SRC / "0", DES5 / "0" / "0");
ASSERT_EQ_FILES(SRC / "1.txt", DES5 / "0" / "1.txt");
ASSERT_EQ_FILES(SRC / "2.png", DES5 / "0" / "2.png");
ASSERT_EQ_FILES(SRC / UNICODE_EXAMPLE_FILE, DES5 / "0" / UNICODE_EXAMPLE_FILE);
}
}

View File

@ -0,0 +1,52 @@
#include <gtest/gtest.h>
// interface: void getAllFiles(Path path, std::vector<Path> &outList, std::vector<std::string> *extsFilter);
// set up test env for util_fs testing
#define TEST_DIR "getAllFiles"
#include "util_fs/testFunctions.cpp"
#include "helper_functions.h"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
namespace UnitTest
{
TEST_F(FSTestFixture, getAllFiles_without_extsFilter)
{
std::vector<Path> content;
std::vector<Path> content0;
Path path(getTestDirectory().string(), "", false);
Path path0((getTestDirectory() / "0").string(), "", false);
getAllFiles(path, content, nullptr);
getAllFiles(path0, content0, nullptr);
ASSERT_EQ(content.size(), 0);
ASSERT_EQ(content0.size(), 4);
ASSERT_TRUE(isInVector(Path((getTestDirectory() / "0" / "0").string(), "", true), content0));
ASSERT_TRUE(isInVector(Path((getTestDirectory() / "0" / "1.txt").string(), "", true), content0));
ASSERT_TRUE(isInVector(Path((getTestDirectory() / "0" / "2.png").string(), "", true), content0));
ASSERT_TRUE(isInVector(Path((getTestDirectory() / "0" / UNICODE_EXAMPLE_FILE).string(), "", true), content0));
}
TEST_F(FSTestFixture, getAllFiles_with_extsfiler)
{
std::vector<Path> content;
std::vector<std::string> filter;
filter.push_back("txt");
Path path((getTestDirectory() / "0").string(), "", false);
getAllFiles(path, content, &filter);
ASSERT_EQ(content.size(), 1);
ASSERT_EQ(content[0].getFullPath(), (getTestDirectory() / "0" / "1.txt").string());
}
}

View File

@ -0,0 +1,37 @@
#include <gtest/gtest.h>
// interface: void getAllFolders(Path path, std::vector<Path> &outList)
#define TEST_DIR "getAllFolders"
#include "util_fs/testFunctions.cpp"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
namespace UnitTest
{
TEST_F(FSTestFixture, getAllFolders1)
{
Path path(getTestDirectory().string(), "", false);
std::vector<Path> content;
getAllFolders(path, content);
ASSERT_EQ(content.size(), 1);
ASSERT_EQ(content[0].getFullPath(), (getTestDirectory() / "0").string());
}
TEST_F(FSTestFixture, getAllFolder2)
{
Path path((getTestDirectory() / "0").string(), "", false);
std::vector<Path> content;
getAllFolders(path, content);
ASSERT_EQ(content.size(), 0);
}
}

View File

@ -0,0 +1,32 @@
#include <gtest/gtest.h>
#include "Common.h"
#include "util/UtilFs.h"
using namespace UTIL::STRING;
namespace UnitTest
{
TEST(sanitizeFilePath, slash1)
{
std::string str = "/hi/ho/hihihi\\";
ASSERT_STREQ("/hi/ho/hihihi/", sanitizeFilePath(str, '/').c_str());
}
TEST(sanitizeFilePath, slash_with_dot)
{
std::string str = "/hi/./ho/hihihi\\";
ASSERT_STREQ("/hi/./ho/hihihi/", sanitizeFilePath(str, '/').c_str());
}
TEST(sanitizeFilePath, slash_with_dots)
{
std::string str = "/hi/../ho/hihihi\\";
ASSERT_STREQ("/hi/../ho/hihihi/", sanitizeFilePath(str, '/').c_str());
}
}

View File

@ -129,5 +129,5 @@ if(WIN32)
SetSharedRuntime(usercore)
endif()
LinkWithGTest(usercore)
link_with_gtest(usercore)
install_library(usercore)

View File

@ -1,15 +0,0 @@
if(NOT WIN32 OR MINGW)
if(UNIX)
add_desura_test(UtilLinux_test util "util")
endif()
add_desura_test(gcBuff_test util "util")
add_desura_test(MD5_test util "util")
add_desura_test(util_fs_copyFile util_fs "util;util_fs")
add_desura_test(util_fs_copyFolder util_fs "util;util_fs")
add_desura_test(util_fs_getAllFiles util_fs "util;util_fs")
add_desura_test(util_fs_getAllFolders util_fs "util;util_fs")
add_desura_test(util_string_sanitizeFilePath util_string "util")
endif()

View File

@ -1,24 +0,0 @@
#define BOOST_TEST_MODULE MD5_test
#include <boost/test/unit_test.hpp>
#include "Common.h"
#include "util/MD5Progressive.h"
#define EMPTY_MD5 "d41d8cd98f00b204e9800998ecf8427e"
static const char TEST_CSTRING[] = "this is a little test";
#define TEST_CSTRING_SIZE sizeof(TEST_CSTRING)
#define TEST_MD5_RESULT "419d085dcebc10a7e6d7115216e9820d"
BOOST_AUTO_TEST_CASE( MD5Progressive_cons )
{
MD5Progressive md5;
BOOST_REQUIRE_EQUAL(md5.finish(), EMPTY_MD5);
}
BOOST_AUTO_TEST_CASE( MD5Progressive_update )
{
MD5Progressive md5;
md5.update(TEST_CSTRING, TEST_CSTRING_SIZE - 1);
BOOST_REQUIRE_EQUAL(md5.finish(), TEST_MD5_RESULT);
}

View File

@ -1,58 +0,0 @@
#define BOOST_TEST_MODULE gcBuff_test
#include <boost/test/unit_test.hpp>
#include "Common.h"
#include "util/gcBuff.h"
#define TEST_SIZE 20
#define TEST_CSTRING "this is a test!"
#define TEST_CSTRING_SIZE sizeof(TEST_CSTRING)
const char TEST_STATIC_CSTRING[] = TEST_CSTRING;
BOOST_AUTO_TEST_CASE( gcBuff_constructor )
{
//construct our objects
gcBuff buf1(TEST_SIZE);
gcBuff buf2(TEST_CSTRING, TEST_CSTRING_SIZE);
BOOST_REQUIRE_EQUAL(buf1.size(), TEST_SIZE);
BOOST_REQUIRE_EQUAL(buf2.size(), TEST_CSTRING_SIZE);
BOOST_REQUIRE_EQUAL(buf2.c_ptr(), TEST_STATIC_CSTRING);
}
BOOST_AUTO_TEST_CASE( gcBuff_copy_constructor )
{
gcBuff buf_ori(TEST_CSTRING, TEST_CSTRING_SIZE);
gcBuff buf_copy_ref(buf_ori);
gcBuff buf_copy_ptr(&buf_ori);
BOOST_REQUIRE_EQUAL(buf_copy_ref.c_ptr(), buf_ori.c_ptr());
BOOST_REQUIRE_EQUAL(buf_copy_ref.size(), buf_ori.size());
BOOST_REQUIRE_EQUAL(buf_copy_ptr.c_ptr(), buf_ori.c_ptr());
BOOST_REQUIRE_EQUAL(buf_copy_ptr.size(), buf_ori.size());
}
BOOST_AUTO_TEST_CASE( gcBuff_cpy )
{
gcBuff buf(TEST_SIZE);
buf.cpy(TEST_CSTRING, TEST_CSTRING_SIZE);
BOOST_REQUIRE_EQUAL(buf.size(), TEST_SIZE);
BOOST_REQUIRE_EQUAL(buf.c_ptr(), TEST_CSTRING);
}
BOOST_AUTO_TEST_CASE( gcBuff_charArr_cast )
{
gcBuff buf(TEST_CSTRING, TEST_CSTRING_SIZE);
BOOST_REQUIRE_EQUAL(static_cast<char*>(buf), TEST_CSTRING);
}
BOOST_AUTO_TEST_CASE( gcBuff_element_access_operator )
{
gcBuff buf(TEST_STATIC_CSTRING, TEST_CSTRING_SIZE);
for (int i = 0; i < TEST_CSTRING_SIZE; i++)
BOOST_REQUIRE_EQUAL(buf[i], TEST_STATIC_CSTRING[i]);
}

View File

@ -1,89 +0,0 @@
#ifndef TEST_DIR
#error you have to define TEST_DIR first
#endif
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
#define UNICODE_EXAMPLE_FILE "ʨʬжװᇪ"
void createTestDirectory();
void deleteTestDirectory();
const fs::path& getTestDirectory();
/*
* this will create the following directory tree:
* ./0/0
* ./0/1.txt
* ./0/2.png
* ./0/UNICODE_EXAMPLE_FILE
*/
void fillWithTestData();
#define BOOST_REQUIRE_EQUAL_FILES(/* const fs::path& */ file1, /* const fs::path& */ file2) \
{ \
fs::ifstream file1_stream(file1); \
fs::ifstream file2_stream(file2); \
\
std::string file1_string; \
std::string file2_string; \
\
for (std::string file1_string_buf; std::getline(file1_stream, file1_string_buf);) \
file1_string += file1_string_buf; \
for (std::string file2_string_buf; std::getline(file2_stream, file2_string_buf);) \
file2_string += file2_string_buf; \
\
BOOST_REQUIRE_EQUAL( file1_string, file2_string ); \
\
file1_stream.close(); \
file2_stream.close(); \
}
// some macros
#define START_UTIL_FS_TEST_CASE BOOST_AUTO_TEST_CASE( setup_env ) \
{ \
createTestDirectory(); \
}
#define END_UTIL_FS_TEST_CASE BOOST_AUTO_TEST_CASE( destroy_env ) \
{ \
deleteTestDirectory(); \
}
void createTestDirectory()
{
fs::create_directories(getTestDirectory());
fillWithTestData();
}
void deleteTestDirectory()
{
fs::remove_all(getTestDirectory());
}
const fs::path& getTestDirectory()
{
static const fs::path TEST_DIR_ROOT = fs::current_path()/"unit_test"/"util_fs"/TEST_DIR;
return TEST_DIR_ROOT;
}
void fillWithTestData()
{
const fs::path &testDir = getTestDirectory();
fs::create_directories(testDir);
std::vector<std::string> firstLevel = {"0"};
std::vector<std::string> secondLevel = {"0", "1.txt", "2.png", UNICODE_EXAMPLE_FILE};
for (const std::string& i : firstLevel)
{
fs::create_directory(testDir / i );
for (const std::string& j : secondLevel)
{
fs::path newFilePath = testDir / i / j;
fs::ofstream newFileStream(newFilePath);
newFileStream << "this is a test file" << std::endl;
newFileStream.close();
}
}
}

View File

@ -1,41 +0,0 @@
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
// interface: void copyFile(Path src, Path dest);
// void copyFile(std::string src, std::string dest)
// set up test env for util_fs testing
#define TEST_DIR "copyFile"
#include "testFunctions.cpp"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
#define SRC (getTestDirectory()/"0"/"1.txt")
#define DES1 (getTestDirectory()/"0"/"jsdakl1")
#define DES2 (getTestDirectory()/"0"/"jsdakl2")
START_UTIL_FS_TEST_CASE
BOOST_AUTO_TEST_CASE( copyFile_Path )
{
Path src(SRC.string(), "", false);
Path des(DES1.string(), "", false);
BOOST_REQUIRE( !fs::exists(DES1) );
copyFile(src, des);
BOOST_REQUIRE( fs::exists(DES1) );
BOOST_REQUIRE_EQUAL_FILES(SRC, DES1);
}
BOOST_AUTO_TEST_CASE( copyFile_string )
{
BOOST_REQUIRE( !fs::exists(DES2) );
copyFile(SRC.string(), DES2.string());
BOOST_REQUIRE( fs::exists(DES2) );
BOOST_REQUIRE_EQUAL_FILES (SRC, DES2);
}
END_UTIL_FS_TEST_CASE

View File

@ -1,94 +0,0 @@
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
// interface: void copyFolder(Path src, Path dest, std::vector<std::string> *vIgnoreList = NULL, bool copyOverExisting = true);
// void copyFolder(std::string src, std::string dest, std::vector<std::string> *vIgnoreList = NULL, bool copyOverExisting = true)
// set up test env for util_fs testing
#define TEST_DIR "copyFolder"
#include "testFunctions.cpp"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
#define SRC (getTestDirectory()/"0")
#define DES1 (getTestDirectory()/"1")
#define DES2 (getTestDirectory()/"2")
#define DES3 (getTestDirectory()/"3")
#define DES4 (getTestDirectory()/"4")
#define SRC5 (getTestDirectory()/"S5")
#define DES5 (getTestDirectory()/"D5")
START_UTIL_FS_TEST_CASE
BOOST_AUTO_TEST_CASE( copyFolder_Path_NULL_default )
{
Path src(SRC.string(), "", false);
Path des(DES1.string(), "", false);
BOOST_REQUIRE( !fs::exists(DES1) );
copyFolder(src, des);
BOOST_REQUIRE( fs::exists(DES1) );
BOOST_REQUIRE_EQUAL_FILES( SRC/"0", DES1/"0" );
BOOST_REQUIRE_EQUAL_FILES( SRC/"1.txt", DES1/"1.txt" );
BOOST_REQUIRE_EQUAL_FILES( SRC/"2.png", DES1/"2.png" );
BOOST_REQUIRE_EQUAL_FILES( SRC/UNICODE_EXAMPLE_FILE, DES1/UNICODE_EXAMPLE_FILE );
}
BOOST_AUTO_TEST_CASE( copyFolder_Path_vector_default )
{
Path src(SRC.string(), "", false);
Path des(DES2.string(), "", false);
std::vector<std::string> ignoreList = {"1.txt"};
BOOST_REQUIRE( !fs::exists(DES2) );
copyFolder(src, des, &ignoreList);
BOOST_REQUIRE( fs::exists(DES2) );
BOOST_REQUIRE_EQUAL_FILES( SRC/"0", DES2/"0" );
BOOST_REQUIRE( !fs::exists(DES2/"1.txt") );
BOOST_REQUIRE_EQUAL_FILES( SRC/"2.png", DES2/"2.png" );
BOOST_REQUIRE_EQUAL_FILES( SRC/UNICODE_EXAMPLE_FILE, DES2/UNICODE_EXAMPLE_FILE );
}
BOOST_AUTO_TEST_CASE( copyFolder_string_NULL_default )
{
BOOST_REQUIRE( !fs::exists(DES3) );
copyFolder(SRC.string(), DES3.string());
BOOST_REQUIRE( fs::exists(DES3) );
BOOST_REQUIRE_EQUAL_FILES( SRC/"0", DES3/"0" );
BOOST_REQUIRE_EQUAL_FILES( SRC/"1.txt", DES3/"1.txt" );
BOOST_REQUIRE_EQUAL_FILES( SRC/"2.png", DES3/"2.png" );
BOOST_REQUIRE_EQUAL_FILES( SRC/UNICODE_EXAMPLE_FILE, DES3/UNICODE_EXAMPLE_FILE );
}
BOOST_AUTO_TEST_CASE( copyFolder_string_vector_default )
{
std::vector<std::string> ignoreList = {"1.txt"};
BOOST_REQUIRE( !fs::exists(DES4) );
copyFolder(SRC.string(), DES4.string(), &ignoreList);
BOOST_REQUIRE( fs::exists(DES4) );
BOOST_REQUIRE_EQUAL_FILES( SRC/"0", DES4/"0" );
BOOST_REQUIRE( !fs::exists(DES4/"1.txt") );
BOOST_REQUIRE_EQUAL_FILES( SRC/"2.png", DES4/"2.png" );
BOOST_REQUIRE_EQUAL_FILES( SRC/UNICODE_EXAMPLE_FILE, DES4/UNICODE_EXAMPLE_FILE );
}
BOOST_AUTO_TEST_CASE( copyFolder_string_rec )
{
// here we copy a directory with files into a directory ...
copyFolder(SRC.string(), (SRC5/"0").string());
BOOST_REQUIRE( !fs::exists(DES5) );
// ... so we can copy the directory with the directory with files ;)
copyFolder(SRC5.string(), DES5.string() );
BOOST_REQUIRE( fs::exists(DES5) );
BOOST_REQUIRE( fs::exists(DES5/"0") );
BOOST_REQUIRE_EQUAL_FILES( SRC/"0", DES5/"0"/"0" );
BOOST_REQUIRE_EQUAL_FILES( SRC/"1.txt", DES5/"0"/"1.txt" );
BOOST_REQUIRE_EQUAL_FILES( SRC/"2.png", DES5/"0"/"2.png" );
BOOST_REQUIRE_EQUAL_FILES( SRC/UNICODE_EXAMPLE_FILE, DES5/"0"/UNICODE_EXAMPLE_FILE );
}
END_UTIL_FS_TEST_CASE

View File

@ -1,50 +0,0 @@
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
// interface: void getAllFiles(Path path, std::vector<Path> &outList, std::vector<std::string> *extsFilter);
// set up test env for util_fs testing
#define TEST_DIR "getAllFiles"
#include "testFunctions.cpp"
#include "helper_functions.h"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
START_UTIL_FS_TEST_CASE
BOOST_AUTO_TEST_CASE( getAllFiles_without_extsFilter )
{
std::vector<Path> content;
std::vector<Path> content0;
Path path( getTestDirectory().string(), "", false);
Path path0( (getTestDirectory()/"0").string(), "", false);
getAllFiles(path, content, nullptr);
getAllFiles(path0, content0, nullptr);
BOOST_REQUIRE_EQUAL(content.size(), 0);
BOOST_REQUIRE_EQUAL(content0.size(), 4);
BOOST_REQUIRE( isInVector(Path((getTestDirectory()/"0"/"0").string(), "", true), content0) );
BOOST_REQUIRE( isInVector(Path((getTestDirectory()/"0"/"1.txt").string(), "", true), content0) );
BOOST_REQUIRE( isInVector(Path((getTestDirectory()/"0"/"2.png").string(), "", true), content0) );
BOOST_REQUIRE( isInVector(Path((getTestDirectory()/"0"/UNICODE_EXAMPLE_FILE).string(), "", true), content0) );
}
BOOST_AUTO_TEST_CASE( getAllFiles_with_extsfiler )
{
std::vector<Path> content;
std::vector<std::string> filter = {"txt"};
Path path( (getTestDirectory()/"0").string(), "", false);
getAllFiles(path, content, &filter);
BOOST_REQUIRE_EQUAL(content.size(), 1);
BOOST_REQUIRE_EQUAL(content[0].getFullPath(), (getTestDirectory()/"0"/"1.txt").string());
}
END_UTIL_FS_TEST_CASE

View File

@ -1,38 +0,0 @@
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
// interface: void getAllFolders(Path path, std::vector<Path> &outList)
#define TEST_DIR "getAllFolders"
#include "testFunctions.cpp"
#include "Common.h"
#include "util/UtilFs.h"
#include "util/UtilFsPath.h"
using namespace UTIL::FS;
START_UTIL_FS_TEST_CASE
BOOST_AUTO_TEST_CASE( getAllFolders1 )
{
Path path( getTestDirectory().string(), "", false);
std::vector<Path> content;
getAllFolders(path, content);
BOOST_REQUIRE_EQUAL( content.size(), 1 );
BOOST_REQUIRE_EQUAL( content[0].getFullPath(), (getTestDirectory()/"0").string() );
}
BOOST_AUTO_TEST_CASE( getAllFolder2)
{
Path path( (getTestDirectory()/"0").string(), "", false);
std::vector<Path> content;
getAllFolders(path, content);
BOOST_REQUIRE_EQUAL( content.size(), 0 );
}
END_UTIL_FS_TEST_CASE

View File

@ -1,29 +0,0 @@
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include "Common.h"
#include "util/UtilFs.h"
using namespace UTIL::STRING;
typedef std::string String;
BOOST_AUTO_TEST_CASE( sanitizeFilePath_slash1 )
{
String str = "/hi/ho/hihihi\\";
BOOST_REQUIRE_EQUAL("/hi/ho/hihihi/", sanitizeFilePath(str, '/'));
}
BOOST_AUTO_TEST_CASE( sanitizeFilePath_slash_with_dot )
{
String str = "/hi/./ho/hihihi\\";
BOOST_REQUIRE_EQUAL("/hi/./ho/hihihi/", sanitizeFilePath(str, '/'));
}
BOOST_AUTO_TEST_CASE( sanitizeFilePath_slash_with_dots )
{
String str = "/hi/../ho/hihihi\\";
BOOST_REQUIRE_EQUAL("/hi/../ho/hihihi/", sanitizeFilePath(str, '/'));
}