Skip to content

Commit

Permalink
F!! Created untested seam for EmptyFileCreator #172
Browse files Browse the repository at this point in the history
Co-Authored-By: Clare Macrae <[email protected]>
  • Loading branch information
isidore and claremacrae committed Aug 30, 2021
1 parent ffdf91b commit 419265e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ApprovalTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions ApprovalTests/utilities/EmptyFileCreatorDisposer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <sstream>
#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);
}
}
19 changes: 19 additions & 0 deletions ApprovalTests/utilities/EmptyFileCreatorDisposer.h
Original file line number Diff line number Diff line change
@@ -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();
};
}
15 changes: 15 additions & 0 deletions ApprovalTests/utilities/EmptyFileCreatorFactory.cpp
Original file line number Diff line number Diff line change
@@ -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;

}
14 changes: 14 additions & 0 deletions ApprovalTests/utilities/EmptyFileCreatorFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <functional>

namespace ApprovalTests
{
using EmptyFileCreator = std::function<void(std::string)>;

class EmptyFileCreatorFactory
{
public:
static void defaultCreator(std::string fullFilePath);
static EmptyFileCreator currentCreator;
};
}
4 changes: 2 additions & 2 deletions ApprovalTests/utilities/FileUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ApprovalTests/utilities/FileUtils.h"
#include "ApprovalTests/writers/StringWriter.h"
#include "EmptyFileCreatorFactory.h"
#include "SystemUtils.h"

#include <fstream>
Expand Down Expand Up @@ -35,8 +36,7 @@ namespace ApprovalTests
{
if (!fileExists(fullFilePath))
{
StringWriter s("", "");
s.write(fullFilePath);
EmptyFileCreatorFactory::currentCreator(fullFilePath);
}
}

Expand Down
10 changes: 9 additions & 1 deletion tests/DocTest_Tests/utilities/FileUtilsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {});

}


0 comments on commit 419265e

Please sign in to comment.