diff --git a/ApprovalTests/CMakeLists.txt b/ApprovalTests/CMakeLists.txt index 41488aeb..8ca59245 100644 --- a/ApprovalTests/CMakeLists.txt +++ b/ApprovalTests/CMakeLists.txt @@ -128,6 +128,10 @@ add_library(${PROJECT_NAME} utilities/CartesianProduct.h utilities/DateUtils.cpp utilities/DateUtils.h + utilities/EmptyFileCreatorDisposer.cpp + utilities/EmptyFileCreatorDisposer.h + utilities/EmptyFileCreatorFactory.cpp + utilities/EmptyFileCreatorFactory.h utilities/ExceptionCollector.h utilities/ExceptionCollector.cpp utilities/FileUtils.h diff --git a/ApprovalTests/utilities/EmptyFileCreatorDisposer.cpp b/ApprovalTests/utilities/EmptyFileCreatorDisposer.cpp new file mode 100644 index 00000000..4cd7280d --- /dev/null +++ b/ApprovalTests/utilities/EmptyFileCreatorDisposer.cpp @@ -0,0 +1,17 @@ +#include +#include "EmptyFileCreatorDisposer.h" + +namespace ApprovalTests +{ + + EmptyFileCreatorDisposer::EmptyFileCreatorDisposer(EmptyFileCreator creator) + { + previous_result = std::move(EmptyFileCreatorFactory::currentCreator); + EmptyFileCreatorFactory::currentCreator = std::move(creator); + } + + EmptyFileCreatorDisposer::~EmptyFileCreatorDisposer() + { + EmptyFileCreatorFactory::currentCreator = std::move(previous_result); + } +} diff --git a/ApprovalTests/utilities/EmptyFileCreatorDisposer.h b/ApprovalTests/utilities/EmptyFileCreatorDisposer.h new file mode 100644 index 00000000..f17f5872 --- /dev/null +++ b/ApprovalTests/utilities/EmptyFileCreatorDisposer.h @@ -0,0 +1,19 @@ +#pragma once + +#include "ApprovalTests/utilities/Macros.h" +#include "EmptyFileCreatorFactory.h" + +namespace ApprovalTests +{ + class APPROVAL_TESTS_NO_DISCARD EmptyFileCreatorDisposer + { + private: + EmptyFileCreator previous_result; + + public: + explicit EmptyFileCreatorDisposer(EmptyFileCreator creator); + EmptyFileCreatorDisposer(const EmptyFileCreatorDisposer&) = default; + + ~EmptyFileCreatorDisposer(); + }; +} diff --git a/ApprovalTests/utilities/EmptyFileCreatorFactory.cpp b/ApprovalTests/utilities/EmptyFileCreatorFactory.cpp new file mode 100644 index 00000000..354fc06b --- /dev/null +++ b/ApprovalTests/utilities/EmptyFileCreatorFactory.cpp @@ -0,0 +1,15 @@ +#include "ApprovalTests/writers/StringWriter.h" +#include "EmptyFileCreatorFactory.h" +namespace ApprovalTests +{ + + void EmptyFileCreatorFactory::defaultCreator(std::string fullFilePath) + { + StringWriter s("", ""); + s.write(fullFilePath); + } + + EmptyFileCreator EmptyFileCreatorFactory::currentCreator = + EmptyFileCreatorFactory::defaultCreator; + +} diff --git a/ApprovalTests/utilities/EmptyFileCreatorFactory.h b/ApprovalTests/utilities/EmptyFileCreatorFactory.h new file mode 100644 index 00000000..f7a8423e --- /dev/null +++ b/ApprovalTests/utilities/EmptyFileCreatorFactory.h @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace ApprovalTests +{ + using EmptyFileCreator = std::function; + + class EmptyFileCreatorFactory + { + public: + static void defaultCreator(std::string fullFilePath); + static EmptyFileCreator currentCreator; + }; +} diff --git a/ApprovalTests/utilities/FileUtils.cpp b/ApprovalTests/utilities/FileUtils.cpp index 45d305a8..693917a2 100644 --- a/ApprovalTests/utilities/FileUtils.cpp +++ b/ApprovalTests/utilities/FileUtils.cpp @@ -1,5 +1,6 @@ #include "ApprovalTests/utilities/FileUtils.h" #include "ApprovalTests/writers/StringWriter.h" +#include "EmptyFileCreatorFactory.h" #include "SystemUtils.h" #include @@ -35,8 +36,7 @@ namespace ApprovalTests { if (!fileExists(fullFilePath)) { - StringWriter s("", ""); - s.write(fullFilePath); + EmptyFileCreatorFactory::currentCreator(fullFilePath); } } diff --git a/tests/DocTest_Tests/utilities/FileUtilsTests.cpp b/tests/DocTest_Tests/utilities/FileUtilsTests.cpp index 6edbc191..98ce0a1e 100644 --- a/tests/DocTest_Tests/utilities/FileUtilsTests.cpp +++ b/tests/DocTest_Tests/utilities/FileUtilsTests.cpp @@ -39,6 +39,14 @@ TEST_CASE("readFileReturnEmptyIfMissing() returns empty string if file is missin TEST_CASE("check impossible directories don't explode") { - CHECK(!FileUtils::fileExists( + CHECK(!FileUtils::fileExists( "../../../../../../../../../../../../../../../../../../../../../a.txt")); } + +TEST_CASE("Empty File Creation") +{ + //auto disposer = ApprovalTests::FileUtils::useEmptyFileCreator([](std::string fileName) {}); + +} + +