Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tests.hpp"
- // -------------------------------------------------------------------------
- // Runs the run() method of all registered subclasses of Test
- //
- // The output is formatted in the simple Test Anything Protocol (TAP), see
- // http://testanything.org/wiki/index.php/Main_Page
- //
- // Example output
- //
- // 1..2
- // ok 1 - (TestNoFnordsAround)
- // not ok 2 - Wrong Wizzle Number: Expected 9, actual 7 (TestWizzleFizzle)
- // # This is a comment
- //
- // Uses <typeinfo>, boost::regex and boost::program_options
- // The regex is used in the command line for filtering the tests to run
- // typeinfo is used for obtaining the test class name at runtime.
- //
- // -------------------------------------------------------------------------
- // -------------------------------------------------------------------------
- // Implementation of tests
- // -------------------------------------------------------------------------
- /* Example of a failing test
- to be excluded when the test suite will be used later */
- class TestTestNotOK : public Test {
- public:
- virtual void run() {
- throw "Example";
- };
- };
- /* --- Further tests follow here:
- class Test... : public Test {
- public :
- virtual void run() {
- }
- };
- */
- // -------------------------------------------------------------------------
- // main() - Reads command line arguments & executes the tests
- // -------------------------------------------------------------------------
- int main( int argc, char *argv[] ) {
- TestRunner tr;
- tr.getOptions(argc,argv);
- tr << new TestTestNotOK( );
- bool ok = tr.run();
- return ok ? 0 : -1;
- }
- // -------------------------------------------------------------------------
- // The tiny test framework
- // -------------------------------------------------------------------------
- // --- Determine the current class Name
- const char* Test::className( ) {
- return typeid(*this).name();
- }
- void Test::setup() {
- }
- // --- TestRunner Implementation
- bool TestRunner::run() {
- int n = 0;
- int n_ok = 0;
- list<Test*>::iterator i;
- printf("%d..%d\n",tests.size()?1:0,tests.size());
- for (i=tests.begin();i!=tests.end();i++) {
- n++;
- (*i)->setup();
- try {
- (*i)->run();
- printf("ok %d -",n);
- n_ok++;
- } catch (const char* f) {
- printf("not ok %d - %s", n, f);
- };
- printf(" (%s)\n", (*i)->className());
- (*i)->teardown();
- }
- printf("\n");
- return (n_ok == n);
- }
- bool TestRunner::getOptions(int argc, char* argv[]) {
- string pat;
- // Declare the supported options ( opt = boost::program_options )
- opt::options_description desc("Allowed options");
- desc.add_options()
- ("help", "produce help message")
- ("pattern,p", opt::value<string>(&pat), "regex pattern to filter classes to be executed");
- opt::variables_map varMap;
- opt::store(opt::parse_command_line(argc, argv, desc), varMap);
- opt::notify(varMap);
- if (varMap.count("help")) {
- cout << desc << "\n";
- return false;
- }
- pattern = varMap.count("pattern") ?
- new boost::regex( pat ) : 0;
- if (pattern) cout << pat;
- return true;
- }
- TestRunner::~TestRunner() {
- if (pattern) delete pattern;
- }
- TestRunner& operator<< (TestRunner& tr, Test* t) {
- if ( tr.pattern == 0
- || boost::regex_search( t->className(), *(tr.pattern)))
- tr.tests.push_back(t);
- return tr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement