Cleaned up ANSI colour code impl a bit

This commit is contained in:
Phil Nash 2012-10-31 18:04:22 +00:00
parent 778f9c4fc7
commit 355b95fda1
4 changed files with 112 additions and 70 deletions

View File

@ -10,11 +10,13 @@
#include "catch_console_colour.hpp" #include "catch_console_colour.hpp"
#if defined( CATCH_CONFIG_USE_ANSI_COLOUR_CODES )
#include <unistd.h>
namespace Catch { namespace Catch {
#if defined( CATCH_CONFIG_USE_POSIX_COLOUR_CODES ) // use POSIX/ ANSI console terminal codes
// use POSIX console terminal codes
// Implementation contributed by Adam Strzelecki (http://github.com/nanoant) // Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
// https://github.com/philsquared/Catch/pull/131 // https://github.com/philsquared/Catch/pull/131
@ -27,40 +29,46 @@ namespace Catch {
set( TextColour::None ); set( TextColour::None );
} }
namespace { const char colourEscape = '\033'; }
void TextColour::set( Colours colour ) { void TextColour::set( Colours colour ) {
if( isatty( fileno(stdout) ) ) { if( isatty( fileno(stdout) ) ) {
switch( colour ) { switch( colour ) {
case TextColour::FileName: case TextColour::FileName:
std::cout << "\e[1m"; // bold std::cout << colourEscape << "[0m"; // white
break; break;
case TextColour::ResultError: case TextColour::ResultError:
std::cout << "\e[1;31m"; // bright red std::cout << colourEscape << "[1;31m"; // bold red
break; break;
case TextColour::ResultSuccess: case TextColour::ResultSuccess:
std::cout << "\e[1;32m"; // bright green std::cout << colourEscape << "[1;32m"; // bold green
break; break;
case TextColour::Error: case TextColour::Error:
std::cout << "\e[0;31m"; // dark red std::cout << colourEscape << "[0;31m"; // red
break; break;
case TextColour::Success: case TextColour::Success:
std::cout << "\e[0;32m"; // dark green std::cout << colourEscape << "[0;32m"; // green
break; break;
case TextColour::OriginalExpression: case TextColour::OriginalExpression:
std::cout << "\e[0;36m"; // cyan std::cout << colourEscape << "[0;36m"; // cyan
break; break;
case TextColour::ReconstructedExpression: case TextColour::ReconstructedExpression:
std::cout << "\e[0;33m"; // yellow std::cout << colourEscape << "[0;33m"; // yellow
break; break;
case TextColour::None: case TextColour::None:
std::cout << "\e[0m"; // reset std::cout << colourEscape << "[0m"; // reset to white
} }
} }
} }
} // namespace Catch
#elif defined ( CATCH_PLATFORM_WINDOWS ) #elif defined ( CATCH_PLATFORM_WINDOWS )
#include <windows.h> #include <windows.h>
namespace Catch {
namespace { namespace {
WORD mapConsoleColour( TextColour::Colours colour ) { WORD mapConsoleColour( TextColour::Colours colour ) {
@ -124,14 +132,18 @@ namespace Catch {
m_impl->set( colour ); m_impl->set( colour );
} }
} // end namespace Catch
#else #else
namespace Catch {
TextColour::TextColour( Colours ){} TextColour::TextColour( Colours ){}
TextColour::~TextColour(){} TextColour::~TextColour(){}
void TextColour::set( Colours ){} void TextColour::set( Colours ){}
#endif
} // end namespace Catch } // end namespace Catch
#endif
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED #endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED

View File

@ -184,35 +184,41 @@ namespace Catch {
} }
switch( assertionResult.getResultType() ) { switch( assertionResult.getResultType() ) {
case ResultWas::ThrewException: case ResultWas::ThrewException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( assertionResult.hasExpression() ) if( assertionResult.hasExpression() )
m_config.stream << " with unexpected"; m_config.stream << " with unexpected";
else else
m_config.stream << "Unexpected"; m_config.stream << "Unexpected";
m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'"; m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'";
} }
break; break;
case ResultWas::DidntThrowException: case ResultWas::DidntThrowException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( assertionResult.hasExpression() ) if( assertionResult.hasExpression() )
m_config.stream << " because no exception was thrown where one was expected"; m_config.stream << " because no exception was thrown where one was expected";
else else
m_config.stream << "No exception thrown where one was expected"; m_config.stream << "No exception thrown where one was expected";
} }
break; break;
case ResultWas::Info: case ResultWas::Info:
streamVariableLengthText( "info", assertionResult.getMessage() ); {
TextColour colour( TextColour::ReconstructedExpression );
streamVariableLengthText( "info", assertionResult.getMessage() );
}
break; break;
case ResultWas::Warning: case ResultWas::Warning:
streamVariableLengthText( "warning", assertionResult.getMessage() ); {
TextColour colour( TextColour::ReconstructedExpression );
streamVariableLengthText( "warning", assertionResult.getMessage() );
}
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'"; m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'";
} }
break; break;
case ResultWas::Unknown: // These cases are here to prevent compiler warnings case ResultWas::Unknown: // These cases are here to prevent compiler warnings
case ResultWas::Ok: case ResultWas::Ok:

View File

