Skip to content

Commit ad06082

Browse files
committed
Added unit-tests
1 parent 37529aa commit ad06082

9 files changed

+262
-23
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class YourEmploy : public WSJCppEmployBase {
7878
public:
7979
YourEmploy();
8080
static std::string name() { return "YourEmploy"; }
81-
void do();
81+
virtual bool init() override;
82+
void doSomething();
8283
private:
8384
std::string TAG;
8485
};

src/wsjcpp_employees.cpp

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
std::map<std::string, WSJCppEmployBase*> *g_pWSJCppEmployees = nullptr;
99
std::vector<std::string> *g_pWSJCppInitEmployees = nullptr;
10+
std::vector<std::string> *g_pWSJCppInitWith = nullptr;
1011

1112
// ---------------------------------------------------------------------
1213

@@ -19,15 +20,21 @@ void WSJCppEmployees::initGlobalVariables() {
1920
// WSJCppLog::info(std::string(), "Create init employees vector");
2021
g_pWSJCppInitEmployees = new std::vector<std::string>();
2122
}
23+
if (g_pWSJCppInitWith == nullptr) {
24+
// WSJCppLog::info(std::string(), "Create init employees vector");
25+
g_pWSJCppInitWith = new std::vector<std::string>();
26+
}
2227
}
2328

2429
// ---------------------------------------------------------------------
2530

