-
Notifications
You must be signed in to change notification settings - Fork 129
/
Test.h
131 lines (114 loc) · 3.65 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
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <sstream>
#include <vector>
#include <functional>
#include "Shared.h"
namespace test
{
typedef std::function<void()> Test;
class TestFailedException
{
public:
TestFailedException(std::string message)
: m_message(message)
{
}
std::string m_message;
};
class TestSuite
{
private:
typedef std::pair<std::string, Test> RegisteredTest;
typedef std::vector<RegisteredTest> RegisteredTests;
public:
TestSuite()
: m_verbose(false)
{
}
void registerTest(const std::string& testName, Test test)
{
m_tests.push_back(RegisteredTest(testName, test));
}
void setVerbose(bool b)
{
m_verbose = b;
}
bool runAllTests()
{
bool allPassed = true;
for (RegisteredTests::const_iterator test = m_tests.begin(); test != m_tests.end(); ++test)
{
if (m_verbose)
{
std::cout << test->first << " " << std::flush;
}
try
{
test->second();
if (m_verbose)
{
std::cout << "PASS" << std::endl << std::flush;
}
else
{
std::cout << "." << std::flush;
}
}
catch (TestFailedException e)
{
allPassed = false;
if (!m_verbose)
{
std::cout << test->first << " ";
}
std::cout << "FAIL: " << e.m_message << std::endl << std::flush;
}
}
if (!m_verbose)
{
std::cout << std::endl << std::flush;
}
return allPassed;
}
static TestSuite& getInstance()
{
static TestSuite instance;
return instance;
}
private:
bool m_verbose;
RegisteredTests m_tests;
};
class AutoTestRegister
{
public:
AutoTestRegister(const std::string& name, Test test)
{
TestSuite::getInstance().registerTest(name, test);
}
};
}
#define TEST(SUITENAME, TESTNAME) \
void SUITENAME##_##TESTNAME(); \
static test::AutoTestRegister autoTestRegister_##SUITENAME##_##TESTNAME(#SUITENAME "_" #TESTNAME, SUITENAME##_##TESTNAME); \
void SUITENAME##_##TESTNAME()
#define CHECK(X) \
if (!(X)) \
{ \
std::ostringstream os; \
os << #X << " "; \
os << __FILE__ << ":" << __LINE__; \
throw test::TestFailedException(os.str()); \
}
#define CHECK_EQUAL(X, Y) \
if (!((X) == (Y))) \
{ \
std::ostringstream os; \
os << "(" << #X << " == " << #Y << ") "; \
os << "expected:" << (X) << " actual:" << (Y) << " "; \
os << __FILE__ << ":" << __LINE__; \
throw test::TestFailedException(os.str()); \
}
#endif // TEST_H