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 +#include + #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& 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 m_reporterRegistry; + std::auto_ptr 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::const_iterator it = TestRegistry::instance().getAllTests().begin(); - std::vector::const_iterator itEnd = TestRegistry::instance().getAllTests().end(); + std::vector::const_iterator it = Hub::getTestCaseRegistry().getAllTests().begin(); + std::vector::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 allTests = TestRegistry::instance().getAllTests(); + std::vector 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 allTests = TestRegistry::instance().getAllTests(); + std::vector 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& getAllTests() const + virtual const std::vector& 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 AutoReg( void (C::*method)(), const char* name, const char* description ) { - TestRegistry::instance().registerTest( TestCaseInfo( new MethodTestCase( method ), name, description ) ); + Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new MethodTestCase( method ), name, description ) ); } + + ~AutoReg(); + +private: + AutoReg( const AutoReg& ); + void operator=( const AutoReg& ); }; template