2010-12-31 23:54:35 +01:00
|
|
|
/*
|
|
|
|
* catch_hub_impl.hpp
|
2011-01-11 10:13:31 +01:00
|
|
|
* Catch
|
2010-12-31 23:54:35 +01:00
|
|
|
*
|
|
|
|
* Created by Phil on 31/12/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "catch_hub.h"
|
2011-01-07 11:22:24 +01:00
|
|
|
#include "catch_reporter_registry.hpp"
|
|
|
|
#include "catch_test_case_registry_impl.hpp"
|
2011-01-11 10:13:31 +01:00
|
|
|
#include "catch_runner_impl.hpp"
|
2011-01-18 10:20:06 +01:00
|
|
|
#include "catch_stream.hpp"
|
2010-12-31 23:54:35 +01:00
|
|
|
|
|
|
|
namespace Catch
|
2011-01-18 10:20:06 +01:00
|
|
|
{
|
2011-01-07 11:01:40 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2011-01-07 11:22:24 +01:00
|
|
|
Hub::Hub
|
|
|
|
()
|
2011-01-07 11:01:40 +01:00
|
|
|
: m_reporterRegistry( new ReporterRegistry ),
|
|
|
|
m_testCaseRegistry( new TestRegistry )
|
2010-12-31 23:54:35 +01:00
|
|
|
{
|
|
|
|
}
|
2011-01-11 10:13:31 +01:00
|
|
|
|
2011-01-07 11:22:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
Hub& Hub::me
|
|
|
|
()
|
2011-01-07 11:01:40 +01:00
|
|
|
{
|
|
|
|
static Hub hub;
|
|
|
|
return hub;
|
|
|
|
}
|
|
|
|
|
2011-01-11 10:13:31 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
void Hub::setRunner( IRunner* runner )
|
|
|
|
{
|
|
|
|
me().m_runner = runner;
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
void Hub::setResultCapture( IResultCapture* resultCapture )
|
|
|
|
{
|
|
|
|
me().m_resultCapture = resultCapture;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
IResultCapture& Hub::getResultCapture
|
|
|
|
()
|
|
|
|
{
|
|
|
|
return *me().m_resultCapture;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
IRunner& Hub::getRunner
|
|
|
|
()
|
|
|
|
{
|
|
|
|
return *me().m_runner;
|
|
|
|
}
|
|
|
|
|
2011-01-07 11:22:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
IReporterRegistry& Hub::getReporterRegistry
|
|
|
|
()
|
2010-12-31 23:54:35 +01:00
|
|
|
{
|
|
|
|
return *me().m_reporterRegistry.get();
|
|
|
|
}
|
2011-01-07 11:01:40 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2011-01-07 11:22:24 +01:00
|
|
|
ITestCaseRegistry& Hub::getTestCaseRegistry
|
|
|
|
()
|
2011-01-07 11:01:40 +01:00
|
|
|
{
|
2011-01-07 11:22:24 +01:00
|
|
|
return *me().m_testCaseRegistry.get();
|
2011-01-07 11:01:40 +01:00
|
|
|
}
|
2011-01-18 10:20:06 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
std::streambuf* Hub::createStreamBuf
|
|
|
|
(
|
|
|
|
const std::string& streamName
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if( streamName == "stdout" ) return std::cout.rdbuf();
|
|
|
|
if( streamName == "stderr" ) return std::cerr.rdbuf();
|
|
|
|
if( streamName == "debug" ) return new StreamBufImpl<OutputDebugWriter>;
|
|
|
|
|
|
|
|
throw std::domain_error( "Unknown stream: " + streamName );
|
|
|
|
}
|
2011-01-27 00:23:33 +01:00
|
|
|
|
|
|
|
struct GeneratorInfo
|
|
|
|
{
|
|
|
|
GeneratorInfo
|
|
|
|
(
|
|
|
|
std::size_t size
|
|
|
|
)
|
|
|
|
: m_size( size ),
|
|
|
|
m_currentIndex( 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moveNext
|
|
|
|
()
|
|
|
|
{
|
|
|
|
if( ++m_currentIndex == m_size )
|
|
|
|
{
|
2011-01-27 23:29:36 +01:00
|
|
|
m_currentIndex = 0;
|
2011-01-27 00:23:33 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t getCurrentIndex
|
|
|
|
()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return m_currentIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t m_size;
|
|
|
|
std::size_t m_currentIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GeneratorsForTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
~GeneratorsForTest
|
|
|
|
()
|
|
|
|
{
|
|
|
|
deleteAll( m_generatorsInOrder );
|
|
|
|
}
|
|
|
|
|
|
|
|
GeneratorInfo& getGeneratorInfo
|
|
|
|
(
|
|
|
|
const std::string& fileInfo,
|
|
|
|
std::size_t size
|
|
|
|
)
|
|
|
|
{
|
|
|
|
std::map<std::string, GeneratorInfo*>::const_iterator it = m_generatorsByName.find( fileInfo );
|
|
|
|
if( it == m_generatorsByName.end() )
|
|
|
|
{
|
|
|
|
GeneratorInfo* info = new GeneratorInfo( size );
|
|
|
|
m_generatorsByName.insert( std::make_pair( fileInfo, info ) );
|
|
|
|
m_generatorsInOrder.push_back( info );
|
|
|
|
return *info;
|
|
|
|
}
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
|
2011-01-27 23:29:36 +01:00
|
|
|
bool moveNext
|
2011-01-27 00:23:33 +01:00
|
|
|
()
|
|
|
|
{
|
|
|
|
std::vector<GeneratorInfo*>::const_iterator it = m_generatorsInOrder.begin();
|
2011-01-27 23:29:36 +01:00
|
|
|
std::vector<GeneratorInfo*>::const_iterator itEnd = m_generatorsInOrder.end();
|
2011-01-27 00:23:33 +01:00
|
|
|
for(; it != itEnd; ++it )
|
|
|
|
{
|
2011-01-27 23:29:36 +01:00
|
|
|
if( (*it)->moveNext() )
|
|
|
|
return true;
|
2011-01-27 00:23:33 +01:00
|
|
|
}
|
2011-01-27 23:29:36 +01:00
|
|
|
return false;
|
2011-01-27 00:23:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<std::string, GeneratorInfo*> m_generatorsByName;
|
|
|
|
std::vector<GeneratorInfo*> m_generatorsInOrder;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
GeneratorsForTest* Hub::findGeneratorsForCurrentTest
|
|
|
|
()
|
|
|
|
{
|
|
|
|
std::string testName = getResultCapture().getCurrentTestName();
|
|
|
|
|
|
|
|
std::map<std::string, GeneratorsForTest*>::const_iterator it =
|
|
|
|
m_generatorsByTestName.find( testName );
|
|
|
|
return it != m_generatorsByTestName.end()
|
|
|
|
? it->second
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
GeneratorsForTest& Hub::getGeneratorsForCurrentTest
|
|
|
|
()
|
|
|
|
{
|
|
|
|
GeneratorsForTest* generators = findGeneratorsForCurrentTest();
|
|
|
|
if( !generators )
|
|
|
|
{
|
|
|
|
std::string testName = getResultCapture().getCurrentTestName();
|
|
|
|
generators = new GeneratorsForTest();
|
|
|
|
m_generatorsByTestName.insert( std::make_pair( testName, generators ) );
|
|
|
|
}
|
|
|
|
return *generators;
|
|
|
|
}
|
2011-01-18 10:20:06 +01:00
|
|
|
|
2011-01-27 00:23:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
size_t Hub::getGeneratorIndex
|
|
|
|
(
|
|
|
|
const std::string& fileInfo,
|
|
|
|
size_t totalSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return me().getGeneratorsForCurrentTest()
|
|
|
|
.getGeneratorInfo( fileInfo, totalSize )
|
|
|
|
.getCurrentIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
bool Hub::advanceGeneratorsForCurrentTest
|
|
|
|
()
|
|
|
|
{
|
|
|
|
GeneratorsForTest* generators = me().findGeneratorsForCurrentTest();
|
|
|
|
return generators && generators->moveNext();
|
|
|
|
}
|
2010-12-31 23:54:35 +01:00
|
|
|
}
|