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

Fixed line wrap bug

Added line wrap tests and modified wrap algo to wrap within words using a -
Commit
2927c240a13326a978667f9ff5c87ec461089bdc
Author
Phil Nash ·
Parents
29426b6359af

Files · Download .tar.gz

Changes

 include/internal/catch_line_wrap.hpp | 10 ++++++++--
 projects/SelfTest/TestMain.cpp       | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/include/internal/catch_line_wrap.hpp b/include/internal/catch_line_wrap.hpp
index c6e03d20..8a734fd8 100644
--- a/include/internal/catch_line_wrap.hpp
+++ b/include/internal/catch_line_wrap.hpp
@@ -25,8 +25,14 @@ namespace Catch {
             for( std::size_t pos = 0; pos < paragraph.size(); ++pos ) {
                 if( pos == width ) {
                     addIndent( os, indent );
-                    os << paragraph.substr( 0, wrapPoint ) << "\n";
-                    return recursivelyWrapLine( os, paragraph.substr( wrapPoint+1 ), columns, indent+tab );
+                    if( paragraph[wrapPoint] == ' ' ) {
+                        os << paragraph.substr( 0, wrapPoint ) << "\n";
+                        while( paragraph[++wrapPoint] == ' ' );
+                    }
+                    else {
+                        os << paragraph.substr( 0, --wrapPoint ) << "-\n";
+                    }
+                    return recursivelyWrapLine( os, paragraph.substr( wrapPoint ), columns, indent+tab );
                 }
                 if( paragraph[pos] == '\t' ) {
                     tab = pos;
diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp
index e6c3830b..45e002a8 100644
--- a/projects/SelfTest/TestMain.cpp
+++ b/projects/SelfTest/TestMain.cpp
@@ -10,6 +10,7 @@
 #endif
 
 #include "catch_self_test.hpp"
+#include "catch_line_wrap.h"
 
 TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" ) {
     using namespace Catch;
@@ -422,5 +423,37 @@ TEST_CASE( "selftest/tags", "" ) {
         CHECK( oneTag.matchesTags( "~[hide]" ) == false );
 
     }
+}
+
+TEST_CASE( "Long strings can be wrapped", "[wrap]" ) {
+    // guide:                 123456789012345678
+    std::string testString = "one two three four";
     
+    SECTION( "No wrapping", "" ) {
+        CHECK( Catch::wrapLongStrings( testString, 80, 0 ) == testString );
+        CHECK( Catch::wrapLongStrings( testString, 18, 0 ) == testString );
+    }
+    SECTION( "Wrapped once", "" ) {
+        CHECK( Catch::wrapLongStrings( testString, 17, 0 ) == "one two three\nfour" );
+        CHECK( Catch::wrapLongStrings( testString, 16, 0 ) == "one two three\nfour" );
+        CHECK( Catch::wrapLongStrings( testString, 15, 0 ) == "one two three\nfour" );
+        CHECK( Catch::wrapLongStrings( testString, 14, 0 ) == "one two three\nfour" );
+        CHECK( Catch::wrapLongStrings( testString, 13, 0 ) == "one two\nthree four" );
+    }
+    SECTION( "Wrapped twice", "" ) {
+        CHECK( Catch::wrapLongStrings( testString, 9, 0 ) == "one two\nthree\nfour" );
+        CHECK( Catch::wrapLongStrings( testString, 8, 0 ) == "one two\nthree\nfour" );
+    }
+    SECTION( "Wrapped three times", "" ) {
+        CHECK( Catch::wrapLongStrings( testString, 7, 0 ) == "one\ntwo\nthree\nfour" );
+        CHECK( Catch::wrapLongStrings( testString, 5, 0 ) == "one\ntwo\nthree\nfour" );
+    }
+    SECTION( "Short wrap", "" ) {
+        CHECK( Catch::wrapLongStrings( "abcdef", 4, 0 ) == "abc-\ndef" );
+        CHECK( Catch::wrapLongStrings( "abcdefg", 4, 0 ) == "abc-\ndefg" );
+        CHECK( Catch::wrapLongStrings( "abcdefgh", 4, 0 ) == "abc-\ndef-\ngh" );
+
+        CHECK( Catch::wrapLongStrings( testString, 4, 0 ) == "one\ntwo\nthr-\nee\nfour" );
+        CHECK( Catch::wrapLongStrings( testString, 3, 0 ) == "one\ntwo\nth-\nree\nfo-\nur" );
+    }
 }