46 std::string_view expected_message,
47 std::string_view reason = {},
48 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
50 auto expected_type = detail::exception_type_name<Exception>();
53 std::invoke(std::forward<Function>(function));
54 }
catch (Exception
const& exception) {
55 auto actual_message = std::string_view(exception.what());
56 if (actual_message == expected_message) {
60 auto actual_string = detail::to_string(actual_message);
61 auto expected_string = detail::to_string(expected_message);
62 auto message = std::format(
63 "Expected exception message <{}> but caught <{}>",
67 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_string), location);
68 }
catch (std::exception
const& exception) {
69 auto actual_string = detail::exception_type_name(exception);
70 auto message = std::format(
71 "Expected function to throw exception of type <{}> but caught <{}>",
75 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_type), location);
77 auto message = std::format(
78 "Expected function to throw exception of type <{}> but caught <unknown exception>",
81 throw assertion_error(message, reason,
"unknown exception", std::move(expected_type), location);
84 auto expected_string = detail::to_string(expected_message);
85 auto message = std::format(
86 "Expected function to throw exception of type <{}> with message <{}>",
90 throw assertion_error(message, reason,
"no exception", std::move(expected_string), location);
112 std::regex
const& expected_message,
113 std::string_view reason = {},
114 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
116 auto expected_type = detail::exception_type_name<Exception>();
119 std::invoke(std::forward<Function>(function));
120 }
catch (Exception
const& exception) {
121 auto actual_message = std::string_view(exception.what());
122 if (std::regex_match(actual_message.begin(), actual_message.end(), expected_message)) {
126 auto actual_string = detail::to_string(actual_message);
127 auto message = std::format(
"Expected exception message to match regex but caught <{}>", actual_string);
128 throw assertion_error(message, reason, std::move(actual_string),
"regex", location);
129 }
catch (std::exception
const& exception) {
130 auto actual_string = detail::exception_type_name(exception);
131 auto message = std::format(
132 "Expected function to throw exception of type <{}> but caught <{}>",
136 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_type), location);
138 auto message = std::format(
139 "Expected function to throw exception of type <{}> but caught <unknown exception>",
142 throw assertion_error(message, reason,
"unknown exception", std::move(expected_type), location);
145 auto message = std::format(
146 "Expected function to throw exception of type <{}> with message matching regex",
149 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=KAYCXX_ASSERT_DEFAULT_SOURCE)
Asserts that the function throws an exception of the expected type with the exact expected message.
Definition assert_throw.hpp:44