2005-10-12 01:46:19 +02:00
|
|
|
#include "TestRunner.hpp"
|
|
|
|
#include "textui/TextTestResult.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
/*
|
|
|
|
* A command line based tool to run tests.
|
|
|
|
* TestRunner expects as its only argument the name of a TestCase class.
|
|
|
|
* TestRunner prints out a trace as the tests are executed followed by a
|
|
|
|
* summary at the end.
|
|
|
|
*
|
|
|
|
* You can add to the tests that the TestRunner knows about by
|
|
|
|
* making additional calls to "addTest (...)" in main.
|
|
|
|
*
|
|
|
|
* Here is the synopsis:
|
|
|
|
*
|
|
|
|
* TestRunner [-wait] ExampleTestCase
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
using namespace std;
|
|
|
|
|
2007-08-29 04:03:12 +02:00
|
|
|
|
2005-10-12 01:46:19 +02:00
|
|
|
typedef pair<string, Test *> mapping;
|
|
|
|
typedef vector<pair<string, Test *> > mappings;
|
|
|
|
|
2007-12-08 21:46:40 +01:00
|
|
|
bool run(const string& name, Test *test, bool verbose)
|
2007-08-29 04:03:12 +02:00
|
|
|
{
|
|
|
|
if(verbose)
|
|
|
|
cout << "Running " << name << endl;
|
|
|
|
TextTestResult result(name, verbose);
|
|
|
|
test->run (&result);
|
|
|
|
cout << result;
|
2007-12-08 21:46:40 +01:00
|
|
|
return result.wasSuccessful();
|
2007-08-29 04:03:12 +02:00
|
|
|
} // run
|
|
|
|
|
|
|
|
void printBanner ()
|
2005-10-12 01:46:19 +02:00
|
|
|
{
|
2007-08-29 04:03:12 +02:00
|
|
|
cout << "Usage: driver [-v] [ -wait ] testName, where name is the name of a test case class" << endl;
|
2005-10-12 01:46:19 +02:00
|
|
|
} // printBanner
|
|
|
|
|
2007-12-08 21:46:40 +01:00
|
|
|
bool TestRunner::run(int ac, const char **av)
|
2005-10-12 01:46:19 +02:00
|
|
|
{
|
2007-12-08 21:46:40 +01:00
|
|
|
bool ok = true;
|
2005-10-12 01:46:19 +02:00
|
|
|
string testCase;
|
|
|
|
int numberOfTests = 0;
|
2006-10-12 00:51:04 +02:00
|
|
|
int opt = 0;
|
2005-10-12 01:46:19 +02:00
|
|
|
|
|
|
|
for(int i = 1; i < ac; i++)
|
|
|
|
{
|
|
|
|
if(string(av[i]) == "-wait")
|
|
|
|
{
|
|
|
|
m_wait = true;
|
2006-10-12 00:51:04 +02:00
|
|
|
++opt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-08-29 04:03:12 +02:00
|
|
|
if(string(av[i]) == "-v")
|
2006-10-12 00:51:04 +02:00
|
|
|
{
|
2007-08-29 04:03:12 +02:00
|
|
|
verbose_ = true;
|
2006-10-12 00:51:04 +02:00
|
|
|
++opt;
|
2005-10-12 01:46:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
testCase = av[i];
|
|
|
|
|
|
|
|
if(testCase == "")
|
|
|
|
{
|
|
|
|
printBanner ();
|
2007-12-08 21:46:40 +01:00
|
|
|
return ok;
|
2005-10-12 01:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Test *testToRun = NULL;
|
|
|
|
|
|
|
|
for(mappings::iterator it = m_mappings.begin();
|
|
|
|
it != m_mappings.end();
|
|
|
|
++it)
|
|
|
|
{
|
|
|
|
if((*it).first == testCase)
|
|
|
|
{
|
|
|
|
testToRun = (*it).second;
|
2007-12-08 21:46:40 +01:00
|
|
|
ok &= ::run((*it).first, testToRun, verbose_);
|
2005-10-12 01:46:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfTests++;
|
|
|
|
|
|
|
|
if(!testToRun)
|
|
|
|
{
|
|
|
|
cout << "Test " << testCase << " not found." << endl;
|
2007-12-08 21:46:40 +01:00
|
|
|
return false;
|
2005-10-12 01:46:19 +02:00
|
|
|
}
|
|
|
|
} // for ...
|
|
|
|
|
2006-10-12 00:51:04 +02:00
|
|
|
if((ac-opt) == 1)
|
2005-10-12 01:46:19 +02:00
|
|
|
{
|
|
|
|
// run everything
|
|
|
|
for(mappings::iterator it = m_mappings.begin(); it != m_mappings.end(); ++it)
|
|
|
|
{
|
2007-12-08 21:46:40 +01:00
|
|
|
ok &= ::run((*it).first, (*it).second, verbose_);
|
2005-10-12 01:46:19 +02:00
|
|
|
}
|
2007-12-08 21:46:40 +01:00
|
|
|
return ok;
|
2005-10-12 01:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(numberOfTests == 0)
|
|
|
|
{
|
|
|
|
printBanner ();
|
2007-12-08 21:46:40 +01:00
|
|
|
return false;
|
2005-10-12 01:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(m_wait)
|
|
|
|
{
|
|
|
|
cout << "<RETURN> to continue" << endl;
|
|
|
|
cin.get ();
|
|
|
|
}
|
2007-12-08 21:46:40 +01:00
|
|
|
|
|
|
|
return ok;
|
2005-10-12 01:46:19 +02:00
|
|
|
} // run
|
|
|
|
|
|
|
|
TestRunner::~TestRunner ()
|
|
|
|
{
|
|
|
|
for(mappings::iterator it = m_mappings.begin ();
|
|
|
|
it != m_mappings.end ();
|
|
|
|
++it)
|
|
|
|
delete it->second;
|
|
|
|
} // ~TestRunner
|
|
|
|
|