47 std::string_view expected_message,
48 std::string_view reason = {},
49 std::optional<std::source_location> location = std::source_location::current()
51 auto expected_type = detail::exception_type_name<Exception>();
54 std::invoke(std::forward<Function>(function));
55 }
catch (Exception
const& exception) {
56 auto actual_message = std::string_view(exception.what());
57 if (actual_message == expected_message) {
61 auto actual_string = detail::to_string(actual_message);
62 auto expected_string = detail::to_string(expected_message);
63 auto message = std::format(
64 "Expected exception message <{}> but caught <{}>",
68 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_string), location);
69 }
catch (std::exception
const& exception) {
70 auto actual_string = detail::exception_type_name(exception);
71 auto message = std::format(
72 "Expected function to throw exception of type <{}> but caught <{}>",
76 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_type), location);
78 auto message = std::format(
79 "Expected function to throw exception of type <{}> but caught <unknown exception>",
82 throw assertion_error(message, reason,
"unknown exception", std::move(expected_type), location);
85 auto expected_string = detail::to_string(expected_message);
86 auto message = std::format(
87 "Expected function to throw exception of type <{}> with message <{}>",
91 throw assertion_error(message, reason,
"no exception", std::move(expected_string), location);
113 std::regex
const& expected_message,
114 std::string_view reason = {},
115 std::optional<std::source_location> location = std::source_location::current()
117 auto expected_type = detail::exception_type_name<Exception>();
120 std::invoke(std::forward<Function>(function));
121 }
catch (Exception
const& exception) {
122 auto actual_message = std::string_view(exception.what());
123 if (std::regex_match(actual_message.begin(), actual_message.end(), expected_message)) {
127 auto actual_string = detail::to_string(actual_message);
128 auto message = std::format(
"Expected exception message to match regex but caught <{}>", actual_string);
129 throw assertion_error(message, reason, std::move(actual_string),
"regex", location);
130 }
catch (std::exception
const& exception) {
131 auto actual_string = detail::exception_type_name(exception);
132 auto message = std::format(
133 "Expected function to throw exception of type <{}> but caught <{}>",
137 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_type), location);
139 auto message = std::format(
140 "Expected function to throw exception of type <{}> but caught <unknown exception>",
143 throw assertion_error(message, reason,
"unknown exception", std::move(expected_type), location);
146 auto message = std::format(
147 "Expected function to throw exception of type <{}> with message matching regex",
150 throw assertion_error(message, reason,
"no exception",
"regex", location);
void assert_throw(Function &&function, std::string_view expected_message, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
Asserts that the function throws an exception of the expected type with the exact expected message.
Definition assert_throw.hpp:45