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

Preserve empty and single-character fields when splitting strings (#3234)

* Preserve single-character fields when splitting strings

* Preserve empty fields when splitting strings

---------

Co-authored-by: astunm <astunm@users.noreply.github.com>
Upstream-commit: catchorg/Catch2@36c1bc3ef209a649741f36d9913d63451e8d07b5
Commit
f5760790f3453c88309a8f8daf8a8e4f84e5b078
Author
user ·
Committer
Leitwolf11
Parents
b080d91b9058

Files · Download .tar.gz

Changes

 src/Progmasoft/Catch3/Internal/StringManip.cpp          |  5 ++---
 tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp | 17 +++++++++++++++++
 tests/SelfTest/UsageTests/EnumToString.tests.cpp        | 16 ++++++++++++++++
 3 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/src/Progmasoft/Catch3/Internal/StringManip.cpp b/src/Progmasoft/Catch3/Internal/StringManip.cpp
index 34e4fbbc..70b260fb 100644
--- a/src/Progmasoft/Catch3/Internal/StringManip.cpp
+++ b/src/Progmasoft/Catch3/Internal/StringManip.cpp
@@ -92,12 +92,11 @@ namespace Catch {
         std::size_t start = 0;
         for(std::size_t pos = 0; pos < str.size(); ++pos ) {
             if( str[pos] == delimiter ) {
-                if( pos - start > 1 )
-                    subStrings.push_back( str.substr( start, pos-start ) );
+                subStrings.push_back( str.substr( start, pos-start ) );
                 start = pos+1;
             }
         }
-        if( start < str.size() )
+        if( !str.empty() )
             subStrings.push_back( str.substr( start, str.size()-start ) );
         return subStrings;
     }
diff --git a/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp b/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp
index f744ad9e..8264a560 100644
--- a/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp
+++ b/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp
@@ -85,6 +85,23 @@ TEST_CASE("splitString", "[string-manip]") {
     CHECK_THAT(splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"}));
 }
 
+TEST_CASE( "Splitting preserves empty and single-character fields",
+           "[string-manip][approvals]" ) {
+    using namespace Catch::Matchers;
+    using Catch::splitStringRef;
+    using Catch::StringRef;
+    CHECK_THAT( splitStringRef( "a,b,c", ',' ),
+                Equals( std::vector<StringRef>{ "a", "b", "c" } ) );
+    CHECK_THAT( splitStringRef( "a,", ',' ),
+                Equals( std::vector<StringRef>{ "a", "" } ) );
+    CHECK_THAT( splitStringRef( ",a,,b,", ',' ),
+                Equals( std::vector<StringRef>{ "", "a", "", "b", "" } ) );
+    CHECK_THAT( splitStringRef( "a,,,b", ',' ),
+                Equals( std::vector<StringRef>{ "a", "", "", "b" } ) );
+    CHECK_THAT( splitStringRef( ",", ',' ),
+                Equals( std::vector<StringRef>{ "", "" } ) );
+}
+
 TEST_CASE("startsWith", "[string-manip]") {
     using Catch::startsWith;
 
diff --git a/tests/SelfTest/UsageTests/EnumToString.tests.cpp b/tests/SelfTest/UsageTests/EnumToString.tests.cpp
index 4d259263..c1aea4da 100644
--- a/tests/SelfTest/UsageTests/EnumToString.tests.cpp
+++ b/tests/SelfTest/UsageTests/EnumToString.tests.cpp
@@ -163,3 +163,19 @@ TEST_CASE( "Enum backed by larger underlying type", "[enum][toString]" ) {
     REQUIRE( stringify( LargeEnum::Flag2 ) == "Flag2" );
     REQUIRE( stringify( LargeEnum::Flag5 ) == "Flag5" );
 }
+
+enum SingleCharacterEnum {
+    A,
+    B,
+    C
+};
+
+CATCH_REGISTER_ENUM( SingleCharacterEnum, A, B, C )
+
+TEST_CASE( "Single-character enum values can be registered",
+           "[enum][toString][approvals]" ) {
+    using Catch::Detail::stringify;
+    REQUIRE( stringify( A ) == "A" );
+    REQUIRE( stringify( B ) == "B" );
+    REQUIRE( stringify( C ) == "C" );
+}