bes  Updated for version 3.20.13
run_tests_cppunit.h
1 //
2 // Created by James Gallagher on 2/25/22.
3 //
4 
5 // This file is part of the BES.
6 
7 // Copyright (c) 2022 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
34 #ifndef HYRAX_GIT_RUN_TESTS_CPPUNIT_H
35 #define HYRAX_GIT_RUN_TESTS_CPPUNIT_H
36 
37 #include <unistd.h>
38 #include <cppunit/TextTestRunner.h>
39 #include <cppunit/extensions/TestFactoryRegistry.h>
40 #include <cppunit/extensions/HelperMacros.h>
41 
42 #include "BESDebug.h"
43 
44 bool debug = false;
45 
46 #undef DBG
47 #define DBG(x) do { if (debug) (x); } while(false);
48 
49 bool debug2 = false;
50 
51 #undef DBG2
52 #define DBG2(x) do { if (debug) (x); } while(false);
53 
62 template<class CLASS>
63 bool bes_run_tests(int argc, char *argv[], const std::string &besdebug_contexts)
64 {
65  CppUnit::TextTestRunner runner;
66  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
67 
68  int option_char;
69  while ((option_char = getopt(argc, argv, "dDbh")) != -1) {
70  switch (option_char) {
71  case 'd':
72  debug = true; // debug is a global
73  break;
74  case 'D':
75  debug2 = true; // debug2 is a global
76  break;
77  case 'b':
78  BESDebug::SetUp(besdebug_contexts);
79  break;
80  case 'h': { // help - show test names
81  std::cerr << "Usage: the following tests can be run individually or in combination:" << std::endl;
82  auto &tests = CLASS::suite()->getTests();
83  unsigned int prefix_len = CLASS::suite()->getName().append("::").length();
84  for (auto &t: tests) {
85  std::cerr << t->getName().replace(0, prefix_len, "") << std::endl;
86  }
87  exit(EXIT_SUCCESS);
88  }
89  default:
90  break;
91  }
92  }
93 
94  argc -= optind;
95  argv += optind;
96 
97  if (0 == argc) { // run them all
98  return runner.run("");
99  }
100  else {
101  bool wasSuccessful = true;
102  int i = 0;
103  while (i < argc) {
104  std::string test = CLASS::suite()->getName().append("::").append(argv[i++]);
105  if (debug) std::cerr << "Running " << test << std::endl;
106  wasSuccessful = wasSuccessful && runner.run(test);
107  }
108  return wasSuccessful;
109  }
110 }
111 
112 #endif //HYRAX_GIT_RUN_TESTS_CPPUNIT_H
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:98