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;
190 template <pr
int_iterator OutIter>
191 OutIter stringify_stacktrace(OutIter out,
const Stacktrace& stacktrace)
193 if (!stacktrace.empty())
195 out = format::format_to(
207 inline StringT stringify_no_match_report(
const CallReport& call,
const std::span<const MatchReport> matchReports)
210 ss <<
"No match for ";
212 std::ostreambuf_iterator{ss},
216 if (std::ranges::empty(matchReports))
218 ss <<
"No expectations available.\n";
223 std::ostreambuf_iterator{ss},
224 "{} available expectation(s):\n",
225 std::ranges::size(matchReports));
227 for (
const auto& report : matchReports)
230 std::ostreambuf_iterator{ss},
236 stringify_stacktrace(
237 std::ostreambuf_iterator{ss},
240 return std::move(ss).str();
244 inline StringT stringify_inapplicable_match_report(
const CallReport& call,
const std::span<const MatchReport> matchReports)
247 ss <<
"No applicable match for ";
249 std::ostreambuf_iterator{ss},
253 ss <<
"Tested expectations:\n";
254 for (
const auto& report : matchReports)
257 std::ostreambuf_iterator{ss},
262 stringify_stacktrace(
263 std::ostreambuf_iterator{ss},
266 return std::move(ss).str();
270 inline StringT stringify_report(
const CallReport& call,
const MatchReport& matchReport)
273 ss <<
"Found match for ";
275 std::ostreambuf_iterator{ss},
280 std::ostreambuf_iterator{ss},
284 stringify_stacktrace(
285 std::ostreambuf_iterator{ss},
288 return std::move(ss).str();
292 inline StringT stringify_unfulfilled_expectation(
const ExpectationReport& expectationReport)
295 ss <<
"Unfulfilled expectation:\n";
297 std::ostreambuf_iterator{ss},
301 return std::move(ss).str();
305 inline StringT stringify_unhandled_exception(
306 const CallReport& call,
307 const ExpectationReport& expectationReport,
308 const std::exception_ptr& exception)
311 ss <<
"Unhandled exception: ";
315 std::rethrow_exception(exception);
317 catch (
const std::exception& e)
325 ss <<
"Unknown exception type.\n";
328 ss <<
"while checking expectation:\n";
330 std::ostreambuf_iterator{ss},
336 std::ostreambuf_iterator{ss},
340 return std::move(ss).str();
351 std::invocable<const StringT&>
auto successReporter,
352 std::invocable<const StringT&>
auto warningReporter,
353 std::invocable<const StringT&>
auto failReporter>
362 detail::stringify_no_match_report(
call, matchReports));
369 detail::stringify_inapplicable_match_report(
call, matchReports));
375 detail::stringify_report(
call, matchReport));
380 if (0 == std::uncaught_exceptions())
383 detail::stringify_unfulfilled_expectation(expectationReport));
389 if (0 == std::uncaught_exceptions())
398 const std::exception_ptr exception)
override
401 detail::stringify_unhandled_exception(
call, expectationReport, exception));
405 void send_success(
const StringT& msg)
407 std::invoke(successReporter, msg);
410 void send_warning(
const StringT& msg)
412 std::invoke(warningReporter, msg);
416 void send_fail(
const StringT& msg)
419 std::invoke(failReporter, msg);
444 std::vector<MatchReport> matchReports)
override
452 const auto msg = detail::stringify_no_match_report(
call, matchReports);
455 *m_Out << msg <<
'\n';
458 const std::source_location loc{
call.fromLoc};
461 {std::move(
call), std::move(matchReports)},
469 std::vector<MatchReport> matchReports)
override
477 const auto msg = detail::stringify_inapplicable_match_report(
call, matchReports);
480 *m_Out << msg <<
'\n';
483 const std::source_location loc{
call.fromLoc};
486 {std::move(
call), std::move(matchReports)},
493 [[maybe_unused]]
const MatchReport matchReport)
noexcept override
501 if (0 == std::uncaught_exceptions())
503 const auto msg = detail::stringify_unfulfilled_expectation(expectationReport);
506 *m_Out << msg <<
'\n';
511 std::move(expectationReport)};
517 if (0 == std::uncaught_exceptions())
521 *m_Out << message <<
'\n';
524 throw Error{message};
531 const std::exception_ptr exception)
override
535 *m_Out << detail::stringify_unhandled_exception(
call, expectationReport, exception)
549namespace mimicpp::detail
552 inline std::unique_ptr<IReporter>& get_reporter() noexcept
554 static std::unique_ptr<IReporter> reporter{
555 std::make_unique<DefaultReporter>(&std::cerr)};
560 inline void report_no_matches(
561 CallReport callReport,
562 std::vector<MatchReport> matchReports)
568 std::move(callReport),
569 std::move(matchReports));
578 inline void report_inapplicable_matches(
579 CallReport callReport,
580 std::vector<MatchReport> matchReports)
584 ->report_inapplicable_matches(
586 std::move(callReport),
587 std::move(matchReports));
595 inline void report_full_match(
596 CallReport callReport,
597 MatchReport matchReport)
noexcept
601 std::move(callReport),
602 std::move(matchReport));
605 inline void report_unfulfilled_expectation(
606 ExpectationReport expectationReport)
609 ->report_unfulfilled_expectation(std::move(expectationReport));
612 inline void report_error(
StringT message)
615 ->report_error(std::move(message));
618 inline void report_unhandled_exception(
619 CallReport callReport,
620 ExpectationReport expectationReport,
621 const std::exception_ptr& exception)
624 ->report_unhandled_exception(
625 std::move(callReport),
626 std::move(expectationReport),
641 template <std::derived_from<IReporter> T,
typename... Args>
642 requires std::constructible_from<T, Args...>
645 detail::get_reporter() = std::make_unique<T>(
646 std::forward<Args>(args)...);
651 template <
typename T>
652 class ReporterInstaller
655 template <
typename... Args>
656 explicit ReporterInstaller(Args&&... args)
659 std::forward<Args>(args)...);
A reporter, which creates text messages and reports them via the provided callbacks.
Definition Reporter.hpp:356
void report_error(const StringT message) override
Expects rather unspecific errors.
Definition Reporter.hpp:387
void report_full_match(const CallReport call, const MatchReport matchReport) noexcept override
Expects the report about a full matching expectation.
Definition Reporter.hpp:372
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:395
void report_unfulfilled_expectation(const ExpectationReport expectationReport) override
Expects the report of an unfulfilled expectation.
Definition Reporter.hpp:378
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:359
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:366
Contains the extracted info from a typed call::Info.
Definition Reports.hpp:203
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:528
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:442
DefaultReporter(std::ostream *out=nullptr) noexcept
Definition Reporter.hpp:436
void report_unfulfilled_expectation(ExpectationReport expectationReport) override
Expects the report of an unfulfilled expectation.
Definition Reporter.hpp:498
void report_full_match(const CallReport call, const MatchReport matchReport) noexcept override
Expects the report about a full matching expectation.
Definition Reporter.hpp:491
void report_error(const StringT message) override
Expects rather unspecific errors.
Definition Reporter.hpp:515
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:467
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 std::tuple< CallReport, std::vector< MatchReport > > & 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:331
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:417
MatchResult evaluate_match_report(const MatchReport &report) noexcept
Determines, whether a match report actually denotes a full, inapplicable or no match.
Definition Reports.hpp:468
void install_reporter(Args &&... args)
Replaces the previous reporter with a newly constructed one.
Definition Reporter.hpp:643
Error< std::tuple< CallReport, std::vector< MatchReport > > > UnmatchedCallT
Definition Reporter.hpp:425
Error< ExpectationReport > UnfulfilledExpectationT
Definition Reporter.hpp:426
constexpr detail::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Printer.hpp:590
Definition BoostTest.hpp:20
@ inapplicable
Definition Fwd.hpp:334
@ none
Definition Fwd.hpp:333
@ full
Definition Fwd.hpp:335
void unreachable()
Invokes undefined behavior.
Definition Utility.hpp:91
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Printer.hpp:41
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:344