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

catch_tostring: tostring for enum class types

Commit
cd2a5aa688be79226f646aeb94f7bd27c39f033f
Author
Andy Sawyer ·
Parents
142f8f4b1e7f

Files · Download .tar.gz

Changes

 include/internal/catch_tostring.h | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index e8e38296..dbb31423 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -22,6 +22,11 @@
 #endif
 
 namespace Catch {
+
+// Why we're here.
+template<typename T>
+std::string toString( T const& value );
+
 namespace Detail {
 
 // SFINAE is currently disabled by default for all compilers.
@@ -64,10 +69,38 @@ namespace Detail {
 
 #endif
 
+#if defined(CATCH_CPP11_OR_GREATER)
+    template<typename T,
+             bool IsEmum = std::is_enum<T>::value
+             >
+    struct EnumStringMaker
+    {
+        static std::string convert( T const& ) { return "{?}"; }
+    };
+
+    template<typename T>
+    struct EnumStringMaker<T,true>
+    {
+        static std::string convert( T const& v )
+        {
+            return ::Catch::toString(
+                static_cast<typename std::underlying_type<T>::type>(v)
+                );
+        }
+    };
+#endif
     template<bool C>
     struct StringMakerBase {
+#if defined(CATCH_CPP11_OR_GREATER)
+        template<typename T>
+        static std::string convert( T const& v )
+        {
+            return EnumStringMaker<T>::convert( v );
+        }
+#else
         template<typename T>
         static std::string convert( T const& ) { return "{?}"; }
+#endif
     };
 
     template<>
@@ -89,9 +122,6 @@ namespace Detail {
 
 } // end namespace Detail
 
-template<typename T>
-std::string toString( T const& value );
-
 template<typename T>
 struct StringMaker :
     Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {};