~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to third-party/cppunit/cppunit/src/cppunit/TestPath.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cppunit/Portability.h>
 
2
#include <cppunit/Test.h>
 
3
#include <cppunit/TestPath.h>
 
4
#include <stdexcept>
 
5
 
 
6
 
 
7
CPPUNIT_NS_BEGIN
 
8
 
 
9
 
 
10
TestPath::TestPath()
 
11
{
 
12
}
 
13
 
 
14
 
 
15
TestPath::TestPath( Test *root )
 
16
{
 
17
  add( root );
 
18
}
 
19
 
 
20
 
 
21
TestPath::TestPath( const TestPath &other, 
 
22
                    int indexFirst, 
 
23
                    int count )
 
24
{
 
25
  int countAdjustment = 0;
 
26
  if ( indexFirst < 0 )
 
27
  {
 
28
    countAdjustment = indexFirst;
 
29
    indexFirst = 0;
 
30
  }
 
31
 
 
32
  if ( count < 0 )
 
33
    count = other.getTestCount();
 
34
  else
 
35
    count += countAdjustment;
 
36
 
 
37
  int index = indexFirst;
 
38
  while ( count-- > 0  &&  index < other.getTestCount() )
 
39
    add( other.getTestAt( index++ ) );
 
40
}
 
41
 
 
42
 
 
43
TestPath::TestPath( Test *searchRoot, 
 
44
                    const std::string &pathAsString )
 
45
{
 
46
  PathTestNames testNames;
 
47
 
 
48
  Test *parentTest = findActualRoot( searchRoot, pathAsString, testNames );
 
49
  add( parentTest );
 
50
 
 
51
  for ( unsigned int index = 1; index < testNames.size(); ++index )
 
52
  {
 
53
    bool childFound = false;
 
54
    for ( int childIndex =0; childIndex < parentTest->getChildTestCount(); ++childIndex )
 
55
    {
 
56
      if ( parentTest->getChildTestAt( childIndex )->getName() == testNames[index] )
 
57
      {
 
58
        childFound = true;
 
59
        parentTest = parentTest->getChildTestAt( childIndex );
 
60
        break;
 
61
      }
 
62
    }
 
63
 
 
64
    if ( !childFound )
 
65
      throw std::invalid_argument( "TestPath::TestPath(): failed to resolve test name <"+
 
66
                                   testNames[index] + "> of path <" + pathAsString + ">" );
 
67
 
 
68
    add( parentTest );
 
69
  }
 
70
}
 
71
 
 
72
 
 
73
TestPath::TestPath( const TestPath &other )
 
74
  : m_tests( other.m_tests )
 
75
{
 
76
}
 
77
 
 
78
 
 
79
TestPath::~TestPath()
 
80
{
 
81
}
 
82
 
 
83
 
 
84
TestPath &
 
85
TestPath::operator =( const TestPath &other )
 
86
{
 
87
  if ( &other != this )
 
88
    m_tests = other.m_tests;
 
89
  return *this;
 
90
}
 
91
 
 
92
 
 
93
bool 
 
94
TestPath::isValid() const
 
95
{
 
96
  return getTestCount() > 0;
 
97
}
 
98
 
 
99
 
 
100
void 
 
101
TestPath::add( Test *test )
 
102
{
 
103
  m_tests.push_back( test );
 
104
}
 
105
 
 
106
 
 
107
void 
 
108
TestPath::add( const TestPath &path )
 
109
{
 
110
  for ( int index =0; index < path.getTestCount(); ++index )
 
111
    add( path.getTestAt( index ) );
 
112
}
 
113
 
 
114
 
 
115
void 
 
116
TestPath::insert( Test *test, 
 
117
                  int index )
 
118
{
 
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 );
 
122
}
 
123
 
 
124
void 
 
125
TestPath::insert( const TestPath &path, 
 
126
                  int index )
 
127
{
 
128
  int itemIndex = path.getTestCount() -1;
 
129
  while ( itemIndex >= 0 )
 
130
    insert( path.getTestAt( itemIndex-- ), index );
 
131
}
 
