forked from approvals/ApprovalTests.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrubberTests.cpp
96 lines (86 loc) · 2.81 KB
/
ScrubberTests.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "doctest/doctest.h"
#include "ApprovalTests/scrubbers/Scrubbers.h"
#include <iostream> // needed to fix linker error on XCode Release builds
#include "ApprovalTests/Approvals.h"
#include <cstdlib>
namespace
{
int random(int max)
{
return std::rand() % max;
}
}
TEST_CASE("createRegexScrubber")
{
// begin-snippet: complex_regex_scrubbing
auto input = "1) Hello 1234 World";
auto scrubber = ApprovalTests::Scrubbers::createRegexScrubber(
std::regex(R"(\d+)"), [](const auto& match) {
auto match_text = match.str();
auto match_integer = std::stoi(match_text);
if (match_integer < 10)
{
return match_text;
}
else
{
return std::string("[number]");
}
});
// end-snippet
ApprovalTests::Approvals::verify(input, ApprovalTests::Options(scrubber));
}
TEST_CASE("createRegexScrubber with fixed result")
{
using namespace ApprovalTests;
auto input = "Hello 1234 World";
auto scrubber = Scrubbers::createRegexScrubber(std::regex(R"(\d+)"), "number");
Approvals::verify(input, Options(scrubber));
}
TEST_CASE("createRegexScrubber with string input and fixed result")
{
// begin-snippet: simple_regex_scrubbing
using namespace ApprovalTests;
std::stringstream os;
os << "Hello " << random(1000) << " World";
Approvals::verify(os.str(),
Options(Scrubbers::createRegexScrubber(R"(\d+)", "[number]")));
// end-snippet
}
TEST_CASE("regex scrubber")
{
using namespace ApprovalTests;
auto input = "9012 Hello 1234 World 1234 5678";
auto scrubber =
Scrubbers::scrubRegex(input, std::regex(R"(\d\d\d\d)"), [](const auto& match) {
return "number(" + match.str() + ")\n";
});
Approvals::verify(scrubber);
}
TEST_CASE("regex scrubber with full customisation")
{
auto input = "9012 Hello 1234 World 1234 5678";
ApprovalTests::Scrubber scrubber = [](const std::string& text) {
return ApprovalTests::Scrubbers::scrubRegex(
text, std::regex(R"(\d\d\d\d)"), [](const auto& match) {
return "number(" + match.str() + ")\n";
});
};
// begin-snippet: scrubber_in_options_object
ApprovalTests::Approvals::verify(input,
ApprovalTests::Options().withScrubber(scrubber));
// end-snippet
}
TEST_CASE("regex scrubber with empty input does not loop endlessly")
{
using namespace ApprovalTests;
const char* input = "hello";
{
auto scrubber = Scrubbers::createRegexScrubber("", "[replacement]");
CHECK(scrubber(input) == input);
}
{
auto scrubber = Scrubbers::createRegexScrubber("R()", "[replacement]");
CHECK(scrubber(input) == input);
}
}