Peano
Loading...
Searching...
No Matches
TreeTestCaseCollection.cpp
Go to the documentation of this file.
2#include "tarch/Assertions.h"
3
4
5#include <iostream>
6
7
8tarch::logging::Log tarch::tests::TreeTestCaseCollection::_log("tarch::tests::TreeTestCaseCollection");
9
10
11tarch::tests::TreeTestCaseCollection::TreeTestCaseCollection(const std::string& treeTestCaseCollectionName, bool deleteTestCases, bool writeToLog):
12 TestCase::TestCase( treeTestCaseCollectionName ),
13 _writeToLog(writeToLog),
14 _deleteTestCases(deleteTestCases) {
15 assertion3( isNameWithoutHierarchy( treeTestCaseCollectionName ), treeTestCaseCollectionName, deleteTestCases, writeToLog);
16}
17
18
20 if (_deleteTestCases) {
21 for (std::list<tarch::tests::TestCase*>::iterator p = _testCases.begin(); p!=_testCases.end(); p++ ) {
22 tarch::tests::TestCase* currentTestCase = *p;
23 delete currentTestCase;
24 }
25 for (std::map<std::string, TreeTestCaseCollection*>::iterator p = _subTests.begin(); p!=_subTests.end(); p++ ) {
26 TreeTestCaseCollection* currentTestCase = (*p).second;
27 delete currentTestCase;
28 }
29 }
30}
31
32
34 return testCaseName.rfind("::") == std::string::npos;
35}
36
37
38std::string tarch::tests::TreeTestCaseCollection::getFirstIdentifierInHierarchy(const std::string& testCaseName) {
39 std::string result = testCaseName.substr(0, testCaseName.find("::"));
40
41 assertion2( isNameWithoutHierarchy( result), result, testCaseName );
42
43 return result;
44}
45
46
48 assertion( !isNameWithoutHierarchy( testCaseName) );
49
50 std::string result = testCaseName.substr(testCaseName.find("::")+2);
51
52 return result;
53}
54
55
59
60
61void tarch::tests::TreeTestCaseCollection::run( const std::string& prefix ) {
62 std::string logInformation;
63 if ( (prefix + _testCaseName).length()>0 ) {
64 logInformation = "running test case collection \"" + prefix + _testCaseName + "\" ...";
65 }
66 else {
67 logInformation = "running test case collection ...";
68 }
69
70 if (
71 _subTests.empty()
72 and
73 _testCases.empty()
74 ) {
75 logInformation += "... WARNING: no tests defined ... ";
76 }
77
78 // run nested test cases recursively
79 std::string fullQualifiedName = _testCaseName.length()>0 ? prefix +_testCaseName + "." : prefix;
80 for (std::map<std::string, TreeTestCaseCollection*>::iterator p = _subTests.begin(); p!=_subTests.end(); p++ ) {
81 (*p).second->run( fullQualifiedName );
82 _errors += (*p).second->getNumberOfErrors();
83 }
84
85 for (std::list<tarch::tests::TestCase*>::iterator p = _testCases.begin(); p!=_testCases.end(); p++ ) {
86 tarch::tests::TestCase* currentTestCase = *p;
87 logInformation += ".";
88 currentTestCase->run();
89 int additionalErrors = currentTestCase->getNumberOfErrors();
90 if (additionalErrors>0) {
91 logInformation += "x";
92 _errors += currentTestCase->getNumberOfErrors();
93 }
94 else {
95 logInformation += ".";
96 }
97 }
98
99 if ( _errors==0 and _testCases.empty() and _subTests.empty() ) {
100 logInformation += " pass (no local tests)";
101 }
102 else if ( _errors==0 and _testCases.empty() ) {
103 logInformation += " pass (no local tests, subtests all successful)";
104 }
105 else if (_errors==0 and _testCases.size()==1) {
106 logInformation += " ok (1 test)";
107 }
108 else if (_errors==0) {
109 logInformation += " ok (" + std::to_string(_testCases.size()) + " tests)";
110 }
111 else {
112 logInformation += " failed";
113 }
114
115 if ( _writeToLog and _testCaseName!="" ) {
116 logInfo("run()",logInformation );
117 }
118}
119
120
122 addTestCase( testCase->getTestCaseName(), testCase );
123}
124
125
126void tarch::tests::TreeTestCaseCollection::addTestCase( const std::string& fullQualifiedPath, TestCase* testCase ) {
127 if ( isNameWithoutHierarchy(fullQualifiedPath) ) {
128 _testCases.push_back(testCase);
129 }
130 else {
131 std::string firstPartOfIdentifier = getFirstIdentifierInHierarchy( fullQualifiedPath );
132 std::string secondPartOfIdentifier = getRemainingPathWithoutIdentifier( fullQualifiedPath );
133 if (_subTests.find(firstPartOfIdentifier) == _subTests.end() ) {
134 _subTests.insert(
135 std::pair<std::string,TreeTestCaseCollection*>(
136 firstPartOfIdentifier,
137 new TreeTestCaseCollection( firstPartOfIdentifier, _deleteTestCases, _writeToLog )
138 )
139 );
140 }
141 _subTests[firstPartOfIdentifier]->addTestCase(secondPartOfIdentifier, testCase);
142 }
143}
#define assertion2(expr, param0, param1)
#define assertion3(expr, param0, param1, param2)
#define assertion(expr)
#define logInfo(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:411
Log Device.
Definition Log.h:516
Represents one test case.
Definition TestCase.h:57
int getNumberOfErrors() const
Definition TestCase.cpp:11
virtual void run()=0
This routine is triggered by the TestCaseCollection.
std::string getTestCaseName() const
Definition TestCase.cpp:15
static std::string getRemainingPathWithoutIdentifier(const std::string &testCaseName)
static bool isNameWithoutHierarchy(const std::string &testCaseName)
void addTestCase(const std::string &fullQualifiedPath, TestCase *testCase)
Adds a new test case.
virtual void run()
Runs all test cases assigned.
static tarch::logging::Log _log
Log interface the class writes to.
TreeTestCaseCollection(const std::string &testCaseCollectionName="", bool deleteTestCases=true, bool writeToLog=true)
Creates a test case collection.
static std::string getFirstIdentifierInHierarchy(const std::string &testCaseName)