-
Notifications
You must be signed in to change notification settings - Fork 51
/
DocTestDocumentationSamples.cpp
112 lines (103 loc) · 3.1 KB
/
DocTestDocumentationSamples.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
#include "doctest.h"
#include "ApprovalTests/Approvals.h"
#include "ApprovalTests/namers/NamerFactory.h"
#include <vector>
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")
{
Greeting object_under_test;
SUBCASE("British")
{
ApprovalTests::Approvals::verify(object_under_test.getGreetingFor(British));
}
SUBCASE("American")
{
ApprovalTests::Approvals::verify(object_under_test.getGreetingFor(American));
}
SUBCASE("French")
{
ApprovalTests::Approvals::verify(object_under_test.getGreetingFor(French));
}
}
// end-snippet
// begin-snippet: approvals_multiple_output_files_dynamic
TEST_CASE("ApprovalTests-MultipleOutputFiles-DataDriven")
{
// 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 = ApprovalTests::NamerFactory::appendToOutputFilename(greeting.getNationality());
ApprovalTests::Approvals::verify(greeting.getGreeting());
}
}
// end-snippet
// begin-snippet: approvals_multiple_output_files_hard_coded
TEST_CASE("ApprovalTests-MultipleOutputFiles-ForOneObject")
{
Greeting object_under_test;
{
auto section = ApprovalTests::NamerFactory::appendToOutputFilename("British");
ApprovalTests::Approvals::verify(object_under_test.getGreetingFor(British));
}
{
auto section = ApprovalTests::NamerFactory::appendToOutputFilename("American");
ApprovalTests::Approvals::verify(object_under_test.getGreetingFor(American));
}
{
auto section = ApprovalTests::NamerFactory::appendToOutputFilename("French");
ApprovalTests::Approvals::verify(object_under_test.getGreetingFor(French));
}
}
// end-snippet