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 replace(inPlace) function

and added tests (should have done that in the first place - I'll never learn!)
Commit
458b3ae257d46cecd8bc555a9ea59284997f12e4
Author
Phil Nash ·
Parents
db0421e840a0

Files · Download .tar.gz

Changes

 include/internal/catch_common.h               |  1 +
 include/internal/catch_common.hpp             | 16 ++++++++++++++-
 include/reporters/catch_reporter_teamcity.hpp | 20 ++++++-------------
 projects/SelfTest/TestMain.cpp                | 28 +++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 15 deletions(-)

diff --git a/include/internal/catch_common.h b/include/internal/catch_common.h
index 2001c297..68d23688 100644
--- a/include/internal/catch_common.h
+++ b/include/internal/catch_common.h
@@ -71,6 +71,7 @@ namespace Catch {
     void toLowerInPlace( std::string& s );
     std::string toLower( std::string const& s );
     std::string trim( std::string const& str );
+    bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis );
 
     struct pluralise {
         pluralise( std::size_t count, std::string const& label );
diff --git a/include/internal/catch_common.hpp b/include/internal/catch_common.hpp
index 6147aa5f..29e9ed5a 100644
--- a/include/internal/catch_common.hpp
+++ b/include/internal/catch_common.hpp
@@ -36,7 +36,21 @@ namespace Catch {
 
         return start != std::string::npos ? str.substr( start, 1+end-start ) : "";
     }
-
+    
+    bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
+        bool replaced = false;
+        std::size_t i = str.find( replaceThis );
+        while( i != std::string::npos ) {
+            replaced = true;
+            str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
+            if( i < str.size()-withThis.size() )
+                i = str.find( replaceThis, i+withThis.size() );
+            else
+                i = std::string::npos;
+        }
+        return replaced;
+    }
+    
     pluralise::pluralise( std::size_t count, std::string const& label )
     :   m_count( count ),
         m_label( label )
diff --git a/include/reporters/catch_reporter_teamcity.hpp b/include/reporters/catch_reporter_teamcity.hpp
index 127ad7b6..2de57d6a 100644
--- a/include/reporters/catch_reporter_teamcity.hpp
+++ b/include/reporters/catch_reporter_teamcity.hpp
@@ -21,22 +21,14 @@ namespace Catch {
         :   StreamingReporterBase( _config )
         {}
         
-        static bool replace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
-            std::size_t i = str.find( replaceThis );
-            if( i != std::string::npos ) {
-                str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
-                return true;
-            }
-            return false;
-        }
         static std::string escape( std::string const& str ) {
             std::string escaped = str;
-            while(  replace( escaped, "\'", "|\'" ) ||
-                    replace( escaped, "\n", "|n" ) ||
-                    replace( escaped, "\r", "|r" ) ||
-                    replace( escaped, "|", "||" ) ||
-                    replace( escaped, "[", "|[" ) ||
-                    replace( escaped, "]", "|]" ) );
+            replaceInPlace( escaped, "\'", "|\'" );
+            replaceInPlace( escaped, "\n", "|n" );
+            replaceInPlace( escaped, "\r", "|r" );
+            replaceInPlace( escaped, "|", "||" );
+            replaceInPlace( escaped, "[", "|[" );
+            replaceInPlace( escaped, "]", "|]" );
             return escaped;
         }
         virtual ~TeamCityReporter();
diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp
index 69176788..154a2994 100644
--- a/projects/SelfTest/TestMain.cpp
+++ b/projects/SelfTest/TestMain.cpp
@@ -348,6 +348,34 @@ private:
     std::vector<ColourIndex> colours;
 };
 
+TEST_CASE( "replaceInPlace", "" ) {
+    std::string letters = "abcdefcg";
+    SECTION( "replace single char" ) {
+        CHECK( replaceInPlace( letters, "b", "z" ) );
+        CHECK( letters == "azcdefcg" );
+    }
+    SECTION( "replace two chars" ) {
+        CHECK( replaceInPlace( letters, "c", "z" ) );
+        CHECK( letters == "abzdefzg" );
+    }
+    SECTION( "replace first char" ) {
+        CHECK( replaceInPlace( letters, "a", "z" ) );
+        CHECK( letters == "zbcdefcg" );
+    }
+    SECTION( "replace last char" ) {
+        CHECK( replaceInPlace( letters, "g", "z" ) );
+        CHECK( letters == "abcdefcz" );
+    }
+    SECTION( "replace all chars" ) {
+        CHECK( replaceInPlace( letters, letters, "replaced" ) );
+        CHECK( letters == "replaced" );
+    }
+    SECTION( "replace no chars" ) {
+        CHECK_FALSE( replaceInPlace( letters, "x", "z" ) );
+        CHECK( letters == letters );
+    }
+}
+
 // !TBD: This will be folded into Text class
 TEST_CASE( "Strings can be rendered with colour", "[.colour]" ) {