catch2/include/internal/catch_config.hpp

326 lines
10 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_CONFIG_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_CONFIG_HPP_INCLUDED
2012-08-23 21:08:50 +02:00
#include "catch_test_spec.h"
#include "catch_context.h"
#include "catch_interfaces_config.h"
#include "catch_stream.h"
#include <memory>
#include <vector>
#include <string>
2011-01-05 22:04:13 +01:00
#include <iostream>
#ifndef CATCH_CONFIG_CONSOLE_WIDTH
#define CATCH_CONFIG_CONSOLE_WIDTH 80
#endif
namespace CatchOverrides {
class ConfigGuard
{
public:
ConfigGuard()
: origConfig(Catch::getCurrentContext().getConfig())
{}
~ConfigGuard()
{
Catch::getCurrentMutableContext().setConfig(origConfig);
}
const Catch::Ptr<Catch::IConfig const>& value() const {return origConfig;}
private:
ConfigGuard(const ConfigGuard&);
ConfigGuard& operator=(const ConfigGuard&);
const Catch::Ptr<Catch::IConfig const> origConfig;
};
template <typename T>
class Config
{
typedef std::map<int, bool> LineData;
typedef std::map<std::string, LineData> FileLineData;
public:
bool includeSuccessfulResults(const std::string& file, int c) const
{
bool result(false);
FileLineData::const_iterator it = showSuccessfulTestsData.find(file);
if( it != showSuccessfulTestsData.end() )
{
for( LineData::const_iterator lineIt = it->second.begin(); lineIt != it->second.end(); ++lineIt )
{
if( c <= lineIt->first )
break;
result = lineIt->second;
}
}
return result;
}
void insertSuccessfulResults(const std::string& file, int c, bool v)
{
FileLineData::iterator it = showSuccessfulTestsData.find(file);
if( it == showSuccessfulTestsData.end() )
{
LineData tmp;
tmp.insert(std::make_pair(c,v));
showSuccessfulTestsData.insert(std::make_pair(file, tmp));
}
else
{
it->second.insert(std::make_pair(c,v));
}
}
bool warnAboutMissingAssertions(const std::string& file, int c) const
{
bool result(false);
FileLineData::const_iterator it = missingAssertionData.find(file);
if( it != missingAssertionData.end() )
{
for( LineData::const_iterator lineIt = it->second.begin(); lineIt != it->second.end(); ++lineIt )
{
if( c <= lineIt->first )
break;
result = lineIt->second;
}
}
return result;
}
void insertMissingAssertions(const std::string& file, int c, bool v)
{
FileLineData::iterator it = missingAssertionData.find(file);
if( it == missingAssertionData.end() )
{
LineData tmp;
tmp.insert(std::make_pair(c,v));
missingAssertionData.insert(std::make_pair(file, tmp));
}
else
{
it->second.insert(std::make_pair(c,v));
}
}
static Config<T>& instance()
{
if( !s_instance )
{
s_instance = new Config<T>();
}
return *s_instance;
}
private:
FileLineData showSuccessfulTestsData;
FileLineData missingAssertionData;
static Config<T>* s_instance;
};
template <typename T>
Config<T>* Config<T>::s_instance = NULL;
template <typename T>
struct ConfigReset
{
ConfigReset( const std::string& file, int c )
{
Config<T>::instance().insertSuccessfulResults(file, c, false);
Config<T>::instance().insertMissingAssertions(file, c, false);
}
};
template <typename T>
struct ConfigShowSuccessfulTests
{
template <typename U>
ConfigShowSuccessfulTests( const std::string& file, int c, U v )
{
Config<T>::instance().insertSuccessfulResults(file, c, v ? true : false);
}
};
template <typename T>
struct ConfigWarnMissingAssertions
{
template <typename U>
ConfigWarnMissingAssertions( const std::string& file, int c, U v )
{
Config<T>::instance().insertMissingAssertions(file, c, v ? true : false);
}
};
}
2012-05-16 00:58:23 +02:00
namespace Catch {
2012-06-08 09:22:56 +02:00
struct ConfigData {
2012-06-08 09:22:56 +02:00
ConfigData()
: listTests( false ),
listTags( false ),
listReporters( false ),
listTestNamesOnly( false ),
2013-05-29 19:34:11 +02:00
showSuccessfulTests( false ),
shouldDebugBreak( false ),
2013-05-29 19:34:11 +02:00
noThrow( false ),
showHelp( false ),
abortAfter( -1 ),
verbosity( Verbosity::Normal ),
warnings( WarnAbout::Nothing ),
showDurations( ShowDurations::DefaultForReporter )
2012-06-08 09:22:56 +02:00
{}
explicit ConfigData(const IConfig* other)
: listTests( false ),
listTags( false ),
listReporters( false ),
showSuccessfulTests( other ? other->includeSuccessfulResults() : false ),
shouldDebugBreak( false ),
noThrow( other ? !other->allowThrows() : false ),
showHelp( false ),
abortAfter( -1 ),
verbosity( Verbosity::Normal ),
warnings( other ? (other->warnAboutMissingAssertions() ? WarnAbout::NoAssertions : WarnAbout::Nothing) : WarnAbout::Nothing ),
showDurations( other ? other->showDurations() : ShowDurations::DefaultForReporter ),
name( other ? other->name() : std::string() )
{}
bool listTests;
bool listTags;
bool listReporters;
bool listTestNamesOnly;
2013-05-29 19:34:11 +02:00
bool showSuccessfulTests;
2012-06-08 09:22:56 +02:00
bool shouldDebugBreak;
2013-05-29 19:34:11 +02:00
bool noThrow;
bool showHelp;
int abortAfter;
Verbosity::Level verbosity;
WarnAbout::What warnings;
ShowDurations::OrNot showDurations;
std::string reporterName;
std::string outputFilename;
std::string name;
std::string processName;
std::vector<std::string> testsOrTags;
2012-06-08 09:22:56 +02:00
};
class Config : public SharedImpl<IConfig> {
private:
Config( Config const& other );
Config& operator = ( Config const& other );
virtual void dummy();
public:
Config()
2012-09-26 19:36:58 +02:00
: m_os( std::cout.rdbuf() )
2012-06-08 09:22:56 +02:00
{}
Config( ConfigData const& data )
2012-06-08 09:22:56 +02:00
: m_data( data ),
m_os( std::cout.rdbuf() )
{
if( !data.testsOrTags.empty() ) {
std::string groupName;
for( std::size_t i = 0; i < data.testsOrTags.size(); ++i ) {
if( i != 0 )
groupName += " ";
groupName += data.testsOrTags[i];
}
TestCaseFilters filters( groupName );
for( std::size_t i = 0; i < data.testsOrTags.size(); ++i ) {
std::string filter = data.testsOrTags[i];
if( startsWith( filter, "[" ) || startsWith( filter, "~[" ) )
filters.addTags( filter );
else
filters.addFilter( TestCaseFilter( filter ) );
}
m_filterSets.push_back( filters );
}
}
virtual ~Config() {
m_os.rdbuf( std::cout.rdbuf() );
2012-09-26 19:36:58 +02:00
m_stream.release();
}
void setFilename( std::string const& filename ) {
2012-06-08 09:22:56 +02:00
m_data.outputFilename = filename;
}
std::string const& getFilename() const {
2012-06-08 09:22:56 +02:00
return m_data.outputFilename ;
}
bool listTests() const { return m_data.listTests; }
bool listTestNamesOnly() const { return m_data.listTestNamesOnly; }
bool listTags() const { return m_data.listTags; }
bool listReporters() const { return m_data.listReporters; }
std::string getProcessName() const {
return m_data.processName;
}
2012-05-16 00:58:23 +02:00
bool shouldDebugBreak() const {
2012-06-08 09:22:56 +02:00
return m_data.shouldDebugBreak;
2010-12-30 19:51:02 +01:00
}
2012-05-16 00:58:23 +02:00
void setStreamBuf( std::streambuf* buf ) {
m_os.rdbuf( buf ? buf : std::cout.rdbuf() );
}
void useStream( std::string const& streamName ) {
2012-09-26 19:36:58 +02:00
Stream stream = createStream( streamName );
setStreamBuf( stream.streamBuf );
m_stream.release();
m_stream = stream;
}
std::string getReporterName() const { return m_data.reporterName; }
void addTestSpec( std::string const& testSpec ) {
TestCaseFilters filters( testSpec );
filters.addFilter( TestCaseFilter( testSpec ) );
m_filterSets.push_back( filters );
}
int abortAfter() const {
return m_data.abortAfter;
}
std::vector<TestCaseFilters> const& filters() const {
return m_filterSets;
}
2013-06-04 23:49:14 +02:00
bool showHelp() const { return m_data.showHelp; }
// IConfig interface
2013-05-29 19:34:11 +02:00
virtual bool allowThrows() const { return !m_data.noThrow; }
virtual std::ostream& stream() const { return m_os; }
virtual std::string name() const { return m_data.name.empty() ? m_data.processName : m_data.name; }
2013-05-29 19:34:11 +02:00
virtual bool includeSuccessfulResults() const { return m_data.showSuccessfulTests; }
virtual bool warnAboutMissingAssertions() const { return m_data.warnings & WarnAbout::NoAssertions; }
virtual ShowDurations::OrNot showDurations() const { return m_data.showDurations; }
2011-01-18 20:49:00 +01:00
private:
2012-06-08 09:22:56 +02:00
ConfigData m_data;
2012-09-26 19:36:58 +02:00
Stream m_stream;
mutable std::ostream m_os;
std::vector<TestCaseFilters> m_filterSets;
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_CONFIG_HPP_INCLUDED