catch2/include/internal/catch_context_impl.hpp

110 lines
3.4 KiB
C++
Raw Normal View History

/*
* 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)
*/
2011-01-07 11:22:24 +01:00
#include "catch_test_case_registry_impl.hpp"
2011-01-11 10:13:31 +01:00
#include "catch_runner_impl.hpp"
#include "catch_generators_impl.hpp"
#include "catch_console_colour_impl.hpp"
2012-05-10 09:17:06 +02:00
#include "catch_exception_translator_registry.hpp"
#include "catch_context.h"
#include "catch_reporter_registry.hpp"
#include "catch_stream.hpp"
2012-05-16 09:02:20 +02:00
namespace Catch {
namespace {
2012-06-05 11:13:52 +02:00
Context* currentContext = NULL;
}
IMutableContext& getCurrentMutableContext() {
2012-06-05 11:13:52 +02:00
if( !currentContext )
currentContext = new Context();
return *currentContext;
}
IContext& getCurrentContext() {
return getCurrentMutableContext();
}
Context::Context()
2011-01-07 11:01:40 +01:00
: m_reporterRegistry( new ReporterRegistry ),
2011-04-20 16:40:40 +02:00
m_testCaseRegistry( new TestRegistry ),
m_exceptionTranslatorRegistry( new ExceptionTranslatorRegistry )
{}
2011-01-11 10:13:31 +01:00
void Context::cleanUp() {
2012-06-05 11:13:52 +02:00
delete currentContext;
currentContext = NULL;
}
void Context::setRunner( IRunner* runner ) {
m_runner = runner;
2011-01-11 10:13:31 +01:00
}
void Context::setResultCapture( IResultCapture* resultCapture ) {
m_resultCapture = resultCapture;
2011-01-11 10:13:31 +01:00
}
IResultCapture& Context::getResultCapture() {
return *m_resultCapture;
2011-01-11 10:13:31 +01:00
}
IRunner& Context::getRunner() {
return *m_runner;
2011-01-11 10:13:31 +01:00
}
IReporterRegistry& Context::getReporterRegistry() {
return *m_reporterRegistry.get();
}
2011-01-07 11:01:40 +01:00
ITestCaseRegistry& Context::getTestCaseRegistry() {
return *m_testCaseRegistry.get();
2011-01-07 11:01:40 +01:00
}
IExceptionTranslatorRegistry& Context::getExceptionTranslatorRegistry() {
return *m_exceptionTranslatorRegistry.get();
2011-04-20 16:40:40 +02:00
}
std::streambuf* Context::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
GeneratorsForTest* Context::findGeneratorsForCurrentTest() {
std::string testName = getResultCapture().getCurrentTestName();
2011-01-27 00:23:33 +01:00
std::map<std::string, GeneratorsForTest*>::const_iterator it =
m_generatorsByTestName.find( testName );
return it != m_generatorsByTestName.end()
? it->second
: NULL;
}
GeneratorsForTest& Context::getGeneratorsForCurrentTest() {
2011-01-27 00:23:33 +01:00
GeneratorsForTest* generators = findGeneratorsForCurrentTest();
2012-05-16 09:02:20 +02:00
if( !generators ) {
std::string testName = getResultCapture().getCurrentTestName();
2011-01-27 00:23:33 +01:00
generators = new GeneratorsForTest();
m_generatorsByTestName.insert( std::make_pair( testName, generators ) );
}
return *generators;
}
2012-05-16 09:02:20 +02:00
size_t Context::getGeneratorIndex( const std::string& fileInfo, size_t totalSize ) {
return getGeneratorsForCurrentTest()
2011-01-27 00:23:33 +01:00
.getGeneratorInfo( fileInfo, totalSize )
.getCurrentIndex();
}
bool Context::advanceGeneratorsForCurrentTest() {
GeneratorsForTest* generators = findGeneratorsForCurrentTest();
2011-01-27 00:23:33 +01:00
return generators && generators->moveNext();
}
}