TestMain.h
1 /*
2  * Copyright (C) 2005-2006 by Dr. Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef PT_UNIT_TESTMAIN_H
29 #define PT_UNIT_TESTMAIN_H
30 
31 #include <Pt/Main.h>
32 #include <Pt/Arg.h>
33 #include <Pt/Unit/Api.h>
34 #include <Pt/Unit/Reporter.h>
35 #include <Pt/Unit/Application.h>
36 #include <fstream>
37 
39 //namespace TestMain
40 //{
41 // static int argc = 0;
42 // static char** argv = 0;
43 //}
44 
45 
46 int main(int argc, char** argv)
47 {
48  //TestMain::argc = argc;
49  //TestMain::argv = argv;
51 
52  Pt::Arg<bool> help(argc, argv, 'h');
53  if( help )
54  {
55  std::cerr << "Usage: " << argv[0] << " [-t <testname>] [-f <logfile>]\n";
56  std::cerr << "Available Tests:\n";
57  std::list<Pt::Unit::Test*>::const_iterator it;
58  for( it = app.tests().begin(); it != app.tests().end(); ++it)
59  {
60  std::cerr << " - "<< (*it)->name() << std::endl;
61  }
62  return 0;
63  }
64 
65  Pt::Unit::BriefReporter consoleReporter;
66  app.attachReporter(consoleReporter);
67 
68  Pt::Arg<std::string> file(argc, argv, 'f');
69  std::ofstream logFile;
70  Pt::Unit::BriefReporter fileReporter;
71  std::string fileName = file.get();
72 
73  if( ! fileName.empty() )
74  {
75  logFile.open( fileName.c_str() );
76  fileReporter.setOutput(logFile);
77  app.attachReporter(fileReporter);
78  }
79 
80  try {
81  Pt::Arg<std::string> test(argc, argv, 't');
82  std::string testName = test.get();
83  if( testName.empty() )
84  {
85  app.run();
86  return app.errors();
87  }
88  else
89  {
90  app.run(testName);
91  return app.errors();
92  }
93 
94  return app.errors();
95  }
96  catch(const std::exception& ex)
97  {
98  std::cerr << ex.what() << std::endl;
99  }
100 
101  return 1;
102 }
103 
104 #endif// PT_UNIT_TESTMAIN_H
void run(const std::string &testName)
Run test by name.
Read and extract command-line options.
Definition: Arg.h:199
Run registered tests.
Definition: Application.h:64
static std::list< Test * > & tests()
Returns a list of all registered test TODO: find another way to query available tests.
unsigned errors() const
Returns the number of errors which occured during a run.
Definition: Application.h:116
void attachReporter(Reporter &r)
Add reporter for test events.