kaycxx-test
C++ unit test framework
Loading...
Searching...
No Matches
assertion_error.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 <optional>
12#include <source_location>
13#include <stdexcept>
14#include <string>
15#include <string_view>
16#include <utility>
17
18namespace kaycxx::test {
19
23class assertion_error : public std::logic_error {
24public:
38 std::string const& message,
39 std::string_view reason,
40 std::string actual,
41 std::string expected,
42 std::optional<std::source_location> location = std::nullopt
43 ) : std::logic_error{make_message(message, reason)},
44 reason_{reason},
45 actual_{std::move(actual)},
46 expected_{std::move(expected)},
47 location_{location}
48 {}
49
55 std::string const& reason() const noexcept {
56 return reason_;
57 }
58
64 std::string const& actual() const noexcept {
65 return actual_;
66 }
67
73 std::string const& expected() const noexcept {
74 return expected_;
75 }
76
82 std::optional<std::source_location> const& location() const noexcept {
83 return location_;
84 }
85
86private:
94 static std::string make_message(std::string const& message, std::string_view reason) {
95 if (reason.empty()) {
96 return message;
97 }
98
99 auto result = message;
100 result += ": ";
101 result += reason;
102 return result;
103 }
104
105 std::string reason_;
106 std::string actual_;
107 std::string expected_;
108 std::optional<std::source_location> location_;
109};
110
111} // namespace kaycxx::test
Thrown when an assertion fails.
Definition assertion_error.hpp:23
std::optional< std::source_location > const & location() const noexcept
Returns the source location of the failed assertion.
Definition assertion_error.hpp:82
std::string const & reason() const noexcept
Returns the optional assertion reason.
Definition assertion_error.hpp:55
assertion_error(std::string const &message, std::string_view reason, std::string actual, std::string expected, std::optional< std::source_location > location=std::nullopt)
Creates an assertion error with already-rendered diagnostic values.
Definition assertion_error.hpp:37
std::string const & actual() const noexcept
Returns the rendered actual value.
Definition assertion_error.hpp:64
std::string const & expected() const noexcept
Returns the rendered expected value.
Definition assertion_error.hpp:73
Unit test framework functions and types.
Definition test.hpp:15