summaryrefslogtreecommitdiffstats
blob: 5affffb5eebcec40fb2e1a5354c250a780dca65e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <cppunit/Portability.h>
#include <cppunit/Test.h>
#include <cppunit/TestPath.h>
#include <stdexcept>


CPPUNIT_NS_BEGIN


TestPath::TestPath()
    : m_tests()
{
}


TestPath::TestPath( Test *root )
    : m_tests()
{
  add( root );
}


TestPath::TestPath( const TestPath &other, 
                    int indexFirst, 
                    int count )
    : m_tests()
{
  int countAdjustment = 0;
  if ( indexFirst < 0 )
  {
    countAdjustment = indexFirst;
    indexFirst = 0;
  }

  if ( count < 0 )
    count = other.getTestCount();
  else
    count += countAdjustment;

  int index = indexFirst;
  while ( count-- > 0  &&  index < other.getTestCount() )
    add( other.getTestAt( index++ ) );
}


TestPath::TestPath( Test *searchRoot, 
                    const std::string &pathAsString )
    : m_tests()
{
  PathTestNames testNames;

  Test *parentTest = findActualRoot( searchRoot, pathAsString, testNames );
  add( parentTest );

  for ( unsigned int index = 1; index < testNames.size(); ++index )
  {
    bool childFound = false;
    for ( int childIndex =0; childIndex < parentTest->getChildTestCount(); ++childIndex )
    {
      if ( parentTest->getChildTestAt( childIndex )->getName() == testNames[index] )
      {
        childFound = true;
        parentTest = parentTest->getChildTestAt( childIndex );
        break;
      }
    }

    if ( !childFound )
      throw std::invalid_argument( "TestPath::TestPath(): failed to resolve test name <"+
                                   testNames[index] + "> of path <" + pathAsString + ">" );

    add( parentTest );
  }
}


TestPath::TestPath( const TestPath &other )
  : m_tests( other.m_tests )
{
}


TestPath::~TestPath()
{
}


TestPath &
TestPath::operator =( const TestPath &other )
{
  if ( &other != this )
    m_tests = other.m_tests;
  return *this;
}


bool 
TestPath::isValid() const
{
  return getTestCount() > 0;
}


void 
TestPath::add( Test *test )
{
  m_tests.push_back( test );
}


void 
TestPath::add( const TestPath &path )
{
  for ( int index =0; index < path.getTestCount(); ++index )
    add( path.getTestAt( index ) );
}


void 
TestPath::insert( Test *test, 
                  int index )
{
  if ( index < 0  ||  index > getTestCount() )
    throw std::out_of_range( "TestPath::insert(): index out of range" );
  m_tests.insert( m_tests.begin() + index, test );
}

void 
TestPath::insert( const TestPath &path, 
                  int index )
{
  int itemIndex = path.getTestCount() -1;
  while ( itemIndex >= 0 )
    insert( path.getTestAt( itemIndex-- ), index );
}


void 
TestPath::removeTests()
{
  while ( isValid() )
    removeTest( 0 );
}


void 
TestPath::removeTest( int index )
{
  checkIndexValid( index );
  m_tests.erase( m_tests.begin() + index );
}


void 
TestPath::up()
{
  checkIndexValid( 0 );
  removeTest( getTestCount() -1 );
}


int 
TestPath::getTestCount() const
{
  return m_tests.size();
}


Test *
TestPath::getTestAt( int index ) const
{
  checkIndexValid( index );
  return m_tests[index];
}


Test *
TestPath::getChildTest() const
{
  return getTestAt( getTestCount() -1 );
}


void 
TestPath::checkIndexValid( int index ) const
{
  if ( index < 0  ||  index >= getTestCount() )
    throw std::out_of_range( "TestPath::checkIndexValid(): index out of range" );
}


std::string 
TestPath::toString() const
{
  std::string asString( "/" );
  for ( int index =0; index < getTestCount(); ++index )
  {
    if ( index > 0 )
      asString += '/';
    asString += getTestAt(index)->getName();
  }

  return asString;
}


Test *
TestPath::findActualRoot( Test *searchRoot,
                          const std::string &pathAsString,
                          PathTestNames &testNames )
{
  bool isRelative = splitPathString( pathAsString, testNames );

  if ( isRelative  &&  pathAsString.empty() )
    return searchRoot;

  if ( testNames.empty() )
    throw std::invalid_argument( "TestPath::TestPath(): invalid root or root name in absolute path" );

  Test *root = isRelative ? searchRoot->findTest( testNames[0] )  // throw if bad test name
                          : searchRoot;
  if ( root->getName() != testNames[0] )
    throw std::invalid_argument( "TestPath::TestPath(): searchRoot does not match path root name" );

  return root;
}


bool
TestPath::splitPathString( const std::string &pathAsString,
                           PathTestNames &testNames )
{
  if ( pathAsString.empty() )
    return true;

  bool isRelative = pathAsString[0] != '/';

  int index = (isRelative ? 0 : 1);
  while ( true )
  {
    int separatorIndex = pathAsString.find( '/', index );
    if ( separatorIndex >= 0 )
    {
      testNames.push_back( pathAsString.substr( index, separatorIndex - index ) );
      index = separatorIndex + 1;
    }
    else
    {
      testNames.push_back( pathAsString.substr( index ) );
      break;
    }
  }

  return isRelative;
}
  

CPPUNIT_NS_END