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
#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;
};
}

View File

@ -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

View File

@ -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()

View File

@ -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 )
{

View File

@ -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>