Skip to content

Commit

Permalink
feat: stringify_match_report
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed May 6, 2024
1 parent e2186ad commit 0e9e08e
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
79 changes: 79 additions & 0 deletions include/mimic++/Reporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,85 @@ namespace mimicpp
return MatchResult::full;
}

/**
* \brief Converts the given report to text.
* \param report The report.
* \return The report text.
*/
[[nodiscard]]
inline StringT stringify_match_report(const MatchReport& report)
{
std::vector<StringT> matchedExpectationDescriptions{};
std::vector<StringT> unmatchedExpectationDescriptions{};

for (const auto& [isMatching, description] : report.expectationReports)
{
if (description)
{
if (isMatching)
{
matchedExpectationDescriptions.emplace_back(*description);
}
else
{
unmatchedExpectationDescriptions.emplace_back(*description);
}
}
}

StringStreamT out{};

switch (evaluate_match_report(report))
{
case MatchResult::full:
out << "Matched expectation:\n";
break;

case MatchResult::inapplicable:
format_to(
std::ostreambuf_iterator{out},
"Inapplicable, but otherwise matched expectation:\n"
"reason: {}\n",
report.timesReport.description.value_or("No reason provided."));
break;

case MatchResult::none:
out << "Unmatched expectation:\n";
break;

// GCOVR_EXCL_START
default: // NOLINT(clang-diagnostic-covered-switch-default)
unreachable();
// GCOVR_EXCL_STOP
}

if (!std::ranges::empty(unmatchedExpectationDescriptions))
{
out << "failed:\n";
for (const auto& desc : unmatchedExpectationDescriptions)
{
format_to(
std::ostreambuf_iterator{out},
"\t{},\n",
desc);
}
}

if (!std::ranges::empty(matchedExpectationDescriptions))
{
out << "passed:\n";
for (const auto& desc : matchedExpectationDescriptions)
{
format_to(
std::ostreambuf_iterator{out},
"\t{},\n",
desc);
}
}

return std::move(out).str();
}

/**
* \brief The reporter interface.
* \details This is the central interface to be used, when creating reporters for external domains.
Expand Down
129 changes: 129 additions & 0 deletions test/Reporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/trompeloeil.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

using namespace mimicpp;

Expand Down Expand Up @@ -661,3 +662,131 @@ TEST_CASE(
std::make_exception_ptr(std::runtime_error{"Test"})));
}
}

TEST_CASE(
"stringify_match_report converts the match report to text representation.",
"[report]"
)
{
namespace Matches = Catch::Matchers;

SECTION("When report denotes a full match.")
{
SECTION("Without any requirements.")
{
const MatchReport report{
.finalizeReport = {},
.timesReport = {true, "finalize description"},
.expectationReports = {}
};

REQUIRE_THAT(
stringify_match_report(report),
Matches::Equals(
"Matched expectation:\n"));
}

SECTION("When contains requirements.")
{
const MatchReport report{
.finalizeReport = {},
.timesReport = {true, "finalize description"},
.expectationReports = {
{true, "Requirement1 description"},
{true, "Requirement2 description"}
}
};

REQUIRE_THAT(
stringify_match_report(report),
Matches::Equals(
"Matched expectation:\n"
"passed:\n"
"\tRequirement1 description,\n"
"\tRequirement2 description,\n"));
}
}

SECTION("When report denotes an inapplicable match.")
{
SECTION("Without any requirements.")
{
const MatchReport report{
.finalizeReport = {},
.timesReport = {false, "finalize description"},
.expectationReports = {}
};

REQUIRE_THAT(
stringify_match_report(report),
Matches::Equals(
"Inapplicable, but otherwise matched expectation:\n"
"reason: finalize description\n"));
}

SECTION("When contains requirements.")
{
const MatchReport report{
.finalizeReport = {},
.timesReport = {false, "finalize description"},
.expectationReports = {
{true, "Requirement1 description"},
{true, "Requirement2 description"}
}
};

REQUIRE_THAT(
stringify_match_report(report),
Matches::Equals(
"Inapplicable, but otherwise matched expectation:\n"
"reason: finalize description\n"
"passed:\n"
"\tRequirement1 description,\n"
"\tRequirement2 description,\n"));
}
}

SECTION("When report denotes an unmatched report.")
{
SECTION("When contains only failed requirements.")
{
const MatchReport report{
.finalizeReport = {},
.timesReport = {true, "finalize description"},
.expectationReports = {
{false, "Requirement1 description"},
{false, "Requirement2 description"}
}
};

REQUIRE_THAT(
stringify_match_report(report),
Matches::Equals(
"Unmatched expectation:\n"
"failed:\n"
"\tRequirement1 description,\n"
"\tRequirement2 description,\n"));
}

SECTION("When contains only mixed requirements.")
{
const MatchReport report{
.finalizeReport = {},
.timesReport = {true, "finalize description"},
.expectationReports = {
{true, "Requirement1 description"},
{false, "Requirement2 description"}
}
};

REQUIRE_THAT(
stringify_match_report(report),
Matches::Equals(
"Unmatched expectation:\n"
"failed:\n"
"\tRequirement2 description,\n"
"passed:\n"
"\tRequirement1 description,\n"));
}
}
}

0 comments on commit 0e9e08e

Please sign in to comment.