From 317145514fad8266edbce1145391524b4e948f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sun, 31 May 2020 22:30:41 +0200 Subject: [PATCH] Add op+(StringRef, StringRef) -> std::string --- src/catch2/internal/catch_stringref.cpp | 8 ++++++ src/catch2/internal/catch_stringref.hpp | 1 + .../Baselines/compact.sw.approved.txt | 4 +++ .../Baselines/console.std.approved.txt | 2 +- .../Baselines/console.sw.approved.txt | 28 ++++++++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 4 ++- .../Baselines/sonarqube.sw.approved.txt | 2 ++ tests/SelfTest/Baselines/tap.sw.approved.txt | 6 +++- tests/SelfTest/Baselines/xml.sw.approved.txt | 28 +++++++++++++++++-- .../IntrospectiveTests/String.tests.cpp | 13 ++++++++- 10 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/catch2/internal/catch_stringref.cpp b/src/catch2/internal/catch_stringref.cpp index 4b0bbae6..df375eba 100644 --- a/src/catch2/internal/catch_stringref.cpp +++ b/src/catch2/internal/catch_stringref.cpp @@ -39,6 +39,14 @@ namespace Catch { return os.write(str.data(), str.size()); } + std::string operator+(StringRef lhs, StringRef rhs) { + std::string ret; + ret.reserve(lhs.size() + rhs.size()); + ret += lhs; + ret += rhs; + return ret; + } + auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& { lhs.append(rhs.data(), rhs.size()); return lhs; diff --git a/src/catch2/internal/catch_stringref.hpp b/src/catch2/internal/catch_stringref.hpp index 1e07b6ad..773d6d33 100644 --- a/src/catch2/internal/catch_stringref.hpp +++ b/src/catch2/internal/catch_stringref.hpp @@ -101,6 +101,7 @@ namespace Catch { friend std::string& operator += (std::string& lhs, StringRef const& sr); friend std::ostream& operator << (std::ostream& os, StringRef const& sr); + friend std::string operator+(StringRef lhs, StringRef rhs); }; diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index d7a756f4..977837ea 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1289,6 +1289,10 @@ String.tests.cpp:: passed: stdStr == "a stringref" for: "a stringre String.tests.cpp:: passed: stdStr.size() == sr.size() for: 11 == 11 String.tests.cpp:: passed: stdStr == "a stringref" for: "a stringref" == "a stringref" String.tests.cpp:: passed: stdStr.size() == sr.size() for: 11 == 11 +String.tests.cpp:: passed: lhs == "some string += the stringref contents" for: "some string += the stringref contents" +== +"some string += the stringref contents" +String.tests.cpp:: passed: together == "abrakadabra" for: "abrakadabra" == "abrakadabra" String.tests.cpp:: passed: with 1 message: 'empty.size() == 0' String.tests.cpp:: passed: with 1 message: 'empty.begin() == empty.end()' String.tests.cpp:: passed: with 1 message: 'stringref.size() == 3' diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index ee6710d3..b20e5353 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1381,5 +1381,5 @@ due to unexpected exception with message: =============================================================================== test cases: 337 | 263 passed | 70 failed | 4 failed as expected -assertions: 1924 | 1772 passed | 131 failed | 21 failed as expected +assertions: 1926 | 1774 passed | 131 failed | 21 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 450d8025..bc46d209 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -9449,6 +9449,32 @@ String.tests.cpp:: PASSED: with expansion: 11 == 11 +------------------------------------------------------------------------------- +StringRef + std::string += StringRef +------------------------------------------------------------------------------- +String.tests.cpp: +............................................................................... + +String.tests.cpp:: PASSED: + REQUIRE( lhs == "some string += the stringref contents" ) +with expansion: + "some string += the stringref contents" + == + "some string += the stringref contents" + +------------------------------------------------------------------------------- +StringRef + StringRef + StringRef +------------------------------------------------------------------------------- +String.tests.cpp: +............................................................................... + +String.tests.cpp:: PASSED: + REQUIRE( together == "abrakadabra" ) +with expansion: + "abrakadabra" == "abrakadabra" + ------------------------------------------------------------------------------- StringRef at compilation time Simple constructors @@ -15033,5 +15059,5 @@ Misc.tests.cpp:: PASSED: =============================================================================== test cases: 337 | 247 passed | 86 failed | 4 failed as expected -assertions: 1941 | 1772 passed | 148 failed | 21 failed as expected +assertions: 1943 | 1774 passed | 148 failed | 21 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index 839cf6e6..a0bfb944 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -1145,6 +1145,8 @@ Matchers.tests.cpp: + + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt index 2809f95b..03be4906 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -155,6 +155,8 @@ + + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index d03b250e..e2f5e004 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -2491,6 +2491,10 @@ ok {test-number} - stdStr.size() == sr.size() for: 11 == 11 ok {test-number} - stdStr == "a stringref" for: "a stringref" == "a stringref" # StringRef ok {test-number} - stdStr.size() == sr.size() for: 11 == 11 +# StringRef +ok {test-number} - lhs == "some string += the stringref contents" for: "some string += the stringref contents" == "some string += the stringref contents" +# StringRef +ok {test-number} - together == "abrakadabra" for: "abrakadabra" == "abrakadabra" # StringRef at compilation time ok {test-number} - with 1 message: 'empty.size() == 0' # StringRef at compilation time @@ -3874,5 +3878,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..1933 +1..1935 diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index 45d444c1..4d39e14d 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -11724,6 +11724,30 @@ Message from section two +
+ + + lhs == "some string += the stringref contents" + + + "some string += the stringref contents" +== +"some string += the stringref contents" + + + +
+
+ + + together == "abrakadabra" + + + "abrakadabra" == "abrakadabra" + + + +
@@ -18031,7 +18055,7 @@ loose text artifact - + - + diff --git a/tests/SelfTest/IntrospectiveTests/String.tests.cpp b/tests/SelfTest/IntrospectiveTests/String.tests.cpp index 8c57e583..3832a2bf 100644 --- a/tests/SelfTest/IntrospectiveTests/String.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/String.tests.cpp @@ -4,7 +4,6 @@ #include TEST_CASE( "StringRef", "[Strings][StringRef]" ) { - using Catch::StringRef; SECTION( "Empty string" ) { @@ -123,6 +122,18 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) { REQUIRE( stdStr.size() == sr.size() ); } } + + SECTION("std::string += StringRef") { + StringRef sr = "the stringref contents"; + std::string lhs("some string += "); + lhs += sr; + REQUIRE(lhs == "some string += the stringref contents"); + } + SECTION("StringRef + StringRef") { + StringRef sr1 = "abraka", sr2 = "dabra"; + std::string together = sr1 + sr2; + REQUIRE(together == "abrakadabra"); + } } TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {