kaycxx-assert
C++ assertion library
Loading...
Searching...
No Matches
assert_not_throw.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Klaus Reimer <k@ailis.de>
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
11#include <concepts>
12#include <exception>
13#include <format>
14#include <functional>
15#include <regex>
16#include <string_view>
17#include <utility>
18
20#include <kaycxx/assert/detail/source_location.hpp>
21#include <kaycxx/assert/detail/exception_type.hpp>
22#include <kaycxx/assert/detail/exception_type_name.hpp>
23#include <kaycxx/assert/detail/to_string.hpp>
24
25namespace kaycxx::assert {
26
38template <std::invocable Function>
40 Function&& function,
41 std::string_view reason = {},
42 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
43) {
44 try {
45 std::invoke(std::forward<Function>(function));
46 } catch (std::exception const& exception) {
47 auto actual_string = std::format(
48 "exception of type <{}> with message <{}>",
49 detail::exception_type_name(exception),
50 detail::to_string(exception.what())
51 );
52 auto message = std::format("Expected function not to throw but caught {}", actual_string);
53 throw assertion_error(message, reason, std::move(actual_string), "no exception", location);
54 } catch (...) {
55 throw assertion_error(
56 "Expected function not to throw but caught <unknown exception>",
57 reason,
58 "unknown exception",
59 "no exception",
60 location
61 );
62 }
63}
64
81template <detail::exception_type Exception, std::invocable Function>
83 Function&& function,
84 std::string_view unexpected_message,
85 std::string_view reason = {},
86 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
87) {
88 auto unexpected_type = detail::exception_type_name<Exception>();
89
90 try {
91 std::invoke(std::forward<Function>(function));
92 } catch (Exception const& exception) {
93 auto actual_message = std::string_view(exception.what());
94 if (actual_message == unexpected_message) {
95 auto expected_string = detail::to_string(unexpected_message);
96 auto actual_string = std::format(
97 "exception of type <{}> with message <{}>",
98 unexpected_type,
99 expected_string
100 );
101 auto message = std::format("Expected function not to throw {}", actual_string);
102 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_string), location);
103 }
104 } catch (...) {
105 // Other exceptions do not match this assertion and are intentionally allowed.
106 }
107}
108
125template <detail::exception_type Exception, std::invocable Function>
127 Function&& function,
128 std::regex const& unexpected_message,
129 std::string_view reason = {},
130 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
131) {
132 auto unexpected_type = detail::exception_type_name<Exception>();
133
134 try {
135 std::invoke(std::forward<Function>(function));
136 } catch (Exception const& exception) {
137 auto actual_message = std::string_view(exception.what());
138 if (std::regex_match(actual_message.begin(), actual_message.end(), unexpected_message)) {
139 auto actual_string = std::format(
140 "exception of type <{}> with message matching regex",
141 unexpected_type
142 );
143 auto message = std::format("Expected function not to throw {}", actual_string);
144 throw assertion_error(message, reason, std::move(actual_string), "regex", location);
145 }
146 } catch (...) {
147 // Other exceptions do not match this assertion and are intentionally allowed.
148 }
149}
150
151} // namespace kaycxx::assert
Defines the assertion_error type.
Assertion functions and related types.
Definition assert.hpp:15
void assert_not_throw(Function &&function, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE)
Asserts that the function does not throw any exception.
Definition assert_not_throw.hpp:39