testmain.h
Go to the documentation of this file.
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 CXXTOOLS_UNIT_TESTMAIN_H
29 #define CXXTOOLS_UNIT_TESTMAIN_H
30 
31 #include <cxxtools/arg.h>
32 #include <cxxtools/unit/reporter.h>
34 #include <cxxtools/log.h>
35 #include <fstream>
36 
37 namespace TestMain
38 {
39  static int argc = 0;
40  static char** argv = 0;
41 }
42 
43 
44 int main(int argc, char** argv)
45 {
46  log_init();
47 
48  TestMain::argc = argc;
49  TestMain::argv = argv;
51 
52  cxxtools::Arg<bool> help(argc, argv, 'h');
53  cxxtools::Arg<bool> list(argc, argv, 'l');
54  if( help || list )
55  {
56  std::cerr << "Usage: " << argv[0] << " [-t <testname>] [-f <logfile>] { testname }\n";
57  std::cerr << "Available Tests:\n";
58  std::list<cxxtools::unit::Test*>::const_iterator it;
59  for( it = app.tests().begin(); it != app.tests().end(); ++it)
60  {
61  std::cerr << " - " << (*it)->name() << std::endl;
62  }
63  return 0;
64  }
65 
66  cxxtools::unit::BriefReporter consoleReporter;
67  app.attachReporter(consoleReporter);
68 
69  cxxtools::Arg<std::string> file(argc, argv, 'f');
70  std::ofstream logFile;
71  cxxtools::unit::BriefReporter fileReporter;
72  std::string fileName = file.getValue();
73 
74  if( ! fileName.empty() )
75  {
76  logFile.open( fileName.c_str() );
77  fileReporter.setOutput(logFile);
78  app.attachReporter(fileReporter);
79  }
80 
81  try
82  {
83  if (argc <= 1)
84  {
85  app.run();
86  }
87  else
88  {
89  for (int a = 1; a < argc; ++a)
90  {
91  std::string testName = argv[a];
92  if (testName == "-t")
93  continue; // just for compatibility
94 
95  app.run(testName);
96  }
97  }
98 
99  return app.errors();
100  }
101  catch(const std::exception& ex)
102  {
103  std::cerr << ex.what() << std::endl;
104  }
105 
106  return 1;
107 }
108 
109 #endif// CXXTOOLS_UNIT_TESTMAIN_H