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

StringRef conversions to/ from std::string

Commit
c659e0fd3dadd397dfed17e59a624fb693d77646
Author
Phil Nash ·
Parents
1aab791d67a8

Files · Download .tar.gz

Changes

 include/internal/catch_stringref.cpp  | 12 +++++++---
 include/internal/catch_stringref.h    |  3 +++
 projects/SelfTest/StringRef.tests.cpp | 42 +++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/include/internal/catch_stringref.cpp b/include/internal/catch_stringref.cpp
index 44db6858..9a1eaf4c 100644
--- a/include/internal/catch_stringref.cpp
+++ b/include/internal/catch_stringref.cpp
@@ -60,8 +60,7 @@ namespace Catch {
     
     StringRef::StringRef( String const& other ) noexcept
     :   m_start( other.c_str() ),
-        m_size( other.size() ),
-        m_data( nullptr )
+        m_size( other.size() )
     {}
     
     StringRef::StringRef( String&& str ) noexcept
@@ -71,7 +70,11 @@ namespace Catch {
     {
         str.m_data = StringData::getEmpty();
     }
-    
+    StringRef::StringRef( std::string const& stdString ) noexcept
+    :   m_start( stdString.c_str() ),
+        m_size( stdString.size() )
+    {}
+
     StringRef::~StringRef() noexcept {
         if( isOwned() )
             m_data->release();
@@ -81,6 +84,9 @@ namespace Catch {
         swap( other );
         return *this;
     }
+    StringRef::operator std::string() const {
+        return std::string( m_start, m_size );
+    }
 
     void StringRef::swap( StringRef& other ) noexcept {
         std::swap( m_start, other.m_start );
diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h
index f84ce99a..cde0667a 100644
--- a/include/internal/catch_stringref.h
+++ b/include/internal/catch_stringref.h
@@ -8,6 +8,7 @@
 #define CATCH_STRINGREF_H_INCLUDED
 
 #include <cstddef>
+#include <string>
 
 namespace Catch {
     
@@ -43,9 +44,11 @@ namespace Catch {
         StringRef( char const* rawChars, size_type size ) noexcept;
         StringRef( String const& other ) noexcept;
         StringRef( String&& other ) noexcept;
+        StringRef( std::string const& stdString ) noexcept;
         ~StringRef() noexcept;
         
         auto operator = ( StringRef other ) noexcept -> StringRef&;
+        operator std::string() const;
 
         void swap( StringRef& other ) noexcept;
         
diff --git a/projects/SelfTest/StringRef.tests.cpp b/projects/SelfTest/StringRef.tests.cpp
index 4ebaa878..6ebbdb13 100644
--- a/projects/SelfTest/StringRef.tests.cpp
+++ b/projects/SelfTest/StringRef.tests.cpp
@@ -150,4 +150,46 @@ TEST_CASE( "StringRef", "[Strings]" ) {
             REQUIRE( data( copied ) == originalPointer );
         }
     }
+
+    SECTION( "from std::string" ) {
+        std::string stdStr = "a standard string";
+
+        SECTION( "implicitly constructed" ) {
+            StringRef sr = stdStr;
+            REQUIRE( sr == "a standard string" );
+            REQUIRE( sr.size() == stdStr.size() );
+        }
+        SECTION( "explicitly constructed" ) {
+            StringRef sr( stdStr );
+            REQUIRE( sr == "a standard string" );
+            REQUIRE( sr.size() == stdStr.size() );
+        }
+        SECTION( "assigned" ) {
+            StringRef sr;
+            sr = stdStr;
+            REQUIRE( sr == "a standard string" );
+            REQUIRE( sr.size() == stdStr.size() );
+        }
+    }
+
+    SECTION( "to std::string" ) {
+        StringRef sr = "a stringref";
+
+        SECTION( "implicitly constructed" ) {
+            std::string stdStr = sr;
+            REQUIRE( stdStr == "a stringref" );
+            REQUIRE( stdStr.size() == sr.size() );
+        }
+        SECTION( "explicitly constructed" ) {
+            std::string stdStr( sr );
+            REQUIRE( stdStr == "a stringref" );
+            REQUIRE( stdStr.size() == sr.size() );
+        }
+        SECTION( "assigned" ) {
+            std::string stdStr;
+            stdStr = sr;
+            REQUIRE( stdStr == "a stringref" );
+            REQUIRE( stdStr.size() == sr.size() );
+        }
+    }
 }