1
#include "CppTestSelector.hxx"
3
#include <boost/algorithm/string.hpp>
10
CommandLineSelector::CommandLineSelector(int argc, char** argv) : mArgNumTimes(0)
12
// Needs to be reworked for many selections and number of repeats
13
for(int i = 1; i < argc; i++)
15
std::string userInput = argv[i];
17
if(isdigit(*userInput.c_str()))
19
int res = atoi(userInput.c_str());
26
mArgResult.push_back(res);
39
if(mArgNumTimes == 0 && (mArgResult.size() > 0 || mPattern.size() > 0) )
45
void CommandLineSelector::DisplayTestCases(std::vector<std::string>& names)
48
int count = (int) names.size();
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;
57
void CommandLineSelector::DisplayTestCases2(std::vector<std::string>& names)
63
const char *sep = "::";
64
const char *indent0 = " ";
65
const char *indent1 = " ";
66
const char *indent2 = " ... ";
69
int count = (int) names.size();
70
std::cout << std::endl;
71
for(i=0; i< count; i++)
76
if((off1 = names[i].find(sep, 0)) >= 0)
78
if((off2 = names[i].find(sep, off1+strlen(sep))) >= 0) //child test
80
off = off2+strlen(sep);
85
off = off1+strlen(sep);
89
std::cout << i << indent << names[i].c_str()+off << std::endl;
91
std::cout << i << indent0 << "Cancel" << std::endl;
92
std::cout << std::endl;
97
void CommandLineSelector::ReadLineInput(std::vector<int> &result, int maxSelection, int &numTimes)
101
std::string userInput;
102
std::string repeatStr;
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()))
114
res = atoi(userInput.c_str());
115
if(res == maxSelection)
117
break; // user wants to cancel
119
if((res < 0) || (res >= maxSelection))
123
result.push_back(res);
127
mPattern = userInput;
130
// see if user specified number of repetitions
131
if((repeat = atoi(repeatStr.c_str())) > 0)
139
void CommandLineSelector::GetUserSelection(std::vector<std::string>& names, std::vector<int> &result, int &numTimes)
141
if(names.size() == 0)
143
std::cout << std::endl << "No tests specified" << std::endl;
150
numTimes = mArgNumTimes;
154
DisplayTestCases2(names);
155
ReadLineInput(result, (int)names.size(), numTimes);
158
if(mPattern.size() == 0)
163
size_t startFrom = 0;
165
if(boost::istarts_with(mPattern, L">="))
167
string pattern = mPattern;
168
boost::algorithm::ireplace_all(pattern, ">=", "");
170
startFrom = atoi(pattern.c_str());
172
else if(boost::istarts_with(mPattern, L">"))
174
string pattern = mPattern;
175
boost::algorithm::ireplace_all(pattern, ">", "");
177
startFrom = atoi(pattern.c_str());
184
if(startFrom > 0 && startFrom < names.size())
187
for(unsigned int i = startFrom; i < names.size(); i++)
195
bool matchedTopLevel = false;
197
for(unsigned int i = 0; i < names.size(); i++)
200
string name = names[i];
202
bool topLevel = (name.find_first_of("::") == (name.find_last_of("::") - 1));
204
if(boost::contains(name, mPattern))
208
matchedTopLevel = true;
211
if(matchedTopLevel && topLevel)
215
else if(matchedTopLevel == false && topLevel == false)
222
matchedTopLevel = false;
231
CppTestSelector::CppTestSelector(void)
235
CppTestSelector::~CppTestSelector(void)
240
// may throw a memory exception if push fails
241
void CppTestSelector::GetTestCases(CppUnit::Test *test, std::vector<std::string>& names)
244
names.push_back (test->getName());
246
if ((childCount = test->getChildTestCount()) == 0)
248
for (int i = 0; i < childCount; i++)
249
GetTestCases (test->getChildTestAt (i), names);
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)
257
CppUnitVector<std::string> testNames ;
258
std::vector<int> selectedTests;
262
GetTestCases(suite, testNames);
263
uiSelector.GetUserSelection(testNames, selectedTests, numTimes);
265
CppUnit::Test *testToRun = NULL;
266
for(i = 0; i< selectedTests.size(); i++)
270
testToRun = suite->findTest(testNames[selectedTests[i]]);
273
testRunner.addTest(testToRun);
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