kaycxx-test
C++ unit test framework
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 <optional>
16#include <regex>
17#include <source_location>
18#include <string_view>
19#include <utility>
20
22#include <kaycxx/test/detail/exception_type.hpp>
23#include <kaycxx/test/detail/exception_type_name.hpp>
24#include <kaycxx/test/detail/to_string.hpp>
25
26namespace kaycxx::test {
27
39template <std::invocable Function>
41 Function&& function,
42 std::string_view reason = {},
43 std::optional<std::source_location> location = std::source_location::current()
44) {
45 try {
46 std::invoke(std::forward<Function>(function));
47 } catch (std::exception const& exception) {
48 auto actual_string = std::format(
49 "exception of type <{}> with message <{}>",
50 detail::exception_type_name(exception),
51 detail::to_string(exception.what())
52 );
53 auto message = std::format("Expected function not to throw but caught {}", actual_string);
54 throw assertion_error(message, reason, std::move(actual_string), "no exception", location);
55 } catch (...) {
56 throw assertion_error(
57 "Expected function not to throw but caught <unknown exception>",
58 reason,
59 "unknown exception",
60 "no exception",
61 location
62 );
63 }
64}
65
82template <detail::exception_type Exception, std::invocable Function>
84 Function&& function,
85 std::string_view unexpected_message,
86 std::string_view reason = {},
87 std::optional<std::source_location> location = std::source_location::current()
88) {
89 auto unexpected_type = detail::exception_type_name<Exception>();
90
91 try {
92 std::invoke(std::forward<Function>(function));
93 } catch (Exception const& exception) {
94 auto actual_message = std::string_view(exception.what());
95 if (actual_message == unexpected_message) {
96 auto expected_string = detail::to_string(unexpected_message);
97 auto actual_string = std::format(
98 "exception of type <{}> with message <{}>",
99 unexpected_type,
100 expected_string
101 );
102 auto message = std::format("Expected function not to throw {}", actual_string);
103 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_string), location);
104 }
105 } catch (...) {
106 // Other exceptions do not match this assertion and are intentionally allowed.
107 }
108}
109
126template <detail::exception_type Exception, std::invocable Function>
128 Function&& function,
129 std::regex const& unexpected_message,
130 std::string_view reason = {},
131 std::optional<std::source_location> location = std::source_location::current()
132) {
133 auto unexpected_type = detail::exception_type_name<Exception>();
134
135 try {
136 std::invoke(std::forward<Function>(function));
137 } catch (Exception const& exception) {
138 auto actual_message = std::string_view(exception.what());
139 if (std::regex_match(actual_message.begin(), actual_message.end(), unexpected_message)) {
140 auto actual_string = std::format(
141 "exception of type <{}> with message matching regex",
142 unexpected_type
143 );
144 auto message = std::format("Expected function not to throw {}", actual_string);
145 throw assertion_error(message, reason, std::move(actual_string), "regex", location);
146 }
147 } catch (...) {
148 // Other exceptions do not match this assertion and are intentionally allowed.
149 }
150}
151
152} // namespace kaycxx::test
Defines the assertion_error type.
Unit test framework functions and types.
Definition test.hpp:15
void assert_not_throw(Function &&function, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
Asserts that the function does not throw any exception.
Definition assert_not_throw.hpp:40