123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-15
- * Changed: 2022-05-20
- *
- * */
- #include <gtest/gtest.h>
- #include <ls_std/ls_std_core.hpp>
- #include <ls_std/ls_std_io.hpp>
- #include "TestHelper.hpp"
- namespace
- {
- class FileTest : public ::testing::Test
- {
- protected:
- FileTest() = default;
- ~FileTest() override = default;
- ::std::string fileLocation = ls_std_test::TestHelper::getResourcesFolderLocation() + "simple.txt";
- void SetUp() override
- {}
- void TearDown() override
- {}
- };
- // comparison operators
- TEST_F(FileTest, operator_equals)
- {
- ls::std::io::File file{this->fileLocation};
- ls::std::io::File file2{this->fileLocation};
- ls::std::io::File file3{"/home/bla/text.txt"};
- ls::std::io::File file4{"\\home/bla\\text.txt"};
- ASSERT_TRUE(file == file2);
- ASSERT_TRUE(file2 == file);
- ASSERT_TRUE(file3 == file4);
- ASSERT_TRUE(file4 == file3);
- }
- TEST_F(FileTest, operator_not_equals)
- {
- ls::std::io::File file{this->fileLocation};
- ls::std::io::File file2{ls_std_test::TestHelper::getResourcesFolderLocation() + "app.exe"};
- ASSERT_TRUE(file != file2);
- ASSERT_TRUE(file2 != file);
- }
- // additional functionality
- TEST_F(FileTest, canExecute)
- {
- #ifdef _WIN32
- ls::std::io::File executableFile{ls_std_test::TestHelper::getResourcesFolderLocation() + "app.exe"};
- #endif
- #if defined(unix) || defined(__APPLE__)
- ls::std::io::File executableFile{ls_std_test::TestHelper::getResourcesFolderLocation() + "app"};
- #endif
- ASSERT_TRUE(executableFile.canExecute());
- }
- TEST_F(FileTest, canExecute_not_executable)
- {
- ls::std::io::File file{this->fileLocation};
- ASSERT_FALSE(file.canExecute());
- }
- TEST_F(FileTest, canRead)
- {
- ls::std::io::File readableFile{this->fileLocation};
- ASSERT_TRUE(readableFile.canRead());
- }
- TEST_F(FileTest, canRead_file_does_not_exist)
- {
- ls::std::io::File file{""};
- EXPECT_THROW({
- try
- {
- file.canRead();
- }
- catch (const ls::std::core::FileOperationException &_exception)
- {
- throw;
- }
- }, ls::std::core::FileOperationException);
- }
- TEST_F(FileTest, canWrite)
- {
- ls::std::io::File readableFile{this->fileLocation};
- ASSERT_TRUE(readableFile.canWrite());
- }
- TEST_F(FileTest, canWrite_not_writable)
- {
- #if defined(unix) || defined(__APPLE__)
- ls::std::io::File noWritableFile{ls_std_test::TestHelper::getResourcesFolderLocation() + "no_writable.txt"};
- ASSERT_FALSE(noWritableFile.canWrite());
- #endif
- #ifdef _WIN32
- ls::std::io::File noWritableFile{ls_std_test::TestHelper::getResourcesFolderLocation() + "no_writable_windows.txt"};
- ASSERT_FALSE(noWritableFile.canWrite());
- #endif
- }
- TEST_F(FileTest, createNewFile)
- {
- ls::std::io::File file{ls_std_test::TestHelper::getResourcesFolderLocation() + "tmp.txt"};
- ASSERT_FALSE(file.exists());
- file.createNewFile();
- ASSERT_TRUE(file.exists());
- file.remove();
- ASSERT_FALSE(file.exists());
- }
- TEST_F(FileTest, createNewFile_file_does_already_exist)
- {
- ls::std::io::File file{ls_std_test::TestHelper::getResourcesFolderLocation() + "simple.txt"};
- EXPECT_THROW({
- try
- {
- file.createNewFile();
- }
- catch (const ls::std::core::FileOperationException &_exception)
- {
- throw;
- }
- }, ls::std::core::FileOperationException);
- }
- TEST_F(FileTest, exists)
- {
- ls::std::io::File file{this->fileLocation};
- ls::std::io::File directory{ls_std_test::TestHelper::getResourcesFolderLocation()};
- ASSERT_TRUE(file.exists());
- ASSERT_TRUE(directory.exists());
- }
- TEST_F(FileTest, exists_does_not_exist)
- {
- ls::std::io::File file{ls_std_test::TestHelper::getResourcesFolderLocation() + "bla.txt"};
- ASSERT_FALSE(file.exists());
- }
- TEST_F(FileTest, getAbsoluteFilePath)
- {
- ls::std::io::File file{this->fileLocation};
- ASSERT_STREQ(this->fileLocation.c_str(), file.getAbsoluteFilePath().c_str());
- ::std::string s = {ls::std::io::FilePathSeparator::get()};
- ::std::string wrongFilePath = "home" + s + s + s + "user" + s + "bla" + s + s + "sub_folder";
- ::std::string expectedFilePath = "home" + s + "user" + s + "bla" + s + "sub_folder";
- ls::std::io::File anotherFile{wrongFilePath};
- ASSERT_STREQ(expectedFilePath.c_str(), anotherFile.getAbsoluteFilePath().c_str());
- }
- TEST_F(FileTest, getName)
- {
- ls::std::io::File file{this->fileLocation};
- ls::std::io::File executableFile{ls_std_test::TestHelper::getResourcesFolderLocation() + "app.exe"};
- ls::std::io::File anotherFile{"bla.txt"};
- ls::std::io::File directory{ls_std_test::TestHelper::getResourcesFolderLocation()};
- ASSERT_STREQ("simple.txt", file.getName().c_str());
- ASSERT_STREQ("app.exe", executableFile.getName().c_str());
- ASSERT_STREQ("bla.txt", anotherFile.getName().c_str());
- ASSERT_STREQ("resources", directory.getName().c_str());
- }
- TEST_F(FileTest, getParent)
- {
- ls::std::io::File file{this->fileLocation};
- ASSERT_STREQ(ls_std_test::TestHelper::getResourcesFolderLocation().c_str(), file.getParent().c_str());
- }
- TEST_F(FileTest, getWorkingDirectory)
- {
- ::std::string workingDirectory = ls::std::io::File::getWorkingDirectory();
- ASSERT_FALSE(workingDirectory.empty());
- }
- TEST_F(FileTest, getSize)
- {
- ls::std::io::File file{this->fileLocation};
- size_t size = file.getSize();
- ASSERT_TRUE(size == 7 || size == 8); // different OS specific new lines
- }
- TEST_F(FileTest, isDirectory)
- {
- ls::std::io::File directory{ls_std_test::TestHelper::getResourcesFolderLocation()};
- ASSERT_TRUE(directory.isDirectory());
- }
- TEST_F(FileTest, isDirectory_is_a_file)
- {
- ls::std::io::File file{this->fileLocation};
- ASSERT_FALSE(file.isDirectory());
- }
- TEST_F(FileTest, isFile)
- {
- const char separator = ls::std::io::FilePathSeparator::get();
- ls::std::io::File file{this->fileLocation};
- ASSERT_TRUE(file.isFile());
- ls::std::io::File file2{ls_std_test::TestHelper::getResourcesFolderLocation() + "list_test" + separator + "bla.txt"};
- ASSERT_TRUE(file2.isFile());
- }
- TEST_F(FileTest, isFile_is_a_directory)
- {
- ls::std::io::File directory{ls_std_test::TestHelper::getResourcesFolderLocation()};
- ASSERT_FALSE(directory.isFile());
- }
- TEST_F(FileTest, lastModified)
- {
- ls::std::io::File file{this->fileLocation};
- ASSERT_TRUE(file.lastModified() > 1590000000);
- }
- TEST_F(FileTest, list)
- {
- ls::std::io::File file{ls_std_test::TestHelper::getResourcesFolderLocation() + "list_test"};
- ::std::list<std::string> filesInDirectory = file.list();
- ::std::string expectedFile{};
- const char separator = ls::std::io::FilePathSeparator::get();
- auto filesIterator = filesInDirectory.begin();
- ASSERT_FALSE(filesInDirectory.empty());
- ASSERT_EQ(7, filesInDirectory.size());
- expectedFile = file.getAbsoluteFilePath() + separator + ".";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + "..";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + "list_test_sub";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- }
- TEST_F(FileTest, listFiles)
- {
- ls::std::io::File file{ls_std_test::TestHelper::getResourcesFolderLocation() + "list_test"};
- ::std::list<::std::string> filesInDirectory = file.listFiles();
- ::std::string expectedFile{};
- const char separator = ls::std::io::FilePathSeparator::get();
- ASSERT_FALSE(filesInDirectory.empty());
- ASSERT_EQ(4, filesInDirectory.size());
- expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
- ASSERT_TRUE((ls::std::core::STLUtils::contains(filesInDirectory, expectedFile)));
- }
- TEST_F(FileTest, makeDirectory)
- {
- ls::std::io::File directory{ls_std_test::TestHelper::getResourcesFolderLocation() + "testDir"};
- ASSERT_FALSE(directory.exists());
- directory.makeDirectory();
- ASSERT_TRUE(directory.exists());
- directory.remove();
- ASSERT_FALSE(directory.exists());
- }
- TEST_F(FileTest, makeDirectory_directory_already_exists)
- {
- ls::std::io::File directory{ls_std_test::TestHelper::getResourcesFolderLocation() + "list_test"};
- EXPECT_THROW({
- try
- {
- directory.makeDirectory();
- }
- catch (const ls::std::core::FileOperationException &_exception)
- {
- throw;
- }
- }, ls::std::core::FileOperationException);
- }
- TEST_F(FileTest, makeDirectories)
- {
- ls::std::io::File directory{ls_std_test::TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp/bla"};
- ASSERT_FALSE(directory.exists());
- directory.makeDirectories();
- ASSERT_TRUE(directory.exists());
- // clean up
- directory.remove();
- directory = ls::std::io::File(ls_std_test::TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp");
- directory.remove();
- directory = ls::std::io::File(ls_std_test::TestHelper::getResourcesFolderLocation() + "testDir/sub");
- directory.remove();
- directory = ls::std::io::File(ls_std_test::TestHelper::getResourcesFolderLocation() + "testDir");
- directory.remove();
- }
- TEST_F(FileTest, remove)
- {
- ::std::string fileName = ls_std_test::TestHelper::getResourcesFolderLocation() + "removable_file.txt";
- ls::std::io::File file{fileName};
- file.createNewFile();
- ASSERT_TRUE(file.exists());
- file.remove();
- ASSERT_FALSE(file.exists());
- }
- TEST_F(FileTest, renameTo)
- {
- ::std::string currentName = ls_std_test::TestHelper::getResourcesFolderLocation() + "tmp_rename_to.txt";
- ::std::string newName = ls_std_test::TestHelper::getResourcesFolderLocation() + "tmp_rename_to_better_name.txt";
- ls::std::io::File file{currentName};
- file.createNewFile();
- ASSERT_TRUE(file.exists());
- ASSERT_STREQ(currentName.c_str(), file.getAbsoluteFilePath().c_str());
- file.renameTo(newName);
- ASSERT_TRUE(file.exists());
- ASSERT_STREQ(newName.c_str(), file.getAbsoluteFilePath().c_str());
- file.remove();
- }
- TEST_F(FileTest, reset)
- {
- ls::std::io::File file{this->fileLocation};
- ASSERT_TRUE(file.exists());
- file.reset(ls_std_test::TestHelper::getResourcesFolderLocation() + "list_test/hello.txt");
- ASSERT_TRUE(file.exists());
- }
- }
|