kaycxx-assert
C++ assertion library
Loading...
Searching...
No Matches
assert_match.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 <format>
12#include <regex>
13#include <string>
14#include <string_view>
15#include <utility>
16
18#include <kaycxx/assert/detail/source_location.hpp>
19#include <kaycxx/assert/detail/regex_matches.hpp>
20#include <kaycxx/assert/detail/string_like.hpp>
21#include <kaycxx/assert/detail/to_string.hpp>
22
23namespace kaycxx::assert {
24
37template <detail::string_like T>
39 T const& actual,
40 std::string_view pattern,
41 std::string_view reason = {},
42 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
43) {
44 auto regex = std::regex(std::string(pattern));
45 if (!detail::regex_matches(actual, regex)) {
46 auto actual_string = detail::to_string(actual);
47 auto expected_string = std::format("/{}/", pattern);
48 auto message = std::format("Expected <{}> to match <{}>", actual_string, expected_string);
49 throw assertion_error(message, reason, std::move(actual_string), std::move(expected_string), location);
50 }
51}
52
65template <detail::string_like T>
67 T const& actual,
68 std::regex const& regex,
69 std::string_view reason = {},
70 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
71) {
72 if (!detail::regex_matches(actual, regex)) {
73 auto actual_string = detail::to_string(actual);
74 auto message = std::format("Expected <{}> to match regex", actual_string);
75
76 throw assertion_error(message, reason, std::move(actual_string), "regex", location);
77 }
78}
79
80} // namespace kaycxx::assert
Defines the assertion_error type.
Assertion functions and related types.
Definition assert.hpp:15
void assert_match(T const &actual, std::string_view pattern, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE)
Asserts that the argument matches the given pattern.
Definition assert_match.hpp:38