mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 23:36:11 +01:00
Unify IExceptionTranslatorRegistry and ExceptionTranslatorRegistry
This commit is contained in:
parent
31b291ba26
commit
cfe859e0f3
@ -153,6 +153,7 @@ set(IMPL_SOURCES
|
|||||||
${SOURCES_DIR}/catch_timer.cpp
|
${SOURCES_DIR}/catch_timer.cpp
|
||||||
${SOURCES_DIR}/catch_tostring.cpp
|
${SOURCES_DIR}/catch_tostring.cpp
|
||||||
${SOURCES_DIR}/catch_totals.cpp
|
${SOURCES_DIR}/catch_totals.cpp
|
||||||
|
${SOURCES_DIR}/catch_translate_exception.cpp
|
||||||
${SOURCES_DIR}/catch_version.cpp
|
${SOURCES_DIR}/catch_version.cpp
|
||||||
${SOURCES_DIR}/internal/catch_assertion_handler.cpp
|
${SOURCES_DIR}/internal/catch_assertion_handler.cpp
|
||||||
${SOURCES_DIR}/internal/catch_case_insensitive_comparisons.cpp
|
${SOURCES_DIR}/internal/catch_case_insensitive_comparisons.cpp
|
||||||
|
@ -38,7 +38,7 @@ namespace Catch {
|
|||||||
ITestCaseRegistry const& getTestCaseRegistry() const override {
|
ITestCaseRegistry const& getTestCaseRegistry() const override {
|
||||||
return m_testCaseRegistry;
|
return m_testCaseRegistry;
|
||||||
}
|
}
|
||||||
IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override {
|
ExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override {
|
||||||
return m_exceptionTranslatorRegistry;
|
return m_exceptionTranslatorRegistry;
|
||||||
}
|
}
|
||||||
ITagAliasRegistry const& getTagAliasRegistry() const override {
|
ITagAliasRegistry const& getTagAliasRegistry() const override {
|
||||||
|
20
src/catch2/catch_translate_exception.cpp
Normal file
20
src/catch2/catch_translate_exception.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
// Copyright Catch2 Authors
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// (See accompanying file LICENSE.txt or copy at
|
||||||
|
// https://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
|
|
||||||
|
#include <catch2/catch_translate_exception.hpp>
|
||||||
|
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
|
||||||
|
|
||||||
|
namespace Catch {
|
||||||
|
namespace Detail {
|
||||||
|
void registerTranslatorImpl(
|
||||||
|
Detail::unique_ptr<IExceptionTranslator>&& translator ) {
|
||||||
|
getMutableRegistryHub().registerTranslator(
|
||||||
|
CATCH_MOVE( translator ) );
|
||||||
|
}
|
||||||
|
} // namespace Detail
|
||||||
|
} // namespace Catch
|
@ -15,6 +15,10 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
namespace Detail {
|
||||||
|
void registerTranslatorImpl(
|
||||||
|
Detail::unique_ptr<IExceptionTranslator>&& translator );
|
||||||
|
}
|
||||||
|
|
||||||
class ExceptionTranslatorRegistrar {
|
class ExceptionTranslatorRegistrar {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -48,9 +52,8 @@ namespace Catch {
|
|||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ExceptionTranslatorRegistrar( std::string(*translateFunction)( T const& ) ) {
|
ExceptionTranslatorRegistrar( std::string(*translateFunction)( T const& ) ) {
|
||||||
getMutableRegistryHub().registerTranslator(
|
Detail::registerTranslatorImpl( Detail::make_unique<ExceptionTranslator<T>>(
|
||||||
Detail::make_unique<ExceptionTranslator<T>>(translateFunction)
|
translateFunction ) );
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -10,5 +10,4 @@
|
|||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
IExceptionTranslator::~IExceptionTranslator() = default;
|
IExceptionTranslator::~IExceptionTranslator() = default;
|
||||||
IExceptionTranslatorRegistry::~IExceptionTranslatorRegistry() = default;
|
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,12 @@
|
|||||||
#ifndef CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED
|
#ifndef CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED
|
||||||
#define CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED
|
#define CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED
|
||||||
|
|
||||||
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
|
|
||||||
#include <catch2/internal/catch_unique_ptr.hpp>
|
#include <catch2/internal/catch_unique_ptr.hpp>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
using exceptionTranslateFunction = std::string(*)();
|
|
||||||
|
|
||||||
class IExceptionTranslator;
|
class IExceptionTranslator;
|
||||||
using ExceptionTranslators = std::vector<Detail::unique_ptr<IExceptionTranslator const>>;
|
using ExceptionTranslators = std::vector<Detail::unique_ptr<IExceptionTranslator const>>;
|
||||||
|
|
||||||
@ -26,12 +23,6 @@ namespace Catch {
|
|||||||
virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0;
|
virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class IExceptionTranslatorRegistry {
|
|
||||||
public:
|
|
||||||
virtual ~IExceptionTranslatorRegistry(); // = default
|
|
||||||
virtual std::string translateActiveException() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Catch
|
} // namespace Catch
|
||||||
|
|
||||||
#endif // CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED
|
#endif // CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED
|
||||||
|
@ -17,7 +17,7 @@ namespace Catch {
|
|||||||
class TestCaseHandle;
|
class TestCaseHandle;
|
||||||
struct TestCaseInfo;
|
struct TestCaseInfo;
|
||||||
class ITestCaseRegistry;
|
class ITestCaseRegistry;
|
||||||
class IExceptionTranslatorRegistry;
|
class ExceptionTranslatorRegistry;
|
||||||
class IExceptionTranslator;
|
class IExceptionTranslator;
|
||||||
class ReporterRegistry;
|
class ReporterRegistry;
|
||||||
class IReporterFactory;
|
class IReporterFactory;
|
||||||
@ -38,7 +38,7 @@ namespace Catch {
|
|||||||
virtual ReporterRegistry const& getReporterRegistry() const = 0;
|
virtual ReporterRegistry const& getReporterRegistry() const = 0;
|
||||||
virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0;
|
virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0;
|
||||||
virtual ITagAliasRegistry const& getTagAliasRegistry() const = 0;
|
virtual ITagAliasRegistry const& getTagAliasRegistry() const = 0;
|
||||||
virtual IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const = 0;
|
virtual ExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const = 0;
|
||||||
|
|
||||||
|
|
||||||
virtual StartupExceptionRegistry const& getStartupExceptionRegistry() const = 0;
|
virtual StartupExceptionRegistry const& getStartupExceptionRegistry() const = 0;
|
||||||
|
@ -6,18 +6,41 @@
|
|||||||
|
|
||||||
// SPDX-License-Identifier: BSL-1.0
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
#include <catch2/internal/catch_exception_translator_registry.hpp>
|
#include <catch2/internal/catch_exception_translator_registry.hpp>
|
||||||
|
#include <catch2/interfaces/catch_interfaces_exception.hpp>
|
||||||
#include <catch2/internal/catch_compiler_capabilities.hpp>
|
#include <catch2/internal/catch_compiler_capabilities.hpp>
|
||||||
#include <catch2/internal/catch_enforce.hpp>
|
#include <catch2/internal/catch_enforce.hpp>
|
||||||
#include <catch2/internal/catch_test_failure_exception.hpp>
|
#include <catch2/internal/catch_test_failure_exception.hpp>
|
||||||
#include <catch2/internal/catch_move_and_forward.hpp>
|
#include <catch2/internal/catch_move_and_forward.hpp>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() {
|
namespace {
|
||||||
|
static std::string tryTranslators(
|
||||||
|
std::vector<Detail::unique_ptr<IExceptionTranslator const>> const&
|
||||||
|
translators ) {
|
||||||
|
if ( translators.empty() ) {
|
||||||
|
std::rethrow_exception( std::current_exception() );
|
||||||
|
} else {
|
||||||
|
return translators[0]->translate( translators.begin() + 1,
|
||||||
|
translators.end() );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct ExceptionTranslatorRegistry::ExceptionTranslatorRegistryImpl {
|
||||||
|
std::vector<Detail::unique_ptr<IExceptionTranslator const>>
|
||||||
|
translators;
|
||||||
|
};
|
||||||
|
|
||||||
|
ExceptionTranslatorRegistry::ExceptionTranslatorRegistry():
|
||||||
|
m_impl( Detail::make_unique<ExceptionTranslatorRegistryImpl>() ) {}
|
||||||
|
ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() = default;
|
||||||
|
|
||||||
void ExceptionTranslatorRegistry::registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator ) {
|
void ExceptionTranslatorRegistry::registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator ) {
|
||||||
m_translators.push_back( CATCH_MOVE( translator ) );
|
m_impl->translators.push_back( CATCH_MOVE( translator ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
|
#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
|
||||||
@ -37,7 +60,7 @@ namespace Catch {
|
|||||||
// First we try user-registered translators. If none of them can
|
// First we try user-registered translators. If none of them can
|
||||||
// handle the exception, it will be rethrown handled by our defaults.
|
// handle the exception, it will be rethrown handled by our defaults.
|
||||||
try {
|
try {
|
||||||
return tryTranslators();
|
return tryTranslators(m_impl->translators);
|
||||||
}
|
}
|
||||||
// To avoid having to handle TFE explicitly everywhere, we just
|
// To avoid having to handle TFE explicitly everywhere, we just
|
||||||
// rethrow it so that it goes back up the caller.
|
// rethrow it so that it goes back up the caller.
|
||||||
@ -61,22 +84,10 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ExceptionTranslatorRegistry::tryTranslators() const {
|
|
||||||
if (m_translators.empty()) {
|
|
||||||
std::rethrow_exception(std::current_exception());
|
|
||||||
} else {
|
|
||||||
return m_translators[0]->translate(m_translators.begin() + 1, m_translators.end());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#else // ^^ Exceptions are enabled // Exceptions are disabled vv
|
#else // ^^ Exceptions are enabled // Exceptions are disabled vv
|
||||||
std::string ExceptionTranslatorRegistry::translateActiveException() const {
|
std::string ExceptionTranslatorRegistry::translateActiveException() const {
|
||||||
CATCH_INTERNAL_ERROR("Attempted to translate active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!");
|
CATCH_INTERNAL_ERROR("Attempted to translate active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ExceptionTranslatorRegistry::tryTranslators() const {
|
|
||||||
CATCH_INTERNAL_ERROR("Attempted to use exception translators under CATCH_CONFIG_DISABLE_EXCEPTIONS!");
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,23 +8,22 @@
|
|||||||
#ifndef CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
|
#ifndef CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
|
||||||
#define CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
|
#define CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
|
||||||
|
|
||||||
#include <catch2/interfaces/catch_interfaces_exception.hpp>
|
|
||||||
#include <catch2/internal/catch_unique_ptr.hpp>
|
#include <catch2/internal/catch_unique_ptr.hpp>
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
class IExceptionTranslator;
|
||||||
|
|
||||||
|
class ExceptionTranslatorRegistry {
|
||||||
|
struct ExceptionTranslatorRegistryImpl;
|
||||||
|
Detail::unique_ptr<ExceptionTranslatorRegistryImpl> m_impl;
|
||||||
|
|
||||||
class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
|
|
||||||
public:
|
public:
|
||||||
~ExceptionTranslatorRegistry() override;
|
ExceptionTranslatorRegistry();
|
||||||
|
~ExceptionTranslatorRegistry();
|
||||||
void registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator );
|
void registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator );
|
||||||
std::string translateActiveException() const override;
|
std::string translateActiveException() const;
|
||||||
std::string tryTranslators() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
ExceptionTranslators m_translators;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,6 +259,7 @@ internal_sources = files(
|
|||||||
'catch_timer.cpp',
|
'catch_timer.cpp',
|
||||||
'catch_tostring.cpp',
|
'catch_tostring.cpp',
|
||||||
'catch_totals.cpp',
|
'catch_totals.cpp',
|
||||||
|
'catch_translate_exception.cpp',
|
||||||
'catch_version.cpp',
|
'catch_version.cpp',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user