~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to tfm/CppTestSelector.cxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "CppTestSelector.hxx"
 
2
#include <iostream>
 
3
#include <boost/algorithm/string.hpp>
 
4
 
 
5
 
 
6
using namespace std;
 
7
 
 
8
 
 
9
 
 
10
CommandLineSelector::CommandLineSelector(int argc, char** argv) : mArgNumTimes(0)
 
11
{
 
12
   // Needs to be reworked for many selections and number of repeats
 
13
        for(int i = 1; i < argc; i++)
 
14
        {
 
15
                std::string userInput = argv[i];
 
16
 
 
17
                if(isdigit(*userInput.c_str()))
 
18
                {
 
19
                   int res = atoi(userInput.c_str());
 
20
 
 
21
                   if(res < 0)
 
22
                           continue;
 
23
            
 
24
         if(i + 1 < argc)
 
25
         {
 
26
                 mArgResult.push_back(res);  
 
27
         }
 
28
         else
 
29
         {
 
30
            mArgNumTimes = res;
 
31
         }
 
32
      }
 
33
      else
 
34
      {
 
35
         mPattern = userInput;
 
36
      }
 
37
        }
 
38
 
 
39
   if(mArgNumTimes == 0 && (mArgResult.size() > 0 || mPattern.size() > 0) )
 
40
   {
 
41
      mArgNumTimes = 1;
 
42
   }
 
43
}
 
44
 
 
45
void CommandLineSelector::DisplayTestCases(std::vector<std::string>& names)
 
46
{
 
47
   int i;
 
48
   int count = (int) names.size();
 
49
   
 
50
   std::cout << std::endl;
 
51
   for(i=0; i< count; i++)
 
52
      std::cout << i << "   " << names[i] << std::endl;
 
53
   std::cout << i << "   Cancel" << std::endl;
 
54
   std::cout << std::endl;
 
55
}
 
56
 
 
57
void CommandLineSelector::DisplayTestCases2(std::vector<std::string>& names)
 
58
{
 
59
   int i;
 
60
   int off   = 0;
 
61
   int off1  = 0;
 
62
   int off2  = 0;
 
63
   const char *sep = "::";
 
64
   const char *indent0 = "  ";
 
65
   const char *indent1 = "    ";
 
66
   const char *indent2 = "    ...  ";
 
67
   const char *indent;
 
68
 
 
69
   int count = (int) names.size();
 
70
   std::cout << std::endl;
 
71
   for(i=0; i< count; i++)
 
72
   {
 
73
          off  = 0;
 
74
          indent = indent0;
 
75
     
 
76
     if((off1 = names[i].find(sep, 0)) >= 0) 
 
77
          {
 
78
                  if((off2 = names[i].find(sep, off1+strlen(sep))) >= 0)  //child test
 
79
                  {
 
80
           off = off2+strlen(sep);
 
81
                          indent = indent2;
 
82
                  }
 
83
                  else
 
84
                  {
 
85
           off = off1+strlen(sep);
 
86
                          indent = indent1;
 
87
                  }
 
88
          }
 
89
          std::cout << i << indent << names[i].c_str()+off << std::endl;
 
90
   }
 
91
   std::cout << i << indent0 << "Cancel" << std::endl;
 
92
   std::cout << std::endl;
 
93
}
 
94
 
 
95
 
 
96
 
 
97
void CommandLineSelector::ReadLineInput(std::vector<int> &result, int maxSelection, int &numTimes)
 
98
{
 
99
   int res = 0;
 
100
   int repeat = 1;
 
101
   std::string userInput; 
 
102
   std::string repeatStr; 
 
103
   //long index = 0;
 
104
 
 
105
   numTimes = 1;
 
106
   while(true)
 
107
   {
 
108
      std::cout << std::endl;
 
109
      std::cout << "Enter a test number (or pattern) followed by number of repetitions: ";
 
110
      std::cin >> userInput;
 
111
      std::cin >> repeatStr;
 
112
      if(isdigit(*userInput.c_str()))
 
113
      {
 
114
         res = atoi(userInput.c_str());
 
115
         if(res == maxSelection)  
 
116
         {
 
117
            break;  // user wants to cancel
 
118
         }
 
119
         if((res < 0) || (res >= maxSelection))
 
120
         {
 
121
            break;
 
122
         }
 
123
         result.push_back(res);
 
124
      }
 
125
      else
 
126
      {
 
127
         mPattern = userInput;
 
128
      }
 
129
 
 
130
      // see if user specified number of repetitions
 
131
      if((repeat = atoi(repeatStr.c_str())) > 0)
 
132
      {
 
133
         numTimes = repeat;
 
134
         break;
 
135
      }
 
136
   }
 
137
}
 
138
 
 
139
void CommandLineSelector::GetUserSelection(std::vector<std::string>& names, std::vector<int> &result, int &numTimes)
 
