mimic++ v10.0.1
Loading...
Searching...
No Matches
DefaultReporter.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2026.
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]]
83 void report_no_matches(CallReport call, std::vector<MatchReport> noMatchReports) override
84 {
85 auto const msg = stringify_no_matches(call, noMatchReports);
86 if (m_Out)
87 {
88 *m_Out << msg << '\n';
89 }
90
91 throw UnmatchedCallT{msg, std::move(call), call.fromLoc};
92 }
93
94 [[noreturn]]
95 void report_inapplicable_matches(CallReport call, std::vector<MatchReport> reports) override
96
97 {
98 const auto msg = stringify_inapplicable_matches(call, reports);
99 if (m_Out)
100 {
101 *m_Out << msg << '\n';
102 }
103
104 throw UnmatchedCallT{msg, std::move(call), call.fromLoc};
105 }
106
108 [[maybe_unused]] CallReport const call,
109 [[maybe_unused]] MatchReport const match) noexcept override
110 {
111 MIMICPP_ASSERT(std::holds_alternative<state_applicable>(match.expectationReport.controlReport), "Report denotes inapplicable expectation.");
112 }
113
114 void report_unfulfilled_expectation(ExpectationReport expectationReport) override
115 {
116 if (0 == std::uncaught_exceptions())
117 {
118 auto const msg = stringify_unfulfilled_expectation(expectationReport);
119 if (m_Out)
120 {
121 *m_Out << msg << '\n';
122 }
123
124 throw UnfulfilledExpectationT{msg, std::move(expectationReport)};
125 }
126 }
127
128 void report_error(const StringT message) override
129 {
130 if (0 == std::uncaught_exceptions())
131 {
132 if (m_Out)
133 {
134 *m_Out << message << '\n';
135 }
136
137 throw Error{message};
138 }
139 }
140
142 const CallReport call,
143 const ExpectationReport expectationReport,
144 const std::exception_ptr exception) override
145 {
146 if (m_Out)
147 {
148 *m_Out << stringify_unhandled_exception(call, expectationReport, exception)
149 << '\n';
150 }
151 }
152
153 private:
154 std::ostream* m_Out{};
155 };
156}
157
158#endif
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:54
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Contains the extracted info from a typed call::Info.
Definition CallReport.hpp:43
void report_no_matches(CallReport call, std::vector< MatchReport > noMatchReports) override
Expects reports on all non-matching expectations. This is only called when no better options are avai...
Definition DefaultReporter.hpp:83
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:141
DefaultReporter(std::ostream &out) noexcept
Definition DefaultReporter.hpp:77
void report_inapplicable_matches(CallReport call, std::vector< MatchReport > reports) override
Handles reports for all inapplicable but otherwise matching expectations. This function is called onl...
Definition DefaultReporter.hpp:95
void report_full_match(CallReport const call, MatchReport const match) noexcept override
Handles the report for a fully matching expectation.
Definition DefaultReporter.hpp:107
void report_unfulfilled_expectation(ExpectationReport expectationReport) override
Handles the report of an unfulfilled expectation.
Definition DefaultReporter.hpp:114
void report_error(const StringT message) override
Handles general or unspecified errors.
Definition DefaultReporter.hpp:128
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
Contains the expectation-report and the match-results to a specific call.
Definition MatchReport.hpp:22
A thin wrapper around general source-location info.
Definition SourceLocation.hpp:33
Definition Call.hpp:24
Definition BasicReporter.hpp:26
StringT stringify_unhandled_exception(CallReport const &call, ExpectationReport const &expectationReport, std::exception_ptr const &exception)
Definition StringifyReports.hpp:471
Error< ExpectationReport > UnfulfilledExpectationT
Definition DefaultReporter.hpp:64
StringT stringify_unfulfilled_expectation(ExpectationReport const &expectationReport)
Definition StringifyReports.hpp:455
StringT stringify_inapplicable_matches(CallReport const &call, std::span< MatchReport > reports)
Definition StringifyReports.hpp:354
Error< CallReport > UnmatchedCallT
Definition DefaultReporter.hpp:63
StringT stringify_no_matches(CallReport const &call, std::span< MatchReport > noMatchReports)
Definition StringifyReports.hpp:394
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:397