-
Notifications
You must be signed in to change notification settings - Fork 51
/
CustomizingToStringTests.cpp
121 lines (104 loc) · 3.55 KB
/
CustomizingToStringTests.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
#include "doctest/doctest.h"
#include "ApprovalTests/utilities/StringMaker.h"
#include "ApprovalTests/Approvals.h"
#include "ApprovalTests/CombinationApprovals.h"
#include <ostream>
using namespace ApprovalTests;
//----------------------------------------------------------------------------------------
// operator<<(std::ostream&)
struct OStreamPrintable
{
int field1_ = 0;
// begin-snippet: customising_to_string_with_ostream_operator
friend std::ostream& operator<<(std::ostream& os, const OStreamPrintable& printable)
{
os << "From ostream: " << printable.field1_ << '\n';
return os;
}
// end-snippet
};
TEST_CASE("Test toString from ostream operator")
{
OStreamPrintable p;
Approvals::verify(p);
}
//----------------------------------------------------------------------------------------
// StringMaker
struct StringMakerPrintable
{
int field1_ = 0;
};
#if defined(__GNUC__) && __GNUC__ < 7
// begin-snippet: customising_to_string_with_string_maker_specialization_gcc5_and_6
namespace ApprovalTests
{
template <> std::string StringMaker::toString(const StringMakerPrintable& printable)
{
return "From StringMaker: " + std::to_string(printable.field1_);
}
}
// end-snippet
#else
// begin-snippet: customising_to_string_with_string_maker_specialization
template <>
std::string ApprovalTests::StringMaker::toString(const StringMakerPrintable& printable)
{
return "From StringMaker: " + std::to_string(printable.field1_);
}
// end-snippet
#endif
TEST_CASE("Test toString from StringMaker")
{
StringMakerPrintable p;
Approvals::verify(p);
}
TEST_CASE("Test Combinations inputs from StringMaker")
{
std::vector<StringMakerPrintable> inputs1{StringMakerPrintable(),
StringMakerPrintable()};
std::vector<StringMakerPrintable> inputs2{StringMakerPrintable()};
ApprovalTests::CombinationApprovals::verifyAllCombinations(
[](auto /*input1*/, auto /*input2*/) { return "placeholder"; }, inputs1, inputs2);
}
//----------------------------------------------------------------------------------------
// Template
struct TemplatePrintable
{
int field1_ = 0;
};
// begin-snippet: customising_to_string_with_custom_to_string_class
class CustomToStringClass
{
public:
template <typename T> static std::string toString(const T& printable)
{
return "From Template: " + std::to_string(printable.field1_);
}
};
// end-snippet
TEST_CASE("Test toString from Template")
{
TemplatePrintable p;
// begin-snippet: customising_to_string_with_custom_to_string_class_usage1
ApprovalTests::TApprovals<
ApprovalTests::ToStringCompileTimeOptions<CustomToStringClass>>::verify(p);
// end-snippet
}
TEST_CASE("Test toString from Template and using")
{
TemplatePrintable p;
// begin-snippet: customising_to_string_with_custom_to_string_class_usage2
using MyApprovals = ApprovalTests::TApprovals<
ApprovalTests::ToStringCompileTimeOptions<CustomToStringClass>>;
MyApprovals::verify(p);
// end-snippet
}
TEST_CASE("Test Combinations inputs from StringMaker - with CustomToStringClass")
{
std::vector<TemplatePrintable> inputs1{TemplatePrintable(), TemplatePrintable()};
std::vector<TemplatePrintable> inputs2{TemplatePrintable()};
using MyCombinationApprovals = ApprovalTests::TCombinationApprovals<
ApprovalTests::ToStringCompileTimeOptions<CustomToStringClass>>;
MyCombinationApprovals::verifyAllCombinations(
[](auto /*input1*/, auto /*input2*/) { return "placeholder"; }, inputs1, inputs2);
}