Skip to content

Commit e4f457c

Browse files
committed
Fixed #18 Redesign for unit-tests
1 parent 5c26e93 commit e4f457c

37 files changed

+325
-194
lines changed

src/wsjcpp_unit_tests.cpp

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,105 @@
11
#include "wsjcpp_unit_tests.h"
22
#include <cmath>
33

4+
// ---------------------------------------------------------------------
5+
// WsjcppUnitTestBase
6+
47
WsjcppUnitTestBase::WsjcppUnitTestBase(const std::string &sTestName) {
58
m_sTestName = sTestName;
69
TAG = m_sTestName;
10+
m_bTestResult = true;
711
WsjcppUnitTests::addUnitTest(sTestName, this);
812
}
913

1014
// ---------------------------------------------------------------------
1115

12-
std::string WsjcppUnitTestBase::name() {
16+
std::string WsjcppUnitTestBase::getName() {
1317
return m_sTestName;
1418
}
1519

1620
// ---------------------------------------------------------------------
1721

18-
void WsjcppUnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint,
22+
void WsjcppUnitTestBase::ok(const std::string &sSuccessMessage) {
23+
// print obly success message
24+
WsjcppLog::ok(TAG, sSuccessMessage);
25+
}
26+
27+
// ---------------------------------------------------------------------
28+
29+
void WsjcppUnitTestBase::fail(const std::string &sFailedMessage) {
30+
WsjcppLog::err(TAG, sFailedMessage);
31+
m_bTestResult = false;
32+
}
33+
34+
// ---------------------------------------------------------------------
35+
36+
bool WsjcppUnitTestBase::runTest() {
37+
WsjcppLog::info(TAG, "Start unit-test");
38+
WsjcppLog::info(TAG, "Do before unit-test");
39+
if (!doBeforeTest()) {
40+
fail("Problem with before unit-test");
41+
return false;
42+
}
43+
WsjcppLog::info(TAG, "Execute unit-test");
44+
try {
45+
executeTest();
46+
} catch(const std::exception& e) {
47+
fail(e.what());
48+
} catch(...) {
49+
50+
}
51+
if (m_bTestResult) {
52+
ok("Test passed.");
53+
} else {
54+
fail("Test failed.");
55+
}
56+
WsjcppLog::info(TAG, "Do after unit-test");
57+
if (!doAfterTest()) {
58+
fail("Problem with after unit-test");
59+
}
60+
WsjcppLog::info(TAG, "End unit-test");
61+
return m_bTestResult;
62+
}
63+
64+
// ---------------------------------------------------------------------
65+
66+
bool WsjcppUnitTestBase::compareS(const std::string &sMark,
1967
const std::string &sValue, const std::string &sExpected) {
2068
if (sValue != sExpected) {
21-
WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + sExpected + "', but got '" + sValue + "'");
22-
bTestSuccess = false;
69+
fail(" {" + sMark + "} Expected '" + sExpected + "', but got '" + sValue + "'");
70+
return false;
2371
}
72+
return true;
2473
}
2574

2675
// ---------------------------------------------------------------------
2776

