diff options
| author | Phil Nash | 2013-03-11 18:38:29 +0000 |
|---|---|---|
| committer | Phil Nash | 2013-03-11 18:38:29 +0000 |
| commit | 3df6c0d1b4b444be040464befd6e9ebdd3287d42 (patch) | |
| tree | 6530327f1e66713875a2e07ee287a90f0fcd86de /include | |
| parent | a04981b4507110d5d73c23ec403d908bafb33bec (diff) | |
| download | catch3-3df6c0d1b4b444be040464befd6e9ebdd3287d42.tar.gz | |
Refactored ConsoleColour impl. Tweaked the (Windows) colours a bit.
Also fixed issue that would cause warnings on some compilers when doing REQUIRE( p ); where p is a pointer.
Moved to build 23
Diffstat (limited to 'include')
| -rw-r--r-- | include/internal/catch_capture.hpp | 2 | ||||
| -rw-r--r-- | include/internal/catch_console_colour.hpp | 15 | ||||
| -rw-r--r-- | include/internal/catch_console_colour_impl.hpp | 206 | ||||
| -rw-r--r-- | include/internal/catch_version.hpp | 2 |
4 files changed, 113 insertions, 112 deletions
diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index a32528a9..4cc9705f 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -70,7 +70,7 @@ inline bool isTrue( bool value ){ return value; } if( internal_catch_action & Catch::ResultAction::Debug ) BreakIntoDebugger(); \ if( internal_catch_action & Catch::ResultAction::Abort ) throw Catch::TestFailureException(); \ if( !Catch::shouldContinueOnFailure( resultDisposition ) ) throw Catch::TestFailureException(); \ - if( Catch::isTrue( false ) ){ bool this_is_here_to_invoke_warnings = ( originalExpr ); Catch::isTrue( this_is_here_to_invoke_warnings ); } \ + Catch::isTrue( false && originalExpr ); \ } /////////////////////////////////////////////////////////////////////////////// diff --git a/include/internal/catch_console_colour.hpp b/include/internal/catch_console_colour.hpp index 6cb0b828..c508a64b 100644 --- a/include/internal/catch_console_colour.hpp +++ b/include/internal/catch_console_colour.hpp @@ -12,11 +12,7 @@ namespace Catch { - struct ConsoleColourImpl; - - class TextColour : NonCopyable { - public: - + struct IConsoleColourCodes : NonCopyable { enum Colours { None, @@ -30,13 +26,18 @@ namespace Catch { OriginalExpression, ReconstructedExpression }; - + + virtual void set( Colours colour ) = 0; + }; + + class TextColour : public IConsoleColourCodes { + public: TextColour( Colours colour = None ); void set( Colours colour ); ~TextColour(); private: - ConsoleColourImpl* m_impl; + IConsoleColourCodes* m_impl; }; } // end namespace Catch diff --git a/include/internal/catch_console_colour_impl.hpp b/include/internal/catch_console_colour_impl.hpp index d3b1c12c..58c926e7 100644 --- a/include/internal/catch_console_colour_impl.hpp +++ b/include/internal/catch_console_colour_impl.hpp @@ -10,129 +10,141 @@ #include "catch_console_colour.hpp" -#if !defined(CATCH_CONFIG_USE_ANSI_COLOUR_CODES) && !defined(CATCH_PLATFORM_WINDOWS) -#define CATCH_CONFIG_USE_ANSI_COLOUR_CODES 1 -#endif +#if defined ( CATCH_PLATFORM_WINDOWS ) ///////////////////////////////////////// -#if defined( CATCH_CONFIG_USE_ANSI_COLOUR_CODES ) +#include <windows.h> + +namespace { + using namespace Catch; + + WORD mapConsoleColour( IConsoleColourCodes::Colours colour ) { + switch( colour ) { + case IConsoleColourCodes::FileName: + return FOREGROUND_INTENSITY; // greyed out + case IConsoleColourCodes::ResultError: + return FOREGROUND_RED | FOREGROUND_INTENSITY; // bright red + case IConsoleColourCodes::ResultSuccess: + return FOREGROUND_GREEN | FOREGROUND_INTENSITY; // bright green + case IConsoleColourCodes::Error: + return FOREGROUND_RED | FOREGROUND_INTENSITY; // bright red + case IConsoleColourCodes::Success: + return FOREGROUND_GREEN; // dark green + case IConsoleColourCodes::OriginalExpression: + return FOREGROUND_BLUE | FOREGROUND_GREEN; // turquoise + case IConsoleColourCodes::ReconstructedExpression: + return FOREGROUND_RED | FOREGROUND_GREEN; // greeny-yellow + 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; + +} // end anon namespace + +#else // Not Windows - assumed to be POSIX compatible ////////////////////////// #include <unistd.h> -namespace Catch { +namespace { + using namespace Catch; // use POSIX/ ANSI console terminal codes // Implementation contributed by Adam Strzelecki (http://github.com/nanoant) // https://github.com/philsquared/Catch/pull/131 + + struct AnsiConsoleColourCodes : IConsoleColourCodes { - TextColour::TextColour( Colours colour ) { - if( colour ) - set( colour ); - } - - TextColour::~TextColour() { - set( TextColour::None ); - } - - namespace { const char colourEscape = '\033'; } + ~AnsiConsoleColourCodes() { + set( None ); + } - inline bool shouldUseColour() { - static bool s_shouldUseColour - = CATCH_CONFIG_USE_ANSI_COLOUR_CODES != 0 && - isatty( fileno(stdout) ) && - !isDebuggerActive(); - return s_shouldUseColour; - } - void TextColour::set( Colours colour ) { - if( shouldUseColour() ) { + void set( Colours colour ) { + const char colourEscape = '\033'; switch( colour ) { - case TextColour::FileName: + case FileName: std::cout << colourEscape << "[0m"; // white/ normal break; - case TextColour::ResultError: - std::cout << colourEscape << "[1;31m"; // bold red - break; - case TextColour::ResultSuccess: - std::cout << colourEscape << "[1;32m"; // bold green - break; - case TextColour::Error: + case ResultError: std::cout << colourEscape << "[0;31m"; // red break; - case TextColour::Success: + case ResultSuccess: std::cout << colourEscape << "[0;32m"; // green break; - case TextColour::OriginalExpression: + case Error: + std::cout << colourEscape << "[1;31m"; // bold red + break; + case Success: + std::cout << colourEscape << "[1;32m"; // bold green + break; + case OriginalExpression: std::cout << colourEscape << "[0;36m"; // cyan break; - case TextColour::ReconstructedExpression: + case ReconstructedExpression: std::cout << colourEscape << "[0;33m"; // yellow break; - case TextColour::None: + case None: std::cout << colourEscape << "[0m"; // reset } } - } + }; + inline bool shouldUseColourForPlatform() { + return isatty( fileno(stdout) ); + } + + typedef AnsiConsoleColourCodes PlatformConsoleColourCodes; + } // namespace Catch -#elif defined ( CATCH_PLATFORM_WINDOWS ) +#endif // not Windows -#include <windows.h> +namespace { + struct NoConsoleColourCodes : IConsoleColourCodes { + void set( Colours ) {} + }; +} namespace Catch { - namespace { - - WORD mapConsoleColour( TextColour::Colours colour ) { - switch( colour ) { - case TextColour::FileName: - return FOREGROUND_INTENSITY; // greyed out - case TextColour::ResultError: - return FOREGROUND_RED | FOREGROUND_INTENSITY; // bright red - case TextColour::ResultSuccess: - return FOREGROUND_GREEN | FOREGROUND_INTENSITY; // bright green - case TextColour::Error: - return FOREGROUND_RED; // dark red - case TextColour::Success: - return FOREGROUND_GREEN; // dark green - case TextColour::OriginalExpression: - return FOREGROUND_BLUE | FOREGROUND_GREEN; // turquoise - case TextColour::ReconstructedExpression: - return FOREGROUND_RED | FOREGROUND_GREEN; // greeny-yellow - default: return 0; - } - } - } - - struct ConsoleColourImpl { - - ConsoleColourImpl() - : hStdout( GetStdHandle(STD_OUTPUT_HANDLE) ), - wOldColorAttrs( 0 ) - { - GetConsoleScreenBufferInfo( hStdout, &csbiInfo ); - wOldColorAttrs = csbiInfo.wAttributes; - } - - ~ConsoleColourImpl() { - SetConsoleTextAttribute( hStdout, wOldColorAttrs ); - } - - void set( TextColour::Colours colour ) { - WORD consoleColour = mapConsoleColour( colour ); - if( consoleColour > 0 ) - SetConsoleTextAttribute( hStdout, consoleColour ); - } - - HANDLE hStdout; - CONSOLE_SCREEN_BUFFER_INFO csbiInfo; - WORD wOldColorAttrs; - }; - - TextColour::TextColour( Colours colour ) - : m_impl( new ConsoleColourImpl() ) - { + 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 ) - m_impl->set( colour ); + set( colour ); } TextColour::~TextColour() { @@ -145,16 +157,4 @@ namespace Catch { } // end namespace Catch -#else - -namespace Catch { - - TextColour::TextColour( Colours ){} - TextColour::~TextColour(){} - void TextColour::set( Colours ){} - -} // end namespace Catch - -#endif - #endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED diff --git a/include/internal/catch_version.hpp b/include/internal/catch_version.hpp index b1839c74..e1485d5c 100644 --- a/include/internal/catch_version.hpp +++ b/include/internal/catch_version.hpp @@ -13,7 +13,7 @@ namespace Catch { // These numbers are maintained by a script - Version libraryVersion( 0, 9, 22, "integration" ); + Version libraryVersion( 0, 9, 23, "integration" ); } #endif // TWOBLUECUBES_CATCH_VERSION_HPP_INCLUDED |