140
{
 
141
   if(names.size() == 0)
 
142
   {
 
143
      std::cout << std::endl << "No tests specified" <<  std::endl;
 
144
           return;
 
145
   }
 
146
  
 
147
   if(mArgNumTimes > 0)
 
148
   {
 
149
      result = mArgResult;
 
150
      numTimes = mArgNumTimes;
 
151
   }
 
152
   else
 
153
   {
 
154
      DisplayTestCases2(names);
 
155
      ReadLineInput(result, (int)names.size(), numTimes);
 
156
   }
 
157
 
 
158
   if(mPattern.size() == 0)
 
159
   {
 
160
      return;
 
161
   }
 
162
 
 
163
   size_t startFrom = 0;
 
164
 
 
165
   if(boost::istarts_with(mPattern, L">="))
 
166
   {
 
167
      string pattern = mPattern;
 
168
      boost::algorithm::ireplace_all(pattern, ">=", "");
 
169
 
 
170
      startFrom = atoi(pattern.c_str());
 
171
   }
 
172
   else if(boost::istarts_with(mPattern, L">"))
 
173
   {
 
174
      string pattern = mPattern;
 
175
      boost::algorithm::ireplace_all(pattern, ">", "");
 
176
 
 
177
      startFrom = atoi(pattern.c_str());
 
178
      if(startFrom > 0)
 
179
      {
 
180
         startFrom++;
 
181
      }
 
182
   }
 
183
 
 
184
   if(startFrom > 0 && startFrom < names.size())
 
185
   {
 
186
      // Match by > or >=
 
187
      for(unsigned int i = startFrom; i < names.size(); i++)
 
188
      {
 
189
         result.push_back(i);
 
190
      }
 
191
      return;
 
192
   }
 
193
 
 
194
 
 
195
   bool matchedTopLevel = false;
 
196
 
 
197
   for(unsigned int i = 0; i < names.size(); i++)
 
198
   {
 
199
      // Match by pattern
 
200
      string name = names[i];
 
201
 
 
202
      bool topLevel = (name.find_first_of("::") == (name.find_last_of("::") - 1));
 
203
 
 
204
      if(boost::contains(name, mPattern))
 
205
      {
 
206
         if(topLevel)
 
207
         {
 
208
            matchedTopLevel = true;
 
209
         }
 
210
 
 
211
         if(matchedTopLevel && topLevel)
 
212
         {
 
213
            result.push_back(i);
 
214
         }
 
215
         else if(matchedTopLevel == false && topLevel == false)
 
216
         {
 
217
            result.push_back(i);
 
218
         }
 
219
      }
 
220
      else
 
221
      {
 
222
         matchedTopLevel = false;
 
223
      }
 
224
   }
 
225
 
 
226
}
 
227
 
 
228
 
 
229
 
 
230
 
 
231
CppTestSelector::CppTestSelector(void)
 
232
{
 
233
}
 
234
 
 
235
CppTestSelector::~CppTestSelector(void)
 
236
{
 
237
    
 
238
}
 
239
 
 
240
// may throw a memory exception if push fails
 
241
void CppTestSelector::GetTestCases(CppUnit::Test *test, std::vector<std::string>& names)
 
242
{
 
243
  int childCount = 0;
 
244
  names.push_back (test->getName());
 
245
  
 
246
  if ((childCount = test->getChildTestCount()) == 0)
 
247
     return;
 
248
  for (int i = 0; i < childCount; i++)
 
249
    GetTestCases (test->getChildTestAt (i), names);
 
250
}
 
251
 
 
252
 
 
253
// return number of selected tests or suites
 
254
// may throw memory exception 
 
255
int CppTestSelector::SelectTests(CppUnit::Test *suite, CppUnit::TestRunner &testRunner, UISelector &uiSelector, int &numTimes)
 
256
{
 
257
        CppUnitVector<std::string>    testNames ;
 
258
        std::vector<int>   selectedTests;
 
259
        size_t i = 0;
 
260
        numTimes = 1;
 
261
 
 
262
   GetTestCases(suite, testNames);
 
263
   uiSelector.GetUserSelection(testNames, selectedTests, numTimes);
 
264
 
 
265
   CppUnit::Test *testToRun = NULL;
 
266
   for(i = 0; i< selectedTests.size(); i++)
 
267
   {
 
268
      try
 
269
           {
 
270
              testToRun = suite->findTest(testNames[selectedTests[i]]);
 
271
              if(testToRun)
 
272
         {
 
273
             testRunner.addTest(testToRun);
 
274
         }
 
275
           }
 
276
           catch (...)
 
277
      {   
 
278
                   //std::cout << "test not found:   " << testNames[selectedTests[i]] << std::endl;
 
279
                   // test not found, but this should never happen
 
280
                  // because the test names were found from the given suite
 
281
      }
 
282
   }
 
283
 
 
284
   return (int) i;
 
285
}
 
286
 
 
287
 
 
288
 
 
289
 
 
290