forked from approvals/ApprovalTests.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileApproverTests.cpp
More file actions
83 lines (69 loc) · 2.5 KB
/
FileApproverTests.cpp
File metadata and controls
83 lines (69 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "doctest/doctest.h"
#include "ApprovalTests/writers/StringWriter.h"
#include "../../DocTest_Tests/reporters/TestReporter.h"
#include "ApprovalTests/namers/ApprovalTestNamer.h"
#include "ApprovalTests/core/ApprovalException.h"
#include "ApprovalTests/core/FileApprover.h"
#include "ApprovalTests/Approvals.h"
using namespace ApprovalTests;
TEST_CASE("ItVerifiesApprovedFileExists")
{
ApprovalTestNamer namer;
StringWriter writer("Hello");
TestReporter reporter;
std::string approved = namer.getApprovedFile(".txt");
std::string received = namer.getReceivedFile(".txt");
std::string expected = "Failed Approval: \n"
"Approval File Not Found \n"
"File: \"" +
approved + "\"";
REQUIRE_THROWS_MESSAGE(FileApprover::verify(namer, writer, reporter), expected);
remove(approved.c_str());
remove(received.c_str());
}
TEST_CASE("ItVerifiesExistingFiles")
{
ApprovalTestNamer namer;
Approvals::verifyExistingFile(namer.getDirectory() + "../../sample.txt");
Approvals::verifyExistingFile(namer.getDirectory() + "../../sample.txt", Options());
}
TEST_CASE("ItScrubsExistingFiles")
{
ApprovalTestNamer namer;
Approvals::verifyExistingFile(
namer.getDirectory() + "../../sample.txt",
Options(Scrubbers::createRegexScrubber("hired", "FIRED")));
}
TEST_CASE("ItIgnoresLineEndingDifferences")
{
FileUtils::writeToFile("a.txt", "1\r\n2\n3\r\n4\r\n5");
FileUtils::writeToFile("b.txt", "1\n2\r\n3\r\n4\n5");
FileApprover::verify("a.txt", "b.txt");
}
TEST_CASE("ItComparesTheEntireFile")
{
FileUtils::writeToFile("a.txt", "12345");
FileUtils::writeToFile("b.txt", "123");
CHECK_THROWS_AS(FileApprover::verify("a.txt", "b.txt"), ApprovalMismatchException);
}
// begin-snippet: create_custom_comparator
class LengthComparator : public ApprovalComparator
{
public:
bool contentsAreEquivalent(std::string receivedPath,
std::string approvedPath) const override
{
return FileUtils::fileSize(receivedPath) == FileUtils::fileSize(approvedPath);
}
};
// end-snippet
TEST_CASE("ItUsesCustomComparator")
{
FileUtils::writeToFile("a.length", "12345");
FileUtils::writeToFile("b.length", "56789");
// begin-snippet: use_custom_comparator
auto disposer = FileApprover::registerComparatorForExtension(
".length", std::make_shared<LengthComparator>());
// end-snippet
FileApprover::verify("a.length", "b.length");
}