-
Notifications
You must be signed in to change notification settings - Fork 2
Add exception handling to with test macro #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -641,6 +641,13 @@ template <class...> inline auto cfg = default_cfg{}; | |
<!-- | ||
#endif | ||
|
||
#if __cpp_exceptions < 199711L | ||
#define UT_NO_EXCEPTIONS | ||
#else | ||
#include <exception> | ||
#include <stdexcept> | ||
#endif | ||
|
||
#pragma once | ||
|
||
namespace ut::inline v2_1_2 { | ||
|
@@ -673,6 +680,7 @@ template<mode Mode> struct test_begin { const char* file_name{}; int line{}; con | |
template<mode Mode> struct test_end { const char* file_name{}; int line{}; const char* name{}; enum { FAILED, PASSED, COMPILE_TIME } result{}; }; | ||
template<class TExpr> struct assert_pass { const char* file_name{}; int line{}; TExpr expr{}; }; | ||
template<class TExpr> struct assert_fail { const char* file_name{}; int line{}; TExpr expr{}; }; | ||
struct exception_fail { const char* file_name{}; int line{}; const char* name{}; const char* except{}; }; | ||
struct fatal { }; | ||
template<class TMsg> struct log { const TMsg& msg; bool result{}; }; | ||
struct summary { enum { FAILED, PASSED, COMPILE_TIME }; unsigned asserts[2]{}; /* FAILED, PASSED */ unsigned tests[3]{}; /* FAILED, PASSED, COMPILE_TIME */ }; | ||
|
@@ -691,6 +699,10 @@ class outputter { | |
os << event.file_name << ':' << event.line << ':' << "FAILED:" << '\"' << current_test.name << "\": " << event.expr; | ||
} | ||
} | ||
constexpr auto on(const events::exception_fail& event) { | ||
if (initial_new_line == '\n') { os << initial_new_line; } else { initial_new_line = '\n'; } | ||
os << event.file_name << ':' <<event.line << ':' << "EXCEPTION: " << '\"' << current_test.name << "\": " << event.except << "\n"; | ||
} | ||
constexpr auto on(const events::fatal&) { } | ||
template<class TMsg> constexpr auto on(const events::log<TMsg>& event) { | ||
if (!__builtin_is_constant_evaluated() && !event.result) { | ||
|
@@ -744,6 +756,10 @@ struct reporter { | |
++summary.asserts[events::summary::FAILED]; | ||
outputter.on(event); | ||
} | ||
constexpr auto on(const events::exception_fail& event) { | ||
++summary.asserts[events::summary::FAILED]; | ||
outputter.on(event); | ||
} | ||
constexpr auto on(const events::fatal& event) { | ||
++summary.tests[events::summary::FAILED]; | ||
outputter.on(event); | ||
|
@@ -787,7 +803,21 @@ struct runner { | |
|
||
#ifndef UT_COMPILE_TIME_ONLY | ||
reporter.on(events::test_begin<events::mode::run_time>{file_name, line, name}); | ||
test(); | ||
#ifndef UT_NO_EXCEPTIONS | ||
try { | ||
#endif | ||
test(); | ||
#ifndef UT_NO_EXCEPTIONS | ||
} catch (const char* string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we just catch anything which either has what or is printable? |
||
reporter.on(events::exception_fail{ file_name, line, name, string }); | ||
} catch(...) { | ||
try { | ||
std::rethrow_exception(std::current_exception()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I follow why rethrow is required here, could you elaborate? my initial thought is that catching exception would be the same but I guess I'm missing something? |
||
} catch (const std::exception& ex) { | ||
reporter.on(events::exception_fail{ file_name, line, name, ex.what() }); | ||
} | ||
} | ||
#endif | ||
reporter.on(events::test_end<events::mode::run_time>{file_name, line, name}); | ||
#endif | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be possible not to rely on stl, can we just do throw and
catch (const auto& ex) requires requires { ex.waht(); }
?