-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_minimal.h
212 lines (183 loc) Β· 8.13 KB
/
test_minimal.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
// Copyright (C) 2020-2023 Sami VΓ€isΓ€nen
// Copyright (C) 2020-2023 Ensisoft http://www.ensisoft.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <vector>
#include <string>
#include <stdexcept>
#include "base/platform.h"
#include "base/bitflag.h"
namespace test {
// Interface for wrapping multiple "test_main" functions behind an interface.
// This allows a test executable to be built with multiple tests bundled in
// together into a single binary and each test_main is turned into a method
// call instead of being a global function.
// See the macro UNIT_TEST_USE_MAIN_BUNDLE
struct TestBundle
{
virtual int test_main(int argc, char* argv[]) = 0;
std::string name;
};
// Note that this class does *not* derive from std::exception
// on purpose so that the test code that would catch std::exceptions
// won't catch this.
class Fatality {
public:
Fatality(const char* expression,
const char* file,
const char* function,
int line)
: mExpression(expression)
, mFile(file)
, mFunc(function)
, mLine(line)
{}
public:
const char* mExpression = nullptr;
const char* mFile = nullptr;
const char* mFunc = nullptr;
const int mLine = 0;
};
enum class Type {
Performance, Feature, Other
};
enum class Color {
Error, Warning, Success, Message, Info
};
enum class Flags {
TODO = 0x1
};
extern base::bitflag<Type> EnabledTestTypes;
extern std::vector<std::string> EnabledTestNames;
extern std::vector<TestBundle*> TestBundles;
extern unsigned ErrorCount;
// Produce printed message into some output device such as stdout.
void Print(Color color, const char* fmt, ...);
void SetBundleName(const char* source_file_name, std::string name);
// Extract simple bundle name from the filename provided by the __FILE__ macro.
std::string GetBundleName(const char* source_file_name);
// Extract simple filename from the filename provided by the __FILE__ macro.
const char* GetFileName(const char* source_file_name);
// Extract a simple function name from the function name provided by the __PRETTY_FUNCTION__ macro.
const char* GetTestName(const char* function_name);
// Process a testing failure.
void BlurpFailure(const char* expression, const char* file, const char* function, int line, bool fatality);
bool IsEnabledByName(const std::string& name);
bool IsEnabledByType(Type type);
void AddBundle(TestBundle* bundle);
std::string GetPerformanceRecordFileName();
bool ReadYesNo(Color color, const char* prompt);
class TestCaseReporter {
public:
TestCaseReporter(const char* file, const char* func, Type type)
: mFile(file)
, mName(GetTestName(func))
, mType(type)
{
// use the current number of errors to realize if the
// current test case failed or not
mErrors = ErrorCount;
}
~TestCaseReporter()
{
const auto enabled_by_type = IsEnabledByType(mType);
const auto enabled_by_name = IsEnabledByName(mName);
// printing all this information here so that the non-fatal
// test failures print their message *before* the name and result
// of the test execution.
test::Print(test::Color::Message, "Running ");
test::Print(test::Color::Info, "%-50s", mName);
if (enabled_by_type && enabled_by_name)
{
if (mFlags & static_cast<unsigned>(Flags::TODO))
test::Print(test::Color::Warning, "TODO\n");
else if (mErrors == ErrorCount)
test::Print(test::Color::Success, "OK\n");
else test::Print(test::Color::Warning, "Fail\n");
}
else
{
test::Print(test::Color::Message, "Skipped\n");
}
}
inline void SetFlag(Flags flag) noexcept
{ mFlags |= static_cast<unsigned>(flag); }
private:
const char* mFile = nullptr;
const char* mName = nullptr;
const Type mType;
unsigned mErrors = 0;
unsigned mFlags = 0;
};
} // test
#define TEST_CHECK(expr) \
(expr) \
? ((void)0) \
: (test::BlurpFailure(#expr, __FILE__, __FUNCTION__, __LINE__, false))
#define TEST_REQUIRE(expr) \
(expr) \
? ((void)0) \
: (test::BlurpFailure(#expr, __FILE__, __FUNCTION__, __LINE__, true))
#define TEST_MESSAGE(msg, ...) \
test::Print(test::Color::Message, "%s (%d): '" msg "'\n", __FUNCTION__, __LINE__, ## __VA_ARGS__); \
#define TEST_EXCEPTION(expr) \
do { \
bool have_exception = false; \
try { \
expr; \
} catch (const std::exception& e) { \
have_exception = true; \
} \
TEST_REQUIRE(have_exception && "Exception was expected"); \
} while(0)
#define TEST_CASE(type) \
test::TestCaseReporter test_case_reporter(__FILE__, __FUNCTION__, type); \
if (!test::IsEnabledByType(type)) \
return; \
if (!test::IsEnabledByName(test::GetTestName(__PRETTY_FUNCTION__))) \
return;
#define TEST_CASE_TODO \
test_case_reporter.SetFlag(test::Flags::TODO);
// When using a test bundle the test_main is no longer a global function
// but instead becomes a member function that implements/overrides the
// TestBundle::test_main method. A single object is then automatically
// created and appended to the global list of test bundles to be run.
#if defined(UNIT_TEST_BUNDLE)
#define SET_BUNDLE_NAME(name) \
namespace { \
struct TestBundleName { \
TestBundleName(std::string bundle_name) { \
test::SetBundleName(__FILE__, std::move(bundle_name)); \
} \
} test_bundle_name(name); \
}
#define EXPORT_TEST_MAIN(main) \
namespace { \
struct PrivateTestBundle : public test::TestBundle { \
main \
PrivateTestBundle() { \
test::AddBundle(this); \
name = test::GetBundleName(__FILE__); \
} \
} test_bundle; \
} // namespace
#else
#define SET_BUNDLE_NAME(name)
#define EXPORT_TEST_MAIN(main) main
// automatically include the definitions for keeping the compilation
// of existing unit tests simple and without having to explicitly
// add the source file in the build rules.
#include "base/test_minimal.cpp"
#endif