6#ifndef MIMICPP_REPORTER_HPP
7#define MIMICPP_REPORTER_HPP
77 std::vector<MatchReport> matchReports) = 0;
93 std::vector<MatchReport> matchReports) = 0;
143 std::exception_ptr exception) = 0;
155 template <
typename Data = std::
nullptr_t>
157 :
public std::runtime_error
162 const std::string& what,
163 Data&&
data = Data{},
164 const std::source_location& loc = std::source_location::current())
165 : std::runtime_error{what},
166 m_Data{std::move(
data)},
172 const Data&
data() const noexcept
178 const std::source_location&
where() const noexcept
185 std::source_location m_Loc;
191 inline StringT stringify_no_match_report(
const CallReport& call,
const std::span<const MatchReport> matchReports)
194 ss <<
"No match for ";
196 std::ostreambuf_iterator{ss},
200 if (std::ranges::empty(matchReports))
202 ss <<
"No expectations available.\n";
207 std::ostreambuf_iterator{ss},
208 "{} available expectation(s):\n",
209 std::ranges::size(matchReports));
211 for (
const auto& report : matchReports)
214 std::ostreambuf_iterator{ss},
220 return std::move(ss).str();
224 inline StringT stringify_inapplicable_match_report(
const CallReport& call,
const std::span<const MatchReport> matchReports)
227 ss <<
"No applicable match for ";
229 std::ostreambuf_iterator{ss},
233 ss <<
"Tested expectations:\n";
234 for (
const auto& report : matchReports)
237 std::ostreambuf_iterator{ss},
242 return std::move(ss).str();
246 inline StringT stringify_report(
const CallReport& call,
const MatchReport& matchReport)
249 ss <<
"Found match for ";
251 std::ostreambuf_iterator{ss},
256 std::ostreambuf_iterator{ss},
260 return std::move(ss).str();
264 inline StringT stringify_unfulfilled_expectation(
const ExpectationReport& expectationReport)
267 ss <<
"Unfulfilled expectation:\n";
269 std::ostreambuf_iterator{ss},
273 return std::move(ss).str();
277 inline StringT stringify_unhandled_exception(
278 const CallReport& call,
279 const ExpectationReport& expectationReport,
280 const std::exception_ptr& exception)
283 ss <<
"Unhandled exception: ";
287 std::rethrow_exception(exception);
289 catch (
const std::exception& e)
297 ss <<
"Unknown exception type.\n";
300 ss <<
"while checking expectation:\n";
302 std::ostreambuf_iterator{ss},
308 std::ostreambuf_iterator{ss},
312 return std::move(ss).str();
323 std::invocable<const StringT&>
auto successReporter,
324 std::invocable<const StringT&>
auto warningReporter,
325 std::invocable<const StringT&>
auto failReporter>
334 detail::stringify_no_match_report(call, matchReports));
341 detail::stringify_inapplicable_match_report(call, matchReports));
347 detail::stringify_report(call, matchReport));
352 if (0 == std::uncaught_exceptions())
355 detail::stringify_unfulfilled_expectation(expectationReport));
361 if (0 == std::uncaught_exceptions())
370 const std::exception_ptr exception)
override
373 detail::stringify_unhandled_exception(call, expectationReport, exception));
377 void send_success(
const StringT& msg)
379 std::invoke(successReporter, msg);
382 void send_warning(
const StringT& msg)
384 std::invoke(warningReporter, msg);
388 void send_fail(
const StringT& msg)
391 std::invoke(failReporter, msg);
416 std::vector<MatchReport> matchReports)
override
424 const auto msg = detail::stringify_no_match_report(call, matchReports);
427 *m_Out << msg <<
'\n';
430 const std::source_location loc{call.
fromLoc};
433 {std::move(call), std::move(matchReports)},
441 std::vector<MatchReport> matchReports)
override
449 const auto msg = detail::stringify_inapplicable_match_report(call, matchReports);
452 *m_Out << msg <<
'\n';
455 const std::source_location loc{call.
fromLoc};
458 {std::move(call), std::move(matchReports)},
465 [[maybe_unused]]
const MatchReport matchReport)
noexcept override
473 if (0 == std::uncaught_exceptions())
475 const auto msg = detail::stringify_unfulfilled_expectation(expectationReport);
478 *m_Out << msg <<
'\n';
483 std::move(expectationReport)};
489 if (0 == std::uncaught_exceptions())
493 *m_Out << message <<
'\n';
496 throw Error{message};
503 const std::exception_ptr exception)
override
507 *m_Out << detail::stringify_unhandled_exception(call, expectationReport, exception)
521namespace mimicpp::detail
524 inline std::unique_ptr<IReporter>& get_reporter() noexcept
526 static std::unique_ptr<IReporter> reporter{
527 std::make_unique<DefaultReporter>(&std::cerr)};
532 inline void report_no_matches(
533 CallReport callReport,
534 std::vector<MatchReport> matchReports)
540 std::move(callReport),
541 std::move(matchReports));
550 inline void report_inapplicable_matches(
551 CallReport callReport,
552 std::vector<MatchReport> matchReports)
556 ->report_inapplicable_matches(
558 std::move(callReport),
559 std::move(matchReports));
567 inline void report_full_match(
568 CallReport callReport,
569 MatchReport matchReport)
noexcept
573 std::move(callReport),
574 std::move(matchReport));
577 inline void report_unfulfilled_expectation(
578 ExpectationReport expectationReport)
581 ->report_unfulfilled_expectation(std::move(expectationReport));
584 inline void report_error(
StringT message)
587 ->report_error(std::move(message));
590 inline void report_unhandled_exception(
591 CallReport callReport,
592 ExpectationReport expectationReport,
593 const std::exception_ptr& exception)
596 ->report_unhandled_exception(
597 std::move(callReport),
598 std::move(expectationReport),
613 template <std::derived_from<IReporter> T,
typename... Args>
614 requires std::constructible_from<T, Args...>
617 detail::get_reporter() = std::make_unique<T>(
618 std::forward<Args>(args)...);
623 template <
typename T>
624 class ReporterInstaller
627 template <
typename... Args>
628 explicit ReporterInstaller(Args&&... args)
631 std::forward<Args>(args)...);
A reporter, which creates text messages and reports them via the provided callbacks.
Definition Reporter.hpp:328
void report_error(const StringT message) override
Expects rather unspecific errors.
Definition Reporter.hpp:359
void report_full_match(const CallReport call, const MatchReport matchReport) noexcept override
Expects the report about a full matching expectation.
Definition Reporter.hpp:344
void report_unhandled_exception(const CallReport call, const ExpectationReport expectationReport, const std::exception_ptr exception) override
Expects reports about unhandled exceptions, during handle_call.
Definition Reporter.hpp:367
void report_unfulfilled_expectation(const ExpectationReport expectationReport) override
Expects the report of an unfulfilled expectation.
Definition Reporter.hpp:350
void report_no_matches(const CallReport call, const std::vector< MatchReport > matchReports) override
Expects reports about all none matching expectations. This is only called, if there are no better opt...
Definition Reporter.hpp:331
void report_inapplicable_matches(const CallReport call, const std::vector< MatchReport > matchReports) override
Expects reports about all inapplicable matching expectations. This is only called,...
Definition Reporter.hpp:338
Contains the extracted info from a typed call::Info.
Definition Reports.hpp:203
std::source_location fromLoc
Definition Reports.hpp:217
The default reporter.
Definition Reporter.hpp:405
void report_unhandled_exception(const CallReport call, const ExpectationReport expectationReport, const std::exception_ptr exception) override
Expects reports about unhandled exceptions, during handle_call.
Definition Reporter.hpp:500
void report_no_matches(CallReport call, std::vector< MatchReport > matchReports) override
Expects reports about all none matching expectations. This is only called, if there are no better opt...
Definition Reporter.hpp:414
DefaultReporter(std::ostream *out=nullptr) noexcept
Definition Reporter.hpp:408
void report_unfulfilled_expectation(ExpectationReport expectationReport) override
Expects the report of an unfulfilled expectation.
Definition Reporter.hpp:470
void report_full_match(const CallReport call, const MatchReport matchReport) noexcept override
Expects the report about a full matching expectation.
Definition Reporter.hpp:463
void report_error(const StringT message) override
Expects rather unspecific errors.
Definition Reporter.hpp:487
void report_inapplicable_matches(CallReport call, std::vector< MatchReport > matchReports) override
Expects reports about all inapplicable matching expectations. This is only called,...
Definition Reporter.hpp:439
Definition Reporter.hpp:158
Error(const std::string &what, Data &&data=Data{}, const std::source_location &loc=std::source_location::current())
Definition Reporter.hpp:161
const Data & data() const noexcept
Definition Reporter.hpp:172
const std::source_location & where() const noexcept
Definition Reporter.hpp:178
Contains the extracted info from a typed expectation.
Definition Reports.hpp:316
The reporter interface.
Definition Reporter.hpp:56
IReporter(const IReporter &)=default
virtual void report_unhandled_exception(CallReport call, ExpectationReport expectationReport, std::exception_ptr exception)=0
Expects reports about unhandled exceptions, during handle_call.
virtual void report_error(StringT message)=0
Expects rather unspecific errors.
virtual void report_no_matches(CallReport call, std::vector< MatchReport > matchReports)=0
Expects reports about all none matching expectations. This is only called, if there are no better opt...
virtual void report_full_match(CallReport call, MatchReport matchReport) noexcept=0
Expects the report about a full matching expectation.
virtual ~IReporter()=default
Defaulted virtual destructor.
IReporter(IReporter &&)=default
IReporter & operator=(const IReporter &)=default
virtual void report_unfulfilled_expectation(ExpectationReport expectationReport)=0
Expects the report of an unfulfilled expectation.
virtual void report_inapplicable_matches(CallReport call, std::vector< MatchReport > matchReports)=0
Expects reports about all inapplicable matching expectations. This is only called,...
IReporter & operator=(IReporter &&)=default
Contains the detailed information for match outcomes.
Definition Reports.hpp:402
MatchResult evaluate_match_report(const MatchReport &report) noexcept
Determines, whether a match report actually denotes a full, inapplicable or no match.
Definition Reports.hpp:453
void install_reporter(Args &&... args)
Replaces the previous reporter with a newly constructed one.
Definition Reporter.hpp:615
constexpr detail::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Printer.hpp:593
Definition BoostTest.hpp:20
void unreachable()
Invokes undefined behavior.
Definition Utility.hpp:89
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Printer.hpp:41
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:342