-
Notifications
You must be signed in to change notification settings - Fork 51
/
OverviewExamples.cpp
111 lines (99 loc) · 2.47 KB
/
OverviewExamples.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
#include "doctest/doctest.h"
#include "ApprovalTests/Approvals.h"
#include <sstream>
#include <string>
#include <vector>
using namespace ApprovalTests;
class StringList
{
public:
bool contains(const std::string& value) const
{
auto iter = std::find(contents.begin(), contents.end(), value);
return (iter != contents.end());
}
std::string toString() const
{
std::stringstream os;
bool written = false;
os << "[";
for (const auto& thing : contents)
{
if (written)
{
os << ", ";
}
else
{
written = true;
}
os << '"' << thing << '"';
}
os << "]";
return os.str();
}
public:
using Contents = std::vector<std::string>;
Contents contents;
};
class Sandwich
{
public:
std::string getBread() const
{
return bread;
}
StringList getCondiments() const
{
return condiments;
}
StringList getFillings() const
{
return fillings;
}
public:
std::string bread;
StringList condiments;
StringList fillings;
};
static Sandwich createSandwichForTest()
{
Sandwich result;
result.bread = "Sourdough";
result.condiments.contents = {"Mayo", "Pepper", "Olive Oil"};
result.fillings.contents = {"Tomato", "Lettuce", "Cheddar"};
return result;
}
std::ostream& operator<<(std::ostream& os, const Sandwich& sandwich)
{
os << "sandwich {\n";
os << " bread: \"" << sandwich.bread << "\",\n";
os << " condiments: " << sandwich.condiments.toString() << ",\n";
os << " fillings: " << sandwich.fillings.toString() << "\n";
os << "}";
return os;
}
TEST_CASE("SandwichExampleWithRequires")
{
// begin-snippet: sandwich_example_with_requires
// Arrange, Act
Sandwich s = createSandwichForTest();
// Assert
REQUIRE("Sourdough" == s.getBread());
REQUIRE(s.getCondiments().contains("Mayo"));
REQUIRE(s.getCondiments().contains("Pepper"));
REQUIRE(s.getCondiments().contains("Olive Oil"));
REQUIRE(s.getFillings().contains("Tomato"));
REQUIRE(s.getFillings().contains("Lettuce"));
REQUIRE(s.getFillings().contains("Cheddar"));
// end-snippet
}
TEST_CASE("SandwichExampleWithApprovals")
{
// begin-snippet: sandwich_example_with_approvals
// Arrange, Act
Sandwich s = createSandwichForTest();
// Assert
Approvals::verify(s);
// end-snippet
}