-
Notifications
You must be signed in to change notification settings - Fork 51
/
Approvals.h
347 lines (307 loc) · 12.6 KB
/
Approvals.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once
#include <string>
#include <functional>
#include <exception>
#include <utility>
#include "ApprovalTests/ApprovalsMacroDefaults.h"
#include "ApprovalTests/ApprovalTestsVersion.h"
#include "ApprovalTests/core/FileApprover.h"
#include "reporters/DefaultReporter.h"
#include "reporters/DefaultReporterDisposer.h"
#include "ApprovalTests/core/Reporter.h"
#include "namers/ApprovalTestNamer.h"
#include "writers/ExistingFile.h"
#include "namers/ExistingFileNamer.h"
#include "namers/SubdirectoryDisposer.h"
#include "namers/DefaultNamerDisposer.h"
#include "scrubbers/Scrubbers.h"
#include "core/Options.h"
#include "utilities/MoreHelpMessages.h"
#include "utilities/StringMaker.h"
namespace ApprovalTests
{
// TCompileTimeOptions must have a type ToStringConverter, which must have a method toString()
template <typename TCompileTimeOptions> class TApprovals
{
private:
TApprovals() = default;
~TApprovals() = default;
public:
static std::shared_ptr<ApprovalNamer> getDefaultNamer()
{
return DefaultNamerFactory::getDefaultNamer()();
}
static void verify(const std::string& contents,
const Options& options = Options())
{
StringWriter writer(options.scrub(contents),
options.fileOptions().getFileExtension());
FileApprover::verify(*getDefaultNamer(), writer, options.getReporter());
}
// Note that this overload ignores any scrubber in options
static void verify(const ApprovalWriter& writer,
const Options& options = Options())
{
FileApprover::verify(*getDefaultNamer(), writer, options.getReporter());
}
template <typename T>
using IsNotDerivedFromWriter =
typename std::enable_if<!std::is_base_of<ApprovalWriter, T>::value,
int>::type;
template <typename T, typename = IsNotDerivedFromWriter<T>>
static void verify(const T& contents, const Options& options = Options())
{
verify(TCompileTimeOptions::ToStringConverter::toString(contents), options);
}
template <typename T,
typename Function,
typename = Detail::EnableIfNotDerivedFromReporter<Function>>
static void
verify(const T& contents, Function converter, const Options& options = Options())
{
std::stringstream s;
converter(contents, s);
verify(s.str(), options);
}
static void
verifyExceptionMessage(const std::function<void(void)>& functionThatThrows,
const Options& options = Options())
{
std::string message = "*** no exception thrown ***";
try
{
functionThatThrows();
}
catch (const std::exception& e)
{
message = e.what();
}
verify(message, options);
}
template <typename Iterator>
static void verifyAll(
const std::string& header,
const Iterator& start,
const Iterator& finish,
std::function<void(typename Iterator::value_type, std::ostream&)> converter,
const Options& options = Options())
{
std::stringstream s;
if (!header.empty())
{
s << header << "\n\n\n";
}
for (auto it = start; it != finish; ++it)
{
converter(*it, s);
s << '\n';
}
verify(s.str(), options);
}
template <typename Container>
static void verifyAll(
const std::string& header,
const Container& list,
std::function<void(typename Container::value_type, std::ostream&)> converter,
const Options& options = Options())
{
verifyAll<typename Container::const_iterator>(
header, list.begin(), list.end(), converter, options);
}
template <typename T>
static void verifyAll(const std::string& header,
const std::vector<T>& list,
const Options& options = Options())
{
int i = 0;
verifyAll<std::vector<T>>(
header,
list,
[&](T e, std::ostream& s) {
s << "[" << i++
<< "] = " << TCompileTimeOptions::ToStringConverter::toString(e);
},
options);
}
template <typename T>
static void verifyAll(const std::vector<T>& list,
const Options& options = Options())
{
verifyAll<T>("", list, options);
}
// Note that this method ignores any scrubber in options
static void verifyExistingFile(const std::string& filePath,
const Options& options = Options())
{
ExistingFile writer(filePath);
ExistingFileNamer namer(filePath);
FileApprover::verify(namer, writer, options.getReporter());
}
/**@name Customising Approval Tests
These static methods customise various aspects
of Approval Tests behaviour.
*/
///@{
/// See \userguide{Configuration,using-sub-directories-for-approved-files,Using sub-directories for approved files}
static SubdirectoryDisposer
useApprovalsSubdirectory(const std::string& subdirectory = "approval_tests")
{
return SubdirectoryDisposer(subdirectory);
}
/// See \userguide{Reporters,registering-a-default-reporter,Registering a default reporter}
static DefaultReporterDisposer
useAsDefaultReporter(const std::shared_ptr<Reporter>& reporter)
{
return DefaultReporterDisposer(reporter);
}
/// See \userguide{Reporters,front-loaded-reporters,Front Loaded Reporters}
static FrontLoadedReporterDisposer
useAsFrontLoadedReporter(const std::shared_ptr<Reporter>& reporter)
{
return FrontLoadedReporterDisposer(reporter);
}
/// See \userguide{Namers,registering-a-custom-namer,Registering a Custom Namer}
static DefaultNamerDisposer useAsDefaultNamer(NamerCreator namerCreator)
{
return DefaultNamerDisposer(std::move(namerCreator));
}
///@}
#if !APPROVAL_TESTS_HIDE_DEPRECATED_CODE
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS
static void verify(std::string contents, const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verify(contents, Options(reporter));
}
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS
static void verifyWithExtension(std::string contents,
const std::string& fileExtensionWithDot,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verify(
contents,
Options(reporter).fileOptions().withFileExtension(fileExtensionWithDot));
}
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS
static void verify(const ApprovalWriter& writer, const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verify(writer, Options(reporter));
}
template <typename T, typename = IsNotDerivedFromWriter<T>>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void verify(const T& contents,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verify(TCompileTimeOptions::ToStringConverter::toString(contents), reporter);
}
template <typename T, typename = IsNotDerivedFromWriter<T>>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void
verifyWithExtension(const T& contents,
const std::string& fileExtensionWithDot,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verify(
contents,
Options(reporter).fileOptions().withFileExtension(fileExtensionWithDot));
}
template <typename T,
typename Function,
typename = Detail::EnableIfNotDerivedFromReporter<Function>>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void
verify(const T& contents, Function converter, const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verify(contents, converter, Options(reporter));
}
template <typename T,
typename Function,
typename = Detail::EnableIfNotDerivedFromReporter<Function>>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void
verifyWithExtension(const T& contents,
Function converter,
const std::string& fileExtensionWithDot,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verify(
contents,
converter,
Options(reporter).fileOptions().withFileExtension(fileExtensionWithDot));
}
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS
static void verifyExceptionMessage(std::function<void(void)> functionThatThrows,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verifyExceptionMessage(functionThatThrows, Options(reporter));
}
template <typename Iterator>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void verifyAll(
std::string header,
const Iterator& start,
const Iterator& finish,
std::function<void(typename Iterator::value_type, std::ostream&)> converter,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verifyAll(header, start, finish, converter, Options(reporter));
}
template <typename Container>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void verifyAll(
std::string header,
const Container& list,
std::function<void(typename Container::value_type, std::ostream&)> converter,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verifyAll(header, list, converter, Options(reporter));
}
template <typename T>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void verifyAll(
std::string header, const std::vector<T>& list, const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verifyAll(header, list, Options(reporter));
}
template <typename T>
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS static void
verifyAll(const std::vector<T>& list, const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verifyAll<T>(list, Options(reporter));
}
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS
static void verifyExistingFile(const std::string filePath,
const Reporter& reporter)
{
APPROVAL_TESTS_DEPRECATED_USE_OPTIONS_CPP11
verifyExistingFile(filePath, Options(reporter));
}
#endif
};
#ifndef APPROVAL_TESTS_DEFAULT_STREAM_CONVERTER
// begin-snippet: customising_to_string_default_converter
#define APPROVAL_TESTS_DEFAULT_STREAM_CONVERTER StringMaker
// end-snippet
#endif
// Warning: Do not use CompileTimeOptions directly.
// This interface is subject to change, as future
// compile-time options are added.
template <typename TToString> struct CompileTimeOptions
{
using ToStringConverter = TToString;
// more template types may be added to CompileTimeOptions in future, if we add
// more flexibility that requires compile-time configuration.
};
// Template parameter TToString must have a method toString()
// This interface will not change, as future compile-time options are added.
template <typename TToString>
struct ToStringCompileTimeOptions : CompileTimeOptions<TToString>
{
};
using Approvals =
TApprovals<ToStringCompileTimeOptions<APPROVAL_TESTS_DEFAULT_STREAM_CONVERTER>>;
}