catch2/include/internal/catch_config.hpp

251 lines
7.6 KiB
C++
Raw Normal View History

/*
2011-01-01 01:29:58 +01:00
* catch_config.hpp
* Catch
*
* 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"
2011-01-05 22:04:13 +01:00
#include "catch_hub.h"
#include <memory>
#include <vector>
#include <string>
2011-01-05 22:04:13 +01:00
#include <iostream>
namespace Catch
{
class Config : public IReporterConfig
{
private:
Config( const Config& other );
Config& operator = ( const Config& other );
public:
struct Include { enum What
{
FailedOnly,
SuccessfulResults
}; };
2011-01-01 01:37:49 +01: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
}; };
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
Config()
: m_reporter( NULL ),
2011-01-01 01:37:49 +01:00
m_listSpec( List::None ),
2010-12-30 19:51:02 +01:00
m_shouldDebugBreak( false ),
m_showHelp( false ),
m_streambuf( std::cout.rdbuf() ),
m_os( m_streambuf ),
m_includeWhat( Include::FailedOnly )
{}
///////////////////////////////////////////////////////////////////////////
~Config()
{
setStreamBuf( NULL );
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
void setReporter( const std::string& reporterName )
{
if( m_reporter.get() )
return setError( "Only one reporter may be specified" );
setReporter( Hub::getReporterRegistry().create( reporterName, *this ) );
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
void addTestSpec( const std::string& testSpec )
{
m_testSpecs.push_back( testSpec );
}
2011-01-18 20:49:00 +01:00
///////////////////////////////////////////////////////////////////////////
bool testsSpecified() const
{
return !m_testSpecs.empty();
}
///////////////////////////////////////////////////////////////////////////
const std::vector<std::string>& getTestSpecs() const
{
return m_testSpecs;
}
///////////////////////////////////////////////////////////////////////////
List::What getListSpec( void ) const
{
return m_listSpec;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
2011-01-01 01:37:49 +01:00
void setListSpec( List::What listSpec )
{
m_listSpec = listSpec;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
void setFilename( const std::string& filename )
{
m_filename = filename;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
2011-01-18 20:49:00 +01:00
const std::string& getFilename() const
{
return m_filename;
}
2011-01-18 20:49:00 +01:00
///////////////////////////////////////////////////////////////////////////
const std::string& getMessage() const
{
return m_message;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
void setError( const std::string& errorMessage )
{
m_message = errorMessage + "\n\n" + "Usage: ...";
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
void setReporter( IReporter* reporter )
{
m_reporter = std::auto_ptr<IReporter>( reporter );
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
2011-01-18 20:49:00 +01:00
IReporter* getReporter() const
{
if( !m_reporter.get() )
2011-01-18 20:49:00 +01:00
const_cast<Config*>( this )->setReporter( Hub::getReporterRegistry().create( "basic", *this ) );
return m_reporter.get();
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
2011-01-01 01:37:49 +01: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
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
2011-01-01 01:37:49 +01:00
List::What listAs() const
{
2011-01-31 11:10:20 +01:00
return static_cast<List::What>( m_listSpec & List::AsMask );
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
void setIncludeWhat( Include::What includeWhat )
{
m_includeWhat = includeWhat;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
2011-01-31 11:10:20 +01:00
void setShouldDebugBreak( bool shouldDebugBreakFlag )
{
2011-01-31 11:10:20 +01:00
m_shouldDebugBreak = shouldDebugBreakFlag;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
void setName( const std::string& name )
{
m_name = name;
}
///////////////////////////////////////////////////////////////////////////
std::string getName() const
{
return m_name;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
bool shouldDebugBreak() const
{
return m_shouldDebugBreak;
}
2011-01-11 20:48:48 +01:00
///////////////////////////////////////////////////////////////////////////
2011-01-31 11:10:20 +01:00
void setShowHelp( bool showHelpFlag )
2010-12-30 19:51:02 +01:00
{
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
///////////////////////////////////////////////////////////////////////////
2010-12-30 19:51:02 +01:00
bool showHelp() const
{
return m_showHelp;
}
///////////////////////////////////////////////////////////////////////////
virtual std::ostream& stream() const
{
return m_os;
}
///////////////////////////////////////////////////////////////////////////
void setStreamBuf( std::streambuf* buf )
{
// Delete previous stream buf if we own it
if( m_streambuf && dynamic_cast<StreamBufBase*>( m_streambuf ) )
delete m_streambuf;
m_streambuf = buf;
m_os.rdbuf( buf ? buf : std::cout.rdbuf() );
}
///////////////////////////////////////////////////////////////////////////
void useStream( const std::string& streamName )
{
setStreamBuf( Hub::createStreamBuf( streamName ) );
}
///////////////////////////////////////////////////////////////////////////
virtual bool includeSuccessfulResults() const
{
return m_includeWhat == Include::SuccessfulResults;
}
2011-01-18 20:49:00 +01:00
private:
std::auto_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