diff options
| author | Martin Hořeňovský | 2022-05-15 10:35:07 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-05-15 10:35:12 +0200 |
| commit | 316025a0d87b12ca880255f7b2082e86beeeeac4 (patch) | |
| tree | 36b6dae003ff0becbe216353dd155e4b3af8aa6b /src/catch2 | |
| parent | 33aeb603fee982d8f7d5aec3e746741e8776942e (diff) | |
| download | catch3-316025a0d87b12ca880255f7b2082e86beeeeac4.tar.gz | |
Properly handle startup reporter registration errors
Diffstat (limited to 'src/catch2')
| -rw-r--r-- | src/catch2/reporters/catch_reporter_registrars.cpp | 30 | ||||
| -rw-r--r-- | src/catch2/reporters/catch_reporter_registrars.hpp | 42 |
2 files changed, 59 insertions, 13 deletions
diff --git a/src/catch2/reporters/catch_reporter_registrars.cpp b/src/catch2/reporters/catch_reporter_registrars.cpp new file mode 100644 index 00000000..fd1bc76c --- /dev/null +++ b/src/catch2/reporters/catch_reporter_registrars.cpp @@ -0,0 +1,30 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include <catch2/reporters/catch_reporter_registrars.hpp> + +#include <catch2/internal/catch_compiler_capabilities.hpp> + +namespace Catch { + namespace Detail { + + void registerReporterImpl( std::string const& name, + IReporterFactoryPtr reporterPtr ) { + CATCH_TRY { + getMutableRegistryHub().registerReporter( + name, CATCH_MOVE( reporterPtr ) ); + } + CATCH_CATCH_ALL { + // Do not throw when constructing global objects, instead + // register the exception to be processed later + getMutableRegistryHub().registerStartupException(); + } + } + + } // namespace Detail +} // namespace Catch diff --git a/src/catch2/reporters/catch_reporter_registrars.hpp b/src/catch2/reporters/catch_reporter_registrars.hpp index e0922524..e33ecf8b 100644 --- a/src/catch2/reporters/catch_reporter_registrars.hpp +++ b/src/catch2/reporters/catch_reporter_registrars.hpp @@ -12,6 +12,7 @@ #include <catch2/interfaces/catch_interfaces_reporter.hpp> #include <catch2/interfaces/catch_interfaces_reporter_factory.hpp> #include <catch2/internal/catch_compiler_capabilities.hpp> +#include <catch2/internal/catch_unique_name.hpp> #include <catch2/internal/catch_unique_ptr.hpp> #include <catch2/internal/catch_move_and_forward.hpp> #include <catch2/internal/catch_void_type.hpp> @@ -21,6 +22,7 @@ namespace Catch { namespace Detail { + template <typename T, typename = void> struct has_description : std::false_type {}; @@ -29,7 +31,13 @@ namespace Catch { T, void_t<decltype( T::getDescription() )>> : std::true_type {}; - } + + //! Indirection for reporter registration, so that the error handling is + //! independent on the reporter's concrete type + void registerReporterImpl( std::string const& name, + IReporterFactoryPtr reporterPtr ); + + } // namespace Detail class IEventListener; using IEventListenerPtr = Detail::unique_ptr<IEventListener>; @@ -51,7 +59,8 @@ namespace Catch { class ReporterRegistrar { public: explicit ReporterRegistrar( std::string const& name ) { - getMutableRegistryHub().registerReporter( name, Detail::make_unique<ReporterFactory<T>>() ); + registerReporterImpl( name, + Detail::make_unique<ReporterFactory<T>>() ); } }; @@ -95,17 +104,24 @@ namespace Catch { #if !defined(CATCH_CONFIG_DISABLE) -#define CATCH_REGISTER_REPORTER( name, reporterType ) \ - CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ - CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - namespace{ Catch::ReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name ); } \ - CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION - -#define CATCH_REGISTER_LISTENER( listenerType ) \ - CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ - CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - namespace { Catch::ListenerRegistrar<listenerType> catch_internal_RegistrarFor##listenerType(#listenerType); } \ - CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION +# define CATCH_REGISTER_REPORTER( name, reporterType ) \ + CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace { \ + Catch::ReporterRegistrar<reporterType> INTERNAL_CATCH_UNIQUE_NAME( \ + catch_internal_RegistrarFor )( name ); \ + } \ + CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION + +# define CATCH_REGISTER_LISTENER( listenerType ) \ + CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace { \ + Catch::ListenerRegistrar<listenerType> INTERNAL_CATCH_UNIQUE_NAME( \ + catch_internal_RegistrarFor )( #listenerType ); \ + } \ + CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION + #else // CATCH_CONFIG_DISABLE #define CATCH_REGISTER_REPORTER(name, reporterType) |