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