1
#include <cppunit/Portability.h>
2
#include <cppunit/Test.h>
3
#include <cppunit/TestPath.h>
15
TestPath::TestPath( Test *root )
21
TestPath::TestPath( const TestPath &other,
25
int countAdjustment = 0;
28
countAdjustment = indexFirst;
33
count = other.getTestCount();
35
count += countAdjustment;
37
int index = indexFirst;
38
while ( count-- > 0 && index < other.getTestCount() )
39
add( other.getTestAt( index++ ) );
43
TestPath::TestPath( Test *searchRoot,
44
const std::string &pathAsString )
46
PathTestNames testNames;
48
Test *parentTest = findActualRoot( searchRoot, pathAsString, testNames );
51
for ( unsigned int index = 1; index < testNames.size(); ++index )
53
bool childFound = false;
54
for ( int childIndex =0; childIndex < parentTest->getChildTestCount(); ++childIndex )
56
if ( parentTest->getChildTestAt( childIndex )->getName() == testNames[index] )
59
parentTest = parentTest->getChildTestAt( childIndex );
65
throw std::invalid_argument( "TestPath::TestPath(): failed to resolve test name <"+
66
testNames[index] + "> of path <" + pathAsString + ">" );
73
TestPath::TestPath( const TestPath &other )
74
: m_tests( other.m_tests )
85
TestPath::operator =( const TestPath &other )
88
m_tests = other.m_tests;
94
TestPath::isValid() const
96
return getTestCount() > 0;
101
TestPath::add( Test *test )
103
m_tests.push_back( test );
108
TestPath::add( const TestPath &path )
110
for ( int index =0; index < path.getTestCount(); ++index )
111
add( path.getTestAt( index ) );
116
TestPath::insert( Test *test,
119
if ( index < 0 || index > getTestCount() )
120
throw std::out_of_range( "TestPath::insert(): index out of range" );
121
m_tests.insert( m_tests.begin() + index, test );
125
TestPath::insert( const TestPath &path,
128
int itemIndex = path.getTestCount() -1;
129
while ( itemIndex >= 0 )
130
insert( path.getTestAt( itemIndex-- ), index );
135
TestPath::removeTests()
143
TestPath::removeTest( int index )
145
checkIndexValid( index );
146
m_tests.erase( m_tests.begin() + index );
153
checkIndexValid( 0 );
154
removeTest( getTestCount() -1 );
159
TestPath::getTestCount() const
161
return m_tests.size();
166
TestPath::getTestAt( int index ) const
168
checkIndexValid( index );
169
return m_tests[index];
174
TestPath::getChildTest() const
176
return getTestAt( getTestCount() -1 );
181
TestPath::checkIndexValid( int index ) const
183
if ( index < 0 || index >= getTestCount() )
184
throw std::out_of_range( "TestPath::checkIndexValid(): index out of range" );
189
TestPath::toString() const
191
std::string asString( "/" );
192
for ( int index =0; index < getTestCount(); ++index )
196
asString += getTestAt(index)->getName();
204
TestPath::findActualRoot( Test *searchRoot,
205
const std::string &pathAsString,
206
PathTestNames &testNames )
208
bool isRelative = splitPathString( pathAsString, testNames );
210
if ( isRelative && pathAsString.empty() )
213
if ( testNames.empty() )
214
throw std::invalid_argument( "TestPath::TestPath(): invalid root or root name in absolute path" );
216
Test *root = isRelative ? searchRoot->findTest( testNames[0] ) // throw if bad test name
218
if ( root->getName() != testNames[0] )
219
throw std::invalid_argument( "TestPath::TestPath(): searchRoot does not match path root name" );
226
TestPath::splitPathString( const std::string &pathAsString,
227
PathTestNames &testNames )
229
bool isRelative = (pathAsString.substr(0,1) != "/");
231
int index = (isRelative ? 0 : 1);
234
int separatorIndex = pathAsString.find( '/', index );
235
if ( separatorIndex >= 0 )
237
testNames.push_back( pathAsString.substr( index, separatorIndex - index ) );
238
index = separatorIndex + 1;
242
testNames.push_back( pathAsString.substr( index ) );