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

This commit is contained in:
Phil Nash 2014-04-22 18:23:42 +01:00
parent f219194199
commit 7059b2cdac
4 changed files with 30 additions and 6 deletions

View File

@ -84,6 +84,10 @@ namespace Catch {
.describe( "skip exception tests" ) .describe( "skip exception tests" )
.bind( &ConfigData::noThrow ); .bind( &ConfigData::noThrow );
cli["-i"]["--invisibles"]
.describe( "show invisibles (tabs, newlines)" )
.bind( &ConfigData::showInvisibles );
cli["-o"]["--out"] cli["-o"]["--out"]
.describe( "output filename" ) .describe( "output filename" )
.bind( &ConfigData::outputFilename, "filename" ); .bind( &ConfigData::outputFilename, "filename" );

View File

@ -35,6 +35,7 @@ namespace Catch {
shouldDebugBreak( false ), shouldDebugBreak( false ),
noThrow( false ), noThrow( false ),
showHelp( false ), showHelp( false ),
showInvisibles( false ),
abortAfter( -1 ), abortAfter( -1 ),
verbosity( Verbosity::Normal ), verbosity( Verbosity::Normal ),
warnings( WarnAbout::Nothing ), warnings( WarnAbout::Nothing ),
@ -50,6 +51,7 @@ namespace Catch {
bool shouldDebugBreak; bool shouldDebugBreak;
bool noThrow; bool noThrow;
bool showHelp; bool showHelp;
bool showInvisibles;
int abortAfter; int abortAfter;
@ -154,6 +156,7 @@ namespace Catch {
} }
bool showHelp() const { return m_data.showHelp; } bool showHelp() const { return m_data.showHelp; }
bool showInvisibles() const { return m_data.showInvisibles; }
// IConfig interface // IConfig interface
virtual bool allowThrows() const { return !m_data.noThrow; } virtual bool allowThrows() const { return !m_data.noThrow; }

View File

@ -46,6 +46,7 @@ namespace Catch {
virtual bool shouldDebugBreak() const = 0; virtual bool shouldDebugBreak() const = 0;
virtual bool warnAboutMissingAssertions() const = 0; virtual bool warnAboutMissingAssertions() const = 0;
virtual int abortAfter() const = 0; virtual int abortAfter() const = 0;
virtual bool showInvisibles() const = 0;
virtual ShowDurations::OrNot showDurations() const = 0; virtual ShowDurations::OrNot showDurations() const = 0;
virtual std::vector<TestCaseFilters> const& filters() const = 0; virtual std::vector<TestCaseFilters> const& filters() const = 0;
}; };

View File

@ -10,6 +10,7 @@
#include "catch_common.h" #include "catch_common.h"
#include "catch_sfinae.hpp" #include "catch_sfinae.hpp"
#include "catch_interfaces_config.h"
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
@ -180,16 +181,31 @@ std::string toString( T const& value ) {
// Built in overloads // Built in overloads
inline std::string toString( std::string const& value ) { 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 ) { 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 ) for(size_t i = 0; i < value.size(); ++i )
oss << static_cast<char>( value[i] <= 0xff ? value[i] : '?'); s += value[i] <= 0xff ? static_cast<char>( value[i] ) : '?';
oss << "\""; return toString( s );
return oss.str();
} }
inline std::string toString( const char* const value ) { inline std::string toString( const char* const value ) {