28-
bool WsjcppUnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected) {
77+
bool WsjcppUnitTestBase::compareN(const std::string &sMark, int nValue, int nExpected) {
2978
if (nValue != nExpected) {
30-
WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
31-
bTestSuccess = false;
79+
fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
3280
return false;
3381
}
3482
return true;
3583
}
3684

3785
// ---------------------------------------------------------------------
3886

39-
bool WsjcppUnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected) {
87+
bool WsjcppUnitTestBase::compareD(const std::string &sMark, double nValue, double nExpected) {
4088
if (abs(nValue - nExpected) > std::numeric_limits<double>::epsilon()) {
41-
WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
42-
bTestSuccess = false;
89+
fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
4390
return false;
4491
}
4592
return true;
4693
}
4794

4895
// ---------------------------------------------------------------------
4996

50-
void WsjcppUnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected) {
97+
bool WsjcppUnitTestBase::compareB(const std::string &sMark, bool bValue, bool bExpected) {
5198
if (bValue != bExpected) {
52-
WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'");
53-
bTestSuccess = false;
99+
fail(" {" + sMark + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'");
100+
return false;
54101
}
102+
return true;
55103
}
56104

57105
// ---------------------------------------------------------------------
@@ -72,7 +120,7 @@ void WsjcppUnitTests::addUnitTest(const std::string &sTestName, WsjcppUnitTestBa
72120
bool bFound = false;
73121
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
74122
WsjcppUnitTestBase* p = g_pWsjcppUnitTests->at(i);
75-
if (p->name() == sTestName) {
123+
if (p->getName() == sTestName) {
76124
bFound = true;
77125
}
78126
}

src/wsjcpp_unit_tests.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
#ifndef WSJCPP_UNIT_TESTS_H
22
#define WSJCPP_UNIT_TESTS_H
33

4-
#include <map>
5-
#include <vector>
64
#include <wsjcpp_core.h>
5+
#include <sstream>
76

87
class WsjcppUnitTestBase {
98
public:
109
WsjcppUnitTestBase(const std::string &sTestName);
11-
std::string name();
12-
virtual void init() = 0;
13-
virtual bool run() = 0;
10+
std::string getName();
11+
void ok(const std::string &sSuccessMessage);
12+
void fail(const std::string &sFailedMessage);
13+
bool runTest();
14+
15+
virtual bool doBeforeTest() = 0;
16+
virtual void executeTest() = 0;
17+
virtual bool doAfterTest() = 0;
1418
protected:
1519
std::string TAG;
1620

17-
void compareS(bool &bTestSuccess, const std::string &sPoint, const std::string &sValue, const std::string &sExpected);
18-
bool compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected);
19-
bool compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected);
20-
void compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected);
21-
21+
bool compareS(const std::string &sMark, const std::string &sValue, const std::string &sExpected);
22+
bool compareN(const std::string &sMark, int nValue, int nExpected);
23+
bool compareD(const std::string &sMark, double nValue, double nExpected);
24+
bool compareB(const std::string &sMark, bool bValue, bool bExpected);
25+
template<typename T1, typename T2> bool compare(const std::string &sMark, T1 tGotValue, T2 tExpectedValue) {
26+
if (tGotValue != tExpectedValue) {
27+
std::stringstream ss;
28+
ss << " {mark: " << sMark << "} Expected '" << tExpectedValue << "', but got '" << tGotValue << "'";
29+
fail(ss.str());
30+
return false;
31+
}
32+
return true;
33+
}
2234
private:
35+
bool m_bTestResult;
2336
std::string m_sTestName;
2437
};
2538

src/wsjcpp_unit_tests_main.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ int main(int argc, char** argv) {
3636
int nSuccess = 0;
3737
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
3838
WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i);
39-
std::string sTestName = pUnitTest->name();
40-
WsjcppLog::info(TAG, "Run test " + sTestName);
41-
if (pUnitTest->run()) {
42-
WsjcppLog::ok(sTestName, "Test passed");
39+
if (pUnitTest->runTest()) {
4340
nSuccess++;
44-
} else {
45-
WsjcppLog::err(sTestName, "Test failed");
4641
}
4742
}
4843
WsjcppLog::info(TAG, "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll));
@@ -54,7 +49,7 @@ int main(int argc, char** argv) {
5449
std::string sOutput = "\nList of unit-tests:\n";
5550
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
5651
WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i);
57-
sOutput += " - " + pUnitTest->name() + "\n";
52+
sOutput += " - " + pUnitTest->getName() + "\n";
5853
}
5954
WsjcppLog::info(TAG, sOutput);
6055
return -1;
@@ -70,15 +65,11 @@ int main(int argc, char** argv) {
7065
bool bTestFound = false;
7166
for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) {
7267
WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i);
73-
if (pUnitTest->name() == sArg3) {
68+
if (pUnitTest->getName() == sArg3) {
7469
bTestFound = true;
75-
WsjcppLog::info(TAG, "Run test " + pUnitTest->name());
76-
bool bResult = pUnitTest->run();
70+
bool bResult = pUnitTest->runTest();
7771
if (bResult) {
78-
WsjcppLog::ok(TAG, pUnitTest->name() + " Test passed");
7972
nSuccess++;
80-
} else {
81-
WsjcppLog::err(TAG, pUnitTest->name() + " Test failed");
8273
}
8374
}
8475
}

