From 0dbcf218c353e801d4fa26d14696e81f6b832cad Mon Sep 17 00:00:00 2001 From: Andy Sawyer Date: Tue, 17 Sep 2013 22:22:47 +0100 Subject: [PATCH 1/3] Add allocator support to StringMaker - also extracted out 'rangeToString', in an attempt to make it easier to add support for other containers --- include/internal/catch_tostring.hpp | 22 +++++++++++++++------- single_include/catch.hpp | 24 ++++++++++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index 78c725ec..48bc6830 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -99,19 +99,27 @@ struct StringMaker { } }; -template -struct StringMaker > { - static std::string convert( std::vector const& v ) { +namespace Detail { + template + std::string rangeToString( InputIterator first, InputIterator last ) { std::ostringstream oss; oss << "{ "; - for( std::size_t i = 0; i < v.size(); ++ i ) { - oss << toString( v[i] ); - if( i < v.size() - 1 ) - oss << ", "; + if( first != last ) { + oss << toString( *first ); + for( ++first ; first != last ; ++first ) { + oss << ", " << toString( *first ); + } } oss << " }"; return oss.str(); } +} + +template +struct StringMaker > { + static std::string convert( std::vector const& v ) { + return Detail::rangeToString( v.begin(), v.end() ); + } }; namespace Detail { diff --git a/single_include/catch.hpp b/single_include/catch.hpp index 523fa088..00dd9fc5 100644 --- a/single_include/catch.hpp +++ b/single_include/catch.hpp @@ -1,6 +1,6 @@ /* * CATCH v1.0 build 10 (master branch) - * Generated: 2013-09-14 19:56:34.776409 + * Generated: 2013-09-17 22:21:47.780755 * ---------------------------------------------------------- * This file has been merged from multiple headers. Please don't edit it directly * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. @@ -722,19 +722,27 @@ struct StringMaker { } }; -template -struct StringMaker > { - static std::string convert( std::vector const& v ) { +namespace Detail { + template + std::string rangeToString( InputIterator first, InputIterator last ) { std::ostringstream oss; oss << "{ "; - for( std::size_t i = 0; i < v.size(); ++ i ) { - oss << toString( v[i] ); - if( i < v.size() - 1 ) - oss << ", "; + if( first != last ) { + oss << toString( *first ); + for( ++first ; first != last ; ++first ) { + oss << ", " << toString( *first ); + } } oss << " }"; return oss.str(); } +} + +template +struct StringMaker > { + static std::string convert( std::vector const& v ) { + return Detail::rangeToString( v.begin(), v.end() ); + } }; namespace Detail { From d6f23a9a36cf008b870a2d7c51e422dc209d3241 Mon Sep 17 00:00:00 2001 From: Andy Sawyer Date: Sat, 21 Sep 2013 18:45:42 +0100 Subject: [PATCH 2/3] catch_tostring : moved defintion of rangeToString Detail::rangeToString is now defined after the various toString overloads. This results in them being accessible with rangeToString is instantiated (in this case, by StringMaker). This (sort-of) fixes the problem where contained types are toString'd incorrectly. Consider: std::vector v { "abc" }; Before: Catch::toString( v ) == "{ abc }" After: Catch::toString( v ) == "{ "abc" }" (note the extra pair of quotes around the "abc" - these are added by Catch::toString( std::string ) which is now called by rangeToString) --- include/internal/catch_tostring.hpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index 48bc6830..d5714e8d 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -101,18 +101,7 @@ struct StringMaker { namespace Detail { template - std::string rangeToString( InputIterator first, InputIterator last ) { - std::ostringstream oss; - oss << "{ "; - if( first != last ) { - oss << toString( *first ); - for( ++first ; first != last ; ++first ) { - oss << ", " << toString( *first ); - } - } - oss << " }"; - return oss.str(); - } + std::string rangeToString( InputIterator first, InputIterator last ); } template @@ -238,6 +227,22 @@ inline std::string toString( std::nullptr_t ) { } #endif + namespace Detail { + template + std::string rangeToString( InputIterator first, InputIterator last ) { + std::ostringstream oss; + oss << "{ "; + if( first != last ) { + oss << toString( *first ); + for( ++first ; first != last ; ++first ) { + oss << ", " << toString( *first ); + } + } + oss << " }"; + return oss.str(); + } +} + } // end namespace Catch #endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED From 7974e1cb557e75e81f85bf3160b30d312f59089f Mon Sep 17 00:00:00 2001 From: Andy Sawyer Date: Sat, 21 Sep 2013 19:08:23 +0100 Subject: [PATCH 3/3] updated single-include version --- single_include/catch.hpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/single_include/catch.hpp b/single_include/catch.hpp index 00dd9fc5..dba2e304 100644 --- a/single_include/catch.hpp +++ b/single_include/catch.hpp @@ -1,6 +1,6 @@ /* * CATCH v1.0 build 10 (master branch) - * Generated: 2013-09-17 22:21:47.780755 + * Generated: 2013-09-21 19:07:52.759646 * ---------------------------------------------------------- * This file has been merged from multiple headers. Please don't edit it directly * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. @@ -724,18 +724,7 @@ struct StringMaker { namespace Detail { template - std::string rangeToString( InputIterator first, InputIterator last ) { - std::ostringstream oss; - oss << "{ "; - if( first != last ) { - oss << toString( *first ); - for( ++first ; first != last ; ++first ) { - oss << ", " << toString( *first ); - } - } - oss << " }"; - return oss.str(); - } + std::string rangeToString( InputIterator first, InputIterator last ); } template @@ -861,6 +850,22 @@ inline std::string toString( std::nullptr_t ) { } #endif + namespace Detail { + template + std::string rangeToString( InputIterator first, InputIterator last ) { + std::ostringstream oss; + oss << "{ "; + if( first != last ) { + oss << toString( *first ); + for( ++first ; first != last ; ++first ) { + oss << ", " << toString( *first ); + } + } + oss << " }"; + return oss.str(); + } +} + } // end namespace Catch // #included from: catch_assertionresult.h