2
2
#include < wsjcpp_core.h>
3
3
#include < wsjcpp_unit_tests.h>
4
4
5
+ void printHelp (const std::string &sProgramName ) {
6
+ std::string sOutput = " \n Help:\n " ;
7
+ sOutput +=
8
+ " '" + sProgramName + " ' - run all unit-tests\n "
9
+ " '" + sProgramName + " help' - print this help\n "
10
+ " '" + sProgramName + " list' - print list of unit-tests\n "
11
+ " '" + sProgramName + " run <TestName>' - run single unit-test\n "
12
+ ;
13
+ WsjcppLog::info (" UnitTests" , sOutput );
14
+ }
15
+
5
16
int main (int argc, char ** argv) {
6
17
WsjcppCore::initRandom ();
7
18
std::string TAG = " UnitTests" ;
@@ -16,9 +27,68 @@ int main(int argc, char** argv) {
16
27
return -1 ;
17
28
}
18
29
19
- if (!WsjcppUnitTests::runUnitTests ()) {
20
- WsjcppLog::err (TAG, " Some unit tests failed" );
30
+ WsjcppUnitTests::initGlobalVariables ();
31
+ std::string sProgramName (argv[0 ]);
32
+
33
+ if (argc == 1 ) {
34
+ int nAll = g_pWsjcppUnitTests->size ();
35
+ WsjcppLog::info (" runUnitTests" , " All tests count " + std::to_string (nAll));
36
+ int nSuccess = 0 ;
37
+ for (int i = 0 ; i < g_pWsjcppUnitTests->size (); i++) {
38
+ WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at (i);
39
+ std::string sTestName = pUnitTest->name ();
40
+ WsjcppLog::info (" runUnitTests" , " Run test " + sTestName );
41
+ if (pUnitTest->run ()) {
42
+ WsjcppLog::ok (sTestName , " Test passed" );
43
+ nSuccess++;
44
+ } else {
45
+ WsjcppLog::err (sTestName , " Test failed" );
46
+ }
47
+ }
48
+ WsjcppLog::info (TAG, " Passed tests " + std::to_string (nSuccess) + " / " + std::to_string (nAll));
49
+ bool bResult = nSuccess == nAll;
50
+ return bResult ? 0 : -1 ;
51
+ } else if (argc == 2 ) {
52
+ std::string sArg2 (argv[1 ]);
53
+ if (sArg2 == " list" ) {
54
+ std::string sOutput = " \n List of unit-tests:\n " ;
55
+ for (int i = 0 ; i < g_pWsjcppUnitTests->size (); i++) {
56
+ WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at (i);
57
+ sOutput += " - " + pUnitTest->name () + " \n " ;
58
+ }
59
+ WsjcppLog::info (TAG, sOutput );
60
+ return -1 ;
61
+ } else if (sArg2 == " help" ) {
62
+ printHelp (sProgramName );
63
+ return -1 ;
64
+ }
65
+ } else if (argc == 3 ) {
66
+ std::string sArg2 (argv[1 ]);
67
+ std::string sArg3 (argv[2 ]);
68
+ if (sArg2 == " run" ) {
69
+ int nSuccess = 0 ;
70
+ bool bTestFound = false ;
71
+ for (int i = 0 ; i < g_pWsjcppUnitTests->size (); i++) {
72
+ WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at (i);
73
+ if (pUnitTest->name () == sArg3 ) {
74
+ bTestFound = true ;
75
+ if (pUnitTest->run ()) {
76
+ WsjcppLog::ok (TAG, pUnitTest->name () + " Test passed" );
77
+ nSuccess++;
78
+ } else {
79
+ WsjcppLog::err (TAG, pUnitTest->name () + " Test failed" );
80
+ }
81
+ }
82
+ }
83
+ if (!bTestFound) {
84
+ WsjcppLog::err (TAG, " Test not found try help" );
85
+ }
86
+ return -1 ;
87
+ }
88
+ printHelp (sProgramName );
21
89
return -1 ;
22
90
}
91
+
92
+ printHelp (sProgramName );
23
93
return 0 ;
24
94
}
0 commit comments