6#ifndef MIMICPP_REPORTER_HPP
7#define MIMICPP_REPORTER_HPP
77 std::vector<MatchReport> matchReports
94 std::vector<MatchReport> matchReports
147 std::exception_ptr exception
160 template <
typename Data = std::
nullptr_t>
162 :
public std::runtime_error
167 const std::string& what,
168 Data&&
data = Data{},
169 const std::source_location& loc = std::source_location::current()
171 : std::runtime_error{what},
172 m_Data{std::move(
data)},
178 const Data&
data() const noexcept
184 const std::source_location&
where() const noexcept
191 std::source_location m_Loc;
197 inline StringT stringify_no_match_report(
const CallReport& call,
const std::span<const MatchReport> matchReports)
200 ss <<
"No match for ";
202 std::ostreambuf_iterator{ss},
206 if (std::ranges::empty(matchReports))
208 ss <<
"No expectations available.\n";
213 std::ostreambuf_iterator{ss},
214 "{} available expectation(s):\n",
215 std::ranges::size(matchReports));
217 for (
const auto& report : matchReports)
220 std::ostreambuf_iterator{ss},
226 return std::move(ss).str();
230 inline StringT stringify_inapplicable_match_report(
const CallReport& call,
const std::span<const MatchReport> matchReports)
233 ss <<
"No applicable match for ";
235 std::ostreambuf_iterator{ss},
239 ss <<
"Tested expectations:\n";
240 for (
const auto& report : matchReports)
243 std::ostreambuf_iterator{ss},
248 return std::move(ss).str();
252 inline StringT stringify_report(
const CallReport& call,
const MatchReport& matchReport)
255 ss <<
"Found match for ";
257 std::ostreambuf_iterator{ss},
262 std::ostreambuf_iterator{ss},
266 return std::move(ss).str();
270 inline StringT stringify_unfulfilled_expectation(
const ExpectationReport& expectationReport)
273 ss <<
"Unfulfilled expectation:\n";
275 std::ostreambuf_iterator{ss},
279 return std::move(ss).str();
283 inline StringT stringify_unhandled_exception(
284 const CallReport& call,
285 const ExpectationReport& expectationReport,
286 const std::exception_ptr& exception
290 ss <<
"Unhandled exception: ";
294 std::rethrow_exception(exception);
296 catch (
const std::exception& e)
304 ss <<
"Unknown exception type.\n";
307 ss <<
"while checking expectation:\n";
309 std::ostreambuf_iterator{ss},
315 std::ostreambuf_iterator{ss},
319 return std::move(ss).str();
330 std::invocable<const StringT&>
auto successReporter,
331 std::invocable<const StringT&>
auto warningReporter,
332 std::invocable<const StringT&>
auto failReporter
342 detail::stringify_no_match_report(call, matchReports));
349 detail::stringify_inapplicable_match_report(call, matchReports));
355 detail::stringify_report(call, matchReport));
360 if (0 == std::uncaught_exceptions())
363 detail::stringify_unfulfilled_expectation(expectationReport));
369 if (0 == std::uncaught_exceptions())
378 const std::exception_ptr exception
382 detail::stringify_unhandled_exception(call, expectationReport, exception));
386 void send_success(
const StringT& msg)
388 std::invoke(successReporter, msg);
391 void send_warning(
const StringT& msg)
393 std::invoke(warningReporter, msg);
397 void send_fail(
const StringT& msg)
400 std::invoke(failReporter, msg);
425 std::vector<MatchReport> matchReports
434 const auto msg = detail::stringify_no_match_report(call, matchReports);
437 *m_Out << msg <<
'\n';
440 const std::source_location loc{call.
fromLoc};
443 {std::move(call), std::move(matchReports)},
451 std::vector<MatchReport> matchReports
460 const auto msg = detail::stringify_inapplicable_match_report(call, matchReports);
463 *m_Out << msg <<
'\n';
466 const std::source_location loc{call.
fromLoc};
469 {std::move(call), std::move(matchReports)},
486 if (0 == std::uncaught_exceptions())
488 const auto msg = detail::stringify_unfulfilled_expectation(expectationReport);
491 *m_Out << msg <<
'\n';
496 std::move(expectationReport)
503 if (0 == std::uncaught_exceptions())
507 *m_Out << message <<
'\n';
510 throw Error{message};
517 const std::exception_ptr exception
522 *m_Out << detail::stringify_unhandled_exception(call, expectationReport, exception)
536namespace mimicpp::detail
539 inline std::unique_ptr<IReporter>& get_reporter() noexcept
541 static std::unique_ptr<IReporter> reporter{
542 std::make_unique<DefaultReporter>(&std::cerr)
548 inline void report_no_matches(
549 CallReport callReport,
550 std::vector<MatchReport> matchReports
557 std::move(callReport),
558 std::move(matchReports));
567 inline void report_inapplicable_matches(
568 CallReport callReport,
569 std::vector<MatchReport> matchReports
574 ->report_inapplicable_matches(
576 std::move(callReport),
577 std::move(matchReports));
585 inline void report_full_match(
586 CallReport callReport,
587 MatchReport matchReport
592 std::move(callReport),
593 std::move(matchReport));
596 inline void report_unfulfilled_expectation(
597 ExpectationReport expectationReport
601 ->report_unfulfilled_expectation(std::move(expectationReport));
604 inline void report_error(
StringT message)
607 ->report_error(std::move(message));
610 inline void report_unhandled_exception(
611 CallReport callReport,
612 ExpectationReport expectationReport,
613 const std::exception_ptr& exception
617 ->report_unhandled_exception(
618 std::move(callReport),
619 std::move(expectationReport),
634 template <std::derived_from<IReporter> T,
typename... Args>
635 requires std::constructible_from<T, Args...>
638 detail::get_reporter() = std::make_unique<T>(
639 std::forward<Args>(args)...);
644 template <
typename T>
645 class ReporterInstaller
648 template <
typename... Args>
649 explicit ReporterInstaller(Args&&... args)
652 std::forward<Args>(args)...);
A reporter, which creates text messages and reports them via the provided callbacks.
Definition Reporter.hpp:336
void report_error(const StringT message) override
Expects rather unspecific errors.
Definition Reporter.hpp:367
void report_full_match(const CallReport call, const MatchReport matchReport) noexcept override
Expects the report about a full matching expectation.
Definition Reporter.hpp:352
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:375
void report_unfulfilled_expectation(const ExpectationReport expectationReport) override
Expects the report of an unfulfilled expectation.
Definition Reporter.hpp:358
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:339
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:346
Contains the extracted info from a typed call::Info.
Definition Reports.hpp:205
std::source_location fromLoc
Definition Reports.hpp:219
The default reporter.
Definition Reporter.hpp:414
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:514
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:423
DefaultReporter(std::ostream *out=nullptr) noexcept
Definition Reporter.hpp:417
void report_unfulfilled_expectation(ExpectationReport expectationReport) override
Expects the report of an unfulfilled expectation.
Definition Reporter.hpp:482
void report_full_match(const CallReport call, const MatchReport matchReport) noexcept override
Expects the report about a full matching expectation.
Definition Reporter.hpp:474
void report_error(const StringT message) override
Expects rather unspecific errors.
Definition Reporter.hpp:501
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:449
Definition Reporter.hpp:163
Error(const std::string &what, Data &&data=Data{}, const std::source_location &loc=std::source_location::current())
Definition Reporter.hpp:166
const Data & data() const noexcept
Definition Reporter.hpp:178
const std::source_location & where() const noexcept
Definition Reporter.hpp:184
Contains the extracted info from a typed expectation.
Definition Reports.hpp:320
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,...
Contains the detailed information for match outcomes.
Definition Reports.hpp:407
MatchResult evaluate_match_report(const MatchReport &report) noexcept
Determines, whether a match report actually denotes a full, inapplicable or no match.
Definition Reports.hpp:458
void install_reporter(Args &&... args)
Replaces the previous reporter with a newly constructed one.
Definition Reporter.hpp:636
constexpr detail::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Printer.hpp:597
Definition BoostTest.hpp:20
void unreachable()
Invokes undefined behavior.
Definition Utility.hpp:93
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Printer.hpp:41
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:207