@ -551,6 +551,11 @@
buildSettings = { buildSettings = {
CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = NO; CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
GCC_PREPROCESSOR_DEFINITIONS = CATCH_CONFIG_USE_ANSI_COLOUR_CODES;
"GCC_PREPROCESSOR_DEFINITIONS[arch=*]" = (
CATCH_CONFIG_USE_ANSI_COLOUR_CODES,
"DEBUG=1",
);
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
WARNING_CFLAGS = ( WARNING_CFLAGS = (
"-Weverything", "-Weverything",
@ -564,6 +569,7 @@
buildSettings = { buildSettings = {
CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = NO; CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
GCC_PREPROCESSOR_DEFINITIONS = CATCH_CONFIG_USE_ANSI_COLOUR_CODES;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
WARNING_CFLAGS = ( WARNING_CFLAGS = (
"-Weverything", "-Weverything",

View File

@ -1,5 +1,5 @@
/* /*
* Generated: 2012-10-30 09:08:37.538907 * Generated: 2012-10-31 18:04:01.157950
* ---------------------------------------------------------- * ----------------------------------------------------------
* This file has been merged from multiple headers. Please don't edit it directly * This file has been merged from multiple headers. Please don't edit it directly
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
@ -4858,11 +4858,13 @@ namespace Catch {
} // end namespace Catch } // end namespace Catch
#if defined( CATCH_CONFIG_USE_ANSI_COLOUR_CODES )
#include <unistd.h>
namespace Catch { namespace Catch {
#if defined( CATCH_CONFIG_USE_POSIX_COLOUR_CODES ) // use POSIX/ ANSI console terminal codes
// use POSIX console terminal codes
// Implementation contributed by Adam Strzelecki (http://github.com/nanoant) // Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
// https://github.com/philsquared/Catch/pull/131 // https://github.com/philsquared/Catch/pull/131
@ -4875,40 +4877,46 @@ namespace Catch {
set( TextColour::None ); set( TextColour::None );
} }
namespace { const char colourEscape = '\033'; }
void TextColour::set( Colours colour ) { void TextColour::set( Colours colour ) {
if( isatty( fileno(stdout) ) ) { if( isatty( fileno(stdout) ) ) {
switch( colour ) { switch( colour ) {
case TextColour::FileName: case TextColour::FileName:
std::cout << "\e[1m"; // bold std::cout << colourEscape << "[0m"; // white
break; break;
case TextColour::ResultError: case TextColour::ResultError:
std::cout << "\e[1;31m"; // bright red std::cout << colourEscape << "[1;31m"; // bold red
break; break;
case TextColour::ResultSuccess: case TextColour::ResultSuccess:
std::cout << "\e[1;32m"; // bright green std::cout << colourEscape << "[1;32m"; // bold green
break; break;
case TextColour::Error: case TextColour::Error:
std::cout << "\e[0;31m"; // dark red std::cout << colourEscape << "[0;31m"; // red
break; break;
case TextColour::Success: case TextColour::Success:
std::cout << "\e[0;32m"; // dark green std::cout << colourEscape << "[0;32m"; // green
break; break;
case TextColour::OriginalExpression: case TextColour::OriginalExpression:
std::cout << "\e[0;36m"; // cyan std::cout << colourEscape << "[0;36m"; // cyan
break; break;
case TextColour::ReconstructedExpression: case TextColour::ReconstructedExpression:
std::cout << "\e[0;33m"; // yellow std::cout << colourEscape << "[0;33m"; // yellow
break; break;
case TextColour::None: case TextColour::None:
std::cout << "\e[0m"; // reset std::cout << colourEscape << "[0m"; // reset to white
} }
} }
} }
} // namespace Catch
#elif defined ( CATCH_PLATFORM_WINDOWS ) #elif defined ( CATCH_PLATFORM_WINDOWS )
#include <windows.h> #include <windows.h>
namespace Catch {
namespace { namespace {
WORD mapConsoleColour( TextColour::Colours colour ) { WORD mapConsoleColour( TextColour::Colours colour ) {
@ -4972,16 +4980,20 @@ namespace Catch {
m_impl->set( colour ); m_impl->set( colour );
} }
} // end namespace Catch
#else #else
namespace Catch {
TextColour::TextColour( Colours ){} TextColour::TextColour( Colours ){}
TextColour::~TextColour(){} TextColour::~TextColour(){}
void TextColour::set( Colours ){} void TextColour::set( Colours ){}
#endif
} // end namespace Catch } // end namespace Catch
#endif
// #included from: catch_generators_impl.hpp // #included from: catch_generators_impl.hpp
#define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED #define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED
@ -5502,35 +5514,41 @@ namespace Catch {
} }
switch( assertionResult.getResultType() ) { switch( assertionResult.getResultType() ) {
case ResultWas::ThrewException: case ResultWas::ThrewException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( assertionResult.hasExpression() ) if( assertionResult.hasExpression() )
m_config.stream << " with unexpected"; m_config.stream << " with unexpected";
else else
m_config.stream << "Unexpected"; m_config.stream << "Unexpected";
m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'"; m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'";
} }
break; break;
case ResultWas::DidntThrowException: case ResultWas::DidntThrowException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( assertionResult.hasExpression() ) if( assertionResult.hasExpression() )
m_config.stream << " because no exception was thrown where one was expected"; m_config.stream << " because no exception was thrown where one was expected";
else else
m_config.stream << "No exception thrown where one was expected"; m_config.stream << "No exception thrown where one was expected";
} }
break; break;
case ResultWas::Info: case ResultWas::Info:
streamVariableLengthText( "info", assertionResult.getMessage() ); {
TextColour colour( TextColour::ReconstructedExpression );
streamVariableLengthText( "info", assertionResult.getMessage() );
}
break; break;
case ResultWas::Warning: case ResultWas::Warning:
streamVariableLengthText( "warning", assertionResult.getMessage() ); {
TextColour colour( TextColour::ReconstructedExpression );
streamVariableLengthText( "warning", assertionResult.getMessage() );
}
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'"; m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'";
} }
break; break;
case ResultWas::Unknown: // These cases are here to prevent compiler warnings case ResultWas::Unknown: // These cases are here to prevent compiler warnings
case ResultWas::Ok: case ResultWas::Ok: