Version: 9.16.0
MainTest.hxx
Go to the documentation of this file.
1// Copyright (C) 2013-2026 CEA, EDF, OPEN CASCADE
2//
3// This library is free software; you can redistribute it and/or
4// modify it under the terms of the GNU Lesser General Public
5// License as published by the Free Software Foundation; either
6// version 2.1 of the License, or (at your option) any later version.
7//
8// This library is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11// Lesser General Public License for more details.
12//
13// You should have received a copy of the GNU Lesser General Public
14// License along with this library; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16//
17// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18//
19// Author : Frederic Pons (OpenCascade)
20
21#ifndef __XAOMAINTEST_HXX__
22#define __XAOMAINTEST_HXX__
23
24#include <cppunit/CompilerOutputter.h>
25#include <cppunit/TestResult.h>
26#include <cppunit/TestResultCollector.h>
27#include <cppunit/TextTestProgressListener.h>
28#include <cppunit/BriefTestProgressListener.h>
29#include <cppunit/extensions/TestFactoryRegistry.h>
30#include <cppunit/TestRunner.h>
31#include <stdexcept>
32
33#include <cstring>
34#include <iostream>
35#include <fstream>
36
38{
39public:
40 bool List;
41 bool Err;
42 std::string Test;
43};
44
45Arguments parseArguments(int argc, char* argv[])
46{
47 Arguments res;
48 res.List = false;
49 res.Err = false;
50 res.Test = "";
51
52 if (argc > 1)
53 {
54 int i = 0;
55 while (++i < argc) // skip 0
56 {
57 if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list") == 0)
58 {
59 res.List = true;
60 }
61 else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--err") == 0)
62 {
63 res.Err = true;
64 }
65 else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--test") == 0)
66 {
67 if (i+1 < argc)
68 res.Test = argv[++i];
69 }
70 }
71 }
72
73 return res;
74}
75
76void listTests(CPPUNIT_NS::Test* test)
77{
78 if (!test) return;
79 std::cout << test->getName() << std::endl;
80 for (int i = 0; i < test->getChildTestCount(); ++i)
81 {
82 listTests(test->getChildTestAt(i));
83 }
84}
85
86// ============================================================================
91// ============================================================================
92int main(int argc, char* argv[])
93{
94 Arguments args = parseArguments(argc, argv);
95
96 // --- Create the event manager and test controller
97 CPPUNIT_NS::TestResult controller;
98
99 // --- Add a listener that colllects test result
100 CPPUNIT_NS::TestResultCollector result;
101 controller.addListener(&result);
102
103 // --- Add a listener that print dots as test run.
104#ifdef WIN32
105 CPPUNIT_NS::TextTestProgressListener progress;
106#else
107 CPPUNIT_NS::BriefTestProgressListener progress;
108#endif
109 controller.addListener(&progress);
110
111 // --- Get the top level suite from the registry
112
113 CPPUNIT_NS::Test *suite =
114 CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
115
116 // --- Adds the test to the list of test to run
117
118 // list tests and exit
119 if (args.List)
120 {
121 listTests(suite);
122 return 0;
123 }
124
125 if (!args.Test.empty())
126 {
127 // find test by name
128 // an exception is raised if not found
129 suite = suite->findTest(args.Test);
130 }
131
132 CPPUNIT_NS::TestRunner runner;
133 runner.addTest(suite);
134 runner.run(controller);
135
136 // --- Print test in a compiler compatible format.
137
138 if (args.Err)
139 {
140 CPPUNIT_NS::CompilerOutputter outputter(&result, std::cerr);
141 outputter.write();
142 }
143 else
144 {
145 std::ofstream testFile;
146 testFile.open("UnitTestsResult", std::ios::out | std::ios::trunc);
147 CPPUNIT_NS::CompilerOutputter outputter(&result, testFile);
148 outputter.write();
149 testFile.close();
150 }
151
152 // --- Run the tests.
153
154 bool wasSucessful = result.wasSuccessful();
155
156 // --- Return error code 1 if the one of test failed.
157
158 return wasSucessful ? 0 : 1;
159}
160
161#endif
int main(int argc, char *argv[])
Definition: MainTest.hxx:92
void listTests(CPPUNIT_NS::Test *test)
Definition: MainTest.hxx:76
Arguments parseArguments(int argc, char *argv[])
Definition: MainTest.hxx:45
Definition: MainTest.hxx:38
std::string Test
Definition: MainTest.hxx:42
bool List
Definition: MainTest.hxx:40
bool Err
Definition: MainTest.hxx:41