11#include " wsjcpp_unit_tests.h"
22#include < cmath>
33
4+ // ---------------------------------------------------------------------
5+ // WsjcppUnitTestBase
6+
47WsjcppUnitTestBase::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 }
0 commit comments