diff options
| author | JosJuice <josjuice@gmail.com> | 2021-05-02 18:03:29 +0200 |
|---|---|---|
| committer | JosJuice <josjuice@gmail.com> | 2021-05-02 18:07:02 +0200 |
| commit | 1f6bb06f2cddb715df0d3f59068f16fe7f9a8caa (patch) | |
| tree | f7c1808bca4deedf9b0f4b4792f37309fd510d57 /Source/UnitTests/Common/FileUtilTest.cpp | |
| parent | ade9d6c954a4907962fc395fdb62a3badbafa576 (diff) | |
UnitTests: Move some unit tests to where they should be
Two unit tests were in the root UnitTests folder
and were not being built when using CMake.
Diffstat (limited to 'Source/UnitTests/Common/FileUtilTest.cpp')
| -rw-r--r-- | Source/UnitTests/Common/FileUtilTest.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/Source/UnitTests/Common/FileUtilTest.cpp b/Source/UnitTests/Common/FileUtilTest.cpp new file mode 100644 index 0000000000..bc07b753d5 --- /dev/null +++ b/Source/UnitTests/Common/FileUtilTest.cpp @@ -0,0 +1,103 @@ +// Copyright 2020 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include <array> +#include <gtest/gtest.h> + +#include "Common/FileUtil.h" + +class FileUtilTest : public testing::Test +{ +protected: + FileUtilTest() + : m_parent_directory(File::CreateTempDir()), m_file_path(m_parent_directory + "/file.txt"), + m_directory_path(m_parent_directory + "/dir"), + m_invalid_path(m_parent_directory + "/invalid.txt") + { + } + + ~FileUtilTest() override + { + if (!m_parent_directory.empty()) + { + File::DeleteDirRecursively(m_parent_directory); + } + } + + void SetUp() + { + if (m_parent_directory.empty()) + { + FAIL(); + } + } + + constexpr static std::array<File::IfAbsentBehavior, 2> s_warning_behaviors = { + File::IfAbsentBehavior::ConsoleWarning, + File::IfAbsentBehavior::NoConsoleWarning, + }; + + const std::string m_parent_directory; + const std::string m_file_path; + const std::string m_directory_path; + const std::string m_invalid_path; +}; + +static void DeleteShouldNotRemoveDirectory(const std::string& path, File::IfAbsentBehavior behavior) +{ + File::CreateDir(path); + EXPECT_FALSE(File::Delete(path, behavior)); + File::DeleteDir(path, behavior); +} + +static void DeleteShouldRemoveFile(const std::string& path, File::IfAbsentBehavior behavior) +{ + File::CreateEmptyFile(path); + EXPECT_TRUE(File::Delete(path, behavior)); +} + +static void DeleteShouldReturnTrueForInvalidPath(const std::string& path, + File::IfAbsentBehavior behavior) +{ + EXPECT_TRUE(File::Delete(path, behavior)); +} + +static void DeleteDirShouldRemoveDirectory(const std::string& path, File::IfAbsentBehavior behavior) +{ + File::CreateDir(path); + EXPECT_TRUE(File::DeleteDir(path, behavior)); +} + +static void DeleteDirShouldNotRemoveFile(const std::string& path, File::IfAbsentBehavior behavior) +{ + File::CreateEmptyFile(path); + EXPECT_FALSE(File::DeleteDir(path, behavior)); + File::Delete(path, behavior); +} + +static void DeleteDirShouldReturnTrueForInvalidPath(const std::string& path, + File::IfAbsentBehavior behavior) +{ + EXPECT_TRUE(File::DeleteDir(path, behavior)); +} + +TEST_F(FileUtilTest, Delete) +{ + for (const auto behavior : s_warning_behaviors) + { + DeleteShouldNotRemoveDirectory(m_directory_path, behavior); + DeleteShouldRemoveFile(m_file_path, behavior); + DeleteShouldReturnTrueForInvalidPath(m_invalid_path, behavior); + } +} + +TEST_F(FileUtilTest, DeleteDir) +{ + for (const auto behavior : s_warning_behaviors) + { + DeleteDirShouldRemoveDirectory(m_directory_path, behavior); + DeleteDirShouldNotRemoveFile(m_file_path, behavior); + DeleteDirShouldReturnTrueForInvalidPath(m_invalid_path, behavior); + } +} |
