Minor refactoring of CompactReporter

This commit is contained in:
Martin Hořeňovský 2020-05-31 22:34:37 +02:00
parent 317145514f
commit 392e44ec21
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -7,27 +7,27 @@
#include <catch2/reporters/catch_reporter_compact.hpp> #include <catch2/reporters/catch_reporter_compact.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_console_colour.hpp> #include <catch2/internal/catch_console_colour.hpp>
#include <catch2/internal/catch_string_manip.hpp> #include <catch2/internal/catch_string_manip.hpp>
#include <catch2/internal/catch_stringref.hpp>
#include <ostream> #include <ostream>
namespace { namespace {
#ifdef CATCH_PLATFORM_MAC
const char* failedString() { return "FAILED"; }
const char* passedString() { return "PASSED"; }
#else
const char* failedString() { return "failed"; }
const char* passedString() { return "passed"; }
#endif
// Colour::LightGrey // Colour::LightGrey
Catch::Colour::Code dimColour() { return Catch::Colour::FileName; } Catch::Colour::Code dimColour() { return Catch::Colour::FileName; }
std::string bothOrAll( std::size_t count ) { Catch::StringRef bothOrAll( std::size_t count ) {
return count == 1 ? std::string() : switch (count) {
count == 2 ? "both " : "all " ; case 1:
return Catch::StringRef{};
case 2:
return "both "_catch_sr;
default:
return "all "_catch_sr;
}
} }
} // anon namespace } // anon namespace
@ -35,6 +35,15 @@ namespace {
namespace Catch { namespace Catch {
namespace { namespace {
#ifdef CATCH_PLATFORM_MAC
static constexpr Catch::StringRef compactFailedString = "FAILED"_sr;
static constexpr Catch::StringRef compactPassedString = "PASSED"_sr;
#else
static constexpr Catch::StringRef compactFailedString = "failed"_sr;
static constexpr Catch::StringRef compactPassedString = "passed"_sr;
#endif
// Colour, message variants: // Colour, message variants:
// - white: No tests ran. // - white: No tests ran.
// - red: Failed [both/all] N test cases, failed [both/all] M assertions. // - red: Failed [both/all] N test cases, failed [both/all] M assertions.
@ -46,9 +55,9 @@ void printTotals(std::ostream& out, const Totals& totals) {
out << "No tests ran."; out << "No tests ran.";
} else if (totals.testCases.failed == totals.testCases.total()) { } else if (totals.testCases.failed == totals.testCases.total()) {
Colour colour(Colour::ResultError); Colour colour(Colour::ResultError);
const std::string qualify_assertions_failed = const StringRef qualify_assertions_failed =
totals.assertions.failed == totals.assertions.total() ? totals.assertions.failed == totals.assertions.total() ?
bothOrAll(totals.assertions.failed) : std::string(); bothOrAll(totals.assertions.failed) : StringRef{};
out << out <<
"Failed " << bothOrAll(totals.testCases.failed) "Failed " << bothOrAll(totals.testCases.failed)
<< pluralise(totals.testCases.failed, "test case") << ", " << pluralise(totals.testCases.failed, "test case") << ", "
@ -92,7 +101,7 @@ public:
switch (result.getResultType()) { switch (result.getResultType()) {
case ResultWas::Ok: case ResultWas::Ok:
printResultType(Colour::ResultSuccess, passedString()); printResultType(Colour::ResultSuccess, compactPassedString);
printOriginalExpression(); printOriginalExpression();
printReconstructedExpression(); printReconstructedExpression();
if (!result.hasExpression()) if (!result.hasExpression())
@ -102,45 +111,45 @@ public:
break; break;
case ResultWas::ExpressionFailed: case ResultWas::ExpressionFailed:
if (result.isOk()) if (result.isOk())
printResultType(Colour::ResultSuccess, failedString() + std::string(" - but was ok")); printResultType(Colour::ResultSuccess, compactFailedString + " - but was ok"_sr);
else else
printResultType(Colour::Error, failedString()); printResultType(Colour::Error, compactFailedString);
printOriginalExpression(); printOriginalExpression();
printReconstructedExpression(); printReconstructedExpression();
printRemainingMessages(); printRemainingMessages();
break; break;
case ResultWas::ThrewException: case ResultWas::ThrewException:
printResultType(Colour::Error, failedString()); printResultType(Colour::Error, compactFailedString);
printIssue("unexpected exception with message:"); printIssue("unexpected exception with message:");
printMessage(); printMessage();
printExpressionWas(); printExpressionWas();
printRemainingMessages(); printRemainingMessages();
break; break;
case ResultWas::FatalErrorCondition: case ResultWas::FatalErrorCondition:
printResultType(Colour::Error, failedString()); printResultType(Colour::Error, compactFailedString);
printIssue("fatal error condition with message:"); printIssue("fatal error condition with message:");
printMessage(); printMessage();
printExpressionWas(); printExpressionWas();
printRemainingMessages(); printRemainingMessages();
break; break;
case ResultWas::DidntThrowException: case ResultWas::DidntThrowException:
printResultType(Colour::Error, failedString()); printResultType(Colour::Error, compactFailedString);
printIssue("expected exception, got none"); printIssue("expected exception, got none");
printExpressionWas(); printExpressionWas();
printRemainingMessages(); printRemainingMessages();
break; break;
case ResultWas::Info: case ResultWas::Info:
printResultType(Colour::None, "info"); printResultType(Colour::None, "info"_sr);
printMessage(); printMessage();
printRemainingMessages(); printRemainingMessages();
break; break;
case ResultWas::Warning: case ResultWas::Warning:
printResultType(Colour::None, "warning"); printResultType(Colour::None, "warning"_sr);
printMessage(); printMessage();
printRemainingMessages(); printRemainingMessages();
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
printResultType(Colour::Error, failedString()); printResultType(Colour::Error, compactFailedString);
printIssue("explicitly"); printIssue("explicitly");
printRemainingMessages(Colour::None); printRemainingMessages(Colour::None);
break; break;
@ -159,7 +168,7 @@ private:
stream << result.getSourceInfo() << ':'; stream << result.getSourceInfo() << ':';
} }
void printResultType(Colour::Code colour, std::string const& passOrFail) const { void printResultType(Colour::Code colour, StringRef passOrFail) const {
if (!passOrFail.empty()) { if (!passOrFail.empty()) {
{ {
Colour colourGuard(colour); Colour colourGuard(colour);
@ -169,7 +178,7 @@ private:
} }
} }
void printIssue(std::string const& issue) const { void printIssue(char const* issue) const {
stream << ' ' << issue; stream << ' ' << issue;
} }