-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathCppUTestApprovals.h
86 lines (71 loc) · 2.86 KB
/
CppUTestApprovals.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
#pragma once
#include "ApprovalTests/namers/ApprovalTestNamer.h"
#include "ApprovalTests/integrations/FrameworkIntegrations.h"
#include "ApprovalTests/utilities/Macros.h"
#ifdef APPROVALS_CPPUTEST_EXISTING_MAIN
#define APPROVALS_CPPUTEST
#endif
#ifdef APPROVALS_CPPUTEST
#define APPROVAL_TESTS_INCLUDE_CPPS
// begin-snippet: required_headers_for_cpputest
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/TestPlugin.h>
#include <CppUTest/TestRegistry.h>
// end-snippet
namespace ApprovalTests
{
class ApprovalTestsCppUTestPlugin : public TestPlugin
{
private:
// We need to be able to delete currentTest at the end of the
// test, to prevent CppUTest's leak-checking from triggering,
// due to an undeleted std::string - so we use std::unique_ptr.
std::unique_ptr<ApprovalTests::TestName> currentTest;
public:
ApprovalTestsCppUTestPlugin() : TestPlugin("ApprovalTestsCppUTestPlugin")
{
// Turn off CppUTest's leak checks.
// On some platforms, CppUTest's leak-checking reports leaks
// in this code, because the way the platform's std::string manages life-times
// of string storage is not compatible with the requirements of the
// CppUTest leak-checks.
MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
}
APPROVAL_TESTS_NO_DISCARD static std::string
cppUTestToString(const SimpleString& string)
{
return std::string{string.asCharString()};
}
void preTestAction(UtestShell& shell, TestResult& result) override
{
currentTest.reset(new ApprovalTests::TestName);
currentTest->setFileName(cppUTestToString(shell.getFile()));
currentTest->sections.emplace_back(cppUTestToString(shell.getGroup()));
currentTest->sections.emplace_back(cppUTestToString(shell.getName()));
ApprovalTests::FrameworkIntegrations::setCurrentTest(currentTest.get());
ApprovalTests::FrameworkIntegrations::setTestPassedNotification(
[]() { CHECK_TRUE(true); });
TestPlugin::preTestAction(shell, result);
}
void postTestAction(UtestShell& shell, TestResult& result) override
{
currentTest = nullptr;
TestPlugin::postTestAction(shell, result);
}
};
inline void initializeApprovalTestsForCppUTest()
{
static ApprovalTests::ApprovalTestsCppUTestPlugin logPlugin;
TestRegistry::getCurrentRegistry()->installPlugin(&logPlugin);
}
}
#ifndef APPROVALS_CPPUTEST_EXISTING_MAIN
int main(int argc, char** argv)
{
ApprovalTests::initializeApprovalTestsForCppUTest();
int result = CommandLineTestRunner::RunAllTests(argc, argv);
TestRegistry::getCurrentRegistry()->resetPlugins();
return result;
}
#endif
#endif // APPROVALS_CPPUTEST