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

Simplified test case registration and started using StringRefs

Commit
720fdf1d02dd5d5566a1f82bda87a2a43cec213d
Author
Phil Nash ·
Parents
79627cdcdb45

Files · Download .tar.gz

Changes

 include/internal/catch_stringref.cpp               | 14 +++--
 include/internal/catch_stringref.h                 |  2 -
 include/internal/catch_test_case_registry_impl.hpp | 44 ++++++---------
 include/internal/catch_test_registry.hpp           | 64 ++++++----------------
 4 files changed, 44 insertions(+), 80 deletions(-)

diff --git a/include/internal/catch_stringref.cpp b/include/internal/catch_stringref.cpp
index 4ee4f7dd..3c4e292d 100644
--- a/include/internal/catch_stringref.cpp
+++ b/include/internal/catch_stringref.cpp
@@ -12,13 +12,17 @@
 
 #include <cstring>
 #include <ostream>
+#include <cassert>
 
 namespace Catch {
-    
-    StringRef StringRef::s_emptyStringRef = "";
+
+    auto getEmptyStringRef() -> StringRef {
+        static StringRef s_emptyStringRef("");
+        return s_emptyStringRef;
+    }
     
     StringRef::StringRef() noexcept
-    :   StringRef( s_emptyStringRef )
+    :   StringRef( getEmptyStringRef() )
     {}
     
     StringRef::StringRef( StringRef const& other ) noexcept
@@ -41,7 +45,9 @@ namespace Catch {
     StringRef::StringRef( char const* rawChars ) noexcept
     :   m_start( rawChars ),
         m_size( static_cast<size_type>( std::strlen( rawChars ) ) )
-    {}
+    {
+        assert( rawChars != nullptr );
+    }
     
     StringRef::StringRef( char const* rawChars, size_type size ) noexcept
     :   m_start( rawChars ),
diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h
index 1cf5670f..dd223beb 100644
--- a/include/internal/catch_stringref.h
+++ b/include/internal/catch_stringref.h
@@ -31,8 +31,6 @@ namespace Catch {
         
         StringData const* m_data = nullptr;
         
-        static StringRef s_emptyStringRef;
-        
         void takeOwnership();
         
     public: // construction/ assignment
diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp
index cbe755e3..6874107c 100644
--- a/include/internal/catch_test_case_registry_impl.hpp
+++ b/include/internal/catch_test_case_registry_impl.hpp
@@ -134,6 +134,10 @@ namespace Catch {
             m_testAsFunction();
         }
     };
+    auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker* {
+        return new TestInvokerAsFunction( testAsFunction );
+    }
+
 
     inline std::string extractClassName( std::string const& classOrQualifiedMethodName ) {
         std::string className = classOrQualifiedMethodName;
@@ -148,40 +152,24 @@ namespace Catch {
         return className;
     }
 
-    void registerTestCase
-        (   ITestInvoker* testCase,
-            char const* classOrQualifiedMethodName,
-            NameAndDesc const& nameAndDesc,
-            SourceLineInfo const& lineInfo ) {
+    ///////////////////////////////////////////////////////////////////////////
+
+    AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags )
+    {
         try {
-            getMutableRegistryHub().registerTest
-            (makeTestCase
-            (testCase,
-             extractClassName(classOrQualifiedMethodName),
-             nameAndDesc.name,
-             nameAndDesc.description,
-             lineInfo));
+            getMutableRegistryHub()
+                    .registerTest(
+                        makeTestCase(
+                            invoker,
+                            extractClassName( classOrMethod.c_str() ),
+                            nameAndTags.name.c_str(),
+                            nameAndTags.tags.c_str(),
+                            lineInfo));
         } catch (...) {
             // Do not throw when constructing global objects, instead register the exception to be processed later
             getMutableRegistryHub().registerStartupException( std::current_exception() );
         }
     }
-    void registerTestCaseFunction
-        (   TestFunction function,
-            SourceLineInfo const& lineInfo,
-            NameAndDesc const& nameAndDesc ) {
-        registerTestCase( new TestInvokerAsFunction( function ), "", nameAndDesc, lineInfo );
-    }
-
-    ///////////////////////////////////////////////////////////////////////////
-
-    AutoReg::AutoReg
-        (   TestFunction function,
-            SourceLineInfo const& lineInfo,
-            NameAndDesc const& nameAndDesc ) {
-        registerTestCaseFunction( function, lineInfo, nameAndDesc );
-    }
-
     AutoReg::~AutoReg() {}
 
 } // end namespace Catch
diff --git a/include/internal/catch_test_registry.hpp b/include/internal/catch_test_registry.hpp
index 374b9228..da0fbc2e 100644
--- a/include/internal/catch_test_registry.hpp
+++ b/include/internal/catch_test_registry.hpp
@@ -11,6 +11,7 @@
 #include "catch_common.h"
 #include "catch_interfaces_testcase.h"
 #include "catch_compiler_capabilities.h"
+#include "catch_stringref.h"
 
 namespace Catch {
 
@@ -26,54 +27,25 @@ public:
     }
 };
 
-typedef void(*TestFunction)();
+auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker*;
 
-struct NameAndDesc {
-    NameAndDesc( const char* _name = "", const char* _description= "" )
-    : name( _name ), description( _description )
-    {}
+template<typename C>
+auto makeTestInvoker( void (C::*testAsMethod)() ) -> ITestInvoker* {
+    return new TestInvokerAsMethod<C>( testAsMethod );
+}
 
-    const char* name;
-    const char* description;
-};
+struct NameAndTags {
 
-void registerTestCase
-    (   ITestInvoker* testCase,
-        char const* className,
-        NameAndDesc const& nameAndDesc,
-        SourceLineInfo const& lineInfo );
-
-struct AutoReg {
-
-    AutoReg
-        (   TestFunction function,
-            SourceLineInfo const& lineInfo,
-            NameAndDesc const& nameAndDesc );
-
-    template<typename C>
-    AutoReg
-        (   void (C::*method)(),
-            char const* className,
-            NameAndDesc const& nameAndDesc,
-            SourceLineInfo const& lineInfo ) {
-
-        registerTestCase
-            (   new TestInvokerAsMethod<C>( method ),
-                className,
-                nameAndDesc,
-                lineInfo );
-    }
+    NameAndTags( StringRef name_ = "", StringRef tags_ = "" ) : name( name_ ), tags( tags_ ) {}
 
-    ~AutoReg();
-
-    AutoReg( AutoReg const& ) = delete;
-    AutoReg& operator = (AutoReg const& ) = delete;
+    StringRef name;
+    StringRef tags;
 };
 
-void registerTestCaseFunction
-    (   TestFunction function,
-        SourceLineInfo const& lineInfo,
-        NameAndDesc const& nameAndDesc );
+struct AutoReg : NonCopyable {
+    AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags );
+    ~AutoReg();
+};
 
 } // end namespace Catch
 
