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 double x = 3.0/0.0;
77 logInformation += std::to_string(x);
78 }
79
80 // run nested test cases recursively
81 std::string fullQualifiedName = _testCaseName.length()>0 ? prefix +_testCaseName + "." : prefix;
82 for (std::map<std::string, TreeTestCaseCollection*>::iterator p = _subTests.begin(); p!=_subTests.end(); p++ ) {
83 (*p).second->run( fullQualifiedName );
84 _errors += (*p).second->getNumberOfErrors();
85 }
86
87 for (std::list<tarch::tests::TestCase*>::iterator p = _testCases.begin(); p!=_testCases.end(); p++ ) {
88 tarch::tests::TestCase* currentTestCase = *p;
89 logInformation += ".";
90 currentTestCase->run();
91 int additionalErrors = currentTestCase->getNumberOfErrors();
92 if (additionalErrors>0) {
93 logInformation += "x";
94 _errors += currentTestCase->getNumberOfErrors();
95 }
96 else {
97 logInformation += ".";
98 }
99 }
100
101 if ( _errors==0 and _testCases.empty() and _subTests.empty() ) {
102 logInformation += " pass (no local tests)";
103 }
104 else if ( _errors==0 and _testCases.empty() ) {
105 logInformation += " pass (no local tests, subtests all successful)";
106 }
107 else if (_errors==0 and _testCases.size()==1) {
108 logInformation += " ok (1 test)";
109 }
110 else if (_errors==0) {
111 logInformation += " ok (" + std::to_string(_testCases.size()) + " tests)";
112 }
113 else {
114 logInformation += " failed";
115 }
116
117 if ( _writeToLog and _testCaseName!="" ) {
118 logInfo("run()",logInformation );
119 }
120}
121
122
124 addTestCase( testCase->getTestCaseName(), testCase );
125}
126
127
128void tarch::tests::TreeTestCaseCollection::addTestCase( const std::string& fullQualifiedPath, TestCase* testCase ) {
129 if ( isNameWithoutHierarchy(fullQualifiedPath) ) {
130 _testCases.push_back(testCase);
131 }
132 else {
133 std::string firstPartOfIdentifier = getFirstIdentifierInHierarchy( fullQualifiedPath );
134 std::string secondPartOfIdentifier = getRemainingPathWithoutIdentifier( fullQualifiedPath );
135 if (_subTests.find(firstPartOfIdentifier) == _subTests.end() ) {
136 _subTests.insert(
137 std::pair<std::string,TreeTestCaseCollection*>(
138 firstPartOfIdentifier,
139 new TreeTestCaseCollection( firstPartOfIdentifier, _deleteTestCases, _writeToLog )
140 )
141 );
142 }
143 _subTests[firstPartOfIdentifier]->addTestCase(secondPartOfIdentifier, testCase);
144 }
145}
#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)