-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.hpp
185 lines (146 loc) · 4.5 KB
/
unit_test.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
#ifndef MCP_BASE_UNIT_TEST_HEADER
#define MCP_BASE_UNIT_TEST_HEADER
// TODO allow EXPECT_XX() << "details";
#include <iostream>
#include <string>
#include <vector>
extern bool MCP_BASE_exit_on_fatal;
extern bool MCP_BASE_has_fatal_message;
namespace base {
using std::string;
using std::vector;
class TestCase {
public:
virtual ~TestCase() {}
virtual void TestBody() = 0;
const string& group() const { return group_; }
const string& name() const { return name_; }
int errors() const { return errors_; }
protected:
TestCase(const string& group, const string& name)
: group_(group), name_(name), errors_(0) {}
void incErrors() { ++errors_; }
private:
string group_;
string name_;
int errors_;
};
class TestRegistry {
public:
~TestRegistry() {}
static TestRegistry* getInstance();
TestCase* registerCase(TestCase* test);
int runAndReset();
private:
typedef vector<TestCase*> Tests;
static TestRegistry* instance_;
Tests tests_; // owned by this
TestRegistry() {}
};
TestRegistry* TestRegistry::instance_ = NULL;
TestRegistry* TestRegistry::getInstance() {
if (instance_ == NULL) instance_ = new TestRegistry;
return instance_;
}
TestCase* TestRegistry::registerCase(TestCase* test) {
tests_.push_back(test);
return test;
}
int TestRegistry::runAndReset() {
int errors = 0;
//MCP_BASE_exit_on_fatal = false;
for (Tests::const_iterator it = tests_.begin(); it != tests_.end(); ++it) {
TestCase* test = *it;
std::cout << "Running " << test->group() << "(" << test->name() << "): ";
test->TestBody();
if (test->errors()) {
errors += test->errors();
std::cout << std::endl;
} else {
std::cout << "PASSED" << std::endl;
}
delete test;
}
tests_.clear();
TestRegistry* me = instance_;
instance_ = NULL;
delete me;
return errors;
}
} // namespace base
#define EXPECT_TRUE(a) \
if (!(a)) { \
if (errors() == 0) std::cout << "FAILED"; \
std::cout << "\n Line " << __LINE__ \
<< " Expected true " << # a << ": " << (a); \
incErrors(); \
}
#define EXPECT_FALSE(a) \
if (a) { \
if (errors() == 0) std::cout << "FAILED"; \
std::cout << "\n Line " << __LINE__ \
<< " Expected false " << # a << ": " << (a); \
incErrors(); \
}
#define EXPECT_EQ(a, b) \
if ((a) != (b)) { \
if (errors() == 0) std::cout << "FAILED"; \
std::cout << "\n Line " << __LINE__ \
<< " Expected equal " << # a << " and " << # b << ": (" \
<< (a) << " " << (b) << ")"; \
incErrors(); \
}
#define EXPECT_NEQ(a, b) \
if ((a) == (b)) { \
if (errors() == 0) std::cout << "FAILED"; \
std::cout << "\n Line " << __LINE__ \
<< " Expected different " << # a << " and " << # b << ": (" \
<< (a) << " " << (b) << ")"; \
incErrors(); \
}
#define EXPECT_SL(a, b) \
if ((a) >= (b)) { \
if (errors() == 0) std::cout << "FAILED"; \
std::cout << "\n Line " << __LINE__ \
<< " Expected smaller " << # a << " and " << # b << ": (" \
<< (a) << " " << (b) << ")"; \
incErrors(); \
}
#define EXPECT_GT(a, b) \
if ((a) <= (b)) { \
if (errors() == 0) std::cout << "FAILED"; \
std::cout << "\n Line " << __LINE__ \
<< " Expected greater " << # a << " and " << # b << ": (" \
<< (a) << " " << (b) << ")"; \
incErrors(); \
}
#define EXPECT_FATAL(a) \
MCP_BASE_has_fatal_message = false; \
(a); \
if (!MCP_BASE_has_fatal_message) { \
if (errors() == 0) std::cout << "FAILED"; \
std::cout << "\n Line " << __LINE__ << " Expected fatal " << # a; \
incErrors(); \
}
#define TEST_CLASS_NAME(test_group, test_name) \
test_group ## _ ## test_name ## _Test
#define TEST(test_group, test_name) \
class TEST_CLASS_NAME(test_group, test_name) : public base::TestCase { \
public: \
TEST_CLASS_NAME(test_group, test_name)(); \
virtual void TestBody(); \
private: \
static TestCase* const me_; \
}; \
\
base::TestCase* const TEST_CLASS_NAME(test_group, test_name)::me_ = \
base::TestRegistry::getInstance()->registerCase( \
new TEST_CLASS_NAME(test_group, test_name)); \
\
TEST_CLASS_NAME(test_group, test_name)::\
TEST_CLASS_NAME(test_group, test_name)() \
: TestCase(# test_group, # test_name) {} \
void TEST_CLASS_NAME(test_group, test_name)::TestBody()
#define RUN_TESTS() \
(base::TestRegistry::getInstance()->runAndReset())
#endif // MCP_BASE_UNIT_TEST_HEADER