Skip to content

Commit 634881d

Browse files
authored
testrunner: moved exception handling to TestFixture::run() and made it unconditional (#4810)
1 parent 92b4225 commit 634881d

2 files changed

Lines changed: 23 additions & 28 deletions

File tree

test/fixture.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
#include "fixture.h"
2020

2121
#include "color.h"
22+
#include "errortypes.h"
2223
#include "options.h"
2324
#include "redirect.h"
2425

2526
#include <cstdio>
2627
#include <cctype>
28+
#include <exception>
2729
#include <iostream>
2830
#include <sstream>
2931
#include <string>
@@ -311,12 +313,27 @@ void TestFixture::printHelp()
311313
void TestFixture::run(const std::string &str)
312314
{
313315
testToRun = str;
314-
if (quiet_tests) {
315-
std::cout << '\n' << classname << ':';
316-
REDIRECT;
317-
run();
318-
} else
319-
run();
316+
try {
317+
if (quiet_tests) {
318+
std::cout << '\n' << classname << ':';
319+
REDIRECT;
320+
run();
321+
}
322+
else
323+
run();
324+
}
325+
catch (const InternalError& e) {
326+
++fails_counter;
327+
errmsg << "InternalError: " << e.errorMessage << std::endl;
328+
}
329+
catch (const std::exception& error) {
330+
++fails_counter;
331+
errmsg << "exception: " << error.what() << std::endl;
332+
}
333+
catch (...) {
334+
++fails_counter;
335+
errmsg << "Unknown exception" << std::endl;
336+
}
320337
}
321338

322339
void TestFixture::processOptions(const options& args)

test/main.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,13 @@
2222

2323
#include <cstdlib>
2424

25-
#ifdef NDEBUG
26-
#include "errortypes.h" // for InternalError
27-
28-
#include <exception>
29-
#include <iostream>
30-
#include <string>
31-
#endif
32-
3325
int main(int argc, char *argv[])
3426
{
3527
// MS Visual C++ memory leak debug tracing
3628
#if defined(_MSC_VER) && defined(_DEBUG)
3729
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
3830
#endif
3931

40-
#ifdef NDEBUG
41-
try {
42-
#endif
4332
Preprocessor::macroChar = '$'; // While macroChar is char(1) per default outside test suite, we require it to be a human-readable character here.
4433

4534
options args(argc, argv);
@@ -50,15 +39,4 @@ int main(int argc, char *argv[])
5039
}
5140
const std::size_t failedTestsCount = TestFixture::runTests(args);
5241
return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
53-
#ifdef NDEBUG
54-
}
55-
catch (const InternalError& e) {
56-
std::cout << e.errorMessage << std::endl;
57-
} catch (const std::exception& error) {
58-
std::cout << error.what() << std::endl;
59-
} catch (...) {
60-
std::cout << "Unknown exception" << std::endl;
61-
}
62-
return EXIT_FAILURE;
63-
#endif
6442
}

0 commit comments

Comments
 (0)