]> git.ipfire.org Git - thirdparty/squid.git/blob - include/unitTestMain.h
Fix SMP mgr:userhash, mgr:sourcehash, and mgr:carp reports (#1844)
[thirdparty/squid.git] / include / unitTestMain.h
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_INCLUDE_UNITTESTMAIN_H
10 #define SQUID_INCLUDE_UNITTESTMAIN_H
11
12 #if ENABLE_DEBUG_SECTION
13 #include "debug/Stream.h"
14 #endif /* ENABLE_DEBUG_SECTION */
15
16 #include <cppunit/BriefTestProgressListener.h>
17 #include <cppunit/TextTestProgressListener.h>
18 #include <cppunit/CompilerOutputter.h>
19 #include <cppunit/extensions/TestFactoryRegistry.h>
20 #include <cppunit/TestResult.h>
21 #include <cppunit/TestResultCollector.h>
22 #include <cppunit/TestRunner.h>
23
24 /// implements test program's main() function while enabling customization
25 class TestProgram
26 {
27 public:
28 virtual ~TestProgram() = default;
29
30 /// Runs before all tests.
31 /// Does nothing by default.
32 virtual void startup() {}
33
34 /// Implements main(), combining all the steps.
35 /// Must be called from main().
36 /// \returns desired main() result.
37 int run(int argc, char *argv[]);
38
39 private:
40 bool runTests();
41 };
42
43 int
44 TestProgram::run(int, char *[])
45 {
46 #if ENABLE_DEBUG_SECTION
47 Debug::Levels[ENABLE_DEBUG_SECTION] = 99;
48 #endif
49
50 startup();
51 const auto result = runTests();
52 return result ? 0 : 1;
53 }
54
55 /// runs all tests registered with CPPUNIT_TEST_SUITE_REGISTRATION() calls
56 /// \returns whether all tests were successful
57 bool
58 TestProgram::runTests()
59 {
60 // Create the event manager and test controller
61 CPPUNIT_NS::TestResult controller;
62
63 // Add a listener that colllects test result
64 CPPUNIT_NS::TestResultCollector result;
65 controller.addListener( &result );
66
67 // Add a listener that print dots as test run.
68 // use BriefTestProgressListener to get names of each test
69 // even when they pass.
70 // CPPUNIT_NS::BriefTestProgressListener progress;
71 CPPUNIT_NS::TextTestProgressListener progress;
72 controller.addListener( &progress );
73
74 // Add the top suite to the test runner
75 CPPUNIT_NS::TestRunner runner;
76 runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
77 runner.run( controller );
78
79 // Print test in a compiler compatible format.
80 CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr );
81 outputter.write();
82
83 return result.wasSuccessful();
84 }
85
86 #endif /* SQUID_INCLUDE_UNITTESTMAIN_H */
87