From 702cfdaf6e3e79f4485190d7f3b4beb2d12ac854 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Thu, 4 Jan 2018 10:05:02 +0000 Subject: [PATCH] Added special handling for vector when stringifying --- include/internal/catch_tostring.h | 25 +++++++++++++++++-- .../UsageTests/ToStringVector.tests.cpp | 10 ++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h index 92161709..2e7091ad 100644 --- a/include/internal/catch_tostring.h +++ b/include/internal/catch_tostring.h @@ -233,7 +233,6 @@ namespace Catch { } } - template struct EnumStringMaker { static std::string convert(const T& t) { @@ -355,10 +354,32 @@ namespace Catch { !std::is_same())), not_this_one>::value; }; + template + std::string rangeToString( Range const& range ) { + return ::Catch::Detail::rangeToString( begin( range ), end( range ) ); + } + + // Handle vector specially + template + std::string rangeToString( std::vector const& v ) { + ReusableStringStream rss; + rss << "{ "; + bool first = true; + for( bool b : v ) { + if( first ) + first = false; + else + rss << ", "; + rss << ::Catch::Detail::stringify( b ); + } + rss << " }"; + return rss.str(); + } + template struct StringMaker::value>::type> { static std::string convert( R const& range ) { - return ::Catch::Detail::rangeToString( begin( range ), end( range ) ); + return rangeToString( range ); } }; diff --git a/projects/SelfTest/UsageTests/ToStringVector.tests.cpp b/projects/SelfTest/UsageTests/ToStringVector.tests.cpp index df9c55ab..0b993c4e 100644 --- a/projects/SelfTest/UsageTests/ToStringVector.tests.cpp +++ b/projects/SelfTest/UsageTests/ToStringVector.tests.cpp @@ -66,3 +66,13 @@ TEST_CASE( "vec> -> toString", "[toString][vector,allocator]" v.push_back( inner { "world" } ); REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" ); } + +// Based on PR by mat-so: https://github.com/catchorg/Catch2/pull/606/files#diff-43562f40f8c6dcfe2c54557316e0f852 +TEST_CASE( "vector -> toString", "[toString][containers][vector]" ) { + std::vector bools; + REQUIRE( ::Catch::Detail::stringify(bools) == "{ }"); + bools.push_back(true); + REQUIRE( ::Catch::Detail::stringify(bools) == "{ true }"); + bools.push_back(false); + REQUIRE( ::Catch::Detail::stringify(bools) == "{ true, false }"); +}