| 1 |
#ifndef TESTUTILS_CPPUNIT_H |
| 2 |
#define TESTUTILS_CPPUNIT_H |
| 3 |
|
| 4 |
#include "./testutils.h" |
| 5 |
|
| 6 |
#include "../application/commandlineutils.h" |
| 7 |
#include "../io/ansiescapecodes.h" |
| 8 |
|
| 9 |
#include <cppunit/TestPath.h> |
| 10 |
#include <cppunit/extensions/TestFactoryRegistry.h> |
| 11 |
#include <cppunit/ui/text/TestRunner.h> |
| 12 |
|
| 13 |
#include <iostream> |
| 14 |
|
| 15 |
using namespace CppUtilities; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
void printTestNames(CPPUNIT_NS::Test *test, Indentation indentation) |
| 21 |
{ |
| 22 |
for (int index = 0, count = test->getChildTestCount(); index != count; ++index) { |
| 23 |
const auto childTest = test->getChildTestAt(index); |
| 24 |
std::cerr << '\n' << indentation << " - " << childTest->getName(); |
| 25 |
printTestNames(childTest, indentation + 4); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
int main(int argc, char **argv) |
| 33 |
{ |
| 34 |
TestApplication testApp(argc, argv); |
| 35 |
if (!testApp) { |
| 36 |
return -1; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
CPPUNIT_NS::TestFactoryRegistry ®istry = CPPUNIT_NS::TestFactoryRegistry::getRegistry(); |
| 41 |
if (testApp.onlyListUnits()) { |
| 42 |
std::cerr << "Available tests:"; |
| 43 |
printTestNames(registry.makeTest(), Indentation(0)); |
| 44 |
std::cerr << '\n'; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
CPPUNIT_NS::TextUi::TestRunner runner; |
| 50 |
if (!testApp.unitsSpecified() || testApp.units().empty()) { |
| 51 |
|
| 52 |
runner.addTest(registry.makeTest()); |
| 53 |
} else { |
| 54 |
|
| 55 |
CPPUNIT_NS::Test *overallTest = registry.makeTest(); |
| 56 |
std::vector<const char *> unavailableUnits; |
| 57 |
for (const char *unit : testApp.units()) { |
| 58 |
try { |
| 59 |
runner.addTest(overallTest->findTest(unit)); |
| 60 |
} catch (const std::invalid_argument &) { |
| 61 |
unavailableUnits.emplace_back(unit); |
| 62 |
} |
| 63 |
} |
| 64 |
if (!unavailableUnits.empty()) { |
| 65 |
std::cerr << "The following tests specified via --unit are not available:"; |
| 66 |
for (const char *unitName : unavailableUnits) { |
| 67 |
std::cerr << "\n - " << unitName; |
| 68 |
} |
| 69 |
std::cerr << "\nAvailable tests:"; |
| 70 |
printTestNames(overallTest, Indentation(0)); |
| 71 |
std::cerr << '\n'; |
| 72 |
return -1; |
| 73 |
} |
| 74 |
} |
| 75 |
std::cerr << EscapeCodes::TextAttribute::Bold << "Executing test cases ..." << EscapeCodes::Phrases::EndFlush; |
| 76 |
const auto ok = runner.run(std::string(), false); |
| 77 |
std::cerr << (ok ? "Tests successful\n" : "Tests failed\n"); |
| 78 |
return !ok; |
| 79 |
} |
| 80 |
|
| 81 |
#endif |