forked from greg-white/sTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.h
483 lines (391 loc) · 13 KB
/
test.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#pragma once
//-----------------------------------------------------------------------------
// sTest v 1.2 - unit testing framework for C++
//
// Copyright (c) 2017 Gregory Wilk
// Source: https://github.com/greg-white/sTest
// License MIT
//-----------------------------------------------------------------------------
// example of use:
/*
#include "test.h"
void test_sub()
{
TEST_GROUP_FUNCTION;
TEST(1 - 1 == 1);
TEST(1 - 2 == -1);
}
int main()
{
try
{
TEST_SECTION("arithmetic");
TEST_GROUP("test_add");
TEST(1 + 1 == 2);
TEST(1 + 2 == 3);
test_sub();
TEST_GROUP("test_mul");
TEST(0 * 1 == 0);
TEST_MERGE(true);
TEST(1 * 2 == 2);
TEST(2 * 1 == 1);
TEST_GROUP("other");
if (TEST_IF(1 * 1 == 1))
TEST(2 / (1 * 1) == 2);
TEST_SUMMARY;
}
catch (...)
{
TEST_EXCEPTION;
}
return 0;
}
*/
//-----------------------------------------------------------------------------
// main macros and includes
//-----------------------------------------------------------------------------
#if !defined(__cplusplus)
#error C++ is required
#endif
#include <cstring> // strrchr
#include <cstdlib> // used for exit(EXIT_FAILURE)
// get source file name without path
#if defined(_WIN32)
#define __FILENAME__ (std::strrchr(__FILE__, '\\') ? std::strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
#define __FILENAME__ (std::strrchr(__FILE__, '/') ? std::strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
// main/basic test macro
#define TEST(X) _test::log<int>(_test::check, #X, __FILENAME__, __LINE__, (X))
// return if test failed
#define TEST_R(X) if(!_test::log<int>(_test::check_skip, #X, __FILENAME__, __LINE__, (X))) return;
// test in 'if' statement
#define TEST_IF(X) _test::log<int>(_test::check_skip, #X, __FILENAME__, __LINE__, (X))
// merge tests into one
#define TEST_MERGE(X) _test::log<int>(_test::merge, nullptr, nullptr, 0, (X))
// print summary of all tests
#define TEST_SUMMARY _test::log<int>(_test::summary)
// summart for catch block after all tests
#define TEST_EXCEPTION _test::log<int>(_test::exception)
// begin test group
#define TEST_GROUP(X) _test::log<int>(_test::begin_group, (X))
// begin test group named as function
#define TEST_GROUP_FUNCTION _test::log<int>(_test::begin_group, __func__)
// begin test section
#define TEST_SECTION(X) _test::log<int>(_test::begin_section, (X))
// begin test section named as function
#define TEST_SECTION_FUNCTION _test::log<int>(_test::begin_section, __func__)
// custom print to output
#define TEST_PRINT(X) _test::log<int>(_test::print, (X))
// true if any test has failed
#define TEST_FAILED _test::log<int>(_test::total_failed)
// option: enable program exit in summary
#define TEST_EXIT(X) _test::log<int>(_test::do_exit, nullptr, nullptr, 0, (X))
// option: wait for user input before exit
#define TEST_WAIT(X) _test::log<int>(_test::do_wait, nullptr, nullptr, 0, (X))
// option: enable program exit after first failed test
#define TEST_ASSERT_MODE(X) _test::log<int>(_test::assert_mode, nullptr, nullptr, 0, (X))
//-----------------------------------------------------------------------------
// output
//-----------------------------------------------------------------------------
#include <iostream> // used for console output
namespace _test
{
template <typename T>
void print_info()
{
std::cout << "sTest v 1.2 ";
std::cout << "<console:text>\n";
std::cout << std::endl;
}
template <typename T>
void print_test_group(const char *name)
{
std::cout << name;
std::cout << std::endl;
}
template <typename T>
void print_test_section(const char *name)
{
std::cout << '[' << name << "]\n";
std::cout << std::endl;
}
// return ture if printed
template <typename T>
bool print_test_check(const char *what, const char *file, int line, bool passed, bool skip, bool merged)
{
if (!passed)
{
if (merged)
std::cout << " failed!";
else
std::cout << " Test failed!";
std::cout << " " << file;
std::cout << ":" << line;
std::cout << " " << what;
if (skip)
std::cout << "\n -skipping next tests";
std::cout << std::endl;
return true;
}
return false;
}
template <typename T>
void print_group_status(long long testFailedCount, long long testCunt, bool testWasSkipped)
{
if (testFailedCount)
std::cout << " -failed: " << testFailedCount << " of " << testCunt;
else
std::cout << " -test count: " << testCunt;
if (testWasSkipped)
std::cout << "*";
std::cout << "\n";
std::cout << std::endl;
}
template <typename T>
void print_summary(long long totalFailedCount, long long totalCount, bool totalWasSkipped)
{
std::cout << "==============================\n";
if (totalFailedCount == 0)
std::cout << "All tests passed!\n";
else
std::cout << "Warning " << totalFailedCount << " tests failed!\n";
std::cout << "Test count: " << totalCount;
if (totalWasSkipped)
std::cout << "*\n*Some tests may be skipped.";
std::cout << std::endl;
}
template <typename T>
void print_assert(long long totalCount)
{
std::cout << "\n==============================\n";
std::cout << "Assert mode, skipping next tests!\n";
std::cout << "Exiting after " << totalCount << " tests.";
std::cout << std::endl;
}
template <typename T>
void print_exception(const char *lastFile, int lastLine)
{
std::cout << "\n==============================\n";
if (lastFile)
{
std::cout << "Exception afer test in: ";
std::cout << lastFile;
std::cout << ":" << lastLine;
}
else
std::cout << "Exception beafore anny test!";
std::cout << std::endl;
}
template <typename T>
void print_print(const char *txt)
{
std::cout << txt;
std::cout << std::endl;
}
template <typename T>
void wait()
{
std::cin.get();
}
}
//-----------------------------------------------------------------------------
// test logic
//-----------------------------------------------------------------------------
namespace _test
{
struct Status
{
long long testCount, failedCount;
bool hasSkipped;
Status()
{
Clear();
}
void Clear()
{
testCount = 0;
failedCount = 0;
hasSkipped = false;
}
};
struct MergedInfo
{
bool counted;
bool failed;
bool printed;
MergedInfo()
{
Clear();
}
void Clear()
{
counted = false;
failed = false;
printed = false;
}
};
struct Options
{
bool exitAtEnd;
bool waitAtExit;
bool exitWhenFailed;
Options()
{
exitAtEnd = true;
waitAtExit = true;
exitWhenFailed = false;
}
};
enum LogType
{
check,
check_skip,
summary,
exception,
begin_group,
begin_section,
print,
merge,
total_failed,
do_exit,
do_wait,
assert_mode
};
template <typename T>
bool log(LogType type, const char *what = nullptr, const char *file = nullptr, int line = 0, bool passed = false)
{
static Status totalStatus, groupStatus;
static bool isMerged;
static MergedInfo merged;
static const char *lastFile;
static int lastLine;
static Options options;
static bool hasTestsOrGroup = false;
static bool info = false;
if (!info)
{
print_info<T>();
info = true;
}
switch (type)
{
case LogType::check:
case LogType::check_skip:
hasTestsOrGroup = true;
if (!isMerged || !merged.counted)
{
totalStatus.testCount++;
groupStatus.testCount++;
merged.counted = isMerged;
}
lastFile = file;
lastLine = line;
if (!passed)
{
if (!isMerged || !merged.failed)
{
totalStatus.failedCount++;
groupStatus.failedCount++;
merged.failed = isMerged;
}
if (type == LogType::check_skip)
{
totalStatus.hasSkipped = true;
groupStatus.hasSkipped = true;
}
}
merged.printed = print_test_check<T>(what, file, line, passed, type == LogType::check_skip, merged.printed) && isMerged;
if (!passed && options.exitWhenFailed)
{
print_assert<T>(totalStatus.testCount);
if (options.waitAtExit)
wait<T>();
exit(EXIT_FAILURE);
}
break;
case LogType::summary:
{
if (hasTestsOrGroup)
print_group_status<T>(groupStatus.failedCount, groupStatus.testCount, groupStatus.hasSkipped);
print_summary<T>(totalStatus.failedCount, totalStatus.testCount, totalStatus.hasSkipped);
bool testIsFailed = totalStatus.failedCount;
totalStatus.Clear();
groupStatus.Clear();
isMerged = false;
merged.Clear();
if (options.exitAtEnd)
{
if (options.waitAtExit)
wait<T>();
if (testIsFailed)
exit(EXIT_FAILURE);
else
exit(EXIT_SUCCESS);
}
break;
}
case LogType::begin_group:
if (what)
{
if (hasTestsOrGroup)
print_group_status<T>(groupStatus.failedCount, groupStatus.testCount, groupStatus.hasSkipped);
print_test_group<T>(what);
groupStatus.Clear();
isMerged = false;
merged.Clear();
hasTestsOrGroup = true;
}
break;
case LogType::begin_section:
if (what)
{
if (hasTestsOrGroup)
{
print_group_status<T>(groupStatus.failedCount, groupStatus.testCount, groupStatus.hasSkipped);
hasTestsOrGroup = false;
}
print_test_section<T>(what);
groupStatus.Clear();
isMerged = false;
merged.Clear();
}
break;
case LogType::exception:
print_exception<T>(lastFile, lastLine);
totalStatus.Clear();
groupStatus.Clear();
isMerged = false;
merged.Clear();
if (options.exitAtEnd)
{
if (options.waitAtExit)
wait<T>();
exit(EXIT_FAILURE);
}
break;
case LogType::merge:
isMerged = passed;
merged.Clear();
break;
case LogType::print:
if (what)
print_print<T>(what);
break;
case LogType::total_failed:
return totalStatus.failedCount != 0;
case LogType::do_exit:
options.exitAtEnd = passed;
break;
case LogType::do_wait:
options.waitAtExit = passed;
break;
case LogType::assert_mode:
options.exitWhenFailed = passed;
break;
default:
break;
}
return passed;
}
}