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

Moved test case registry into hub

Commit
430733c918fd32251d8010311497896d4020841a
Author
Phil Nash ·
Parents
a65f210cb2ce

Files · Download .tar.gz

Changes

 internal/catch_hub.h             | 23 +++++++++++++++++------
 internal/catch_hub_impl.hpp      | 28 +++++++++++++++++++++++++---
 internal/catch_list.hpp          |  4 ++--
 internal/catch_runner_impl.hpp   |  4 ++--
 internal/catch_test_registry.hpp | 25 +++++++++++--------------
 5 files changed, 57 insertions(+), 27 deletions(-)

diff --git a/internal/catch_hub.h b/internal/catch_hub.h
index 1ac60929..9d7696cc 100644
--- a/internal/catch_hub.h
+++ b/internal/catch_hub.h
@@ -13,21 +13,31 @@
 #define TWOBLUECUBES_CATCH_HUB_H_INCLUDED
 
 #include <memory>
+#include <vector>
+
 #include "catch_interfaces_reporter.h"
 
 namespace Catch
 {
+    struct TestCaseInfo;
     struct IResultListener;
-    struct ITestCaseRegistry;
+    struct ITestCaseRegistry
+    {
+    public:
+        
+        virtual void registerTest
+            ( const TestCaseInfo& testInfo 
+            ) = 0;
 
+        virtual const std::vector<TestCaseInfo>& getAllTests
+            () const = 0;
+    };
+    
     class Hub
     {
         Hub();
-        static Hub& me()
-        {
-            static Hub hub;
-            return hub;
-        }
+        static Hub& me();
+        
     public:
         
         static IResultListener& getListener();
@@ -36,6 +46,7 @@ namespace Catch
         
     private:
         std::auto_ptr<IReporterRegistry> m_reporterRegistry;
+        std::auto_ptr<ITestCaseRegistry> m_testCaseRegistry;
     };
 }
 
diff --git a/internal/catch_hub_impl.hpp b/internal/catch_hub_impl.hpp
index 722b140e..7eb7f6de 100644
--- a/internal/catch_hub_impl.hpp
+++ b/internal/catch_hub_impl.hpp
@@ -17,15 +17,37 @@
 
 namespace Catch
 {
-    inline Hub::Hub()
-    : m_reporterRegistry( new ReporterRegistry )
+    ///////////////////////////////////////////////////////////////////////////
+    Hub::Hub()
+    :   m_reporterRegistry( new ReporterRegistry ),
+        m_testCaseRegistry( new TestRegistry )
     {
     }
     
-    inline IReporterRegistry& Hub::getReporterRegistry()
+    Hub& Hub::me()
+    {
+        static Hub hub;
+        return hub;
+    }
+
+    IReporterRegistry& Hub::getReporterRegistry()
     {
         return *me().m_reporterRegistry.get();
     }
+
+    ITestCaseRegistry& Hub::getTestCaseRegistry()
+    {
+        return *me().m_testCaseRegistry.get();
+    }
+    
+    ///////////////////////////////////////////////////////////////////////////
+    AutoReg::AutoReg( TestFunction function, const char* name, const char* description )
+    {
+        Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new FreeFunctionTestCase( function ), name, description ) );
+    }    
+    AutoReg::~AutoReg()
+    {
+    }
 }
 
 #endif // TWOBLUECUBES_CATCH_HUB_IMPL_HPP_INCLUDED
\ No newline at end of file
diff --git a/internal/catch_list.hpp b/internal/catch_list.hpp
index 343dd3f5..76f6f8fa 100644
--- a/internal/catch_list.hpp
+++ b/internal/catch_list.hpp
@@ -35,8 +35,8 @@ namespace Catch
         if( config.listWhat() & Config::List::Tests )
         {
             std::cout << "Available tests:\n";
-            std::vector<TestCaseInfo>::const_iterator it = TestRegistry::instance().getAllTests().begin();
-            std::vector<TestCaseInfo>::const_iterator itEnd = TestRegistry::instance().getAllTests().end();
+            std::vector<TestCaseInfo>::const_iterator it = Hub::getTestCaseRegistry().getAllTests().begin();
+            std::vector<TestCaseInfo>::const_iterator itEnd = Hub::getTestCaseRegistry().getAllTests().end();
             for(; it != itEnd; ++it )
             {
                 // !TBD: consider listAs()
diff --git a/internal/catch_runner_impl.hpp b/internal/catch_runner_impl.hpp
index e4c5e71d..f973aecd 100644
--- a/internal/catch_runner_impl.hpp
+++ b/internal/catch_runner_impl.hpp
@@ -92,7 +92,7 @@ namespace Catch
         
         void runAll()
         {
-            std::vector<TestCaseInfo> allTests = TestRegistry::instance().getAllTests();
+            std::vector<TestCaseInfo> allTests = Hub::getTestCaseRegistry().getAllTests();
             for( std::size_t i=0; i < allTests.size(); ++i )
             {
                 runTest( allTests[i] );
@@ -103,7 +103,7 @@ namespace Catch
         {
             TestSpec testSpec( rawTestSpec );
             
-            std::vector<TestCaseInfo> allTests = TestRegistry::instance().getAllTests();
+            std::vector<TestCaseInfo> allTests = Hub::getTestCaseRegistry().getAllTests();
             std::size_t testsRun = 0;
             for( std::size_t i=0; i < allTests.size(); ++i )
             {
diff --git a/internal/catch_test_registry.hpp b/internal/catch_test_registry.hpp
index 9913f845..15954fe5 100644
--- a/internal/catch_test_registry.hpp
+++ b/internal/catch_test_registry.hpp
@@ -23,17 +23,11 @@
 
 namespace Catch
 {
-class TestRegistry
+    class TestRegistry : public ITestCaseRegistry
 {
 public:
     
-    static TestRegistry& instance()
-    {
-        static TestRegistry reg;
-        return reg;
-    }
-    
-    void registerTest( const TestCaseInfo& testInfo )
+    virtual void registerTest( const TestCaseInfo& testInfo )
     {
         if( m_functions.find( testInfo ) == m_functions.end() )
         {
@@ -42,7 +36,7 @@ public:
         }
     }
         
-    const std::vector<TestCaseInfo>& getAllTests() const
+    virtual const std::vector<TestCaseInfo>& getAllTests() const
     {
         return m_functionsInOrder;
     }
@@ -123,16 +117,19 @@ private:
     
 struct AutoReg
 {
-    AutoReg( TestFunction function, const char* name, const char* description )
-    {
-        TestRegistry::instance().registerTest( TestCaseInfo( new FreeFunctionTestCase( function ), name, description ) );
-    }
+    AutoReg( TestFunction function, const char* name, const char* description );
     
     template<typename C>
     AutoReg( void (C::*method)(), const char* name, const char* description )
     {
-        TestRegistry::instance().registerTest( TestCaseInfo( new MethodTestCase<C>( method ), name, description ) );
+        Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new MethodTestCase<C>( method ), name, description ) );
     }
+    
+    ~AutoReg();
+    
+private:
+    AutoReg( const AutoReg& );
+    void operator=( const AutoReg& );
 };
 
 template<typename T, size_t>