Switched TextColour out for Colour

- Removed TextColour
This commit is contained in:
Phil Nash 2013-04-05 07:59:28 +01:00
parent a3703faa0a
commit f186a912d4
5 changed files with 57 additions and 198 deletions

View File

@ -36,7 +36,7 @@ namespace Catch {
BrightWhite = Bright | White,
// By intention
FileName = Grey,
FileName = LightGrey,
ResultError = BrightRed,
ResultSuccess = BrightGreen,
@ -46,49 +46,21 @@ namespace Catch {
OriginalExpression = Cyan,
ReconstructedExpression = Yellow,
SecondaryText = Grey,
SecondaryText = LightGrey,
Headers = White
};
// Use constructed object for RAII guard
Colour( Code _colourCode );
~Colour();
// Use static method for one-shot changes
static void use( Code _colourCode );
private:
static Detail::IColourImpl* impl;
};
struct IConsoleColourCodes : NonCopyable {
enum Colours {
None,
FileName,
ResultError,
ResultSuccess,
Error,
Success,
OriginalExpression,
ReconstructedExpression,
SecondaryText,
Headers
};
virtual void set( Colours colour ) = 0;
};
class TextColour : public IConsoleColourCodes {
public:
TextColour( Colours colour = None );
void set( Colours colour );
~TextColour();
private:
IConsoleColourCodes* m_impl;
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED

View File

@ -64,60 +64,10 @@ namespace {
WORD originalAttributes;
};
WORD mapConsoleColour( IConsoleColourCodes::Colours colour ) {
enum Win32Colours {
Grey = FOREGROUND_INTENSITY,
BrightRed = FOREGROUND_RED | FOREGROUND_INTENSITY,
BrightGreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
BrightWhite = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
DarkGreen = FOREGROUND_GREEN,
Cyan = FOREGROUND_BLUE | FOREGROUND_GREEN,
Yellow = FOREGROUND_RED | FOREGROUND_GREEN
};
switch( colour ) {
case IConsoleColourCodes::FileName: return Grey;
case IConsoleColourCodes::ResultError: return BrightRed;
case IConsoleColourCodes::ResultSuccess: return BrightGreen;
case IConsoleColourCodes::Error: return BrightRed;
case IConsoleColourCodes::Success: return DarkGreen;
case IConsoleColourCodes::OriginalExpression: return Cyan;
case IConsoleColourCodes::ReconstructedExpression: return Yellow;
case IConsoleColourCodes::SecondaryText: return Grey;
case IConsoleColourCodes::Headers: return 0;
default: return 0;
}
}
struct WindowsConsoleColourCodes : IConsoleColourCodes {
WindowsConsoleColourCodes()
: hStdout( GetStdHandle(STD_OUTPUT_HANDLE) ),
wOldColorAttrs( 0 )
{
GetConsoleScreenBufferInfo( hStdout, &csbiInfo );
wOldColorAttrs = csbiInfo.wAttributes;
}
~WindowsConsoleColourCodes() {
SetConsoleTextAttribute( hStdout, wOldColorAttrs );
}
void set( Colours colour ) {
WORD consoleColour = mapConsoleColour( colour );
if( consoleColour > 0 )
SetConsoleTextAttribute( hStdout, consoleColour );
}
HANDLE hStdout;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
WORD wOldColorAttrs;
};
inline bool shouldUseColourForPlatform() {
return true;
}
typedef WindowsConsoleColourCodes PlatformConsoleColourCodes;
Win32ColourImpl platformColourImpl;
} // end anon namespace
@ -130,6 +80,10 @@ namespace {
namespace Catch {
namespace {
// use POSIX/ ANSI console terminal codes
// Thanks to Adam Strzelecki for original contribution
// (http://github.com/nanoant)
// https://github.com/philsquared/Catch/pull/131
class PosixColourImpl : public Detail::IColourImpl {
public:
PosixColourImpl() {
@ -161,51 +115,10 @@ namespace {
}
};
// use POSIX/ ANSI console terminal codes
// Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
// https://github.com/philsquared/Catch/pull/131
const char* WhiteOrNormal = "[0m";
const char* BrightRed = "[1;31m";
const char* BrightGreen = "[1;32m";
// const char* BrightWhite = "[1;37m";
const char* Green = "[0;32m";
const char* Cyan = "[0;36m";
const char* Yellow = "[0;33m";
const char* LightGrey = "[0;37m";
// const char* DarkGrey = "[1;30m";
struct AnsiConsoleColourCodes : IConsoleColourCodes {
~AnsiConsoleColourCodes() {
set( None );
}
const char* escapeCodeForColour( Colours colour ) {
switch( colour ) {
case FileName: return WhiteOrNormal;
case ResultError: return BrightRed;
case ResultSuccess: return BrightGreen;
case Error: return BrightRed;
case Success: return Green;
case OriginalExpression: return Cyan;
case ReconstructedExpression: return Yellow;
case SecondaryText: return LightGrey;
case Headers: return WhiteOrNormal;
case None: return WhiteOrNormal;
}
}
void set( Colours colour ) {
std::cout << '\033' << escapeCodeForColour( colour );
}
};
inline bool shouldUseColourForPlatform() {
return isatty( fileno(stdout) );
}
typedef AnsiConsoleColourCodes PlatformConsoleColourCodes;
PosixColourImpl platformColourImpl;
} // end anon namespace
@ -213,12 +126,6 @@ namespace {
#endif // not Windows
namespace {
struct NoConsoleColourCodes : Catch::IConsoleColourCodes {
void set( Colours ) {}
};
}
namespace Catch {
namespace {
@ -240,26 +147,6 @@ namespace Catch {
? static_cast<Detail::IColourImpl*>( &platformColourImpl )
: static_cast<Detail::IColourImpl*>( &noColourImpl );
TextColour::TextColour( Colours colour ) : m_impl( NULL ) {
static bool s_shouldUseColour = shouldUseColourForPlatform() &&
!isDebuggerActive();
if( s_shouldUseColour )
m_impl = new PlatformConsoleColourCodes();
else
m_impl = new NoConsoleColourCodes();
if( colour )
set( colour );
}
TextColour::~TextColour() {
delete m_impl;
}
void TextColour::set( Colours colour ) {
m_impl->set( colour );
}
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED

View File

@ -64,25 +64,25 @@ namespace Catch {
tagsWrapper.setRight( maxTagLen ).wrap( it->getTestCaseInfo().tagsAsString );
for( std::size_t i = 0; i < std::max( nameWrapper.size(), tagsWrapper.size() ); ++i ) {
TextColour::Colours colour = TextColour::None;
Colour::Code colour = Colour::None;
if( it->getTestCaseInfo().isHidden )
colour = TextColour::SecondaryText;
colour = Colour::SecondaryText;
std::string nameCol;
if( i < nameWrapper.size() ) {
nameCol = nameWrapper[i];
}
else {
nameCol = " ...";
colour = TextColour::SecondaryText;
colour = Colour::SecondaryText;
}
{
TextColour colourGuard( colour );
Colour colourGuard( colour );
std::cout << nameCol;
}
if( i < tagsWrapper.size() && !tagsWrapper[i].empty() ) {
if( i == 0 ) {
TextColour colourGuard( TextColour::SecondaryText );
Colour colourGuard( Colour::SecondaryText );
std::cout << " " << std::string( maxNameLen - nameCol.size(), '.' ) << " ";
}
else {
@ -143,7 +143,7 @@ namespace Catch {
if( maxTagLen > wrapper.last().size() )
dots += maxTagLen - wrapper.last().size();
{
TextColour colourGuard( TextColour::SecondaryText );
Colour colourGuard( Colour::SecondaryText );
std::cout << std::string( dots, '.' );
}
std::cout << countIt->second

View File

@ -73,7 +73,7 @@ namespace Catch {
}
}
else {
TextColour colour( TextColour::ResultSuccess );
Colour colour( Colour::ResultSuccess );
m_config.stream() << allPrefix << "tests passed ("
<< pluralise( totals.assertions.passed, "assertion" ) << " in "
<< pluralise( totals.testCases.passed, "test case" ) << ")";
@ -130,12 +130,12 @@ namespace Catch {
virtual void NoAssertionsInSection( const std::string& sectionName ) {
startSpansLazily();
TextColour colour( TextColour::ResultError );
Colour colour( Colour::ResultError );
m_config.stream() << "\nNo assertions in section, '" << sectionName << "'\n" << std::endl;
}
virtual void NoAssertionsInTestCase( const std::string& testName ) {
startSpansLazily();
TextColour colour( TextColour::ResultError );
Colour colour( Colour::ResultError );
m_config.stream() << "\nNo assertions in test case, '" << testName << "'\n" << std::endl;
}
@ -146,11 +146,11 @@ namespace Catch {
m_config.stream() << "[End of section: '" << sectionName << "' ";
if( assertions.failed ) {
TextColour colour( TextColour::ResultError );
Colour colour( Colour::ResultError );
ReportCounts( "assertion", assertions);
}
else {
TextColour colour( TextColour::ResultSuccess );
Colour colour( Colour::ResultSuccess );
m_config.stream() << ( assertions.passed > 1 ? "All " : "" )
<< pluralise( assertions.passed, "assertion" ) << " passed" ;
}
@ -166,22 +166,22 @@ namespace Catch {
startSpansLazily();
if( !assertionResult.getSourceInfo().empty() ) {
TextColour colour( TextColour::FileName );
Colour colour( Colour::FileName );
m_config.stream() << assertionResult.getSourceInfo() << ": ";
}
if( assertionResult.hasExpression() ) {
TextColour colour( TextColour::OriginalExpression );
Colour colour( Colour::OriginalExpression );
m_config.stream() << assertionResult.getExpression();
if( assertionResult.succeeded() ) {
TextColour successColour( TextColour::Success );
Colour successColour( Colour::Success );
m_config.stream() << " succeeded";
}
else {
TextColour errorColour( TextColour::Error );
Colour errorColour( Colour::Error );
m_config.stream() << " failed";
if( assertionResult.isOk() ) {
TextColour okAnywayColour( TextColour::Success );
Colour okAnywayColour( Colour::Success );
m_config.stream() << " - but was ok";
}
}
@ -189,7 +189,7 @@ namespace Catch {
switch( assertionResult.getResultType() ) {
case ResultWas::ThrewException:
{
TextColour colour( TextColour::Error );
Colour colour( Colour::Error );
if( assertionResult.hasExpression() )
m_config.stream() << " with unexpected";
else
@ -199,7 +199,7 @@ namespace Catch {
break;
case ResultWas::DidntThrowException:
{
TextColour colour( TextColour::Error );
Colour colour( Colour::Error );
if( assertionResult.hasExpression() )
m_config.stream() << " because no exception was thrown where one was expected";
else
@ -208,19 +208,19 @@ namespace Catch {
break;
case ResultWas::Info:
{
TextColour colour( TextColour::ReconstructedExpression );
Colour colour( Colour::ReconstructedExpression );
streamVariableLengthText( "info", assertionResult.getMessage() );
}
break;
case ResultWas::Warning:
{
TextColour colour( TextColour::ReconstructedExpression );
Colour colour( Colour::ReconstructedExpression );
streamVariableLengthText( "warning", assertionResult.getMessage() );
}
break;
case ResultWas::ExplicitFailure:
{
TextColour colour( TextColour::Error );
Colour colour( Colour::Error );
m_config.stream() << "failed with message: '" << assertionResult.getMessage() << "'";
}
break;
@ -231,21 +231,21 @@ namespace Catch {
case ResultWas::Exception:
if( !assertionResult.hasExpression() ) {
if( assertionResult.succeeded() ) {
TextColour colour( TextColour::Success );
Colour colour( Colour::Success );
m_config.stream() << " succeeded";
}
else {
TextColour colour( TextColour::Error );
Colour colour( Colour::Error );
m_config.stream() << " failed";
if( assertionResult.isOk() ) {
TextColour okAnywayColour( TextColour::Success );
Colour okAnywayColour( Colour::Success );
m_config.stream() << " - but was ok";
}
}
}
if( assertionResult.hasMessage() ) {
m_config.stream() << "\n";
TextColour colour( TextColour::ReconstructedExpression );
Colour colour( Colour::ReconstructedExpression );
streamVariableLengthText( "with message", assertionResult.getMessage() );
}
break;
@ -258,7 +258,7 @@ namespace Catch {
if( assertionResult.getExpandedExpression().size() < 70 )
m_config.stream() << "\t";
}
TextColour colour( TextColour::ReconstructedExpression );
Colour colour( Colour::ReconstructedExpression );
m_config.stream() << assertionResult.getExpandedExpression();
}
m_config.stream() << std::endl;

View File

@ -60,7 +60,7 @@ namespace Catch {
virtual void sectionEnded( SectionStats const& _sectionStats ) {
if( _sectionStats.missingAssertions ) {
lazyPrint();
TextColour colour( TextColour::ResultError );
Colour colour( Colour::ResultError );
stream << "\nNo assertions in section, '" << _sectionStats.sectionInfo.name << "'\n" << std::endl;
}
m_headerPrinted = false;
@ -71,7 +71,7 @@ namespace Catch {
if( _testCaseStats.missingAssertions ) {
lazyPrint();
TextColour colour( TextColour::ResultError );
Colour colour( Colour::ResultError );
stream << "\nNo assertions in test case, '" << _testCaseStats.testInfo.name << "'\n" << std::endl;
}
StreamingReporterBase::testCaseEnded( _testCaseStats );
@ -103,13 +103,13 @@ namespace Catch {
: stream( _stream ),
stats( _stats ),
result( _stats.assertionResult ),
colour( TextColour::None ),
colour( Colour::None ),
message( result.getMessage() ),
messages( _stats.infoMessages )
{
switch( result.getResultType() ) {
case ResultWas::Ok:
colour = TextColour::Success;
colour = Colour::Success;
passOrFail = "PASSED";
//if( result.hasMessage() )
if( _stats.infoMessages.size() == 1 )
@ -119,11 +119,11 @@ namespace Catch {
break;
case ResultWas::ExpressionFailed:
if( result.isOk() ) {
colour = TextColour::Success;
colour = Colour::Success;
passOrFail = "FAILED - but was ok";
}
else {
colour = TextColour::Error;
colour = Colour::Error;
passOrFail = "FAILED";
}
if( _stats.infoMessages.size() == 1 )
@ -132,12 +132,12 @@ namespace Catch {
messageLabel = "with messages";
break;
case ResultWas::ThrewException:
colour = TextColour::Error;
colour = Colour::Error;
passOrFail = "FAILED";
messageLabel = "due to unexpected exception with message";
break;
case ResultWas::DidntThrowException:
colour = TextColour::Error;
colour = Colour::Error;
passOrFail = "FAILED";
messageLabel = "because no exception was thrown where one was expected";
break;
@ -149,7 +149,7 @@ namespace Catch {
break;
case ResultWas::ExplicitFailure:
passOrFail = "FAILED";
colour = TextColour::Error;
colour = Colour::Error;
if( _stats.infoMessages.size() == 1 )
messageLabel = "explicitly with message";
if( _stats.infoMessages.size() > 1 )
@ -157,7 +157,7 @@ namespace Catch {
break;
case ResultWas::Exception:
passOrFail = "FAILED";
colour = TextColour::Error;
colour = Colour::Error;
if( _stats.infoMessages.size() == 1 )
messageLabel = "with message";
if( _stats.infoMessages.size() > 1 )
@ -168,7 +168,7 @@ namespace Catch {
case ResultWas::Unknown:
case ResultWas::FailureBit:
passOrFail = "** internal error **";
colour = TextColour::Error;
colour = Colour::Error;
break;
}
}
@ -191,13 +191,13 @@ namespace Catch {
private:
void printResultType() const {
if( !passOrFail.empty() ) {
TextColour colourGuard( colour );
Colour colourGuard( colour );
stream << passOrFail << ":\n";
}
}
void printOriginalExpression() const {
if( result.hasExpression() ) {
TextColour colourGuard( TextColour::OriginalExpression );
Colour colourGuard( Colour::OriginalExpression );
stream << " ";
if( !result.getTestMacroName().empty() )
stream << result.getTestMacroName() << "( ";
@ -210,7 +210,7 @@ namespace Catch {
void printReconstructedExpression() const {
if( result.hasExpandedExpression() ) {
stream << "with expansion:\n";
TextColour colourGuard( TextColour::ReconstructedExpression );
Colour colourGuard( Colour::ReconstructedExpression );
stream << LineWrapper().setIndent(2).wrap( result.getExpandedExpression() ) << "\n";
}
}
@ -224,14 +224,14 @@ namespace Catch {
}
}
void printSourceInfo() const {
TextColour colourGuard( TextColour::FileName );
Colour colourGuard( Colour::FileName );
stream << result.getSourceInfo() << ": ";
}
std::ostream& stream;
AssertionStats const& stats;
AssertionResult const& result;
TextColour::Colours colour;
Colour::Code colour;
std::string passOrFail;
std::string messageLabel;
std::string message;
@ -253,7 +253,7 @@ namespace Catch {
}
void lazyPrintRunInfo() {
stream << "\n" << getTildes() << "\n";
TextColour colour( TextColour::SecondaryText );
Colour colour( Colour::SecondaryText );
stream << testRunInfo->name
<< " is a CATCH v" << libraryVersion.majorVersion << "."
<< libraryVersion.minorVersion << " b"
@ -277,7 +277,7 @@ namespace Catch {
? currentSectionInfo->lineInfo
: unusedTestCaseInfo->lineInfo );
if( currentSectionInfo ) {
TextColour colourGuard( TextColour::Headers );
Colour colourGuard( Colour::Headers );
std::vector<ThreadedSectionInfo*> sections;
for( ThreadedSectionInfo* section = currentSectionInfo.get();
section;
@ -301,11 +301,11 @@ namespace Catch {
void printOpenHeader( std::string const& _name, SourceLineInfo const& _lineInfo = SourceLineInfo() ) {
stream << getDashes() << "\n";
if( !_lineInfo.empty() ){
TextColour colourGuard( TextColour::FileName );
Colour colourGuard( Colour::FileName );
stream << _lineInfo << "\n\n";
}
{
TextColour colourGuard( TextColour::Headers );
Colour colourGuard( Colour::Headers );
stream << _name << "\n";
}
}
@ -315,7 +315,7 @@ namespace Catch {
stream << "No tests ran";
}
else if( totals.assertions.failed ) {
TextColour colour( TextColour::ResultError );
Colour colour( Colour::ResultError );
printCounts( "test case", totals.testCases );
if( totals.testCases.failed > 0 ) {
stream << " (";
@ -324,7 +324,7 @@ namespace Catch {
}
}
else {
TextColour colour( TextColour::ResultSuccess );
Colour colour( Colour::ResultSuccess );
stream << "All tests passed ("
<< pluralise( totals.assertions.passed, "assertion" ) << " in "
<< pluralise( totals.testCases.passed, "test case" ) << ")";