132
 
 
133
 
 
134
void 
 
135
TestPath::removeTests()
 
136
{
 
137
  while ( isValid() )
 
138
    removeTest( 0 );
 
139
}
 
140
 
 
141
 
 
142
void 
 
143
TestPath::removeTest( int index )
 
144
{
 
145
  checkIndexValid( index );
 
146
  m_tests.erase( m_tests.begin() + index );
 
147
}
 
148
 
 
149
 
 
150
void 
 
151
TestPath::up()
 
152
{
 
153
  checkIndexValid( 0 );
 
154
  removeTest( getTestCount() -1 );
 
155
}
 
156
 
 
157
 
 
158
int 
 
159
TestPath::getTestCount() const
 
160
{
 
161
  return m_tests.size();
 
162
}
 
163
 
 
164
 
 
165
Test *
 
166
TestPath::getTestAt( int index ) const
 
167
{
 
168
  checkIndexValid( index );
 
169
  return m_tests[index];
 
170
}
 
171
 
 
172
 
 
173
Test *
 
174
TestPath::getChildTest() const
 
175
{
 
176
  return getTestAt( getTestCount() -1 );
 
177
}
 
178
 
 
179
 
 
180
void 
 
181
TestPath::checkIndexValid( int index ) const
 
182
{
 
183
  if ( index < 0  ||  index >= getTestCount() )
 
184
    throw std::out_of_range( "TestPath::checkIndexValid(): index out of range" );
 
185
}
 
186
 
 
187
 
 
188
std::string 
 
189
TestPath::toString() const
 
190
{
 
191
  std::string asString( "/" );
 
192
  for ( int index =0; index < getTestCount(); ++index )
 
193
  {
 
194
    if ( index > 0 )
 
195
      asString += '/';
 
196
    asString += getTestAt(index)->getName();
 
197
  }
 
198
 
 
199
  return asString;
 
200
}
 
201
 
 
202
 
 
203
Test *
 
204
TestPath::findActualRoot( Test *searchRoot,
 
205
                          const std::string &pathAsString,
 
206
                          PathTestNames &testNames )
 
207
{
 
208
  bool isRelative = splitPathString( pathAsString, testNames );
 
209
 
 
210
  if ( isRelative  &&  pathAsString.empty() )
 
211
    return searchRoot;
 
212
 
 
213
  if ( testNames.empty() )
 
214
    throw std::invalid_argument( "TestPath::TestPath(): invalid root or root name in absolute path" );
 
215
 
 
216
  Test *root = isRelative ? searchRoot->findTest( testNames[0] )  // throw if bad test name
 
217
                          : searchRoot;
 
218
  if ( root->getName() != testNames[0] )
 
219
    throw std::invalid_argument( "TestPath::TestPath(): searchRoot does not match path root name" );
 
220
 
 
221
  return root;
 
222
}
 
223
 
 
224
 
 
225
bool
 
226
TestPath::splitPathString( const std::string &pathAsString,
 
227
                           PathTestNames &testNames )
 
228
{
 
229
  if ( pathAsString.empty() )
 
230
    return true;
 
231
 
 
232
  bool isRelative = pathAsString[0] != '/';
 
233
 
 
234
  int index = (isRelative ? 0 : 1);
 
235
  while ( true )
 
236
  {
 
237
    int separatorIndex = pathAsString.find( '/', index );
 
238
    if ( separatorIndex >= 0 )
 
239
    {
 
240
      testNames.push_back( pathAsString.substr( index, separatorIndex - index ) );
 
241
      index = separatorIndex + 1;
 
242
    }
 
243
    else
 
244
    {
 
245
      testNames.push_back( pathAsString.substr( index ) );
 
246
      break;
 
247
    }
 
248
  }
 
249
 
 
250
  return isRelative;
 
251
}
 
252
  
 
253
 
 
254
CPPUNIT_NS_END