-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwsjcpp_unit_tests.cpp
More file actions
136 lines (110 loc) · 4.2 KB
/
Copy pathwsjcpp_unit_tests.cpp
File metadata and controls
136 lines (110 loc) · 4.2 KB
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
#include "wsjcpp_unit_tests.h"
#include <cmath>
// ---------------------------------------------------------------------
// WsjcppUnitTestBase
WsjcppUnitTestBase::WsjcppUnitTestBase(const std::string &sTestName) {
m_sTestName = sTestName;
TAG = m_sTestName;
m_bTestResult = true;
WsjcppUnitTests::addUnitTest(sTestName, this);
}
// ---------------------------------------------------------------------
std::string WsjcppUnitTestBase::getName() {
return m_sTestName;
}
// ---------------------------------------------------------------------
void WsjcppUnitTestBase::ok(const std::string &sSuccessMessage) {
// print obly success message
WsjcppLog::ok(TAG, sSuccessMessage);
}
// ---------------------------------------------------------------------
void WsjcppUnitTestBase::fail(const std::string &sFailedMessage) {
WsjcppLog::err(TAG, sFailedMessage);
m_bTestResult = false;
}
// ---------------------------------------------------------------------
bool WsjcppUnitTestBase::runTest() {
WsjcppLog::info(TAG, "Start unit-test");
WsjcppLog::info(TAG, "Do before unit-test");
if (!doBeforeTest()) {
fail("Problem with before unit-test");
return false;
}
WsjcppLog::info(TAG, "Execute unit-test");
try {
executeTest();
} catch(const std::exception& e) {
fail(e.what());
} catch(...) {
}
if (m_bTestResult) {
ok("Test passed.");
} else {
fail("Test failed.");
}
WsjcppLog::info(TAG, "Do after unit-test");
if (!doAfterTest()) {
fail("Problem with after unit-test");
}
WsjcppLog::info(TAG, "End unit-test");
return m_bTestResult;
}
// ---------------------------------------------------------------------
bool WsjcppUnitTestBase::compareS(const std::string &sMark,
const std::string &sValue, const std::string &sExpected) {
if (sValue != sExpected) {
fail(" {" + sMark + "} Expected '" + sExpected + "', but got '" + sValue + "'");
return false;
}
return true;
}
// ---------------------------------------------------------------------
bool WsjcppUnitTestBase::compareN(const std::string &sMark, int nValue, int nExpected) {
if (nValue != nExpected) {
fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
return false;
}
return true;
}
// ---------------------------------------------------------------------
bool WsjcppUnitTestBase::compareD(const std::string &sMark, double nValue, double nExpected) {
if (abs(nValue - nExpected) > std::numeric_limits<double>::epsilon()) {
fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
return false;
}
return true;
}
// ---------------------------------------------------------------------
bool WsjcppUnitTestBase::compareB(const std::string &sMark, bool bValue, bool bExpected) {
if (bValue != bExpected) {
fail(" {" + sMark + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'");
return false;
}
return true;
}
// ---------------------------------------------------------------------
std::vector<WsjcppUnitTestBase*> *g_pWsjcppUnitTests = nullptr;
void WsjcppUnitTests::initGlobalVariables() {
if (g_pWsjcppUnitTests == nullptr) {
// WsjcppLog::info(std::string(), "Create handlers map");
g_pWsjcppUnitTests = new std::vector<WsjcppUnitTestBase*>();
}
}
// ---------------------------------------------------------------------
void WsjcppUnitTests::addUnitTest(const std::string &sTestName, WsjcppUnitTestBase* pUnitTest) {
WsjcppUnitTests::initGlobalVariables();
bool bFound = false;
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
WsjcppUnitTestBase* p = g_pWsjcppUnitTests->at(i);
if (p->getName() == sTestName) {
bFound = true;
}
}
if (bFound) {
WsjcppLog::err(sTestName, "Already registered");
} else {
g_pWsjcppUnitTests->push_back(pUnitTest);
// Log::info(sCmd, "Registered");
}
}
// ---------------------------------------------------------------------