mimic++ v9.2.1
Loading...
Searching...
No Matches
DefaultReporter.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2025.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef MIMICPP_REPORTING_DEFAULT_REPORTER_HPP
7#define MIMICPP_REPORTING_DEFAULT_REPORTER_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
19
20#ifndef MIMICPP_DETAIL_IS_MODULE
21 #include <cstddef>
22 #include <exception>
23 #include <ostream>
24 #include <stdexcept>
25 #include <utility>
26#endif
27
29{
30 template <typename Data = std::nullptr_t>
31 class Error final
32 : public std::logic_error
33 {
34 public:
35 [[nodiscard]]
36 explicit Error(
37 std::string const& what,
38 Data&& data = {},
39 util::SourceLocation loc = {})
40 : std::logic_error{what},
41 m_Data{std::move(data)},
42 m_Loc{std::move(loc)}
43 {
44 }
45
46 [[nodiscard]]
47 Data const& data() const noexcept
48 {
49 return m_Data;
50 }
51
52 [[nodiscard]]
53 util::SourceLocation const& where() const noexcept
54 {
55 return m_Loc;
56 }
57
58 private:
59 Data m_Data;
61 };
62
65
69 class DefaultReporter final
70 : public IReporter
71 {
72 public:
73 [[nodiscard]]
74 DefaultReporter() = default;
75
76 [[nodiscard]]
77 explicit DefaultReporter(std::ostream& out) noexcept
78 : m_Out{std::addressof(out)}
79 {
80 }
81
82 [[noreturn]]
85 std::vector<NoMatchReport> noMatchReports) override
86 {
87 auto const msg = stringify_no_matches(call, noMatchReports);
88 if (m_Out)
89 {
90 *m_Out << msg << '\n';
91 }
92
93 throw UnmatchedCallT{msg, std::move(call), call.fromLoc};
94 }
95
96 [[noreturn]]
99 std::vector<ExpectationReport> expectationReports) override
100
101 {
102 const auto msg = stringify_inapplicable_matches(call, expectationReports);
103 if (m_Out)
104 {
105 *m_Out << msg << '\n';
106 }
107
108 throw UnmatchedCallT{msg, std::move(call), call.fromLoc};
109 }
110
112 [[maybe_unused]] CallReport const call,
113 [[maybe_unused]] ExpectationReport const expectationReport) noexcept override
114 {
115 MIMICPP_ASSERT(std::holds_alternative<state_applicable>(expectationReport.controlReport), "Report denotes inapplicable expectation.");
116 }
117
118 void report_unfulfilled_expectation(ExpectationReport expectationReport) override
119 {
120 if (0 == std::uncaught_exceptions())
121 {
122 auto const msg = stringify_unfulfilled_expectation(expectationReport);
123 if (m_Out)
124 {
125 *m_Out << msg << '\n';
126 }
127
128 throw UnfulfilledExpectationT{msg, std::move(expectationReport)};
129 }
130 }
131
132 void report_error(const StringT message) override
133 {
134 if (0 == std::uncaught_exceptions())
135 {
136 if (m_Out)
137 {
138 *m_Out << message << '\n';
139 }
140
141 throw Error{message};
142 }
143 }
144
146 const CallReport call,
147 const ExpectationReport expectationReport,
148 const std::exception_ptr exception) override
149 {
150 if (m_Out)
151 {
152 *m_Out << stringify_unhandled_exception(call, expectationReport, exception)
153 << '\n';
154 }
155 }
156
157 private:
158 std::ostream* m_Out{};
159 };
160}
161
162#endif
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:51
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Contains the extracted info from a typed call::Info.
Definition CallReport.hpp:43
void report_unhandled_exception(const CallReport call, const ExpectationReport expectationReport, const std::exception_ptr exception) override
Handles reports about unhandled exceptions during handle_call.
Definition DefaultReporter.hpp:145
void report_no_matches(CallReport call, std::vector< NoMatchReport > noMatchReports) override
Expects reports on all non-matching expectations. This is only called when no better options are avai...
Definition DefaultReporter.hpp:83
DefaultReporter(std::ostream &out) noexcept
Definition DefaultReporter.hpp:77
void report_inapplicable_matches(CallReport call, std::vector< ExpectationReport > expectationReports) override
Handles reports for all inapplicable but otherwise matching expectations. This function is called onl...
Definition DefaultReporter.hpp:97
void report_full_match(CallReport const call, ExpectationReport const expectationReport) noexcept override
Handles the report for a fully matching expectation.
Definition DefaultReporter.hpp:111
void report_unfulfilled_expectation(ExpectationReport expectationReport) override
Handles the report of an unfulfilled expectation.
Definition DefaultReporter.hpp:118
void report_error(const StringT message) override
Handles general or unspecified errors.
Definition DefaultReporter.hpp:132
Definition DefaultReporter.hpp:33
Error(std::string const &what, Data &&data={}, util::SourceLocation loc={})
Definition DefaultReporter.hpp:36
CallReport const & data() const noexcept
Definition DefaultReporter.hpp:47
util::SourceLocation const & where() const noexcept
Definition DefaultReporter.hpp:53
Contains the extracted info from a typed expectation.
Definition ExpectationReport.hpp:85
A thin wrapper around general source-location info.
Definition SourceLocation.hpp:38
Definition Call.hpp:24
Definition BasicReporter.hpp:27
StringT stringify_unhandled_exception(CallReport const &call, ExpectationReport const &expectationReport, std::exception_ptr const &exception)
Definition StringifyReports.hpp:466
Error< ExpectationReport > UnfulfilledExpectationT
Definition DefaultReporter.hpp:64
StringT stringify_unfulfilled_expectation(ExpectationReport const &expectationReport)
Definition StringifyReports.hpp:450
StringT stringify_inapplicable_matches(CallReport const &call, std::span< ExpectationReport > expectations)
Definition StringifyReports.hpp:349
Error< CallReport > UnmatchedCallT
Definition DefaultReporter.hpp:63
StringT stringify_no_matches(CallReport const &call, std::span< NoMatchReport > noMatchReports)
Definition StringifyReports.hpp:388
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:391