-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcut.h
86 lines (64 loc) · 1.44 KB
/
mcut.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
/**@mainpage
Micro C++ Unit Tester
@author Pavel Studeny
Example:
@code{.cpp}
TEST_CASE(Example, {
int i = static_cast<int>(2.5);
CHECK(i == 2);
});
@endcode
See test.cpp for more examples.
*/
#include <vector>
#include <functional>
#include <assert.h>
#include <iostream>
/**
Hold a list of tests and run them
*/
class MCUT {
public:
/** Current instance */
static MCUT *instance();
/** Update instance when constructed */
MCUT();
/** Return instance back tuo previous status */
virtual ~MCUT();
/** Name and body of a test */
struct TestCase {
/**
Called by TEST_CASE
@param n test name
@param t test body
*/
TestCase(const std::string &n, std::function < void(bool & rv) > t);
/** test body */
std::function < void(bool & rv) > test;
/** test name, comes from TEST_CASE identifier */
const std::string name;
};
/**
Run all tests
@param description test result, one result per line
@return number of failed tests
*/
int run(std::ostream &description) const;
protected:
/** List pf tests added to the current instance */
std::vector<TestCase> tests;
private:
static MCUT *currentInstance;
MCUT *previousInstance;
};
/**
Verify a condition
@param x expression returning a bool value
*/
#define CHECK(x) if (!(x)) { rv = false; }
/**
Define a test
@param f identifier
@param ... test body
*/
#define TEST_CASE(f, ...) MCUT::TestCase MCUT_TEST_CASE_ ## f(#f, [] (bool & rv) __VA_ARGS__)