-
Notifications
You must be signed in to change notification settings - Fork 51
/
ToStringWrapperExample.cpp
78 lines (64 loc) · 1.93 KB
/
ToStringWrapperExample.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
#include "doctest/doctest.h"
#include "ApprovalTests/Approvals.h"
#include <ostream>
using namespace ApprovalTests;
struct Rectangle3
{
int x, y, width, height;
friend std::ostream& operator<<(std::ostream& os, const Rectangle3& rectangle)
{
os << "[x: " << rectangle.x << " y: " << rectangle.y
<< " width: " << rectangle.width << " height: " << rectangle.height << "]";
return os;
}
};
std::vector<Rectangle3> getRectangles()
{
return {
{4, 50, 100, 61},
{50, 5200, 400, 62},
{60, 3, 7, 63},
};
}
TEST_CASE("MultipleLinesCanBeHardToRead")
{
// begin-snippet: verify_list
Approvals::verifyAll("rectangles", getRectangles());
// end-snippet
}
// begin-snippet: to_string_wrapper_example
struct FormatRectangleForMultipleLines
{
explicit FormatRectangleForMultipleLines(const Rectangle3& rectangle_)
: rectangle(rectangle_)
{
}
const Rectangle3& rectangle;
friend std::ostream& operator<<(std::ostream& os,
const FormatRectangleForMultipleLines& wrapper)
{
os << "(x,y,width,height) = (" << wrapper.rectangle.x << ","
<< wrapper.rectangle.y << "," << wrapper.rectangle.width << ","
<< wrapper.rectangle.height << ")";
return os;
}
};
TEST_CASE("AlternativeFormattingCanBeEasyToRead")
{
Approvals::verifyAll("rectangles", getRectangles(), [](auto r, auto& os) {
os << FormatRectangleForMultipleLines(r);
});
}
// end-snippet
std::ostream& toStringForMultipleLines(std::ostream& os, const Rectangle3& rectangle)
{
os << "(x,y,width,height) = (" << rectangle.x << "," << rectangle.y << ","
<< rectangle.width << "," << rectangle.height << ")";
return os;
}
TEST_CASE("AlternativeFormattingCanBeEasyToRead2")
{
Approvals::verifyAll("rectangles", getRectangles(), [](auto r, auto& os) {
toStringForMultipleLines(os, r);
});
}