Skip to content
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

Add exception handling to with test macro #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion ut
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,13 @@ template <class...> inline auto cfg = default_cfg{};
<!--
#endif

#if __cpp_exceptions < 199711L
#define UT_NO_EXCEPTIONS
#else
#include <exception>
Copy link
Contributor

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(); }?

#include <stdexcept>
#endif

#pragma once

namespace ut::inline v2_1_2 {
Expand Down Expand Up @@ -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 */ };
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
Expand Down