Each reporter keeps its own colour implementation

This opens path to per-reporter colour output customization,
and fixes multiple issues with the old colour implementation.

Under the old implementation, using Win32-backed colouring
would always change the colour used by the console, even if the
actual output was written elsewhere, such as a file passed by
the `--out` flag. This will no longer happen, as the reporter's
colour impl will check that the reporter's stream is pointed
to console before trying to change the colours.

POSIX/ANSI colour implementation suffered a similar-ish issue,
in that it only wrote the colour escape codes into the default
output stream, even if the reporter asking for colouring was
actually writing to a completely different output stream.
This commit is contained in:
Martin Hořeňovský 2022-03-06 19:20:53 +01:00
parent 06f74a0f8e
commit 913f79a661
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
11 changed files with 271 additions and 229 deletions

View File

@ -151,14 +151,16 @@ namespace Catch {
getCurrentMutableContext().setConfig(m_config.get()); getCurrentMutableContext().setConfig(m_config.get());
m_startupExceptions = true; m_startupExceptions = true;
Colour colourGuard( Colour::Red ); auto errStream = makeStream( "%stderr" );
Catch::cerr() << "Errors occurred during startup!" << '\n'; auto colourImpl = makeColourImpl( &config(), errStream.get() );
auto guard = colourImpl->startColour( Colour::Red );
errStream->stream() << "Errors occurred during startup!" << '\n';
// iterate over all exceptions and notify user // iterate over all exceptions and notify user
for ( const auto& ex_ptr : exceptions ) { for ( const auto& ex_ptr : exceptions ) {
try { try {
std::rethrow_exception(ex_ptr); std::rethrow_exception(ex_ptr);
} catch ( std::exception const& ex ) { } catch ( std::exception const& ex ) {
Catch::cerr() << TextFlow::Column( ex.what() ).indent(2) << '\n'; errStream->stream() << TextFlow::Column( ex.what() ).indent(2) << '\n';
} }
} }
} }
@ -194,12 +196,15 @@ namespace Catch {
if( !result ) { if( !result ) {
config(); config();
getCurrentMutableContext().setConfig(m_config.get()); getCurrentMutableContext().setConfig(m_config.get());
Catch::cerr() auto errStream = makeStream( "%stderr" );
<< Colour( Colour::Red ) auto colour = makeColourImpl( &config(), errStream.get() );
errStream->stream()
<< colour->startColour( Colour::Red )
<< "\nError(s) in input:\n" << "\nError(s) in input:\n"
<< TextFlow::Column( result.errorMessage() ).indent( 2 ) << TextFlow::Column( result.errorMessage() ).indent( 2 )
<< "\n\n"; << "\n\n";
Catch::cerr() << "Run with -? for usage\n\n" << std::flush; errStream->stream() << "Run with -? for usage\n\n" << std::flush;
return MaxExitCode; return MaxExitCode;
} }

View File

@ -24,23 +24,48 @@
#include <ostream> #include <ostream>
namespace Catch { namespace Catch {
namespace {
struct IColourImpl { ColourImpl::~ColourImpl() = default;
virtual ~IColourImpl() = default;
virtual void use( Colour::Code _colourCode ) = 0;
};
struct NoColourImpl : IColourImpl { ColourImpl::ColourGuard ColourImpl::startColour( Colour::Code colourCode ) {
void use( Colour::Code ) override {} return ColourGuard(colourCode, this );
static IColourImpl* instance() {
static NoColourImpl s_instance;
return &s_instance;
} }
namespace {
//! A do-nothing implementation of colour, used as fallback for unknown
//! platforms, and when the user asks to deactivate all colours.
class NoColourImpl : public ColourImpl {
public:
NoColourImpl( IStream const* stream ): ColourImpl( stream ) {}
static bool useColourOnPlatform() { return true; }
private:
void use( Colour::Code ) const override {}
}; };
} // anon namespace } // namespace
ColourImpl::ColourGuard::ColourGuard( Colour::Code code,
ColourImpl const* colour ):
m_colourImpl( colour ) {
m_colourImpl->use( code );
}
ColourImpl::ColourGuard::ColourGuard( ColourGuard&& rhs ):
m_colourImpl( rhs.m_colourImpl ) {
rhs.m_moved = true;
}
ColourImpl::ColourGuard&
ColourImpl::ColourGuard::operator=( ColourGuard&& rhs ) {
m_colourImpl = rhs.m_colourImpl;
rhs.m_moved = true;
return *this;
}
ColourImpl::ColourGuard::~ColourGuard() {
if ( !m_moved ) {
m_colourImpl->use( Colour::None );
}
}
} // namespace Catch } // namespace Catch
#if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) #if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI )
@ -57,9 +82,10 @@ namespace Catch {
namespace Catch { namespace Catch {
namespace { namespace {
class Win32ColourImpl : public IColourImpl { class Win32ColourImpl : public ColourImpl {
public: public:
Win32ColourImpl() { Win32ColourImpl(IStream const* stream):
ColourImpl(stream) {
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ),
&csbiInfo ); &csbiInfo );
@ -67,7 +93,16 @@ namespace {
originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY ); originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
} }
void use( Colour::Code _colourCode ) override { static bool useColourOnPlatform() { return true; }
private:
void use( Colour::Code _colourCode ) const override {
// Early exit if we are not writing to the console, because
// Win32 API can only change colour of the console.
if ( !m_stream->isStdout() ) {
return;
}
switch( _colourCode ) { switch( _colourCode ) {
case Colour::None: return setTextAttribute( originalForegroundAttributes ); case Colour::None: return setTextAttribute( originalForegroundAttributes );
case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE );
@ -91,8 +126,7 @@ namespace {
} }
} }
private: void setTextAttribute( WORD _textAttribute ) const {
void setTextAttribute( WORD _textAttribute ) {
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ),
_textAttribute | _textAttribute |
originalBackgroundAttributes ); originalBackgroundAttributes );
@ -101,19 +135,6 @@ namespace {
WORD originalBackgroundAttributes; WORD originalBackgroundAttributes;
}; };
IColourImpl* platformColourInstance() {
static Win32ColourImpl s_instance;
auto const* config = getCurrentContext().getConfig();
UseColour colourMode = config?
config->useColour() : UseColour::Auto;
if( colourMode == UseColour::Auto )
colourMode = UseColour::Yes;
return colourMode == UseColour::Yes
? &s_instance
: NoColourImpl::instance();
}
} // end anon namespace } // end anon namespace
} // end namespace Catch } // end namespace Catch
@ -128,9 +149,33 @@ namespace {
// Thanks to Adam Strzelecki for original contribution // Thanks to Adam Strzelecki for original contribution
// (http://github.com/nanoant) // (http://github.com/nanoant)
// https://github.com/philsquared/Catch/pull/131 // https://github.com/philsquared/Catch/pull/131
class PosixColourImpl : public IColourImpl { class PosixColourImpl : public ColourImpl {
public: public:
void use( Colour::Code _colourCode ) override { PosixColourImpl( IStream const* stream ): ColourImpl( stream ) {}
static bool useColourOnPlatform() {
ErrnoGuard _; // for isatty
return
# if defined( CATCH_PLATFORM_MAC ) || defined( CATCH_PLATFORM_IPHONE )
!isDebuggerActive() &&
# endif
# if !( defined( __DJGPP__ ) && defined( __STRICT_ANSI__ ) )
isatty( STDOUT_FILENO )
# else
false
# endif
;
}
private:
void use( Colour::Code _colourCode ) const override {
auto setColour = [&out =
m_stream->stream()]( char const* escapeCode ) {
// The escape sequence must be flushed to console, otherwise
// if stdin and stderr are intermixed, we'd get accidentally
// coloured output.
out << '\033' << escapeCode << std::flush;
};
switch( _colourCode ) { switch( _colourCode ) {
case Colour::None: case Colour::None:
case Colour::White: return setColour( "[0m" ); case Colour::White: return setColour( "[0m" );
@ -151,90 +196,53 @@ namespace {
default: CATCH_INTERNAL_ERROR( "Unknown colour requested" ); default: CATCH_INTERNAL_ERROR( "Unknown colour requested" );
} }
} }
static IColourImpl* instance() {
static PosixColourImpl s_instance;
return &s_instance;
}
private:
void setColour( const char* _escapeCode ) {
// The escape sequence must be flushed to console, otherwise if
// stdin and stderr are intermixed, we'd get accidentally coloured output.
getCurrentContext().getConfig()->defaultStream()->stream()
<< '\033' << _escapeCode << std::flush;
}
}; };
bool useColourOnPlatform() {
return
#if defined(CATCH_PLATFORM_MAC) || defined(CATCH_PLATFORM_IPHONE)
!isDebuggerActive() &&
#endif
#if !(defined(__DJGPP__) && defined(__STRICT_ANSI__))
isatty(STDOUT_FILENO)
#else
false
#endif
;
}
IColourImpl* platformColourInstance() {
ErrnoGuard guard;
auto const* config = getCurrentContext().getConfig();
UseColour colourMode = config
? config->useColour()
: UseColour::Auto;
if( colourMode == UseColour::Auto )
colourMode = useColourOnPlatform()
? UseColour::Yes
: UseColour::No;
return colourMode == UseColour::Yes
? PosixColourImpl::instance()
: NoColourImpl::instance();
}
} // end anon namespace } // end anon namespace
} // end namespace Catch } // end namespace Catch
#else // not Windows or ANSI ///////////////////////////////////////////////
namespace Catch {
static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); }
} // end namespace Catch
#endif // Windows/ ANSI/ None #endif // Windows/ ANSI/ None
namespace Catch { namespace Catch {
Colour::Colour( Code _colourCode ) { use( _colourCode ); } Detail::unique_ptr<ColourImpl> makeColourImpl(IConfig const* config, IStream const* stream) {
Colour::Colour( Colour&& other ) noexcept { UseColour colourMode = config ? config->useColour() : UseColour::Auto;
m_moved = other.m_moved;
other.m_moved = true; bool createPlatformInstance = false;
} if ( colourMode == UseColour::No ) {
Colour& Colour::operator=( Colour&& other ) noexcept { createPlatformInstance = false;
m_moved = other.m_moved;
other.m_moved = true;
return *this;
} }
Colour::~Colour(){ if( !m_moved ) use( None ); } if ( colourMode == UseColour::Yes ) {
createPlatformInstance = true;
void Colour::use( Code _colourCode ) {
static IColourImpl* impl = platformColourInstance();
// Strictly speaking, this cannot possibly happen.
// However, under some conditions it does happen (see #1626),
// and this change is small enough that we can let practicality
// triumph over purity in this case.
if (impl != nullptr) {
impl->use( _colourCode );
}
} }
std::ostream& operator << ( std::ostream& os, Colour const& ) { if ( colourMode == UseColour::Auto ) {
return os; createPlatformInstance =
#if defined( CATCH_CONFIG_COLOUR_ANSI )
PosixColourImpl::useColourOnPlatform()
#elif defined( CATCH_CONFIG_COLOUR_WINDOWS )
Win32ColourImpl::useColourOnPlatform()
#else
NoColourImpl::useColourOnPlatform()
#endif
;
} }
if ( createPlatformInstance ) {
return
#if defined( CATCH_CONFIG_COLOUR_ANSI )
Detail::make_unique<PosixColourImpl>(stream);
#elif defined( CATCH_CONFIG_COLOUR_WINDOWS )
Detail::make_unique<Win32ColourImpl>(stream);
#else
Detail::make_unique<NoColourImpl>(stream);
#endif
}
return Detail::make_unique<NoColourImpl>(stream);
}
} // end namespace Catch } // end namespace Catch
#if defined(__clang__) #if defined(__clang__)

View File

@ -8,10 +8,15 @@
#ifndef CATCH_CONSOLE_COLOUR_HPP_INCLUDED #ifndef CATCH_CONSOLE_COLOUR_HPP_INCLUDED
#define CATCH_CONSOLE_COLOUR_HPP_INCLUDED #define CATCH_CONSOLE_COLOUR_HPP_INCLUDED
#include <catch2/internal/catch_unique_ptr.hpp>
#include <iosfwd> #include <iosfwd>
namespace Catch { namespace Catch {
struct IConfig;
class IStream;
struct Colour { struct Colour {
enum Code { enum Code {
None = 0, None = 0,
@ -48,22 +53,41 @@ namespace Catch {
SecondaryText = LightGrey, SecondaryText = LightGrey,
Headers = White Headers = White
}; };
// Use constructed object for RAII guard
Colour( Code _colourCode );
Colour( Colour&& other ) noexcept;
Colour& operator=( Colour&& other ) noexcept;
~Colour();
// Use static method for one-shot changes
static void use( Code _colourCode );
private:
bool m_moved = false;
friend std::ostream& operator << (std::ostream& os, Colour const&);
}; };
class ColourImpl {
protected:
//! The associated stream of this ColourImpl instance
IStream const* m_stream;
public:
ColourImpl( IStream const* stream ): m_stream( stream ) {}
class ColourGuard {
ColourImpl const* m_colourImpl;
bool m_moved = false;
friend std::ostream& operator<<(std::ostream& lhs,
ColourGuard const&) {
return lhs;
}
public:
ColourGuard( Colour::Code code,
ColourImpl const* colour );
ColourGuard( ColourGuard const& rhs ) = delete;
ColourGuard& operator=( ColourGuard const& rhs ) = delete;
ColourGuard( ColourGuard&& rhs );
ColourGuard& operator=( ColourGuard&& rhs );
~ColourGuard();
};
virtual ~ColourImpl(); // = default
ColourGuard startColour( Colour::Code colourCode );
private:
virtual void use( Colour::Code colourCode ) const = 0;
};
//! Provides ColourImpl based on global config and target compilation platform
Detail::unique_ptr<ColourImpl> makeColourImpl( IConfig const* config, IStream const* stream );
} // end namespace Catch } // end namespace Catch

View File

@ -174,7 +174,7 @@ namespace Catch {
out << pluralise(tags.size(), "tag"_sr) << "\n\n" << std::flush; out << pluralise(tags.size(), "tag"_sr) << "\n\n" << std::flush;
} }
void defaultListTests(std::ostream& out, std::vector<TestCaseHandle> const& tests, bool isFiltered, Verbosity verbosity) { void defaultListTests(std::ostream& out, ColourImpl* streamColour, std::vector<TestCaseHandle> const& tests, bool isFiltered, Verbosity verbosity) {
// We special case this to provide the equivalent of old // We special case this to provide the equivalent of old
// `--list-test-names-only`, which could then be used by the // `--list-test-names-only`, which could then be used by the
// `--input-file` option. // `--input-file` option.
@ -194,7 +194,7 @@ namespace Catch {
Colour::Code colour = testCaseInfo.isHidden() Colour::Code colour = testCaseInfo.isHidden()
? Colour::SecondaryText ? Colour::SecondaryText
: Colour::None; : Colour::None;
Colour colourGuard(colour); auto colourGuard = streamColour->startColour( colour );
out << TextFlow::Column(testCaseInfo.name).initialIndent(2).indent(4) << '\n'; out << TextFlow::Column(testCaseInfo.name).initialIndent(2).indent(4) << '\n';
if (verbosity >= Verbosity::High) { if (verbosity >= Verbosity::High) {

View File

@ -18,6 +18,7 @@ namespace Catch {
void ReporterBase::listTests(std::vector<TestCaseHandle> const& tests) { void ReporterBase::listTests(std::vector<TestCaseHandle> const& tests) {
defaultListTests(m_stream, defaultListTests(m_stream,
m_colour.get(),
tests, tests,
m_config->hasTestFilters(), m_config->hasTestFilters(),
m_config->verbosity()); m_config->verbosity());

View File

@ -10,6 +10,7 @@
#include <catch2/interfaces/catch_interfaces_reporter.hpp> #include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_stream.hpp> #include <catch2/internal/catch_stream.hpp>
#include <catch2/internal/catch_console_colour.hpp>
namespace Catch { namespace Catch {
/** /**
@ -29,14 +30,14 @@ namespace Catch {
//! Cached output stream from `m_wrapped_stream` to reduce //! Cached output stream from `m_wrapped_stream` to reduce
//! number of indirect calls needed to write output. //! number of indirect calls needed to write output.
std::ostream& m_stream; std::ostream& m_stream;
Detail::unique_ptr<ColourImpl> m_colour;
public: public:
ReporterBase( ReporterConfig const& config ): ReporterBase( ReporterConfig const& config ):
IEventListener( config.fullConfig() ), IEventListener( config.fullConfig() ),
m_wrapped_stream( config.stream() ), m_wrapped_stream( config.stream() ),
m_stream( m_wrapped_stream->stream() ) {} m_stream( m_wrapped_stream->stream() ),
m_colour( makeColourImpl( config.fullConfig(), m_wrapped_stream ) ) {}
/** /**

View File

@ -18,9 +18,6 @@
namespace { namespace {
// Colour::LightGrey
constexpr Catch::Colour::Code dimColour() { return Catch::Colour::FileName; }
constexpr Catch::StringRef bothOrAll( std::uint64_t count ) { constexpr Catch::StringRef bothOrAll( std::uint64_t count ) {
switch (count) { switch (count) {
case 1: case 1:
@ -38,6 +35,9 @@ namespace {
namespace Catch { namespace Catch {
namespace { namespace {
// Colour::LightGrey
static constexpr Colour::Code compactDimColour = Colour::FileName;
#ifdef CATCH_PLATFORM_MAC #ifdef CATCH_PLATFORM_MAC
static constexpr Catch::StringRef compactFailedString = "FAILED"_sr; static constexpr Catch::StringRef compactFailedString = "FAILED"_sr;
static constexpr Catch::StringRef compactPassedString = "PASSED"_sr; static constexpr Catch::StringRef compactPassedString = "PASSED"_sr;
@ -52,11 +52,11 @@ namespace {
// - white: Passed [both/all] N test cases (no assertions). // - white: Passed [both/all] N test cases (no assertions).
// - red: Failed N tests cases, failed M assertions. // - red: Failed N tests cases, failed M assertions.
// - green: Passed [both/all] N tests cases with M assertions. // - green: Passed [both/all] N tests cases with M assertions.
void printTotals(std::ostream& out, const Totals& totals) { void printTotals(std::ostream& out, const Totals& totals, ColourImpl* colourImpl) {
if (totals.testCases.total() == 0) { if (totals.testCases.total() == 0) {
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); auto guard = colourImpl->startColour( Colour::ResultError );
const StringRef qualify_assertions_failed = const StringRef qualify_assertions_failed =
totals.assertions.failed == totals.assertions.total() ? totals.assertions.failed == totals.assertions.total() ?
bothOrAll(totals.assertions.failed) : StringRef{}; bothOrAll(totals.assertions.failed) : StringRef{};
@ -71,13 +71,11 @@ void printTotals(std::ostream& out, const Totals& totals) {
<< pluralise(totals.testCases.total(), "test case"_sr) << pluralise(totals.testCases.total(), "test case"_sr)
<< " (no assertions)."; << " (no assertions).";
} else if (totals.assertions.failed) { } else if (totals.assertions.failed) {
Colour colour(Colour::ResultError); out << colourImpl->startColour( Colour::ResultError ) <<
out <<
"Failed " << pluralise(totals.testCases.failed, "test case"_sr) << ", " "Failed " << pluralise(totals.testCases.failed, "test case"_sr) << ", "
"failed " << pluralise(totals.assertions.failed, "assertion"_sr) << '.'; "failed " << pluralise(totals.assertions.failed, "assertion"_sr) << '.';
} else { } else {
Colour colour(Colour::ResultSuccess); out << colourImpl->startColour( Colour::ResultSuccess ) <<
out <<
"Passed " << bothOrAll(totals.testCases.passed) "Passed " << bothOrAll(totals.testCases.passed)
<< pluralise(totals.testCases.passed, "test case"_sr) << << pluralise(totals.testCases.passed, "test case"_sr) <<
" with " << pluralise(totals.assertions.passed, "assertion"_sr) << '.'; " with " << pluralise(totals.assertions.passed, "assertion"_sr) << '.';
@ -89,12 +87,14 @@ class AssertionPrinter {
public: public:
AssertionPrinter& operator= (AssertionPrinter const&) = delete; AssertionPrinter& operator= (AssertionPrinter const&) = delete;
AssertionPrinter(AssertionPrinter const&) = delete; AssertionPrinter(AssertionPrinter const&) = delete;
AssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) AssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages, ColourImpl* colourImpl_)
: stream(_stream) : stream(_stream)
, result(_stats.assertionResult) , result(_stats.assertionResult)
, messages(_stats.infoMessages) , messages(_stats.infoMessages)
, itMessage(_stats.infoMessages.begin()) , itMessage(_stats.infoMessages.begin())
, printInfoMessages(_printInfoMessages) {} , printInfoMessages(_printInfoMessages)
, colourImpl(colourImpl_)
{}
void print() { void print() {
printSourceInfo(); printSourceInfo();
@ -166,16 +166,13 @@ public:
private: private:
void printSourceInfo() const { void printSourceInfo() const {
Colour colourGuard(Colour::FileName); stream << colourImpl->startColour( Colour::FileName )
stream << result.getSourceInfo() << ':'; << result.getSourceInfo() << ':';
} }
void printResultType(Colour::Code colour, StringRef passOrFail) const { void printResultType(Colour::Code colour, StringRef passOrFail) const {
if (!passOrFail.empty()) { if (!passOrFail.empty()) {
{ stream << colourImpl->startColour(colour) << ' ' << passOrFail;
Colour colourGuard(colour);
stream << ' ' << passOrFail;
}
stream << ':'; stream << ':';
} }
} }
@ -188,8 +185,7 @@ private:
if (result.hasExpression()) { if (result.hasExpression()) {
stream << ';'; stream << ';';
{ {
Colour colour(dimColour()); stream << colourImpl->startColour(compactDimColour) << " expression was:";
stream << " expression was:";
} }
printOriginalExpression(); printOriginalExpression();
} }
@ -203,10 +199,7 @@ private:
void printReconstructedExpression() const { void printReconstructedExpression() const {
if (result.hasExpandedExpression()) { if (result.hasExpandedExpression()) {
{ stream << colourImpl->startColour(compactDimColour) << " for: ";
Colour colour(dimColour());
stream << " for: ";
}
stream << result.getExpandedExpression(); stream << result.getExpandedExpression();
} }
} }
@ -218,25 +211,22 @@ private:
} }
} }
void printRemainingMessages(Colour::Code colour = dimColour()) { void printRemainingMessages(Colour::Code colour = compactDimColour) {
if (itMessage == messages.end()) if (itMessage == messages.end())
return; return;
const auto itEnd = messages.cend(); const auto itEnd = messages.cend();
const auto N = static_cast<std::size_t>(std::distance(itMessage, itEnd)); const auto N = static_cast<std::size_t>(std::distance(itMessage, itEnd));
{ stream << colourImpl->startColour( colour ) << " with "
Colour colourGuard(colour); << pluralise( N, "message"_sr ) << ':';
stream << " with " << pluralise(N, "message"_sr) << ':';
}
while (itMessage != itEnd) { while (itMessage != itEnd) {
// If this assertion is a warning ignore any INFO messages // If this assertion is a warning ignore any INFO messages
if (printInfoMessages || itMessage->type != ResultWas::Info) { if (printInfoMessages || itMessage->type != ResultWas::Info) {
printMessage(); printMessage();
if (itMessage != itEnd) { if (itMessage != itEnd) {
Colour colourGuard(dimColour()); stream << colourImpl->startColour(compactDimColour) << " and";
stream << " and";
} }
continue; continue;
} }
@ -250,6 +240,7 @@ private:
std::vector<MessageInfo> messages; std::vector<MessageInfo> messages;
std::vector<MessageInfo>::const_iterator itMessage; std::vector<MessageInfo>::const_iterator itMessage;
bool printInfoMessages; bool printInfoMessages;
ColourImpl* colourImpl;
}; };
} // anon namespace } // anon namespace
@ -274,7 +265,7 @@ private:
printInfoMessages = false; printInfoMessages = false;
} }
AssertionPrinter printer( m_stream, _assertionStats, printInfoMessages ); AssertionPrinter printer( m_stream, _assertionStats, printInfoMessages, m_colour.get() );
printer.print(); printer.print();
m_stream << '\n' << std::flush; m_stream << '\n' << std::flush;
@ -288,7 +279,7 @@ private:
} }
void CompactReporter::testRunEnded( TestRunStats const& _testRunStats ) { void CompactReporter::testRunEnded( TestRunStats const& _testRunStats ) {
printTotals( m_stream, _testRunStats.totals ); printTotals( m_stream, _testRunStats.totals, m_colour.get() );
m_stream << "\n\n" << std::flush; m_stream << "\n\n" << std::flush;
StreamingReporterBase::testRunEnded( _testRunStats ); StreamingReporterBase::testRunEnded( _testRunStats );
} }

View File

@ -45,13 +45,14 @@ class ConsoleAssertionPrinter {
public: public:
ConsoleAssertionPrinter& operator= (ConsoleAssertionPrinter const&) = delete; ConsoleAssertionPrinter& operator= (ConsoleAssertionPrinter const&) = delete;
ConsoleAssertionPrinter(ConsoleAssertionPrinter const&) = delete; ConsoleAssertionPrinter(ConsoleAssertionPrinter const&) = delete;
ConsoleAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) ConsoleAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, ColourImpl* colourImpl_, bool _printInfoMessages)
: stream(_stream), : stream(_stream),
stats(_stats), stats(_stats),
result(_stats.assertionResult), result(_stats.assertionResult),
colour(Colour::None), colour(Colour::None),
message(result.getMessage()), message(result.getMessage()),
messages(_stats.infoMessages), messages(_stats.infoMessages),
colourImpl(colourImpl_),
printInfoMessages(_printInfoMessages) { printInfoMessages(_printInfoMessages) {
switch (result.getResultType()) { switch (result.getResultType()) {
case ResultWas::Ok: case ResultWas::Ok:
@ -134,23 +135,22 @@ public:
private: private:
void printResultType() const { void printResultType() const {
if (!passOrFail.empty()) { if (!passOrFail.empty()) {
Colour colourGuard(colour); stream << colourImpl->startColour(colour) << passOrFail << ":\n";
stream << passOrFail << ":\n";
} }
} }
void printOriginalExpression() const { void printOriginalExpression() const {
if (result.hasExpression()) { if (result.hasExpression()) {
Colour colourGuard(Colour::OriginalExpression); stream << colourImpl->startColour( Colour::OriginalExpression )
stream << " "; << " " << result.getExpressionInMacro() << '\n';
stream << result.getExpressionInMacro();
stream << '\n';
} }
} }
void printReconstructedExpression() const { void printReconstructedExpression() const {
if (result.hasExpandedExpression()) { if (result.hasExpandedExpression()) {
stream << "with expansion:\n"; stream << "with expansion:\n";
Colour colourGuard(Colour::ReconstructedExpression); stream << colourImpl->startColour( Colour::ReconstructedExpression )
stream << TextFlow::Column(result.getExpandedExpression()).indent(2) << '\n'; << TextFlow::Column( result.getExpandedExpression() )
.indent( 2 )
<< '\n';
} }
} }
void printMessage() const { void printMessage() const {
@ -163,8 +163,8 @@ private:
} }
} }
void printSourceInfo() const { void printSourceInfo() const {
Colour colourGuard(Colour::FileName); stream << colourImpl->startColour( Colour::FileName )
stream << result.getSourceInfo() << ": "; << result.getSourceInfo() << ": ";
} }
std::ostream& stream; std::ostream& stream;
@ -175,6 +175,7 @@ private:
std::string messageLabel; std::string messageLabel;
std::string message; std::string message;
std::vector<MessageInfo> messages; std::vector<MessageInfo> messages;
ColourImpl* colourImpl;
bool printInfoMessages; bool printInfoMessages;
}; };
@ -403,7 +404,7 @@ void ConsoleReporter::assertionEnded(AssertionStats const& _assertionStats) {
lazyPrint(); lazyPrint();
ConsoleAssertionPrinter printer(m_stream, _assertionStats, includeResults); ConsoleAssertionPrinter printer(m_stream, _assertionStats, m_colour.get(), includeResults);
printer.print(); printer.print();
m_stream << '\n' << std::flush; m_stream << '\n' << std::flush;
} }
@ -417,7 +418,7 @@ void ConsoleReporter::sectionEnded(SectionStats const& _sectionStats) {
m_tablePrinter->close(); m_tablePrinter->close();
if (_sectionStats.missingAssertions) { if (_sectionStats.missingAssertions) {
lazyPrint(); lazyPrint();
Colour colour(Colour::ResultError); auto guard = m_colour->startColour( Colour::ResultError );
if (m_sectionStack.size() > 1) if (m_sectionStack.size() > 1)
m_stream << "\nNo assertions in section"; m_stream << "\nNo assertions in section";
else else
@ -475,7 +476,7 @@ void ConsoleReporter::benchmarkEnded(BenchmarkStats<> const& stats) {
} }
void ConsoleReporter::benchmarkFailed( StringRef error ) { void ConsoleReporter::benchmarkFailed( StringRef error ) {
Colour colour(Colour::Red); auto guard = m_colour->startColour( Colour::Red );
(*m_tablePrinter) (*m_tablePrinter)
<< "Benchmark failed (" << error << ')' << "Benchmark failed (" << error << ')'
<< ColumnBreak() << RowBreak(); << ColumnBreak() << RowBreak();
@ -514,13 +515,13 @@ void ConsoleReporter::lazyPrintWithoutClosingBenchmarkTable() {
} }
} }
void ConsoleReporter::lazyPrintRunInfo() { void ConsoleReporter::lazyPrintRunInfo() {
m_stream << '\n' << lineOfChars('~') << '\n'; m_stream << '\n'
Colour colour(Colour::SecondaryText); << lineOfChars( '~' ) << '\n'
m_stream << currentTestRunInfo.name << m_colour->startColour( Colour::SecondaryText )
<< " is a Catch2 v" << libraryVersion() << " host application.\n" << currentTestRunInfo.name << " is a Catch2 v" << libraryVersion()
<< "Run with -? for options\n\n"; << " host application.\n"
<< "Run with -? for options\n\n"
m_stream << "Randomness seeded to: " << m_config->rngSeed() << "\n\n"; << "Randomness seeded to: " << m_config->rngSeed() << "\n\n";
m_testRunInfoPrinted = true; m_testRunInfoPrinted = true;
} }
@ -529,7 +530,7 @@ void ConsoleReporter::printTestCaseAndSectionHeader() {
printOpenHeader(currentTestCaseInfo->name); printOpenHeader(currentTestCaseInfo->name);
if (m_sectionStack.size() > 1) { if (m_sectionStack.size() > 1) {
Colour colourGuard(Colour::Headers); auto guard = m_colour->startColour( Colour::Headers );
auto auto
it = m_sectionStack.begin() + 1, // Skip first section (test case) it = m_sectionStack.begin() + 1, // Skip first section (test case)
@ -541,10 +542,10 @@ void ConsoleReporter::printTestCaseAndSectionHeader() {
SourceLineInfo lineInfo = m_sectionStack.back().lineInfo; SourceLineInfo lineInfo = m_sectionStack.back().lineInfo;
m_stream << lineOfChars('-') << '\n'; m_stream << lineOfChars( '-' ) << '\n'
Colour colourGuard(Colour::FileName); << m_colour->startColour( Colour::FileName ) << lineInfo << '\n'
m_stream << lineInfo << '\n'; << lineOfChars( '.' ) << "\n\n"
m_stream << lineOfChars('.') << "\n\n" << std::flush; << std::flush;
} }
void ConsoleReporter::printClosedHeader(std::string const& _name) { void ConsoleReporter::printClosedHeader(std::string const& _name) {
@ -554,7 +555,7 @@ void ConsoleReporter::printClosedHeader(std::string const& _name) {
void ConsoleReporter::printOpenHeader(std::string const& _name) { void ConsoleReporter::printOpenHeader(std::string const& _name) {
m_stream << lineOfChars('-') << '\n'; m_stream << lineOfChars('-') << '\n';
{ {
Colour colourGuard(Colour::Headers); auto guard = m_colour->startColour( Colour::Headers );
printHeaderString(_name); printHeaderString(_name);
} }
} }
@ -619,9 +620,11 @@ struct SummaryColumn {
void ConsoleReporter::printTotals( Totals const& totals ) { void ConsoleReporter::printTotals( Totals const& totals ) {
if (totals.testCases.total() == 0) { if (totals.testCases.total() == 0) {
m_stream << Colour(Colour::Warning) << "No tests ran\n"; m_stream << m_colour->startColour( Colour::Warning )
<< "No tests ran\n";
} else if (totals.assertions.total() > 0 && totals.testCases.allPassed()) { } else if (totals.assertions.total() > 0 && totals.testCases.allPassed()) {
m_stream << Colour(Colour::ResultSuccess) << "All tests passed"; m_stream << m_colour->startColour( Colour::ResultSuccess )
<< "All tests passed";
m_stream << " (" m_stream << " ("
<< pluralise(totals.assertions.passed, "assertion"_sr) << " in " << pluralise(totals.assertions.passed, "assertion"_sr) << " in "
<< pluralise(totals.testCases.passed, "test case"_sr) << ')' << pluralise(totals.testCases.passed, "test case"_sr) << ')'
@ -648,17 +651,19 @@ void ConsoleReporter::printTotals( Totals const& totals ) {
} }
void ConsoleReporter::printSummaryRow(StringRef label, std::vector<SummaryColumn> const& cols, std::size_t row) { void ConsoleReporter::printSummaryRow(StringRef label, std::vector<SummaryColumn> const& cols, std::size_t row) {
for (auto col : cols) { for (auto col : cols) {
std::string value = col.rows[row]; std::string const& value = col.rows[row];
if (col.label.empty()) { if (col.label.empty()) {
m_stream << label << ": "; m_stream << label << ": ";
if (value != "0") if ( value != "0" ) {
m_stream << value; m_stream << value;
else } else {
m_stream << Colour(Colour::Warning) << "- none -"; m_stream << m_colour->startColour( Colour::Warning )
<< "- none -";
}
} else if (value != "0") { } else if (value != "0") {
m_stream << Colour(Colour::LightGrey) << " | "; m_stream << m_colour->startColour( Colour::LightGrey ) << " | "
m_stream << Colour(col.colour) << m_colour->startColour( col.colour ) << value << ' '
<< value << ' ' << col.label; << col.label;
} }
} }
m_stream << '\n'; m_stream << '\n';
@ -674,14 +679,20 @@ void ConsoleReporter::printTotalsDivider(Totals const& totals) {
while (failedRatio + failedButOkRatio + passedRatio > CATCH_CONFIG_CONSOLE_WIDTH - 1) while (failedRatio + failedButOkRatio + passedRatio > CATCH_CONFIG_CONSOLE_WIDTH - 1)
findMax(failedRatio, failedButOkRatio, passedRatio)--; findMax(failedRatio, failedButOkRatio, passedRatio)--;
m_stream << Colour(Colour::Error) << std::string(failedRatio, '='); m_stream << m_colour->startColour( Colour::Error )
m_stream << Colour(Colour::ResultExpectedFailure) << std::string(failedButOkRatio, '='); << std::string( failedRatio, '=' )
if (totals.testCases.allPassed()) << m_colour->startColour( Colour::ResultExpectedFailure )
m_stream << Colour(Colour::ResultSuccess) << std::string(passedRatio, '='); << std::string( failedButOkRatio, '=' );
else if ( totals.testCases.allPassed() ) {
m_stream << Colour(Colour::Success) << std::string(passedRatio, '='); m_stream << m_colour->startColour( Colour::ResultSuccess )
<< std::string( passedRatio, '=' );
} else { } else {
m_stream << Colour(Colour::Warning) << std::string(CATCH_CONFIG_CONSOLE_WIDTH - 1, '='); m_stream << m_colour->startColour( Colour::Success )
<< std::string( passedRatio, '=' );
}
} else {
m_stream << m_colour->startColour( Colour::Warning )
<< std::string( CATCH_CONFIG_CONSOLE_WIDTH - 1, '=' );
} }
m_stream << '\n'; m_stream << '\n';
} }
@ -691,8 +702,8 @@ void ConsoleReporter::printSummaryDivider() {
void ConsoleReporter::printTestFilters() { void ConsoleReporter::printTestFilters() {
if (m_config->testSpec().hasFilters()) { if (m_config->testSpec().hasFilters()) {
Colour guard(Colour::BrightYellow); m_stream << m_colour->startColour( Colour::BrightYellow ) << "Filters: "
m_stream << "Filters: " << serializeFilters(m_config->getTestsOrTags()) << '\n'; << serializeFilters( m_config->getTestsOrTags() ) << '\n';
} }
} }

View File

@ -19,6 +19,7 @@ namespace Catch {
struct IConfig; struct IConfig;
class TestCaseHandle; class TestCaseHandle;
class ColourImpl;
// Returns double formatted as %.3f (format expected on output) // Returns double formatted as %.3f (format expected on output)
std::string getFormattedDuration( double duration ); std::string getFormattedDuration( double duration );
@ -67,6 +68,7 @@ namespace Catch {
* `--list-test-names-only` option, for people who used it in integrations. * `--list-test-names-only` option, for people who used it in integrations.
*/ */
void defaultListTests( std::ostream& out, void defaultListTests( std::ostream& out,
ColourImpl* streamColour,
std::vector<TestCaseHandle> const& tests, std::vector<TestCaseHandle> const& tests,
bool isFiltered, bool isFiltered,
Verbosity verbosity ); Verbosity verbosity );

View File

@ -20,18 +20,20 @@ namespace Catch {
// Making older compiler happy is hard. // Making older compiler happy is hard.
static constexpr StringRef tapFailedString = "not ok"_sr; static constexpr StringRef tapFailedString = "not ok"_sr;
static constexpr StringRef tapPassedString = "ok"_sr; static constexpr StringRef tapPassedString = "ok"_sr;
static constexpr Colour::Code tapDimColour = Colour::FileName;
class TapAssertionPrinter { class TapAssertionPrinter {
public: public:
TapAssertionPrinter& operator= (TapAssertionPrinter const&) = delete; TapAssertionPrinter& operator= (TapAssertionPrinter const&) = delete;
TapAssertionPrinter(TapAssertionPrinter const&) = delete; TapAssertionPrinter(TapAssertionPrinter const&) = delete;
TapAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, std::size_t _counter) TapAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, std::size_t _counter, ColourImpl* colour_)
: stream(_stream) : stream(_stream)
, result(_stats.assertionResult) , result(_stats.assertionResult)
, messages(_stats.infoMessages) , messages(_stats.infoMessages)
, itMessage(_stats.infoMessages.begin()) , itMessage(_stats.infoMessages.begin())
, printInfoMessages(true) , printInfoMessages(true)
, counter(_counter) {} , counter(_counter)
, colourImpl( colour_ ) {}
void print() { void print() {
itMessage = messages.begin(); itMessage = messages.begin();
@ -104,11 +106,9 @@ namespace Catch {
} }
private: private:
static Colour::Code dimColour() { return Colour::FileName; }
void printSourceInfo() const { void printSourceInfo() const {
Colour colourGuard(dimColour()); stream << colourImpl->startColour( tapDimColour )
stream << result.getSourceInfo() << ':'; << result.getSourceInfo() << ':';
} }
void printResultType(StringRef passOrFail) const { void printResultType(StringRef passOrFail) const {
@ -124,10 +124,8 @@ namespace Catch {
void printExpressionWas() { void printExpressionWas() {
if (result.hasExpression()) { if (result.hasExpression()) {
stream << ';'; stream << ';';
{ stream << colourImpl->startColour( tapDimColour )
Colour colour(dimColour()); << " expression was:";
stream << " expression was:";
}
printOriginalExpression(); printOriginalExpression();
} }
} }
@ -140,10 +138,8 @@ namespace Catch {
void printReconstructedExpression() const { void printReconstructedExpression() const {
if (result.hasExpandedExpression()) { if (result.hasExpandedExpression()) {
{ stream << colourImpl->startColour( tapDimColour ) << " for: ";
Colour colour(dimColour());
stream << " for: ";
}
std::string expr = result.getExpandedExpression(); std::string expr = result.getExpandedExpression();
std::replace(expr.begin(), expr.end(), '\n', ' '); std::replace(expr.begin(), expr.end(), '\n', ' ');
stream << expr; stream << expr;
@ -157,7 +153,7 @@ namespace Catch {
} }
} }
void printRemainingMessages(Colour::Code colour = dimColour()) { void printRemainingMessages(Colour::Code colour = tapDimColour) {
if (itMessage == messages.end()) { if (itMessage == messages.end()) {
return; return;
} }
@ -166,18 +162,15 @@ namespace Catch {
std::vector<MessageInfo>::const_iterator itEnd = messages.end(); std::vector<MessageInfo>::const_iterator itEnd = messages.end();
const std::size_t N = static_cast<std::size_t>(std::distance(itMessage, itEnd)); const std::size_t N = static_cast<std::size_t>(std::distance(itMessage, itEnd));
{ stream << colourImpl->startColour( colour ) << " with "
Colour colourGuard(colour); << pluralise( N, "message"_sr ) << ':';
stream << " with " << pluralise(N, "message"_sr) << ':';
}
for (; itMessage != itEnd; ) { for (; itMessage != itEnd; ) {
// If this assertion is a warning ignore any INFO messages // If this assertion is a warning ignore any INFO messages
if (printInfoMessages || itMessage->type != ResultWas::Info) { if (printInfoMessages || itMessage->type != ResultWas::Info) {
stream << " '" << itMessage->message << '\''; stream << " '" << itMessage->message << '\'';
if (++itMessage != itEnd) { if (++itMessage != itEnd) {
Colour colourGuard(dimColour()); stream << colourImpl->startColour(tapDimColour) << " and";
stream << " and";
} }
} }
} }
@ -190,6 +183,7 @@ namespace Catch {
std::vector<MessageInfo>::const_iterator itMessage; std::vector<MessageInfo>::const_iterator itMessage;
bool printInfoMessages; bool printInfoMessages;
std::size_t counter; std::size_t counter;
ColourImpl* colourImpl;
}; };
} // End anonymous namespace } // End anonymous namespace
@ -202,7 +196,7 @@ namespace Catch {
++counter; ++counter;
m_stream << "# " << currentTestCaseInfo->name << '\n'; m_stream << "# " << currentTestCaseInfo->name << '\n';
TapAssertionPrinter printer(m_stream, _assertionStats, counter); TapAssertionPrinter printer(m_stream, _assertionStats, counter, m_colour.get());
printer.print(); printer.print();
m_stream << '\n' << std::flush; m_stream << '\n' << std::flush;

View File

@ -13,6 +13,7 @@
#include <catch2/interfaces/catch_interfaces_reporter.hpp> #include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_factory.hpp> #include <catch2/interfaces/catch_interfaces_reporter_factory.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_registry.hpp> #include <catch2/interfaces/catch_interfaces_reporter_registry.hpp>
#include <catch2/internal/catch_console_colour.hpp>
#include <catch2/internal/catch_enforce.hpp> #include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_list.hpp> #include <catch2/internal/catch_list.hpp>
#include <catch2/internal/catch_reporter_registry.hpp> #include <catch2/internal/catch_reporter_registry.hpp>
@ -40,11 +41,11 @@ TEST_CASE( "The default listing implementation write to provided stream",
using Catch::Matchers::ContainsSubstring; using Catch::Matchers::ContainsSubstring;
using namespace std::string_literals; using namespace std::string_literals;
std::stringstream sstream; StringIStream sstream;
SECTION( "Listing tags" ) { SECTION( "Listing tags" ) {
std::vector<Catch::TagInfo> tags(1); std::vector<Catch::TagInfo> tags(1);
tags[0].add("fakeTag"_catch_sr); tags[0].add("fakeTag"_catch_sr);
Catch::defaultListTags(sstream, tags, false); Catch::defaultListTags(sstream.stream(), tags, false);
auto listingString = sstream.str(); auto listingString = sstream.str();
REQUIRE_THAT(listingString, ContainsSubstring("[fakeTag]"s)); REQUIRE_THAT(listingString, ContainsSubstring("[fakeTag]"s));
@ -52,7 +53,7 @@ TEST_CASE( "The default listing implementation write to provided stream",
SECTION( "Listing reporters" ) { SECTION( "Listing reporters" ) {
std::vector<Catch::ReporterDescription> reporters( std::vector<Catch::ReporterDescription> reporters(
{ { "fake reporter", "fake description" } } ); { { "fake reporter", "fake description" } } );
Catch::defaultListReporters(sstream, reporters, Catch::Verbosity::Normal); Catch::defaultListReporters(sstream.stream(), reporters, Catch::Verbosity::Normal);
auto listingString = sstream.str(); auto listingString = sstream.str();
REQUIRE_THAT(listingString, ContainsSubstring("fake reporter"s)); REQUIRE_THAT(listingString, ContainsSubstring("fake reporter"s));
@ -63,7 +64,11 @@ TEST_CASE( "The default listing implementation write to provided stream",
{ "fake test name"_catch_sr, "[fakeTestTag]"_catch_sr }, { "fake test name"_catch_sr, "[fakeTestTag]"_catch_sr },
{ "fake-file.cpp", 123456789 } }; { "fake-file.cpp", 123456789 } };
std::vector<Catch::TestCaseHandle> tests({ {&fakeInfo, nullptr} }); std::vector<Catch::TestCaseHandle> tests({ {&fakeInfo, nullptr} });
Catch::defaultListTests(sstream, tests, false, Catch::Verbosity::Normal); Catch::ConfigData cd;
cd.useColour = Catch::UseColour::No;
Catch::Config conf(cd);
auto colour = Catch::makeColourImpl( &conf, &sstream);
Catch::defaultListTests(sstream.stream(), colour.get(), tests, false, Catch::Verbosity::Normal);
auto listingString = sstream.str(); auto listingString = sstream.str();
REQUIRE_THAT( listingString, REQUIRE_THAT( listingString,