forked from approvals/ApprovalTests.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocTestDocsSamples.cpp
143 lines (129 loc) · 3.86 KB
/
DocTestDocsSamples.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "doctest/doctest.h"
#include "ApprovalTests/Approvals.h"
#include "ApprovalTests/namers/NamerFactory.h"
#include <vector>
#include "ApprovalTests/reporters/AutoApproveIfMissingReporter.h"
#include "ApprovalTests/utilities/ExceptionCollector.h"
enum Nationality
{
British,
American,
French
};
struct Greeting
{
Nationality nationality;
explicit Greeting() : nationality(British)
{
}
explicit Greeting(Nationality nationality_) : nationality(nationality_)
{
}
std::string getGreeting() const
{
return getGreetingFor(nationality);
}
std::string getGreetingFor(Nationality aNationality) const
{
switch (aNationality)
{
case British:
return "Cheers";
case American:
return "Howdy";
case French:
return "Bonjour";
default:
return "Unknown";
}
}
std::string getNationality() const
{
switch (nationality)
{
case British:
return "British";
case American:
return "American";
case French:
return "French";
default:
return "Unknown";
}
}
};
// begin-snippet: doctest_multiple_output_files_hard_coded
TEST_CASE("MultipleOutputFiles-ForOneObject")
{
using namespace ApprovalTests;
Greeting object_under_test;
SUBCASE("British")
{
Approvals::verify(object_under_test.getGreetingFor(British));
}
SUBCASE("American")
{
Approvals::verify(object_under_test.getGreetingFor(American));
}
SUBCASE("French")
{
Approvals::verify(object_under_test.getGreetingFor(French));
}
}
// end-snippet
// begin-snippet: approvals_multiple_output_files_dynamic
TEST_CASE("ApprovalTests-MultipleOutputFiles-DataDriven")
{
using namespace ApprovalTests;
// This is an example of how to write multiple different files in a single test.
// Note: For data as small as this, in practice we would recommend passing the
// greetings container in to Approvals::verifyAll(), with a lambda to format the output,
// in order to write all data to a single file.
std::vector<Greeting> greetings{
Greeting(British), Greeting(American), Greeting(French)};
for (auto greeting : greetings)
{
auto section = NamerFactory::appendToOutputFilename(greeting.getNationality());
Approvals::verify(greeting.getGreeting());
}
}
// end-snippet
// begin-snippet: approvals_multiple_output_files_hard_coded
TEST_CASE("ApprovalTests-MultipleOutputFiles-ForOneObject")
{
using namespace ApprovalTests;
Greeting object_under_test;
{
auto section = NamerFactory::appendToOutputFilename("British");
Approvals::verify(object_under_test.getGreetingFor(British));
}
{
auto section = NamerFactory::appendToOutputFilename("American");
Approvals::verify(object_under_test.getGreetingFor(American));
}
{
auto section = NamerFactory::appendToOutputFilename("French");
Approvals::verify(object_under_test.getGreetingFor(French));
}
}
// end-snippet
// begin-snippet: approvals_multiple_output_files_auto_approving
TEST_CASE("ApprovalTests-MultipleOutputFiles-AutoApprove")
{
using namespace ApprovalTests;
ExceptionCollector exceptions; // Allow all files to be written, regardless of errors
std::vector<Greeting> greetings{
Greeting(British), Greeting(American), Greeting(French)};
for (auto greeting : greetings)
{
auto section = NamerFactory::appendToOutputFilename(greeting.getNationality());
exceptions.gather([&]() {
Approvals::verify(
greeting.getGreeting(),
Options(
AutoApproveIfMissingReporter())); // Automatically approve first time
});
}
exceptions.release();
}
// end-snippet