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

Added ability to show “invisibles” in strings (just tabs and newline chars, for now).

Commit
7059b2cdac3c049404775f6542349af8b2fd837a
Author
Phil Nash ·
Parents
f21919419924

Files · Download .tar.gz

Changes

 include/internal/catch_commandline.hpp     |  4 ++++
 include/internal/catch_config.hpp          |  3 +++
 include/internal/catch_interfaces_config.h |  1 +
 include/internal/catch_tostring.hpp        | 28 ++++++++++++++++++++++------
 4 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/include/internal/catch_commandline.hpp b/include/internal/catch_commandline.hpp
index 85a0cabd..2614e4d5 100644
--- a/include/internal/catch_commandline.hpp
+++ b/include/internal/catch_commandline.hpp
@@ -84,6 +84,10 @@ namespace Catch {
             .describe( "skip exception tests" )
             .bind( &ConfigData::noThrow );
 
+        cli["-i"]["--invisibles"]
+            .describe( "show invisibles (tabs, newlines)" )
+            .bind( &ConfigData::showInvisibles );
+
         cli["-o"]["--out"]
             .describe( "output filename" )
             .bind( &ConfigData::outputFilename, "filename" );
diff --git a/include/internal/catch_config.hpp b/include/internal/catch_config.hpp
index 30cd34b9..dccec273 100644
--- a/include/internal/catch_config.hpp
+++ b/include/internal/catch_config.hpp
@@ -35,6 +35,7 @@ namespace Catch {
             shouldDebugBreak( false ),
             noThrow( false ),
             showHelp( false ),
+            showInvisibles( false ),
             abortAfter( -1 ),
             verbosity( Verbosity::Normal ),
             warnings( WarnAbout::Nothing ),
@@ -50,6 +51,7 @@ namespace Catch {
         bool shouldDebugBreak;
         bool noThrow;
         bool showHelp;
+        bool showInvisibles;
 
         int abortAfter;
 
@@ -154,6 +156,7 @@ namespace Catch {
         }
 
         bool showHelp() const { return m_data.showHelp; }
+        bool showInvisibles() const { return m_data.showInvisibles; }
 
         // IConfig interface
         virtual bool allowThrows() const        { return !m_data.noThrow; }
diff --git a/include/internal/catch_interfaces_config.h b/include/internal/catch_interfaces_config.h
index 0c7c3b28..32936591 100644
--- a/include/internal/catch_interfaces_config.h
+++ b/include/internal/catch_interfaces_config.h
@@ -46,6 +46,7 @@ namespace Catch {
         virtual bool shouldDebugBreak() const = 0;
         virtual bool warnAboutMissingAssertions() const = 0;
         virtual int abortAfter() const = 0;
+        virtual bool showInvisibles() const = 0;
         virtual ShowDurations::OrNot showDurations() const = 0;
         virtual std::vector<TestCaseFilters> const& filters() const = 0;
     };
diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp
index 788850ff..85bfa740 100644
--- a/include/internal/catch_tostring.hpp
+++ b/include/internal/catch_tostring.hpp
@@ -10,6 +10,7 @@
 
 #include "catch_common.h"
 #include "catch_sfinae.hpp"
+#include "catch_interfaces_config.h"
 
 #include <sstream>
 #include <iomanip>
@@ -180,16 +181,31 @@ std::string toString( T const& value ) {
 // Built in overloads
 
 inline std::string toString( std::string const& value ) {
-    return "\"" + value + "\"";
+    std::string s = value;
+    if( getCurrentContext().getConfig()->showInvisibles() ) {
+        for(size_t i = 0; i < s.size(); ++i ) {
+            std::string subs;
+            switch( s[i] ) {
+            case '\n': subs = "\\n"; break;
+            case '\t': subs = "\\t"; break;
+            default: break;
+            }
+            if( !subs.empty() ) {
+                s = s.substr( 0, i ) + subs + s.substr( i+1 );
+                ++i;
+            }
+        }
+    }
+    return "\"" + s + "\"";
 }
 
 inline std::string toString( std::wstring const& value ) {
-    std::ostringstream oss;
-    oss << "\"";
+
+    std::string s;
+    s.reserve( value.size() );
     for(size_t i = 0; i < value.size(); ++i )
-        oss << static_cast<char>( value[i] <= 0xff ? value[i] : '?');
-    oss << "\"";
-    return oss.str();
+        s += value[i] <= 0xff ? static_cast<char>( value[i] ) : '?';
+    return toString( s );
 }
 
 inline std::string toString( const char* const value ) {