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)
|
|
|
|
*/
|
2012-09-17 07:42:29 +02:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_CONTEXT_IMPL_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_CONTEXT_IMPL_HPP_INCLUDED
|
|
|
|
|
2011-01-11 10:13:31 +01:00
|
|
|
#include "catch_runner_impl.hpp"
|
2012-05-10 09:17:06 +02:00
|
|
|
|
|
|
|
#include "catch_context.h"
|
2011-01-18 10:20:06 +01:00
|
|
|
#include "catch_stream.hpp"
|
2010-12-31 23:54:35 +01:00
|
|
|
|
2012-05-16 09:02:20 +02:00
|
|
|
namespace Catch {
|
2012-08-08 09:33:54 +02:00
|
|
|
|
|
|
|
class Context : public IMutableContext {
|
|
|
|
|
2013-10-23 16:35:07 +02:00
|
|
|
Context() : m_config( NULL ), m_runner( NULL ), m_resultCapture( NULL ) {}
|
2013-04-23 19:58:56 +02:00
|
|
|
Context( Context const& );
|
|
|
|
void operator=( Context const& );
|
2012-08-08 09:33:54 +02:00
|
|
|
|
|
|
|
public: // IContext
|
2014-05-20 19:49:28 +02:00
|
|
|
virtual IResultCapture* getResultCapture() {
|
|
|
|
return m_resultCapture;
|
2012-08-08 09:33:54 +02:00
|
|
|
}
|
2014-05-20 19:49:28 +02:00
|
|
|
virtual IRunner* getRunner() {
|
|
|
|
return m_runner;
|
2012-08-08 09:33:54 +02:00
|
|
|
}
|
2013-04-23 19:58:56 +02:00
|
|
|
virtual size_t getGeneratorIndex( std::string const& fileInfo, size_t totalSize ) {
|
2012-08-08 09:33:54 +02:00
|
|
|
return getGeneratorsForCurrentTest()
|
|
|
|
.getGeneratorInfo( fileInfo, totalSize )
|
|
|
|
.getCurrentIndex();
|
|
|
|
}
|
|
|
|
virtual bool advanceGeneratorsForCurrentTest() {
|
|
|
|
IGeneratorsForTest* generators = findGeneratorsForCurrentTest();
|
|
|
|
return generators && generators->moveNext();
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:51:53 +02:00
|
|
|
virtual Ptr<IConfig const> getConfig() const {
|
2012-08-08 09:33:54 +02:00
|
|
|
return m_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public: // IMutableContext
|
|
|
|
virtual void setResultCapture( IResultCapture* resultCapture ) {
|
|
|
|
m_resultCapture = resultCapture;
|
|
|
|
}
|
|
|
|
virtual void setRunner( IRunner* runner ) {
|
|
|
|
m_runner = runner;
|
|
|
|
}
|
2013-05-28 19:51:53 +02:00
|
|
|
virtual void setConfig( Ptr<IConfig const> const& config ) {
|
2012-08-08 09:33:54 +02:00
|
|
|
m_config = config;
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-08-08 09:33:54 +02:00
|
|
|
friend IMutableContext& getCurrentMutableContext();
|
|
|
|
|
|
|
|
private:
|
|
|
|
IGeneratorsForTest* findGeneratorsForCurrentTest() {
|
2014-05-20 19:49:28 +02:00
|
|
|
std::string testName = getResultCapture()->getCurrentTestName();
|
2012-08-08 09:33:54 +02:00
|
|
|
|
|
|
|
std::map<std::string, IGeneratorsForTest*>::const_iterator it =
|
|
|
|
m_generatorsByTestName.find( testName );
|
|
|
|
return it != m_generatorsByTestName.end()
|
2013-07-23 09:15:34 +02:00
|
|
|
? it->second
|
|
|
|
: NULL;
|
2012-08-08 09:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IGeneratorsForTest& getGeneratorsForCurrentTest() {
|
|
|
|
IGeneratorsForTest* generators = findGeneratorsForCurrentTest();
|
|
|
|
if( !generators ) {
|
2014-05-20 19:49:28 +02:00
|
|
|
std::string testName = getResultCapture()->getCurrentTestName();
|
2012-08-08 09:33:54 +02:00
|
|
|
generators = createGeneratorsForTest();
|
|
|
|
m_generatorsByTestName.insert( std::make_pair( testName, generators ) );
|
|
|
|
}
|
|
|
|
return *generators;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-10-23 16:35:07 +02:00
|
|
|
Ptr<IConfig const> m_config;
|
2012-08-08 09:33:54 +02:00
|
|
|
IRunner* m_runner;
|
|
|
|
IResultCapture* m_resultCapture;
|
|
|
|
std::map<std::string, IGeneratorsForTest*> m_generatorsByTestName;
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-22 09:55:19 +02:00
|
|
|
namespace {
|
2012-06-05 11:13:52 +02:00
|
|
|
Context* currentContext = NULL;
|
2012-05-22 09:55:19 +02:00
|
|
|
}
|
|
|
|
IMutableContext& getCurrentMutableContext() {
|
2012-06-05 11:13:52 +02:00
|
|
|
if( !currentContext )
|
|
|
|
currentContext = new Context();
|
|
|
|
return *currentContext;
|
2012-05-22 09:55:19 +02:00
|
|
|
}
|
|
|
|
IContext& getCurrentContext() {
|
|
|
|
return getCurrentMutableContext();
|
|
|
|
}
|
2012-02-18 10:58:30 +01:00
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
Stream createStream( std::string const& streamName ) {
|
2012-09-26 19:36:58 +02:00
|
|
|
if( streamName == "stdout" ) return Stream( std::cout.rdbuf(), false );
|
|
|
|
if( streamName == "stderr" ) return Stream( std::cerr.rdbuf(), false );
|
|
|
|
if( streamName == "debug" ) return Stream( new StreamBufImpl<OutputDebugWriter>, true );
|
2011-01-27 00:23:33 +01:00
|
|
|
|
2012-08-08 09:33:54 +02:00
|
|
|
throw std::domain_error( "Unknown stream: " + streamName );
|
2011-01-27 00:23:33 +01:00
|
|
|
}
|
|
|
|
|
2012-08-08 09:33:54 +02:00
|
|
|
void cleanUpContext() {
|
|
|
|
delete currentContext;
|
|
|
|
currentContext = NULL;
|
2011-01-27 00:23:33 +01:00
|
|
|
}
|
2010-12-31 23:54:35 +01:00
|
|
|
}
|
2012-09-17 07:42:29 +02:00
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_CONTEXT_IMPL_HPP_INCLUDED
|