bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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
39
40#ifndef HYRAX_GIT_RUN_TESTS_CPPUNIT_H
41#define HYRAX_GIT_RUN_TESTS_CPPUNIT_H
42
43#include <fstream>
44#include <string>
45#include <iterator>
46
47#include <unistd.h>
48#include <cppunit/TextTestRunner.h>
49#include <cppunit/extensions/TestFactoryRegistry.h>
50#include <cppunit/extensions/HelperMacros.h>
51
52#include "BESDebug.h"
53
54bool debug = false;
55
56#undef DBG
57#define DBG(x) do { if (debug) (x); } while(false)
58
59bool debug2 = false;
60
61#undef DBG2
62#define DBG2(x) do { if (debug2) (x); } while(false)
63
64void show_file(const std::string &filename)
65{
66 std::ifstream t(filename);
67
68 if (t.is_open()) {
69 std::string file_content((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
70 std::cout << "##################################################################" << std::endl;
71 std::cout << "file: " << filename << std::endl;
72 std::cout << ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " << std::endl;
73 std::cout << file_content << std::endl;
74 std::cout << ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " << std::endl;
75 }
76}
77
91template<class CLASS>
92bool bes_run_tests(int argc, char *argv[], const std::string &besdebug_contexts)
93{
94 CppUnit::TextTestRunner runner;
95 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
96
97 int option_char;
98 while ((option_char = getopt(argc, argv, "dDbh")) != -1) {
99 switch (option_char) {
100 case 'd':
101 debug = true; // debug is a global
102 break;
103 case 'D':
104 debug2 = true; // debug2 is a global
105 break;
106 case 'b':
107 BESDebug::SetUp(besdebug_contexts);
108 break;
109 case 'h': { // help - show test names
110 std::cerr << "Usage: the following tests can be run individually or in combination:" << std::endl;
111 auto &tests = CLASS::suite()->getTests();
112 unsigned int prefix_len = CLASS::suite()->getName().append("::").size();
113 for (auto &t: tests) {
114 std::cerr << t->getName().replace(0, prefix_len, "") << std::endl;
115 }
116 exit(EXIT_SUCCESS);
117 }
118 default:
119 break;
120 }
121 }
122
123 argc -= optind;
124 argv += optind;
125
126 if (0 == argc) { // run them all
127 return runner.run("");
128 }
129 else {
130 bool wasSuccessful = true;
131 int i = 0;
132 while (i < argc) {
133 std::string test = CLASS::suite()->getName().append("::").append(argv[i++]);
134 if (debug) std::cerr << "Running " << test << std::endl;
135 wasSuccessful = wasSuccessful && runner.run(test);
136 }
137 return wasSuccessful;
138 }
139}
140
141#endif //HYRAX_GIT_RUN_TESTS_CPPUNIT_H
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition BESDebug.cc:91