diff --git a/src/catch2/internal/catch_string_manip.cpp b/src/catch2/internal/catch_string_manip.cpp index 0444cd38..b9d312e4 100644 --- a/src/catch2/internal/catch_string_manip.cpp +++ b/src/catch2/internal/catch_string_manip.cpp @@ -25,7 +25,7 @@ namespace Catch { bool startsWith( std::string const& s, std::string const& prefix ) { return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); } - bool startsWith( std::string const& s, char prefix ) { + bool startsWith( StringRef s, char prefix ) { return !s.empty() && s[0] == prefix; } bool endsWith( std::string const& s, std::string const& suffix ) { diff --git a/src/catch2/internal/catch_string_manip.hpp b/src/catch2/internal/catch_string_manip.hpp index bd7d0ddf..605d898c 100644 --- a/src/catch2/internal/catch_string_manip.hpp +++ b/src/catch2/internal/catch_string_manip.hpp @@ -17,7 +17,7 @@ namespace Catch { bool startsWith( std::string const& s, std::string const& prefix ); - bool startsWith( std::string const& s, char prefix ); + bool startsWith( StringRef s, char prefix ); bool endsWith( std::string const& s, std::string const& suffix ); bool endsWith( std::string const& s, char suffix ); bool contains( std::string const& s, std::string const& infix ); diff --git a/tests/SelfTest/Baselines/automake.sw.approved.txt b/tests/SelfTest/Baselines/automake.sw.approved.txt index 5d599b6e..7b401531 100644 --- a/tests/SelfTest/Baselines/automake.sw.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.approved.txt @@ -330,6 +330,7 @@ loose text artifact :test-result: PASS shortened hide tags are split apart :test-result: PASS splitString :test-result: FAIL stacks unscoped info in loops +:test-result: PASS startsWith :test-result: PASS std::map is convertible string :test-result: PASS std::pair -> toString :test-result: PASS std::pair -> toString diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index 556db261..1813ff3c 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -2224,6 +2224,9 @@ StringManip.tests.cpp:: passed: splitStringRef("abc", ','), Equals( StringManip.tests.cpp:: passed: splitStringRef("abc,def", ','), Equals(std::vector{"abc", "def"}) for: { abc, def } Equals: { abc, def } Message.tests.cpp:: failed: false with 4 messages: 'Count 1 to 3...' and '1' and '2' and '3' Message.tests.cpp:: failed: false with 4 messages: 'Count 4 to 6...' and '4' and '5' and '6' +StringManip.tests.cpp:: passed: !(startsWith("", 'c')) for: !false +StringManip.tests.cpp:: passed: startsWith(std::string("abc"), 'a') for: true +StringManip.tests.cpp:: passed: startsWith("def"_catch_sr, 'd') for: true ToStringGeneral.tests.cpp:: passed: Catch::Detail::stringify( emptyMap ) == "{ }" for: "{ }" == "{ }" ToStringGeneral.tests.cpp:: passed: Catch::Detail::stringify( map ) == "{ { \"one\", 1 } }" for: "{ { "one", 1 } }" == "{ { "one", 1 } }" ToStringGeneral.tests.cpp:: passed: Catch::Detail::stringify( map ) == "{ { \"abc\", 1 }, { \"def\", 2 }, { \"ghi\", 3 } }" for: "{ { "abc", 1 }, { "def", 2 }, { "ghi", 3 } }" diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index 8704f8b3..13b39435 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1386,6 +1386,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 368 | 292 passed | 70 failed | 6 failed as expected -assertions: 2103 | 1951 passed | 129 failed | 23 failed as expected +test cases: 369 | 293 passed | 70 failed | 6 failed as expected +assertions: 2106 | 1954 passed | 129 failed | 23 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 08cef9ac..ba1d14c9 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -15855,6 +15855,27 @@ with messages: 5 6 +------------------------------------------------------------------------------- +startsWith +------------------------------------------------------------------------------- +StringManip.tests.cpp: +............................................................................... + +StringManip.tests.cpp:: PASSED: + CHECK_FALSE( startsWith("", 'c') ) +with expansion: + !false + +StringManip.tests.cpp:: PASSED: + CHECK( startsWith(std::string("abc"), 'a') ) +with expansion: + true + +StringManip.tests.cpp:: PASSED: + CHECK( startsWith("def"_catch_sr, 'd') ) +with expansion: + true + ------------------------------------------------------------------------------- std::map is convertible string empty @@ -16933,6 +16954,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 368 | 276 passed | 86 failed | 6 failed as expected -assertions: 2120 | 1951 passed | 146 failed | 23 failed as expected +test cases: 369 | 277 passed | 86 failed | 6 failed as expected +assertions: 2123 | 1954 passed | 146 failed | 23 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index 88e96b0f..f6285201 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -1756,6 +1756,7 @@ Count 4 to 6... Message.tests.cpp: + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt index 924a14b1..c95948c3 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -215,6 +215,7 @@ + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index 724cd499..a82febac 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -4004,6 +4004,12 @@ ok {test-number} - splitStringRef("abc,def", ','), Equals(std::vector not ok {test-number} - false with 4 messages: 'Count 1 to 3...' and '1' and '2' and '3' # stacks unscoped info in loops not ok {test-number} - false with 4 messages: 'Count 4 to 6...' and '4' and '5' and '6' +# startsWith +ok {test-number} - !(startsWith("", 'c')) for: !false +# startsWith +ok {test-number} - startsWith(std::string("abc"), 'a') for: true +# startsWith +ok {test-number} - startsWith("def"_catch_sr, 'd') for: true # std::map is convertible string ok {test-number} - Catch::Detail::stringify( emptyMap ) == "{ }" for: "{ }" == "{ }" # std::map is convertible string @@ -4242,5 +4248,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2120 +1..2123 diff --git a/tests/SelfTest/Baselines/teamcity.sw.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.approved.txt index 5291c85d..3867ec4a 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.approved.txt @@ -803,6 +803,8 @@ Message.tests.cpp:|nexpression failed with messages:|n "hi"|n "i Message.tests.cpp:|nexpression failed with messages:|n "Count 1 to 3..."|n "1"|n "2"|n "3"|n CHECK( false )|nwith expansion:|n false|n'] Message.tests.cpp:|nexpression failed with messages:|n "Count 4 to 6..."|n "4"|n "5"|n "6"|n CHECK( false )|nwith expansion:|n false|n'] ##teamcity[testFinished name='stacks unscoped info in loops' duration="{duration}"] +##teamcity[testStarted name='startsWith'] +##teamcity[testFinished name='startsWith' duration="{duration}"] ##teamcity[testStarted name='std::map is convertible string'] ##teamcity[testFinished name='std::map is convertible string' duration="{duration}"] ##teamcity[testStarted name='std::pair -> toString'] diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index bd12e70a..917010be 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -18761,6 +18761,33 @@ loose text artifact + + + + !(startsWith("", 'c')) + + + !false + + + + + startsWith(std::string("abc"), 'a') + + + true + + + + + startsWith("def"_catch_sr, 'd') + + + true + + + +
@@ -19926,6 +19953,6 @@ loose text artifact
- - + + diff --git a/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp b/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp index d78567ca..ad274939 100644 --- a/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/StringManip.tests.cpp @@ -65,3 +65,11 @@ TEST_CASE("splitString", "[string-manip]") { CHECK_THAT(splitStringRef("abc", ','), Equals(std::vector{"abc"})); CHECK_THAT(splitStringRef("abc,def", ','), Equals(std::vector{"abc", "def"})); } + +TEST_CASE("startsWith", "[string-manip]") { + using Catch::startsWith; + + CHECK_FALSE(startsWith("", 'c')); + CHECK(startsWith(std::string("abc"), 'a')); + CHECK(startsWith("def"_catch_sr, 'd')); +}