catch2/include/internal/catch_config.hpp

186 lines
5.1 KiB
C++
Raw Normal View History

/*
* Created by Phil on 08/11/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)
*/
#ifndef TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED
#include "catch_interfaces_reporter.h"
#include "catch_context.h"
#include <memory>
#include <vector>
#include <string>
2011-01-05 22:04:13 +01:00
#include <iostream>
2012-05-16 00:58:23 +02:00
namespace Catch {
class Config : public IReporterConfig {
private:
Config( const Config& other );
Config& operator = ( const Config& other );
public:
2012-05-16 00:58:23 +02:00
struct Include { enum What {
FailedOnly,
SuccessfulResults
}; };
2012-05-16 00:58:23 +02:00
struct List{ enum What {
2011-01-01 01:37:49 +01:00
None = 0,
2011-01-01 01:37:49 +01:00
Reports = 1,
Tests = 2,
All = 3,
2011-01-01 01:37:49 +01:00
WhatMask = 0xf,
2011-01-01 01:37:49 +01:00
AsText = 0x10,
AsXml = 0x11,
2011-01-01 01:37:49 +01:00
AsMask = 0xf0
}; };
Config()
: m_listSpec( List::None ),
2010-12-30 19:51:02 +01:00
m_shouldDebugBreak( false ),
m_showHelp( false ),
m_streambuf( NULL ),
m_os( std::cout.rdbuf() ),
m_includeWhat( Include::FailedOnly )
{}
2012-05-16 00:58:23 +02:00
~Config() {
m_os.rdbuf( std::cout.rdbuf() );
delete m_streambuf;
}
2012-05-16 00:58:23 +02:00
void setReporter( const std::string& reporterName ) {
if( m_reporter.get() )
return setError( "Only one reporter may be specified" );
setReporter( getCurrentContext().getReporterRegistry().create( reporterName, *this ) );
}
2012-05-16 00:58:23 +02:00
void addTestSpec( const std::string& testSpec ) {
m_testSpecs.push_back( testSpec );
}
2011-01-18 20:49:00 +01:00
2012-05-16 00:58:23 +02:00
bool testsSpecified() const {
2011-01-18 20:49:00 +01:00
return !m_testSpecs.empty();
}
2012-05-16 00:58:23 +02:00
const std::vector<std::string>& getTestSpecs() const {
2011-01-18 20:49:00 +01:00
return m_testSpecs;
}
2012-05-16 00:58:23 +02:00
List::What getListSpec( void ) const {
2011-01-18 20:49:00 +01:00
return m_listSpec;
}
2011-01-11 20:48:48 +01:00
2012-05-16 00:58:23 +02:00
void setListSpec( List::What listSpec ) {
m_listSpec = listSpec;
}
2012-05-16 00:58:23 +02:00
void setFilename( const std::string& filename ) {
m_filename = filename;
}
2012-05-16 00:58:23 +02:00
const std::string& getFilename() const {
return m_filename;
}
2012-05-16 00:58:23 +02:00
const std::string& getMessage() const {
2011-01-18 20:49:00 +01:00
return m_message;
}
2012-05-16 00:58:23 +02:00
void setError( const std::string& errorMessage ) {
m_message = errorMessage + "\n\n" + "Usage: ...";
}
2012-05-16 00:58:23 +02:00
void setReporter( IReporter* reporter ) {
m_reporter = reporter;
}
2012-05-16 00:58:23 +02:00
Ptr<IReporter> getReporter() {
if( !m_reporter.get() )
const_cast<Config*>( this )->setReporter( getCurrentContext().getReporterRegistry().create( "basic", *this ) );
return m_reporter;
}
2011-01-11 20:48:48 +01:00
2012-05-16 00:58:23 +02:00
List::What listWhat() const {
2011-01-31 11:10:20 +01:00
return static_cast<List::What>( m_listSpec & List::WhatMask );
2011-01-11 20:48:48 +01:00
}
2012-05-16 00:58:23 +02:00
List::What listAs() const {
2011-01-31 11:10:20 +01:00
return static_cast<List::What>( m_listSpec & List::AsMask );
}
2012-05-16 00:58:23 +02:00
void setIncludeWhat( Include::What includeWhat ) {
m_includeWhat = includeWhat;
}
2011-01-11 20:48:48 +01:00
2012-05-16 00:58:23 +02:00
void setShouldDebugBreak( bool shouldDebugBreakFlag ) {
2011-01-31 11:10:20 +01:00
m_shouldDebugBreak = shouldDebugBreakFlag;
}
2011-01-11 20:48:48 +01:00
2012-05-16 00:58:23 +02:00
void setName( const std::string& name ) {
m_name = name;
}
2012-05-16 00:58:23 +02:00
std::string getName() const {
return m_name;
}
2012-05-16 00:58:23 +02:00
bool shouldDebugBreak() const {
return m_shouldDebugBreak;
}
2011-01-11 20:48:48 +01:00
2012-05-16 00:58:23 +02:00
void setShowHelp( bool showHelpFlag ) {
2011-01-31 11:10:20 +01:00
m_showHelp = showHelpFlag;
2010-12-30 19:51:02 +01:00
}
2011-01-11 20:48:48 +01:00
2012-05-16 00:58:23 +02:00
bool showHelp() const {
2010-12-30 19:51:02 +01:00
return m_showHelp;
}
2012-05-16 00:58:23 +02:00
virtual std::ostream& stream() const {
return m_os;
}
2012-05-16 00:58:23 +02:00
void setStreamBuf( std::streambuf* buf ) {
m_os.rdbuf( buf ? buf : std::cout.rdbuf() );
}
2012-05-16 00:58:23 +02:00
void useStream( const std::string& streamName ) {
std::streambuf* newBuf = Context::createStreamBuf( streamName );
setStreamBuf( newBuf );
delete m_streambuf;
m_streambuf = newBuf;
}
2012-05-16 00:58:23 +02:00
virtual bool includeSuccessfulResults() const {
return m_includeWhat == Include::SuccessfulResults;
}
2011-01-18 20:49:00 +01:00
private:
Ptr<IReporter> m_reporter;
std::string m_filename;
std::string m_message;
2011-01-01 01:37:49 +01:00
List::What m_listSpec;
std::vector<std::string> m_testSpecs;
bool m_shouldDebugBreak;
2010-12-30 19:51:02 +01:00
bool m_showHelp;
std::streambuf* m_streambuf;
mutable std::ostream m_os;
Include::What m_includeWhat;
std::string m_name;
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED