mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
First cut of custom exception support
This commit is contained in:
63
internal/catch_exception_translator_registry.hpp
Normal file
63
internal/catch_exception_translator_registry.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* catch_exception_translator_registry.hpp
|
||||
* Catch
|
||||
*
|
||||
* Created by Phil on 20/04/2011.
|
||||
* Copyright 2011 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_EXCEPTION_TRANSLATOR_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_HPP_INCLUDED
|
||||
|
||||
#include "catch_interfaces_exception.h"
|
||||
|
||||
namespace Catch
|
||||
{
|
||||
class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
virtual void registerTranslator
|
||||
(
|
||||
IExceptionTranslator* translator
|
||||
)
|
||||
{
|
||||
m_translators.push_back( translator );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
virtual std::string translateActiveException
|
||||
()
|
||||
const
|
||||
{
|
||||
return tryTranslators( m_translators.begin() );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
std::string tryTranslators
|
||||
(
|
||||
std::vector<IExceptionTranslator*>::const_iterator it
|
||||
)
|
||||
const
|
||||
{
|
||||
if( it == m_translators.end() )
|
||||
return "Unknown exception";
|
||||
|
||||
try
|
||||
{
|
||||
return (*it)->translate();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
return tryTranslators( it+1 );
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<IExceptionTranslator*> m_translators;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_HPP_INCLUDED
|
@@ -24,6 +24,7 @@ namespace Catch
|
||||
struct IResultCapture;
|
||||
struct ITestCaseRegistry;
|
||||
struct IRunner;
|
||||
struct IExceptionTranslatorRegistry;
|
||||
class GeneratorsForTest;
|
||||
|
||||
class StreamBufBase : public std::streambuf
|
||||
@@ -58,6 +59,9 @@ namespace Catch
|
||||
static ITestCaseRegistry& getTestCaseRegistry
|
||||
();
|
||||
|
||||
static IExceptionTranslatorRegistry& getExceptionTranslatorRegistry
|
||||
();
|
||||
|
||||
static std::streambuf* createStreamBuf
|
||||
( const std::string& streamName
|
||||
);
|
||||
@@ -82,6 +86,7 @@ namespace Catch
|
||||
|
||||
std::auto_ptr<IReporterRegistry> m_reporterRegistry;
|
||||
std::auto_ptr<ITestCaseRegistry> m_testCaseRegistry;
|
||||
std::auto_ptr<IExceptionTranslatorRegistry> m_exceptionTranslatorRegistry;
|
||||
IRunner* m_runner;
|
||||
IResultCapture* m_resultCapture;
|
||||
std::map<std::string, GeneratorsForTest*> m_generatorsByTestName;
|
||||
|
@@ -12,6 +12,7 @@
|
||||
#include "catch_hub.h"
|
||||
#include "catch_reporter_registry.hpp"
|
||||
#include "catch_test_case_registry_impl.hpp"
|
||||
#include "catch_exception_translator_registry.hpp"
|
||||
#include "catch_runner_impl.hpp"
|
||||
#include "catch_generators_impl.hpp"
|
||||
#include "catch_stream.hpp"
|
||||
@@ -22,7 +23,8 @@ namespace Catch
|
||||
Hub::Hub
|
||||
()
|
||||
: m_reporterRegistry( new ReporterRegistry ),
|
||||
m_testCaseRegistry( new TestRegistry )
|
||||
m_testCaseRegistry( new TestRegistry ),
|
||||
m_exceptionTranslatorRegistry( new ExceptionTranslatorRegistry )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -73,6 +75,13 @@ namespace Catch
|
||||
return *me().m_testCaseRegistry.get();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
IExceptionTranslatorRegistry& Hub::getExceptionTranslatorRegistry
|
||||
()
|
||||
{
|
||||
return *me().m_exceptionTranslatorRegistry.get();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
std::streambuf* Hub::createStreamBuf
|
||||
(
|
||||
|
67
internal/catch_interfaces_exception.h
Normal file
67
internal/catch_interfaces_exception.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* catch_exception_interfaces.h
|
||||
* Catch
|
||||
*
|
||||
* Created by Phil on 20/04/2011.
|
||||
* Copyright 2011 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_INTERFACES_EXCEPTIONS_H_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Catch
|
||||
{
|
||||
typedef std::string(*exceptionTranslateFunction)();
|
||||
|
||||
struct IExceptionTranslator
|
||||
{
|
||||
virtual ~IExceptionTranslator(){}
|
||||
virtual std::string translate() const = 0;
|
||||
};
|
||||
|
||||
struct IExceptionTranslatorRegistry
|
||||
{
|
||||
virtual ~IExceptionTranslatorRegistry
|
||||
()
|
||||
{}
|
||||
|
||||
virtual void registerTranslator
|
||||
( IExceptionTranslator* translator
|
||||
) = 0;
|
||||
virtual std::string translateActiveException
|
||||
() const = 0;
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class ExceptionTranslator : public IExceptionTranslator
|
||||
{
|
||||
public:
|
||||
ExceptionTranslator()
|
||||
{
|
||||
Catch::Hub::getExceptionTranslatorRegistry().registerTranslator( this );
|
||||
}
|
||||
|
||||
virtual std::string translate() const
|
||||
{
|
||||
try
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch( T& ex )
|
||||
{
|
||||
return translate( ex );
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::string translate( T& ex ) const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
|
@@ -577,7 +577,7 @@ namespace Catch
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
acceptMessage( "unknown exception" );
|
||||
acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );
|
||||
acceptResult( ResultWas::ThrewException );
|
||||
}
|
||||
m_info.clear();
|
||||
|
Reference in New Issue
Block a user