-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathUTApprovalTestTests.cpp
101 lines (87 loc) · 3.35 KB
/
UTApprovalTestTests.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
// begin-snippet: ut_main
#define APPROVALS_UT
#include "ApprovalTests.hpp"
// end-snippet
#include "tests/DocTest_Tests/reporters/FakeReporter.h"
int main()
{
using namespace boost::ut;
using namespace ApprovalTests;
auto directory = Approvals::useApprovalsSubdirectory("approval_tests");
"ItReportsAndThrowsIfVerifyFails"_test = []() {
auto front_loader = std::make_shared<FakeReporter>(true);
// Use a front-loaded reporter so this is used even we are running
// in a CI system.
auto front_loaded_reporter_disposer =
Approvals::useAsFrontLoadedReporter(front_loader);
bool approvalMissingExceptionReceived = false;
try
{
Approvals::verify("cucumber");
}
catch (const ApprovalMissingException&)
{
approvalMissingExceptionReceived = true;
}
catch (const std::exception& e)
{
expect(approvalMissingExceptionReceived == true_b)
<< "Unexpected exception received: " << e.what();
}
// If these start failing, run the tests with verbose on,
// or ctest's --output-on-failure.
// You will probably find that Boost.UT is unable to find the
// name of the source file.
// For ways to fix this, see:
// https://github.com/approvals/ApprovalTests.cpp/blob/master/doc/TroubleshootingMisconfiguredBuild.md
expect(approvalMissingExceptionReceived == true_b);
expect(front_loader->called == true_b);
};
// begin-snippet: ut_main_usage
"ItCanVerifyAFile"_test = []() {
Approvals::verify(
"Approval Tests can verify text via the golden master method");
};
// end-snippet
test("AnotherWayItCanVerifyAFile") = []() {
Approvals::verify(
"Approval Tests can verify text via the golden master method");
};
// begin-snippet: ut_main_multiple
"ItCanUseMultipleVerify"_test = []() {
{
// Here we simulate test sections, so that Approval Tests uses different
// output file names for the different verify() calls.
auto section = NamerFactory::appendToOutputFilename("section 1");
Approvals::verify(
"Approval Tests can verify text via the golden master method");
}
{
auto section = NamerFactory::appendToOutputFilename("section 2");
Approvals::verify("Approval Tests can verify different text via "
"the golden master method");
}
};
// end-snippet
"YouCanUseAWriter"_test = []() {
// begin-snippet: ut_use_custom_writer
using HtmlWriter = StringWriter;
HtmlWriter writer("<h1>hello world</h1>", ".html");
Approvals::verify(writer);
// end-snippet
};
"YouCanSpecifyYourFileExtension"_test = []() {
// begin-snippet: ut_use_custom_file_extension
Approvals::verifyWithExtension("<h1>hello world</h1>", ".html");
// end-snippet
};
"YouCanSpecifyYourFileExtensionWithToString"_test = []() {
Approvals::verifyWithExtension(1337, ".csv");
};
"YouCanSpecifyYourFileExtensionWithFormatter"_test = []() {
Approvals::verifyWithExtension(
1337,
[](auto value, auto& os) { os << "**value:** " << value; },
".md");
};
}