From aef6cd53320cd209f90495073b9afd447ed4d89a Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Wed, 23 Apr 2014 07:10:10 +0100 Subject: [PATCH] build 40 - Fixed endianness when converting numbers to hex strings - Added option to show invisibles (/t, /n) in printed strings with -i - moved toString() impls to impl file - avoid allocations for static strings used in reporter --- README.md | 2 +- include/internal/catch_version.hpp | 2 +- single_include/catch.hpp | 304 +++++++++++++++++------------ 3 files changed, 186 insertions(+), 122 deletions(-) diff --git a/README.md b/README.md index 3916e0a4..df823885 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![catch logo](catch-logo-small.png) -*v1.0 build 39 (master branch)* +*v1.0 build 40 (master branch)* Build status (on Travis CI) [![Build Status](https://travis-ci.org/philsquared/Catch.png)](https://travis-ci.org/philsquared/Catch) diff --git a/include/internal/catch_version.hpp b/include/internal/catch_version.hpp index 652e7f4e..afa50b04 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( 1, 0, 39, "master" ); + Version libraryVersion( 1, 0, 40, "master" ); } #endif // TWOBLUECUBES_CATCH_VERSION_HPP_INCLUDED diff --git a/single_include/catch.hpp b/single_include/catch.hpp index 57cbca0c..aa752e98 100644 --- a/single_include/catch.hpp +++ b/single_include/catch.hpp @@ -1,6 +1,6 @@ /* - * CATCH v1.0 build 39 (master branch) - * Generated: 2014-04-21 18:50:19.658444 + * CATCH v1.0 build 40 (master branch) + * Generated: 2014-04-23 07:07:39.268798 * ---------------------------------------------------------- * This file has been merged from multiple headers. Please don't edit it directly * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. @@ -576,8 +576,8 @@ private: // #included from: catch_expressionresult_builder.h #define TWOBLUECUBES_CATCH_ASSERTIONRESULT_BUILDER_H_INCLUDED -// #included from: catch_tostring.hpp -#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED +// #included from: catch_tostring.h +#define TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED // #included from: catch_sfinae.hpp #define TWOBLUECUBES_CATCH_SFINAE_HPP_INCLUDED @@ -723,8 +723,21 @@ namespace Detail { } }; - // For display purposes only. - // Does not consider endian-ness + struct Endianness { + enum Arch { Big, Little }; + + static Arch which() { + union { + int asInt; + char asChar[sizeof (int)]; + }; + + asInt = 1; + return ( asChar[sizeof(int)-1] == 1 ) ? Big : Little; + } + }; + + // Writes the raw memory into a string, considering endianness template std::string rawMemoryToString( T value ) { union { @@ -736,8 +749,14 @@ namespace Detail { std::ostringstream oss; oss << "0x"; - for( unsigned char* cp = bytes; cp < bytes+sizeof(T); ++cp ) - oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)*cp; + + int i = 0, end = sizeof(T), inc = 1; + if( Endianness::which() == Endianness::Little ) { + i = end-1; + end = inc = -1; + } + for( ; i != end; i += inc ) + oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)bytes[i]; return oss.str(); } @@ -785,7 +804,7 @@ struct StringMaker > { namespace Detail { template - inline std::string makeString( T const& value ) { + std::string makeString( T const& value ) { return StringMaker::convert( value ); } } // end namespace Detail @@ -804,99 +823,27 @@ std::string toString( T const& value ) { // Built in overloads -inline std::string toString( std::string const& value ) { - return "\"" + value + "\""; -} - -inline std::string toString( std::wstring const& value ) { - std::ostringstream oss; - oss << "\""; - for(size_t i = 0; i < value.size(); ++i ) - oss << static_cast( value[i] <= 0xff ? value[i] : '?'); - oss << "\""; - return oss.str(); -} - -inline std::string toString( const char* const value ) { - return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" ); -} - -inline std::string toString( char* const value ) { - return Catch::toString( static_cast( value ) ); -} - -inline std::string toString( int value ) { - std::ostringstream oss; - oss << value; - return oss.str(); -} - -inline std::string toString( unsigned long value ) { - std::ostringstream oss; - if( value > 8192 ) - oss << "0x" << std::hex << value; - else - oss << value; - return oss.str(); -} - -inline std::string toString( unsigned int value ) { - return toString( static_cast( value ) ); -} - -inline std::string toString( const double value ) { - std::ostringstream oss; - oss << std::setprecision( 10 ) - << std::fixed - << value; - std::string d = oss.str(); - std::size_t i = d.find_last_not_of( '0' ); - if( i != std::string::npos && i != d.size()-1 ) { - if( d[i] == '.' ) - i++; - d = d.substr( 0, i+1 ); - } - return d; -} - -inline std::string toString( bool value ) { - return value ? "true" : "false"; -} - -inline std::string toString( char value ) { - return value < ' ' - ? toString( static_cast( value ) ) - : Detail::makeString( value ); -} - -inline std::string toString( signed char value ) { - return toString( static_cast( value ) ); -} - -inline std::string toString( unsigned char value ) { - return toString( static_cast( value ) ); -} +std::string toString( std::string const& value ); +std::string toString( std::wstring const& value ); +std::string toString( const char* const value ); +std::string toString( char* const value ); +std::string toString( int value ); +std::string toString( unsigned long value ); +std::string toString( unsigned int value ); +std::string toString( const double value ); +std::string toString( bool value ); +std::string toString( char value ); +std::string toString( signed char value ); +std::string toString( unsigned char value ); #ifdef CATCH_CONFIG_CPP11_NULLPTR -inline std::string toString( std::nullptr_t ) { - return "nullptr"; -} +std::string toString( std::nullptr_t ); #endif #ifdef __OBJC__ - inline std::string toString( NSString const * const& nsstring ) { - if( !nsstring ) - return "nil"; - return std::string( "@\"" ) + [nsstring UTF8String] + "\""; - } - inline std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) { - if( !nsstring ) - return "nil"; - return std::string( "@\"" ) + [nsstring UTF8String] + "\""; - } - inline std::string toString( NSObject* const& nsObject ) { - return toString( [nsObject description] ); - } + std::string toString( NSString const * const& nsstring ); + std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ); + std::string toString( NSObject* const& nsObject ); #endif namespace Detail { @@ -1575,6 +1522,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; }; @@ -2925,6 +2873,7 @@ namespace Catch { shouldDebugBreak( false ), noThrow( false ), showHelp( false ), + showInvisibles( false ), abortAfter( -1 ), verbosity( Verbosity::Normal ), warnings( WarnAbout::Nothing ), @@ -2940,6 +2889,7 @@ namespace Catch { bool shouldDebugBreak; bool noThrow; bool showHelp; + bool showInvisibles; int abortAfter; @@ -3043,6 +2993,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; } @@ -4002,6 +3953,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" ); @@ -6697,7 +6652,7 @@ namespace Catch { namespace Catch { // These numbers are maintained by a script - Version libraryVersion( 1, 0, 39, "master" ); + Version libraryVersion( 1, 0, 40, "master" ); } // #included from: catch_message.hpp @@ -7096,6 +7051,122 @@ namespace Catch { } #endif // Platform +// #included from: catch_tostring.hpp +#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED + +namespace Catch { + +std::string toString( std::string const& 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 + "\""; +} +std::string toString( std::wstring const& value ) { + + std::string s; + s.reserve( value.size() ); + for(size_t i = 0; i < value.size(); ++i ) + s += value[i] <= 0xff ? static_cast( value[i] ) : '?'; + return toString( s ); +} + +std::string toString( const char* const value ) { + return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" ); +} + +std::string toString( char* const value ) { + return Catch::toString( static_cast( value ) ); +} + +std::string toString( int value ) { + std::ostringstream oss; + oss << value; + return oss.str(); +} + +std::string toString( unsigned long value ) { + std::ostringstream oss; + if( value > 8192 ) + oss << "0x" << std::hex << value; + else + oss << value; + return oss.str(); +} + +std::string toString( unsigned int value ) { + return toString( static_cast( value ) ); +} + +std::string toString( const double value ) { + std::ostringstream oss; + oss << std::setprecision( 10 ) + << std::fixed + << value; + std::string d = oss.str(); + std::size_t i = d.find_last_not_of( '0' ); + if( i != std::string::npos && i != d.size()-1 ) { + if( d[i] == '.' ) + i++; + d = d.substr( 0, i+1 ); + } + return d; +} + +std::string toString( bool value ) { + return value ? "true" : "false"; +} + +std::string toString( char value ) { + return value < ' ' + ? toString( static_cast( value ) ) + : Detail::makeString( value ); +} + +std::string toString( signed char value ) { + return toString( static_cast( value ) ); +} + +std::string toString( unsigned char value ) { + return toString( static_cast( value ) ); +} + +#ifdef CATCH_CONFIG_CPP11_NULLPTR +std::string toString( std::nullptr_t ) { + return "nullptr"; +} +#endif + +#ifdef __OBJC__ + std::string toString( NSString const * const& nsstring ) { + if( !nsstring ) + return "nil"; + return std::string( "@\"" ) + [nsstring UTF8String] + "\""; + } + std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) { + if( !nsstring ) + return "nil"; + return std::string( "@\"" ) + [nsstring UTF8String] + "\""; + } + std::string toString( NSObject* const& nsObject ) { + return toString( [nsObject description] ); + } +#endif + +} // end namespace Catch + // #included from: ../reporters/catch_reporter_xml.hpp #define TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED @@ -8156,7 +8227,7 @@ namespace Catch { m_atLeastOneTestCasePrinted = true; } void lazyPrintRunInfo() { - stream << "\n" << getTildes() << "\n"; + stream << "\n" << getLineOfChars<'~'>() << "\n"; Colour colour( Colour::SecondaryText ); stream << currentTestRunInfo->name << " is a Catch v" << libraryVersion.majorVersion << "." @@ -8192,19 +8263,19 @@ namespace Catch { SourceLineInfo lineInfo = m_sectionStack.front().lineInfo; if( !lineInfo.empty() ){ - stream << getDashes() << "\n"; + stream << getLineOfChars<'-'>() << "\n"; Colour colourGuard( Colour::FileName ); stream << lineInfo << "\n"; } - stream << getDots() << "\n" << std::endl; + stream << getLineOfChars<'.'>() << "\n" << std::endl; } void printClosedHeader( std::string const& _name ) { printOpenHeader( _name ); - stream << getDots() << "\n"; + stream << getLineOfChars<'.'>() << "\n"; } void printOpenHeader( std::string const& _name ) { - stream << getDashes() << "\n"; + stream << getLineOfChars<'-'>() << "\n"; { Colour colourGuard( Colour::Headers ); printHeaderString( _name ); @@ -8277,26 +8348,19 @@ namespace Catch { } void printTotalsDivider() { - stream << getDoubleDashes() << "\n"; + stream << getLineOfChars<'='>() << "\n"; } void printSummaryDivider() { - stream << getDashes() << "\n"; + stream << getLineOfChars<'-'>() << "\n"; } - static std::string const& getDashes() { - static const std::string dashes( CATCH_CONFIG_CONSOLE_WIDTH-1, '-' ); - return dashes; - } - static std::string const& getDots() { - static const std::string dots( CATCH_CONFIG_CONSOLE_WIDTH-1, '.' ); - return dots; - } - static std::string const& getDoubleDashes() { - static const std::string doubleDashes( CATCH_CONFIG_CONSOLE_WIDTH-1, '=' ); - return doubleDashes; - } - static std::string const& getTildes() { - static const std::string dots( CATCH_CONFIG_CONSOLE_WIDTH-1, '~' ); - return dots; + template + static char const* getLineOfChars() { + static char line[CATCH_CONFIG_CONSOLE_WIDTH] = {0}; + if( !*line ) { + memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 ); + line[CATCH_CONFIG_CONSOLE_WIDTH-1] = 0; + } + return line; } private: