-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTinyCppTest.hpp
292 lines (253 loc) · 6.32 KB
/
TinyCppTest.hpp
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
// https://github.com/kovacsv/TinyCppTest
// Version: 0.1.1
#ifndef TINYCPPTEST_HPP
#define TINYCPPTEST_HPP
#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <cmath>
#ifdef _WIN32
#include <windows.h>
#endif
#define WIN_PATH_SEPARATOR L'\\'
#define PATH_SEPARATOR L'/'
namespace TinyCppTest
{
enum class ConsoleColor
{
Normal,
Red,
Green
};
#ifdef _WIN32
class ColoredConsoleWriter
{
public:
ColoredConsoleWriter (ConsoleColor consoleColor) :
consoleColor (consoleColor),
consoleHandle (NULL)
{
consoleHandle = GetStdHandle (STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo (consoleHandle, &screenBufferInfo);
}
~ColoredConsoleWriter ()
{
SetConsoleTextAttribute (consoleHandle, screenBufferInfo.wAttributes);
}
void Write (const std::string& text)
{
if (consoleColor == ConsoleColor::Green) {
SetConsoleTextAttribute (consoleHandle, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
} else if (consoleColor == ConsoleColor::Red) {
SetConsoleTextAttribute (consoleHandle, FOREGROUND_RED | FOREGROUND_INTENSITY);
}
std::cout << text;
SetConsoleTextAttribute (consoleHandle, screenBufferInfo.wAttributes);
}
private:
ConsoleColor consoleColor;
HANDLE consoleHandle;
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
};
#else
class ColoredConsoleWriter
{
public:
ColoredConsoleWriter (ConsoleColor consoleColor) :
consoleColor (consoleColor)
{
}
~ColoredConsoleWriter ()
{
}
void Write (const std::string& text)
{
if (consoleColor == ConsoleColor::Green) {
std::cout << "\033[32m";
} else if (consoleColor == ConsoleColor::Red) {
std::cout << "\033[31m";
}
std::cout << text;
std::cout << "\033[39m";
}
private:
ConsoleColor consoleColor;
};
#endif
class Test
{
public:
Test (const std::string& testName) :
testName (testName),
testSuccess (true)
{
}
virtual ~Test ()
{
}
bool Run ()
{
ColoredConsoleWriter green (ConsoleColor::Green);
ColoredConsoleWriter red (ConsoleColor::Red);
green.Write ("[ RUNNING ] ");
std::cout << testName;
RunTest ();
if (testSuccess) {
std::cout << std::endl;
green.Write ("[ SUCCESS ]");
} else {
red.Write ("[ FAILURE ]");
}
std::cout << " " << testName << std::endl;
return testSuccess;
}
const std::string& GetName () const
{
return testName;
}
protected:
void TestAssert (bool condition, const std::string& fileName, int lineNumber)
{
if (!condition) {
if (testSuccess) {
std::cout << std::endl;
}
std::cout << "Assertion failed: " << fileName << " (" << lineNumber << ")" << std::endl;
#ifdef _WIN32
OutputDebugStringA (fileName.c_str ());
OutputDebugStringA ("(");
OutputDebugStringA (std::to_string (lineNumber).c_str ());
OutputDebugStringA ("): Assertion failed\n");
#endif
testSuccess = false;
}
}
virtual void RunTest () = 0;
std::string testName;
bool testSuccess;
};
class Suite
{
public:
Suite () :
tests (),
appLocation ()
{
}
bool Run ()
{
ColoredConsoleWriter green (ConsoleColor::Green);
ColoredConsoleWriter red (ConsoleColor::Red);
green.Write ("[ ------- ] ");
std::cout << "Running " << tests.size () << " tests." << std::endl;
std::vector<std::string> failedTestNames;
for (const std::unique_ptr<Test>& test : tests) {
if (!test->Run ()) {
failedTestNames.push_back (test->GetName ());
}
}
green.Write ("[ ------- ] ");
std::cout << "Finished running " << tests.size () << " tests." << std::endl << std::endl;
bool success = (failedTestNames.empty ());
if (success) {
green.Write ("[ ------- ] ");
std::cout << std::endl;
green.Write ("[ SUCCESS ] ");
std::cout << "All tests succeeeded." << std::endl;
green.Write ("[ ------- ] ");
} else {
red.Write ("[ ------- ] ");
std::cout << std::endl;
red.Write ("[ FAILURE ] ");
std::cout << "The following tests are failed:" << std::endl;
for (const std::string& failedTestName : failedTestNames) {
red.Write ("[ FAILURE ] ");
std::cout << failedTestName << std::endl;
}
red.Write ("[ ------- ] ");
}
std::cout << std::endl;
return success;
}
void AddTest (Test* test)
{
tests.push_back (std::unique_ptr<Test> (test));
}
const std::wstring& GetAppLocation () const
{
return appLocation;
}
void SetAppLocation (const std::wstring& newAppLocation)
{
appLocation = newAppLocation;
}
static Suite& Get ()
{
static Suite suite;
return suite;
}
private:
std::vector<std::unique_ptr<Test>> tests;
std::wstring appLocation;
};
inline void SetAppLocation (const std::wstring& newAppLocation)
{
std::wstring appLocation = newAppLocation;
for (size_t i = 0; i < appLocation.length (); ++i) {
if (appLocation[i] == WIN_PATH_SEPARATOR) {
appLocation[i] = PATH_SEPARATOR;
}
}
Suite& suite = Suite::Get ();
suite.SetAppLocation (appLocation);
}
inline std::wstring GetAppLocation ()
{
const Suite& suite = Suite::Get ();
return suite.GetAppLocation ();
}
inline std::wstring GetAppFolderLocation ()
{
std::wstring appLocation = GetAppLocation ();
size_t lastSeparator = appLocation.find_last_of (PATH_SEPARATOR);
if (lastSeparator == std::wstring::npos) {
return std::wstring ();
}
std::wstring directoryPath = appLocation.substr (0, lastSeparator) + PATH_SEPARATOR;
return directoryPath;
}
inline bool RunTests ()
{
Suite& suite = Suite::Get ();
return suite.Run ();
}
inline void RegisterTest (Test* test)
{
Suite& suite = Suite::Get ();
suite.AddTest (test);
}
}
#define TEST(TESTNAME) \
class TESTNAME##_Test : public TinyCppTest::Test { \
public: \
TESTNAME##_Test () : \
TinyCppTest::Test (#TESTNAME) \
{ \
} \
virtual void RunTest () override; \
}; \
static class TESTNAME##_Registrator { \
public: \
TESTNAME##_Registrator () \
{ \
TinyCppTest::RegisterTest (new TESTNAME##_Test ()); \
} \
} TESTNAME##_RegistratorInstance; \
void TESTNAME##_Test::RunTest ()
#define ASSERT(condition) TestAssert (condition, __FILE__, __LINE__)
#define ASSERT_EQ(a, b) TestAssert (a == b, __FILE__, __LINE__)
#define ASSERT_NEQ(a, b) TestAssert (a != b, __FILE__, __LINE__)
#define ASSERT_EQ_EPS(a, b, eps) TestAssert (std::fabs (a - b) < eps, __FILE__, __LINE__)
#define ASSERT_NEQ_EPS(a, b, eps) TestAssert (std::fabs (a - b) >= eps, __FILE__, __LINE__)
#endif