From 7059b2cdac3c049404775f6542349af8b2fd837a Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 22 Apr 2014 18:23:42 +0100 Subject: [PATCH] =?UTF-8?q?Added=20ability=20to=20show=20=E2=80=9Cinvisibl?= =?UTF-8?q?es=E2=80=9D=20in=20strings=20(just=20tabs=20and=20newline=20cha?= =?UTF-8?q?rs,=20for=20now).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 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 #include @@ -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( value[i] <= 0xff ? value[i] : '?'); - oss << "\""; - return oss.str(); + s += value[i] <= 0xff ? static_cast( value[i] ) : '?'; + return toString( s ); } inline std::string toString( const char* const value ) {