Moved test case registry into hub

This commit is contained in:
Phil Nash 2011-01-07 10:01:40 +00:00
parent a65f210cb2
commit 430733c918
5 changed files with 57 additions and 27 deletions

View File

@ -13,21 +13,31 @@
#define TWOBLUECUBES_CATCH_HUB_H_INCLUDED #define TWOBLUECUBES_CATCH_HUB_H_INCLUDED
#include <memory> #include <memory>
#include <vector>
#include "catch_interfaces_reporter.h" #include "catch_interfaces_reporter.h"
namespace Catch namespace Catch
{ {
struct TestCaseInfo;
struct IResultListener; struct IResultListener;
struct ITestCaseRegistry; struct ITestCaseRegistry
{
public:
virtual void registerTest
( const TestCaseInfo& testInfo
) = 0;
virtual const std::vector<TestCaseInfo>& getAllTests
() const = 0;
};
class Hub class Hub
{ {
Hub(); Hub();
static Hub& me() static Hub& me();
{
static Hub hub;
return hub;
}
public: public:
static IResultListener& getListener(); static IResultListener& getListener();
@ -36,6 +46,7 @@ namespace Catch
private: private:
std::auto_ptr<IReporterRegistry> m_reporterRegistry; std::auto_ptr<IReporterRegistry> m_reporterRegistry;
std::auto_ptr<ITestCaseRegistry> m_testCaseRegistry;
}; };
} }

View File

@ -17,15 +17,37 @@
namespace Catch 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(); 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 #endif // TWOBLUECUBES_CATCH_HUB_IMPL_HPP_INCLUDED

View File

@ -35,8 +35,8 @@ namespace Catch
if( config.listWhat() & Config::List::Tests ) if( config.listWhat() & Config::List::Tests )
{ {
std::cout << "Available tests:\n"; std::cout << "Available tests:\n";
std::vector<TestCaseInfo>::const_iterator it = TestRegistry::instance().getAllTests().begin(); std::vector<TestCaseInfo>::const_iterator it = Hub::getTestCaseRegistry().getAllTests().begin();
std::vector<TestCaseInfo>::const_iterator itEnd = TestRegistry::instance().getAllTests().end(); std::vector<TestCaseInfo>::const_iterator itEnd = Hub::getTestCaseRegistry().getAllTests().end();
for(; it != itEnd; ++it ) for(; it != itEnd; ++it )
{ {
// !TBD: consider listAs() // !TBD: consider listAs()

View File

@ -92,7 +92,7 @@ namespace Catch
void runAll() 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 ) for( std::size_t i=0; i < allTests.size(); ++i )
{ {
runTest( allTests[i] ); runTest( allTests[i] );
@ -103,7 +103,7 @@ namespace Catch
{ {
TestSpec testSpec( rawTestSpec ); TestSpec testSpec( rawTestSpec );
std::vector<TestCaseInfo> allTests = TestRegistry::instance().getAllTests(); std::vector<TestCaseInfo> allTests = Hub::getTestCaseRegistry().getAllTests();
std::size_t testsRun = 0; std::size_t testsRun = 0;
for( std::size_t i=0; i < allTests.size(); ++i ) for( std::size_t i=0; i < allTests.size(); ++i )
{ {

View File

@ -23,17 +23,11 @@
namespace Catch namespace Catch
{ {
class TestRegistry class TestRegistry : public ITestCaseRegistry
{ {
public: public:
static TestRegistry& instance() virtual void registerTest( const TestCaseInfo& testInfo )
{
static TestRegistry reg;
return reg;
}
void registerTest( const TestCaseInfo& testInfo )
{ {
if( m_functions.find( testInfo ) == m_functions.end() ) 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; return m_functionsInOrder;
} }
@ -123,16 +117,19 @@ private:
struct AutoReg struct AutoReg
{ {
AutoReg( TestFunction function, const char* name, const char* description ) AutoReg( TestFunction function, const char* name, const char* description );
{
TestRegistry::instance().registerTest( TestCaseInfo( new FreeFunctionTestCase( function ), name, description ) );
}
template<typename C> template<typename C>
AutoReg( void (C::*method)(), const char* name, const char* description ) 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> template<typename T, size_t>