-
Notifications
You must be signed in to change notification settings - Fork 2
/
BUnit.h
67 lines (50 loc) · 1.44 KB
/
BUnit.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
#ifndef BUNIT_H
#define BUNIT_H
#define CLASSNAME(x) x(void (x::*name)()) : TestCase(static_cast<void (TestCase::*)()>(name)) { }
#define TESTS int main() { TestSuite suite;
#define ADD(cl, t) suite.add(new cl(&cl::t));
#define RUN return suite.run();}
#define assert(x) this->assert_(x, #x, __PRETTY_FUNCTION__, __FILE__, __LINE__)
#include<iostream>
#include<vector>
struct FailedAssertError {
std::string condition;
std::string function;
std::string file;
int line;
};
class TestSuite;
class TestCase {
friend class TestSuite;
protected:
void (TestCase::*name)();
TestCase() { }
public:
TestCase(void (TestCase::*name)());
virtual void setUp(){}
virtual void run() {
this->setUp();
(this->*name)();
this->tearDown();
}
virtual void tearDown() {}
virtual ~TestCase() {}
protected:
virtual void assert_(bool cond, std::string condStr, std::string prettyFunction, std::string file, int line) {
if(!cond) {
throw new FailedAssertError{condStr, prettyFunction, file, line};
}
}
};
typedef void (TestCase::*CallbackType)();
class TestSuite {
protected:
std::vector<TestCase*> tests;
public:
TestSuite() {}
void add(TestCase* test);
int run();
private:
std::string parseTestName(const std::string& prettyFunction);
};
#endif