2631
void WSJCppEmployees::deinitGlobalVariables() {
32+
const std::string TAG = "WSJCppEmployees::deinit";
2733
if (g_pWSJCppEmployees != nullptr) {
2834
std::map<std::string, WSJCppEmployBase*>::iterator it;
2935
for (it = g_pWSJCppEmployees->begin(); it != g_pWSJCppEmployees->end(); ++it) {
30-
std::string sName = it->first;
36+
std::string sEmployName = it->first;
37+
WSJCppLog::ok(TAG, sEmployName + " ... UNREGISTERED");
3138
delete it->second;
3239
it->second = nullptr;
3340
}
@@ -41,6 +48,12 @@ void WSJCppEmployees::deinitGlobalVariables() {
4148
delete g_pWSJCppInitEmployees;
4249
g_pWSJCppInitEmployees = nullptr;
4350
}
51+
52+
if (g_pWSJCppInitWith != nullptr) {
53+
g_pWSJCppInitWith->clear();
54+
delete g_pWSJCppInitWith;
55+
g_pWSJCppInitWith = nullptr;
56+
}
4457
}
4558

4659
// ---------------------------------------------------------------------
@@ -59,12 +72,14 @@ void WSJCppEmployees::addEmploy(const std::string &sName, WSJCppEmployBase* pEmp
5972

6073
bool WSJCppEmployees::init(const std::vector<std::string> &vStart) {
6174
WSJCppEmployees::initGlobalVariables();
75+
std::string TAG = "WSJCppEmployees::init";
6276

6377
for (unsigned int i = 0; i < vStart.size(); i++) {
6478
g_pWSJCppInitEmployees->push_back(vStart[i]);
79+
g_pWSJCppInitWith->push_back(vStart[i]);
80+
WSJCppLog::info(TAG, "with " + vStart[i]);
6581
}
66-
67-
std::string TAG = "Employees_init";
82+
6883
bool bRepeat = true;
6984
while (bRepeat) {
7085
bRepeat = false;
@@ -86,18 +101,64 @@ bool WSJCppEmployees::init(const std::vector<std::string> &vStart) {
86101
}
87102
if (pEmploy->loadAfter().size() == nRequireLoaded) {
88103
if (!pEmploy->init()) {
89-
WSJCppLog::err(TAG, "Init " + sEmployName + " ... FAIL");
104+
WSJCppLog::err(TAG, sEmployName + " ... INIT_FAIL");
90105
return false;
91106
}
92107
g_pWSJCppInitEmployees->push_back(sEmployName);
93108
bRepeat = true;
94-
WSJCppLog::ok(TAG, "Init " + sEmployName + " ... OK");
109+
WSJCppLog::ok(TAG, sEmployName + " ... INIT_OK");
95110
}
96111
}
97112
}
98113
return true;
99114
}
100115

116+
// ---------------------------------------------------------------------
117+
118+
bool WSJCppEmployees::deinit() {
119+
const std::string TAG = "WSJCppEmployees::deinit";
120+
if (g_pWSJCppInitEmployees == nullptr
121+
|| g_pWSJCppInitWith == nullptr
122+
|| g_pWSJCppEmployees == nullptr
123+
) {
124+
WSJCppLog::err(TAG, "You must call WSJCppEmployees::init before deinit");
125+
return false;
126+
}
127+
128+
int nInitedCount = g_pWSJCppInitEmployees->size();
129+
for (int i = nInitedCount-1; i >= 0; i--) {
130+
std::string sEmployName = g_pWSJCppInitEmployees->at(i);
131+
if (std::find(g_pWSJCppInitWith->begin(), g_pWSJCppInitWith->end(), sEmployName) != g_pWSJCppInitWith->end()) {
132+
WSJCppLog::info(TAG, sEmployName + " ... SKIP_INIT_WITH");
133+
continue;
134+
}
135+
136+
std::map<std::string, WSJCppEmployBase*>::iterator it;
137+
it = g_pWSJCppEmployees->find(sEmployName);
138+
if (it == g_pWSJCppEmployees->end()) {
139+
WSJCppLog::err(TAG, sEmployName + " ... DEINIT_NOT_FOUND");
140+
return false;
141+
}
142+
WSJCppEmployBase *pEmploy = it->second;
143+
if (pEmploy->deinit()) {
144+
WSJCppLog::ok(TAG, sEmployName + " ... DEINIT_OK");
145+
} else {
146+
WSJCppLog::err(TAG, sEmployName + " ... DEINIT_FAIL");
147+
return false;
148+
}
149+
};
150+
151+
g_pWSJCppInitEmployees->clear();
152+
delete g_pWSJCppInitEmployees;
153+
g_pWSJCppInitEmployees = nullptr;
154+
155+
g_pWSJCppInitWith->clear();
156+
delete g_pWSJCppInitWith;
157+
g_pWSJCppInitWith = nullptr;
158+
return true;
159+
}
160+
161+
101162
// ---------------------------------------------------------------------
102163

103164
// WSJCppEmployBase
@@ -119,19 +180,6 @@ WSJCppEmployBase::~WSJCppEmployBase() {
119180

120181
// ---------------------------------------------------------------------
121182

122-
bool WSJCppEmployBase::init() {
123-
// nothing
124-
return true;
125-
}
126-
127-
// ---------------------------------------------------------------------
128-
129-
void WSJCppEmployBase::deinit() {
130-
// nothing
131-
}
132-
133-
// ---------------------------------------------------------------------
134-
135183
const std::vector<std::string> &WSJCppEmployBase::loadAfter() {
136184
return m_vLoadAfter;
137185
}
@@ -160,6 +208,15 @@ bool WJSCppEmployRuntimeGlobalCache::init() {
160208

161209
// ---------------------------------------------------------------------
162210

211+
bool WJSCppEmployRuntimeGlobalCache::deinit() {
212+
// checking settings
213+
WSJCppLog::info(TAG, "deinit");
214+
m_sStringMap.clear();
215+
return true;
216+
}
217+
218+
// ---------------------------------------------------------------------
219+
163220
void WJSCppEmployRuntimeGlobalCache::set(const std::string &sName, const std::string &sValue) {
164221
m_sStringMap[sName] = sValue;
165222
}

src/wsjcpp_employees.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <map>
55
#include <string>
66
#include <vector>
7+
#include <mutex>
78
#include <wsjcpp_core.h>
89

910
// ---------------------------------------------------------------------
@@ -16,8 +17,8 @@ class WSJCppEmployBase {
1617
const std::vector<std::string> &vLoadAfter
1718
);
1819
virtual ~WSJCppEmployBase();
19-
virtual bool init();
20-
virtual void deinit();
20+
virtual bool init() = 0;
21+
virtual bool deinit() = 0;
2122
const std::vector<std::string> &loadAfter();
2223

2324
private:
@@ -38,6 +39,7 @@ class WSJCppEmployees {
3839
static void deinitGlobalVariables();
3940
static void addEmploy(const std::string &sName, WSJCppEmployBase* pEmploy);
4041
static bool init(const std::vector<std::string> &vLoadAfter);
42+
static bool deinit();
4143
};
4244

4345
// ---------------------------------------------------------------------
@@ -73,7 +75,8 @@ class WJSCppEmployRuntimeGlobalCache : public WSJCppEmployBase {
7375
public:
7476
WJSCppEmployRuntimeGlobalCache();
7577
static std::string name() { return "WJSCppEmployRuntimeGlobalCache"; }
76-
virtual bool init();
78+
virtual bool init() override;
79+
virtual bool deinit() override;
7780
void set(const std::string &sName, const std::string &sValue);
7881
bool has(const std::string &sName);
7982
std::string get(const std::string &sName);

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ list (APPEND WSJCPP_SOURCES "../src/wsjcpp_employees.cpp")
3434

3535
# unit-tests
3636
list (APPEND WSJCPP_INCLUDE_DIRS "src")
37+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_employ1.h")
38+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_employ1.cpp")
39+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_employ_runtime_global_cache.h")
40+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_employ_runtime_global_cache.cpp")
3741

3842
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
3943

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "unit_test_employ1.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
#include <wsjcpp_employees.h>
5+
6+
// ---------------------------------------------------------------------
7+
8+
9+
class Employ1 : public WSJCppEmployBase {
10+
public:
11+
Employ1();
12+
static std::string name() { return "Employ1"; };
13+
virtual bool init() override;
14+
virtual bool deinit() override;
15+
16+
void set(const std::string &sValue);
17+
std::string get();
18+
private:
19+
std::string TAG;
20+
std::string m_sValue;
21+
};
22+
23+
REGISTRY_WJSCPP_EMPLOY(Employ1)
24+
25+
Employ1::Employ1()
26+
: WSJCppEmployBase(Employ1::name(), {"unit-test-employ1"}) {
27+
TAG = Employ1::name();
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
bool Employ1::init() {
33+
m_sValue = "";
34+
return true;
35+
}
36+
37+
// ---------------------------------------------------------------------
38+
39+
bool Employ1::deinit() {
40+
m_sValue = "";
41+
return true;
42+
}
43+
44+
// ---------------------------------------------------------------------
45+
46+
void Employ1::set(const std::string &sValue) {
47+
m_sValue = sValue;
48+
};
49+
50+
// ---------------------------------------------------------------------
51+
52+
std::string Employ1::get() {
53+
return m_sValue;
54+
}
55+
56+
// ---------------------------------------------------------------------
57+
58+
REGISTRY_UNIT_TEST(UnitTestEmploy1)
59+
60+
UnitTestEmploy1::UnitTestEmploy1()
61+
: WSJCppUnitTestBase("UnitTestEmploy1") {
62+
}
63+
64+
// ---------------------------------------------------------------------
65+
66+
void UnitTestEmploy1::init() {
67+
// nothing
68+
}
69+
70+
// ---------------------------------------------------------------------
71+
72+
bool UnitTestEmploy1::run() {
73+
bool bTestSuccess = true;
74+
75+
WSJCppLog::warn(TAG, "init alone employees");
76+
bool bResult = WSJCppEmployees::init({});
77+
compareB(bTestSuccess, "without-employ1", bResult, true);
78+
if (!bResult) {
79+
return bTestSuccess;
80+
}
81+
WJSCppEmployRuntimeGlobalCache *pCache = findWSJCppEmploy<WJSCppEmployRuntimeGlobalCache>();
82+
WSJCppEmployees::deinit();
83+
84+
// start new
85+
86+
bResult = WSJCppEmployees::init({"unit-test-employ1"});
87+
compareB(bTestSuccess, "unit-test-employ1", bResult, true);
88+
if (!bResult) {
89+
return bTestSuccess;
90+
}
91+
92+
Employ1 *pEmploy1 = findWSJCppEmploy<Employ1>();
93+
94+
pEmploy1->set("test4562132");
95+
compareS(bTestSuccess, "value", pEmploy1->get(), "test4562132");
96+
97+
WSJCppEmployees::deinit();
98+
return bTestSuccess;
99+
}
100+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_EMPLOY1_H
2+
#define UNIT_TEST_EMPLOY1_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestEmploy1 : public WSJCppUnitTestBase {
8+
public:
9+
UnitTestEmploy1();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_EMPLOY1_H
15+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "unit_test_employ_runtime_global_cache.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
#include <wsjcpp_employees.h>
5+
6+
REGISTRY_UNIT_TEST(UnitTestEmployRuntimeGlobalCache)
7+
8+
UnitTestEmployRuntimeGlobalCache::UnitTestEmployRuntimeGlobalCache()
9+
: WSJCppUnitTestBase("UnitTestEmployRuntimeGlobalCache") {
10+
}
11+
12+
// ---------------------------------------------------------------------
13+
14+
void UnitTestEmployRuntimeGlobalCache::init() {
15+
// nothing
16+
}
17+
18+
// ---------------------------------------------------------------------
19+
20+
bool UnitTestEmployRuntimeGlobalCache::run() {
21+
bool bTestSuccess = true;
22+
23+
bool bResult = WSJCppEmployees::init({});
24+
compareB(bTestSuccess, "without-employ1", bResult, true);
25+
if (!bResult) {
26+
return bTestSuccess;
27+
}
28+
WJSCppEmployRuntimeGlobalCache *pCache = findWSJCppEmploy<WJSCppEmployRuntimeGlobalCache>();
29+
pCache->set("name1", "value3y2hf9f3h%%");
30+
compareB(bTestSuccess, "name1", pCache->has("name1"), true);
31+
if (pCache->has("name1")) {
32+
compareS(bTestSuccess, "name1-value", pCache->get("name1"), "value3y2hf9f3h%%");
33+
}
34+
compareB(bTestSuccess, "name2", pCache->has("name2"), false);
35+
WSJCppEmployees::deinit();
36+
return bTestSuccess;
37+
}
38+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_EMPLOY_RUNTIME_GLOBAL_CACHE_H
2+
#define UNIT_TEST_EMPLOY_RUNTIME_GLOBAL_CACHE_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestEmployRuntimeGlobalCache : public WSJCppUnitTestBase {
8+
public:
9+
UnitTestEmployRuntimeGlobalCache();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_EMPLOY_RUNTIME_GLOBAL_CACHE_H
15+

0 commit comments

Comments
 (0)