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
Add trim for StringRef
- Commit
f2c2711bdcc583938e444f2039b9ceba840defcf- Author
- Martin Hořeňovský ·
- Parents
b77ab74b723c
Changes
include/internal/catch_string_manip.cpp | 12 ++++++ include/internal/catch_string_manip.h | 3 ++ .../SelfTest/Baselines/compact.sw.approved.txt | 12 ++++++ .../SelfTest/Baselines/console.std.approved.txt | 2 +- .../SelfTest/Baselines/console.sw.approved.txt | 30 ++++++++++++++- projects/SelfTest/Baselines/junit.sw.approved.txt | 2 +- projects/SelfTest/Baselines/xml.sw.approved.txt | 44 +++++++++++++++++++++- .../IntrospectiveTests/StringManip.tests.cpp | 6 +++ 8 files changed, 106 insertions(+), 5 deletions(-) diff --git a/include/internal/catch_string_manip.cpp b/include/internal/catch_string_manip.cpp index 9f459de0..65be34d0 100644 --- a/include/internal/catch_string_manip.cpp +++ b/include/internal/catch_string_manip.cpp @@ -53,6 +53,18 @@ namespace Catch { return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); } + StringRef trim(StringRef ref) { + const auto is_ws = [](char c) { + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; + }; + size_t real_begin = 0; + while (real_begin < ref.size() && is_ws(ref[real_begin])) { ++real_begin; } + size_t real_end = ref.size(); + while (real_end > real_begin && is_ws(ref[real_end - 1])) { --real_end; } + + return ref.substr(real_begin, real_end - real_begin); + } + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { bool replaced = false; std::size_t i = str.find( replaceThis ); diff --git a/include/internal/catch_string_manip.h b/include/internal/catch_string_manip.h index b551ded6..cdb7be5f 100644 --- a/include/internal/catch_string_manip.h +++ b/include/internal/catch_string_manip.h @@ -22,7 +22,10 @@ namespace Catch { bool contains( std::string const& s, std::string const& infix ); void toLowerInPlace( std::string& s ); std::string toLower( std::string const& s ); + //! Returns a new string without whitespace at the start/end std::string trim( std::string const& str ); + //! Returns a substring of the original ref without whitespace. Beware lifetimes! + StringRef trim(StringRef ref); // !!! Be aware, returns refs into original string - make sure original string outlives them std::vector<StringRef> splitStringRef( StringRef str, char delimiter ); diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index b872265f..e6d28f39 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -1311,6 +1311,18 @@ StringManip.tests.cpp:<line number>: passed: trim(std::string(trailing_whitespac StringManip.tests.cpp:<line number>: passed: trim(std::string(whitespace_at_both_ends)) == no_whitespace for: "There is no extra whitespace here" == "There is no extra whitespace here" +StringManip.tests.cpp:<line number>: passed: trim(StringRef(no_whitespace)) == StringRef(no_whitespace) for: There is no extra whitespace here +== +There is no extra whitespace here +StringManip.tests.cpp:<line number>: passed: trim(StringRef(leading_whitespace)) == StringRef(no_whitespace) for: There is no extra whitespace here +== +There is no extra whitespace here +StringManip.tests.cpp:<line number>: passed: trim(StringRef(trailing_whitespace)) == StringRef(no_whitespace) for: There is no extra whitespace here +== +There is no extra whitespace here +StringManip.tests.cpp:<line number>: passed: trim(StringRef(whitespace_at_both_ends)) == StringRef(no_whitespace) for: There is no extra whitespace here +== +There is no extra whitespace here Exception.tests.cpp:<line number>: failed: unexpected exception with message: '3.14' Approx.tests.cpp:<line number>: passed: d == approx( 1.23 ) for: 1.23 == Approx( 1.23 ) Approx.tests.cpp:<line number>: passed: d == approx( 1.22 ) for: 1.23 == Approx( 1.22 ) diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index a52bb9a5..756ea25e 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1381,5 +1381,5 @@ due to unexpected exception with message: =============================================================================== test cases: 300 | 226 passed | 70 failed | 4 failed as expected -assertions: 1558 | 1406 passed | 131 failed | 21 failed as expected +assertions: 1562 | 1410 passed | 131 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 8b03e232..8e5501f7 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -9813,6 +9813,34 @@ with expansion: == "There is no extra whitespace here" +StringManip.tests.cpp:<line number>: PASSED: + REQUIRE( trim(StringRef(no_whitespace)) == StringRef(no_whitespace) ) +with expansion: + There is no extra whitespace here + == + There is no extra whitespace here + +StringManip.tests.cpp:<line number>: PASSED: + REQUIRE( trim(StringRef(leading_whitespace)) == StringRef(no_whitespace) ) +with expansion: + There is no extra whitespace here + == + There is no extra whitespace here + +StringManip.tests.cpp:<line number>: PASSED: + REQUIRE( trim(StringRef(trailing_whitespace)) == StringRef(no_whitespace) ) +with expansion: + There is no extra whitespace here + == + There is no extra whitespace here + +StringManip.tests.cpp:<line number>: PASSED: + REQUIRE( trim(StringRef(whitespace_at_both_ends)) == StringRef(no_whitespace) ) +with expansion: + There is no extra whitespace here + == + There is no extra whitespace here + ------------------------------------------------------------------------------- Unexpected exceptions can be translated ------------------------------------------------------------------------------- @@ -12456,5 +12484,5 @@ Misc.tests.cpp:<line number>: PASSED: =============================================================================== test cases: 300 | 210 passed | 86 failed | 4 failed as expected -assertions: 1575 | 1406 passed | 148 failed | 21 failed as expected +assertions: 1579 | 1410 passed | 148 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index da7853d3..d0e54846 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <testsuitesloose text artifact > - <testsuite name="<exe-name>" errors="17" failures="132" tests="1576" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> + <testsuite name="<exe-name>" errors="17" failures="132" tests="1580" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <properties> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/> <property name="random-seed" value="1"/> diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 9032dd52..d848e615 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -11854,6 +11854,46 @@ Message from section two "There is no extra whitespace here" </Expanded> </Expression> + <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/StringManip.tests.cpp" > + <Original> + trim(StringRef(no_whitespace)) == StringRef(no_whitespace) + </Original> + <Expanded> + There is no extra whitespace here +== +There is no extra whitespace here + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/StringManip.tests.cpp" > + <Original> + trim(StringRef(leading_whitespace)) == StringRef(no_whitespace) + </Original> + <Expanded> + There is no extra whitespace here +== +There is no extra whitespace here + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/StringManip.tests.cpp" > + <Original> + trim(StringRef(trailing_whitespace)) == StringRef(no_whitespace) + </Original> + <Expanded> + There is no extra whitespace here +== +There is no extra whitespace here + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/StringManip.tests.cpp" > + <Original> + trim(StringRef(whitespace_at_both_ends)) == StringRef(no_whitespace) + </Original> + <Expanded> + There is no extra whitespace here +== +There is no extra whitespace here + </Expanded> + </Expression> <OverallResult success="true"/> </TestCase> <TestCase name="Unexpected exceptions can be translated" tags="[!throws][.][failing]" filename="projects/<exe-name>/UsageTests/Exception.tests.cpp" > @@ -14821,7 +14861,7 @@ loose text artifact </Section> <OverallResult success="true"/> </TestCase> - <OverallResults successes="1406" failures="149" expectedFailures="21"/> + <OverallResults successes="1410" failures="149" expectedFailures="21"/> </Group> - <OverallResults successes="1406" failures="148" expectedFailures="21"/> + <OverallResults successes="1410" failures="148" expectedFailures="21"/> </Catch> diff --git a/projects/SelfTest/IntrospectiveTests/StringManip.tests.cpp b/projects/SelfTest/IntrospectiveTests/StringManip.tests.cpp index 31b5c6ce..e0b00565 100644 --- a/projects/SelfTest/IntrospectiveTests/StringManip.tests.cpp +++ b/projects/SelfTest/IntrospectiveTests/StringManip.tests.cpp @@ -10,9 +10,15 @@ static const char * const whitespace_at_both_ends = " \r\n \t There is no extra TEST_CASE("Trim strings", "[string-manip]") { using Catch::trim; using Catch::StringRef; static_assert(std::is_same<std::string, decltype(trim(std::string{}))>::value, "Trimming std::string should return std::string"); + static_assert(std::is_same<StringRef, decltype(trim(StringRef{}))>::value, "Trimming StringRef should return StringRef"); REQUIRE(trim(std::string(no_whitespace)) == no_whitespace); REQUIRE(trim(std::string(leading_whitespace)) == no_whitespace); REQUIRE(trim(std::string(trailing_whitespace)) == no_whitespace); REQUIRE(trim(std::string(whitespace_at_both_ends)) == no_whitespace); + + REQUIRE(trim(StringRef(no_whitespace)) == StringRef(no_whitespace)); + REQUIRE(trim(StringRef(leading_whitespace)) == StringRef(no_whitespace)); + REQUIRE(trim(StringRef(trailing_whitespace)) == StringRef(no_whitespace)); + REQUIRE(trim(StringRef(whitespace_at_both_ends)) == StringRef(no_whitespace)); }