From 4e57661919e07d8f9e7fcefe73db31a96ab18867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sat, 17 Feb 2018 20:41:50 +0100 Subject: [PATCH] StringRef will not take ownership when writing itself to stream This also fixes some tests that were previously failing unnoticed - WTF? --- include/internal/catch_stringref.cpp | 2 +- include/internal/catch_stringref.h | 19 +++++----- .../Baselines/compact.sw.approved.txt | 7 ++-- .../Baselines/console.sw.approved.txt | 27 +++++++++++--- .../SelfTest/Baselines/junit.sw.approved.txt | 8 ++--- .../SelfTest/Baselines/xml.sw.approved.txt | 36 +++++++++++++++---- 6 files changed, 71 insertions(+), 28 deletions(-) diff --git a/include/internal/catch_stringref.cpp b/include/internal/catch_stringref.cpp index b0b2f8e1..604e8465 100644 --- a/include/internal/catch_stringref.cpp +++ b/include/internal/catch_stringref.cpp @@ -112,7 +112,7 @@ namespace Catch { } auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& { - return os << str.c_str(); + return os.write(str.m_start, str.m_size); } } // namespace Catch diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h index 0f9e7801..1c7e6cbe 100644 --- a/include/internal/catch_stringref.h +++ b/include/internal/catch_stringref.h @@ -12,7 +12,7 @@ #include namespace Catch { - + class StringData; /// A non-owning string class (similar to the forthcoming std::string_view) @@ -31,13 +31,13 @@ namespace Catch { char const* m_start; size_type m_size; - + char* m_data = nullptr; - + void takeOwnership(); static constexpr char const* const s_empty = ""; - + public: // construction/ assignment StringRef() noexcept : StringRef( s_empty, 0 ) @@ -83,13 +83,15 @@ namespace Catch { operator std::string() const; void swap( StringRef& other ) noexcept; - + + friend auto operator << (std::ostream& os, StringRef const& sr)->std::ostream&; + public: // operators auto operator == ( StringRef const& other ) const noexcept -> bool; auto operator != ( StringRef const& other ) const noexcept -> bool; - + auto operator[] ( size_type index ) const noexcept -> char; - + public: // named queries auto empty() const noexcept -> bool { return m_size == 0; @@ -100,7 +102,7 @@ namespace Catch { auto numberOfCharacters() const noexcept -> size_type; auto c_str() const -> char const*; - + public: // substrings and searches auto substr( size_type start, size_type size ) const noexcept -> StringRef; @@ -114,7 +116,6 @@ namespace Catch { auto operator + ( StringRef const& lhs, char const* rhs ) -> std::string; auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string; - auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; inline auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { return StringRef( rawChars, size ); diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 7988402a..2cbed817 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -627,7 +627,10 @@ String.tests.cpp:: passed: isOwned( s ) == false for: false == fals String.tests.cpp:: passed: s.c_str() == rawChars for: "hello" == "hello" String.tests.cpp:: passed: isOwned( s ) == false for: false == false String.tests.cpp:: passed: original == "original" -String.tests.cpp:: failed: isSubstring( original ) for: false +String.tests.cpp:: passed: isSubstring( original ) for: true +String.tests.cpp:: passed: isOwned( original ) == false for: false == false +String.tests.cpp:: passed: isSubstring( original ) == false for: false == false +String.tests.cpp:: passed: isOwned( original ) for: true String.tests.cpp:: passed: ss.empty() == false for: false == false String.tests.cpp:: passed: ss.size() == 5 for: 5 == 5 String.tests.cpp:: passed: std::strcmp( ss.c_str(), "hello" ) == 0 for: 0 == 0 @@ -1077,5 +1080,5 @@ Misc.tests.cpp:: passed: v.size() == 5 for: 5 == 5 Misc.tests.cpp:: passed: v.capacity() >= 5 for: 5 >= 5 Misc.tests.cpp:: passed: Misc.tests.cpp:: passed: -Failed 63 test cases, failed 122 assertions. +Failed 62 test cases, failed 121 assertions. diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 8ffefb8c..dd81bad9 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -4998,10 +4998,29 @@ String.tests.cpp:: PASSED: REQUIRE( original == "original" ) -String.tests.cpp:: FAILED: +String.tests.cpp:: +PASSED: REQUIRE( isSubstring( original ) ) with expansion: - false + true + +String.tests.cpp:: +PASSED: + REQUIRE( isOwned( original ) == false ) +with expansion: + false == false + +String.tests.cpp:: +PASSED: + REQUIRE( isSubstring( original ) == false ) +with expansion: + false == false + +String.tests.cpp:: +PASSED: + REQUIRE( isOwned( original ) ) +with expansion: + true ------------------------------------------------------------------------------- StringRef @@ -8531,6 +8550,6 @@ Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 202 | 135 passed | 63 failed | 4 failed as expected -assertions: 1018 | 875 passed | 122 failed | 21 failed as expected +test cases: 202 | 136 passed | 62 failed | 4 failed as expected +assertions: 1021 | 879 passed | 121 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 8fac923f..895e41f9 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -546,11 +546,7 @@ Matchers.tests.cpp: - - -String.tests.cpp: - - + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 01978f3d..8942b851 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -5716,15 +5716,39 @@ Message from section two original == "original" - + isSubstring( original ) - false + true - + + + isOwned( original ) == false + + + false == false + + + + + isSubstring( original ) == false + + + false == false + + + + + isOwned( original ) + + + true + + +
@@ -6054,7 +6078,7 @@ Message from section two
- + @@ -9381,7 +9405,7 @@ loose text artifact
- + - +