forked from approvals/ApprovalTests.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatch2DocsSamples.cpp
More file actions
97 lines (88 loc) · 2.22 KB
/
Catch2DocsSamples.cpp
File metadata and controls
97 lines (88 loc) · 2.22 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "catch2/catch.hpp"
#include "ApprovalTests/Approvals.h"
#include <vector>
using namespace ApprovalTests;
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: catch2_multiple_output_files_dynamic
TEST_CASE("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)
{
SECTION(greeting.getNationality())
{
Approvals::verify(greeting.getGreeting());
}
}
}
// end-snippet
// begin-snippet: catch2_multiple_output_files_hard_coded
TEST_CASE("MultipleOutputFiles-ForOneObject")
{
Greeting object_under_test;
SECTION("British")
{
Approvals::verify(object_under_test.getGreetingFor(British));
}
SECTION("American")
{
Approvals::verify(object_under_test.getGreetingFor(American));
}
SECTION("French")
{
Approvals::verify(object_under_test.getGreetingFor(French));
}
}
// end-snippet