unit-tests.wsjcpp/src/unit_test_core_extract_filename.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ UnitTestCoreExtractFilename::UnitTestCoreExtractFilename()
1212

1313
// ---------------------------------------------------------------------
1414

15-
void UnitTestCoreExtractFilename::init() {
15+
bool UnitTestCoreExtractFilename::doBeforeTest() {
1616
// nothing
17+
return true;
1718
}
1819

1920
// ---------------------------------------------------------------------
2021

21-
bool UnitTestCoreExtractFilename::run() {
22+
void UnitTestCoreExtractFilename::executeTest() {
2223
std::map<std::string, std::string> mapPaths;
2324
mapPaths.insert(std::pair<std::string, std::string>("",""));
2425
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/bin/some","some"));
@@ -35,17 +36,17 @@ bool UnitTestCoreExtractFilename::run() {
3536
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/../../../bin/./some/",""));
3637
mapPaths.insert(std::pair<std::string, std::string>(".//",""));
3738
std::map<std::string, std::string>::iterator it;
38-
39-
int nCounter = 0;
4039
for ( it = mapPaths.begin(); it != mapPaths.end(); it++ ) {
4140
std::string sSource = it->first;
4241
std::string sExpected = it->second;
4342
std::string sGot = WsjcppCore::extractFilename(sSource);
44-
if (sGot != sExpected) {
45-
nCounter++;
46-
WsjcppLog::err(TAG, "Wrong extract filename for: '" + sSource + "'.\n\t Got: '" + sGot + "', but expected: '" + sExpected + "'");
47-
}
48-
43+
compare(sSource, sGot, sExpected);
4944
}
50-
return nCounter == 0;
45+
}
46+
47+
// ---------------------------------------------------------------------
48+
49+
bool UnitTestCoreExtractFilename::doAfterTest() {
50+
// nothing
51+
return true;
5152
}

unit-tests.wsjcpp/src/unit_test_core_extract_filename.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
class UnitTestCoreExtractFilename : public WsjcppUnitTestBase {
77
public:
88
UnitTestCoreExtractFilename();
9-
virtual void init();
10-
virtual bool run();
9+
virtual bool doBeforeTest() override;
10+
virtual void executeTest() override;
11+
virtual bool doAfterTest() override;
1112
};
1213

1314
#endif // UNIT_TEST_CORE_EXCTRACT_FILENAME_H

unit-tests.wsjcpp/src/unit_test_core_normalize_path.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ UnitTestCoreNormalizePath::UnitTestCoreNormalizePath()
1212

1313
// ---------------------------------------------------------------------
1414

15-
void UnitTestCoreNormalizePath::init() {
15+
bool UnitTestCoreNormalizePath::doBeforeTest() {
1616
// nothing
17+
return true;
1718
}
1819

1920
// ---------------------------------------------------------------------
2021

21-
bool UnitTestCoreNormalizePath::run() {
22+
void UnitTestCoreNormalizePath::executeTest() {
2223
std::map<std::string, std::string> mapPaths;
2324
mapPaths.insert(std::pair<std::string, std::string>("",""));
2425
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/bin/some","/usr/local/bin/some"));
@@ -36,16 +37,17 @@ bool UnitTestCoreNormalizePath::run() {
3637
mapPaths.insert(std::pair<std::string, std::string>(".//","./"));
3738
std::map<std::string, std::string>::iterator it;
3839

39-
int nCounter = 0;
4040
for ( it = mapPaths.begin(); it != mapPaths.end(); it++ ) {
4141
std::string sSource = it->first;
4242
std::string sExpected = it->second;
4343
std::string sGot = WsjcppCore::doNormalizePath(sSource);
44-
if (sGot != sExpected) {
45-
nCounter++;
46-
WsjcppLog::err(TAG, "Wrong normalize path for: '" + sSource + "'.\n\t Got: '" + sGot + "', but expected: '" + sExpected + "'");
47-
}
48-
44+
compare(sSource, sGot, sExpected);
4945
}
50-
return nCounter == 0;
51-
}
46+
}
47+
48+
// ---------------------------------------------------------------------
49+
50+
bool UnitTestCoreNormalizePath::doAfterTest() {
51+
// nothing
52+
return true;
53+
}

unit-tests.wsjcpp/src/unit_test_core_normalize_path.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
class UnitTestCoreNormalizePath : public WsjcppUnitTestBase {
77
public:
88
UnitTestCoreNormalizePath();
9-
virtual void init();
10-
virtual bool run();
9+
virtual bool doBeforeTest() override;
10+
virtual void executeTest() override;
11+
virtual bool doAfterTest() override;
1112
};
1213

1314
#endif // UNIT_TEST_CORE_NORMALIZE_PATH_H

unit-tests.wsjcpp/src/unit_test_create_empty_file.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,28 @@ UnitTestCreateEmptyFile::UnitTestCreateEmptyFile()
1010

1111
// ---------------------------------------------------------------------
1212

13-
void UnitTestCreateEmptyFile::init() {
13+
bool UnitTestCreateEmptyFile::doBeforeTest() {
1414
// nothing
15+
return true;
1516
}
1617

1718
// ---------------------------------------------------------------------
1819

19-
bool UnitTestCreateEmptyFile::run() {
20-
bool bTestSuccess = true;
21-
compareB(bTestSuccess, "./data/", WsjcppCore::dirExists("./data/"), true);
20+
void UnitTestCreateEmptyFile::executeTest() {
21+
compareB("./data/", WsjcppCore::dirExists("./data/"), true);
2222
std::string sFilename = "./data/empty.txt";
2323
if (WsjcppCore::fileExists(sFilename)) {
2424
WsjcppCore::removeFile(sFilename);
2525
}
26-
compareB(bTestSuccess, "fileExists 1: " + sFilename, WsjcppCore::fileExists(sFilename), false);
27-
compareB(bTestSuccess, "createEmptyFile 1: " + sFilename, WsjcppCore::createEmptyFile(sFilename), true);
28-
compareB(bTestSuccess, "createEmptyFile 2: " + sFilename, WsjcppCore::createEmptyFile(sFilename), false);
29-
compareB(bTestSuccess, "fileExists 2: " + sFilename, WsjcppCore::fileExists(sFilename), true);
30-
return bTestSuccess;
26+
compareB("fileExists 1: " + sFilename, WsjcppCore::fileExists(sFilename), false);
27+
compareB("createEmptyFile 1: " + sFilename, WsjcppCore::createEmptyFile(sFilename), true);
28+
compareB("createEmptyFile 2: " + sFilename, WsjcppCore::createEmptyFile(sFilename), false);
29+
compareB("fileExists 2: " + sFilename, WsjcppCore::fileExists(sFilename), true);
3130
}
3231

32+
// ---------------------------------------------------------------------
33+
34+
bool UnitTestCreateEmptyFile::doAfterTest() {
35+
// nothing
36+
return true;
37+
}

unit-tests.wsjcpp/src/unit_test_create_empty_file.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
class UnitTestCreateEmptyFile : public WsjcppUnitTestBase {
88
public:
99
UnitTestCreateEmptyFile();
10-
virtual void init();
11-
virtual bool run();
10+
virtual bool doBeforeTest() override;
11+
virtual void executeTest() override;
12+
virtual bool doAfterTest() override;
1213
};
1314

1415
#endif // UNIT_TEST_CREATE_EMPTY_FILE_H

0 commit comments

Comments
 (0)