Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Cleaned up ANSI colour code impl a bit

Commit
355b95fda1de28ba830541768aad32c7b1790290
Author
Phil Nash ·
Parents
778f9c4fc7fa

Files · Download .tar.gz

Changes

 include/internal/catch_console_colour_impl.hpp     | 40 ++++++----
 include/reporters/catch_reporter_basic.hpp         | 48 ++++++------
 .../CatchSelfTest.xcodeproj/project.pbxproj        |  6 ++
 single_include/catch.hpp                           | 88 +++++++++++++---------
 4 files changed, 112 insertions(+), 70 deletions(-)

diff --git a/include/internal/catch_console_colour_impl.hpp b/include/internal/catch_console_colour_impl.hpp
index 101b3220..69470b1b 100644
--- a/include/internal/catch_console_colour_impl.hpp
+++ b/include/internal/catch_console_colour_impl.hpp
@@ -10,11 +10,13 @@
 
 #include "catch_console_colour.hpp"
 
-namespace Catch {
+#if defined( CATCH_CONFIG_USE_ANSI_COLOUR_CODES )
+
+#include <unistd.h>
 
-#if defined( CATCH_CONFIG_USE_POSIX_COLOUR_CODES )
+namespace Catch {
 
-    // use POSIX console terminal codes
+    // use POSIX/ ANSI console terminal codes
     // Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
     // https://github.com/philsquared/Catch/pull/131
     
@@ -27,40 +29,46 @@ namespace Catch {
         set( TextColour::None );
     }
 
+    namespace { const char colourEscape = '\033'; }
+
     void TextColour::set( Colours colour ) {
         if( isatty( fileno(stdout) ) ) {
             switch( colour ) {
                 case TextColour::FileName:
-                    std::cout << "\e[1m";    // bold
+                    std::cout << colourEscape << "[0m";    // white
                     break;
                 case TextColour::ResultError:
-                    std::cout << "\e[1;31m"; // bright red
+                    std::cout << colourEscape << "[1;31m"; // bold red
                     break;
                 case TextColour::ResultSuccess:
-                    std::cout << "\e[1;32m"; // bright green
+                    std::cout << colourEscape << "[1;32m"; // bold green
                     break;
                 case TextColour::Error:
-                    std::cout << "\e[0;31m"; // dark red
+                    std::cout << colourEscape << "[0;31m"; // red
                     break;
                 case TextColour::Success:
-                    std::cout << "\e[0;32m"; // dark green
+                    std::cout << colourEscape << "[0;32m"; // green
                     break;
                 case TextColour::OriginalExpression:
-                    std::cout << "\e[0;36m"; // cyan
+                    std::cout << colourEscape << "[0;36m"; // cyan
                     break;
                 case TextColour::ReconstructedExpression:
-                    std::cout << "\e[0;33m"; // yellow
+                    std::cout << colourEscape << "[0;33m"; // yellow
                     break;
                 case TextColour::None:
-                    std::cout << "\e[0m"; // reset
+                    std::cout << colourEscape << "[0m"; // reset to white
             }
         }
     }
-    
+
+} // namespace Catch
+
 #elif defined ( CATCH_PLATFORM_WINDOWS )
 
 #include <windows.h>
 
+namespace Catch {
+
     namespace {
     
         WORD mapConsoleColour( TextColour::Colours colour ) {
@@ -124,14 +132,18 @@ namespace Catch {
         m_impl->set( colour );
     }
 
+} // end namespace Catch
+
 #else
 
+namespace Catch {
+
     TextColour::TextColour( Colours ){}
     TextColour::~TextColour(){}
     void TextColour::set( Colours ){}
 
-#endif
-
 } // end namespace Catch
 
+#endif
+
 #endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
diff --git a/include/reporters/catch_reporter_basic.hpp b/include/reporters/catch_reporter_basic.hpp
index 52c1c4b0..63074629 100644
--- a/include/reporters/catch_reporter_basic.hpp
+++ b/include/reporters/catch_reporter_basic.hpp
@@ -184,35 +184,41 @@ namespace Catch {
             }
             switch( assertionResult.getResultType() ) {
                 case ResultWas::ThrewException:
-                {
-                    TextColour colour( TextColour::Error );
-                    if( assertionResult.hasExpression() )
-                        m_config.stream << " with unexpected";
-                    else
-                        m_config.stream << "Unexpected";
-                    m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'";
-                }
+                    {
+                        TextColour colour( TextColour::Error );
+                        if( assertionResult.hasExpression() )
+                            m_config.stream << " with unexpected";
+                        else
+                            m_config.stream << "Unexpected";
+                        m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'";
+                    }
                     break;
                 case ResultWas::DidntThrowException:
-                {
-                    TextColour colour( TextColour::Error );
-                    if( assertionResult.hasExpression() )
-                        m_config.stream << " because no exception was thrown where one was expected";
-                    else
-                        m_config.stream << "No exception thrown where one was expected";
-                }
+                    {
+                        TextColour colour( TextColour::Error );
+                        if( assertionResult.hasExpression() )
+                            m_config.stream << " because no exception was thrown where one was expected";
+                        else
+                            m_config.stream << "No exception thrown where one was expected";
+                    }
                     break;
                 case ResultWas::Info:
-                    streamVariableLengthText( "info", assertionResult.getMessage() );
+                    {
+                        TextColour colour( TextColour::ReconstructedExpression );
+                        streamVariableLengthText( "info", assertionResult.getMessage() );
+                    }
                     break;
                 case ResultWas::Warning:
-                    streamVariableLengthText( "warning", assertionResult.getMessage() );
+                    {
+                        TextColour colour( TextColour::ReconstructedExpression );
+                        streamVariableLengthText( "warning", assertionResult.getMessage() );
+                    }
                     break;
                 case ResultWas::ExplicitFailure:
-                {
-                    TextColour colour( TextColour::Error );
-                    m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'";
-                }
+                    {
+                        TextColour colour( TextColour::Error );
+                        m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'";
+                    }
                     break;
                 case ResultWas::Unknown: // These cases are here to prevent compiler warnings
                 case ResultWas::Ok:
diff --git a/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj b/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj
index 63c8d5b2..7667e515 100644
--- a/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj
+++ b/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj
@@ -551,6 +551,11 @@
 			buildSettings = {
 				CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
 				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)";
 				WARNING_CFLAGS = (
 					"-Weverything",
@@ -564,6 +569,7 @@
 			buildSettings = {
 				CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
+				GCC_PREPROCESSOR_DEFINITIONS = CATCH_CONFIG_USE_ANSI_COLOUR_CODES;
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				WARNING_CFLAGS = (
 					"-Weverything",
diff --git a/single_include/catch.hpp b/single_include/catch.hpp
index 6d6b891e..822ebcff 100644
--- a/single_include/catch.hpp
+++ b/single_include/catch.hpp
@@ -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
  *  Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
@@ -4858,11 +4858,13 @@ namespace Catch {
 
 } // end namespace Catch
 
-namespace Catch {
+#if defined( CATCH_CONFIG_USE_ANSI_COLOUR_CODES )
+
+#include <unistd.h>
 
-#if defined( CATCH_CONFIG_USE_POSIX_COLOUR_CODES )
+namespace Catch {
 
-    // use POSIX console terminal codes
+    // use POSIX/ ANSI console terminal codes
     // Implementation contributed by Adam Strzelecki (http://github.com/nanoant)
     // https://github.com/philsquared/Catch/pull/131
 
@@ -4875,40 +4877,46 @@ namespace Catch {
         set( TextColour::None );
     }
 
+    namespace { const char colourEscape = '\033'; }
+
     void TextColour::set( Colours colour ) {
         if( isatty( fileno(stdout) ) ) {
             switch( colour ) {
                 case TextColour::FileName:
-                    std::cout << "\e[1m";    // bold
+                    std::cout << colourEscape << "[0m";    // white
                     break;
                 case TextColour::ResultError:
-                    std::cout << "\e[1;31m"; // bright red
+                    std::cout << colourEscape << "[1;31m"; // bold red
                     break;
                 case TextColour::ResultSuccess:
-                    std::cout << "\e[1;32m"; // bright green
+                    std::cout << colourEscape << "[1;32m"; // bold green
                     break;
                 case TextColour::Error:
-                    std::cout << "\e[0;31m"; // dark red
+                    std::cout << colourEscape << "[0;31m"; // red
                     break;
                 case TextColour::Success:
-                    std::cout << "\e[0;32m"; // dark green
+                    std::cout << colourEscape << "[0;32m"; // green
                     break;
                 case TextColour::OriginalExpression:
-                    std::cout << "\e[0;36m"; // cyan
+                    std::cout << colourEscape << "[0;36m"; // cyan
                     break;
                 case TextColour::ReconstructedExpression:
-                    std::cout << "\e[0;33m"; // yellow
+                    std::cout << colourEscape << "[0;33m"; // yellow
                     break;
                 case TextColour::None:
-                    std::cout << "\e[0m"; // reset
+                    std::cout << colourEscape << "[0m"; // reset to white
             }
         }
     }
 
+} // namespace Catch
+
 #elif defined ( CATCH_PLATFORM_WINDOWS )
 
 #include <windows.h>
 
+namespace Catch {
+
     namespace {
 
         WORD mapConsoleColour( TextColour::Colours colour ) {
@@ -4972,16 +4980,20 @@ namespace Catch {
         m_impl->set( colour );
     }
 
+} // end namespace Catch
+
 #else
 
+namespace Catch {
+
     TextColour::TextColour( Colours ){}
     TextColour::~TextColour(){}
     void TextColour::set( Colours ){}
 
-#endif
-
 } // end namespace Catch
 
+#endif
+
 // #included from: catch_generators_impl.hpp
 #define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED
 
@@ -5502,35 +5514,41 @@ namespace Catch {
             }
             switch( assertionResult.getResultType() ) {
                 case ResultWas::ThrewException:
-                {
-                    TextColour colour( TextColour::Error );
-                    if( assertionResult.hasExpression() )
-                        m_config.stream << " with unexpected";
-                    else
-                        m_config.stream << "Unexpected";
-                    m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'";
-                }
+                    {
+                        TextColour colour( TextColour::Error );
+                        if( assertionResult.hasExpression() )
+                            m_config.stream << " with unexpected";
+                        else
+                            m_config.stream << "Unexpected";
+                        m_config.stream << " exception with message: '" << assertionResult.getMessage() << "'";
+                    }
                     break;
                 case ResultWas::DidntThrowException:
-                {
-                    TextColour colour( TextColour::Error );
-                    if( assertionResult.hasExpression() )
-                        m_config.stream << " because no exception was thrown where one was expected";
-                    else
-                        m_config.stream << "No exception thrown where one was expected";
-                }
+                    {
+                        TextColour colour( TextColour::Error );
+                        if( assertionResult.hasExpression() )
+                            m_config.stream << " because no exception was thrown where one was expected";
+                        else
+                            m_config.stream << "No exception thrown where one was expected";
+                    }
                     break;
                 case ResultWas::Info:
-                    streamVariableLengthText( "info", assertionResult.getMessage() );
+                    {
+                        TextColour colour( TextColour::ReconstructedExpression );
+                        streamVariableLengthText( "info", assertionResult.getMessage() );
+                    }
                     break;
                 case ResultWas::Warning:
-                    streamVariableLengthText( "warning", assertionResult.getMessage() );
+                    {
+                        TextColour colour( TextColour::ReconstructedExpression );
+                        streamVariableLengthText( "warning", assertionResult.getMessage() );
+                    }
                     break;
                 case ResultWas::ExplicitFailure:
-                {
-                    TextColour colour( TextColour::Error );
-                    m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'";
-                }
+                    {
+                        TextColour colour( TextColour::Error );
+                        m_config.stream << "failed with message: '" << assertionResult.getMessage() << "'";
+                    }
                     break;
                 case ResultWas::Unknown: // These cases are here to prevent compiler warnings
                 case ResultWas::Ok: