Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Added special handling for vector<bool> when stringifying

Commit
702cfdaf6e3e79f4485190d7f3b4beb2d12ac854
Author
Phil Nash ·
Parents
e41e8e8384c4

Files · Download .tar.gz

Changes

 include/internal/catch_tostring.h                  | 25 ++++++++++++++++++++--
 .../SelfTest/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<typename T>
     struct EnumStringMaker {
         static std::string convert(const T& t) {
@@ -355,10 +354,32 @@ namespace Catch {
             !std::is_same<decltype(end(std::declval<T>())), not_this_one>::value;
     };
 
+    template<typename Range>
+    std::string rangeToString( Range const& range ) {
+        return ::Catch::Detail::rangeToString( begin( range ), end( range ) );
+    }
+
+    // Handle vector<bool> specially
+    template<typename Allocator>
+    std::string rangeToString( std::vector<bool, Allocator> 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<typename R>
     struct StringMaker<R, typename std::enable_if<is_range<R>::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<vec<string,alloc>> -> 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<bool> -> toString", "[toString][containers][vector]" ) {
+    std::vector<bool> 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 }");
+}