@@ -81,7 +53,7 @@ void registerTestCaseFunction
     #define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \
         static void TestName(); \
         CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \
-        namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &TestName, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); } \
+        namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &TestName ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); } \
         CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS \
         static void TestName()
     #define INTERNAL_CATCH_TESTCASE( ... ) \
@@ -90,7 +62,7 @@ void registerTestCaseFunction
     ///////////////////////////////////////////////////////////////////////////////
     #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \
         CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \
-        namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); } \
+        namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &QualifiedMethod ), CATCH_INTERNAL_LINEINFO, "&" #QualifiedMethod, Catch::NameAndTags{ __VA_ARGS__ } ); } \
         CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS
 
     ///////////////////////////////////////////////////////////////////////////////
@@ -100,7 +72,7 @@ void registerTestCaseFunction
             struct TestName : ClassName{ \
                 void test(); \
             }; \
-            Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &TestName::test, #ClassName, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); \
+            Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ __VA_ARGS__ } ); \
         } \
         CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS \
         void TestName::test()
@@ -110,7 +82,7 @@ void registerTestCaseFunction
     ///////////////////////////////////////////////////////////////////////////////
     #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \
         CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \
-        Catch::AutoReg( Function, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); \
+        Catch::AutoReg( Catch::makeTestInvoker( Function ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); \
         CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS