From ae475a3c19e9b770e66ea3a2ce3eaa2debecd4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 10 May 2022 13:52:16 +0200 Subject: [PATCH] Optional support for listener descriptions --- .../reporters/catch_reporter_registrars.hpp | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/catch2/reporters/catch_reporter_registrars.hpp b/src/catch2/reporters/catch_reporter_registrars.hpp index 31ecb080..19d97b3b 100644 --- a/src/catch2/reporters/catch_reporter_registrars.hpp +++ b/src/catch2/reporters/catch_reporter_registrars.hpp @@ -14,9 +14,23 @@ #include #include #include +#include + +#include namespace Catch { + namespace Detail { + template + struct has_description : std::false_type {}; + + template + struct has_description< + T, + void_t> + : std::true_type {}; + } + class IEventListener; using IEventListenerPtr = Detail::unique_ptr; @@ -45,20 +59,33 @@ namespace Catch { class ListenerRegistrar { class TypedListenerFactory : public EventListenerFactory { + StringRef m_defaultDescription; - IEventListenerPtr - create( IConfig const* config ) const override { - return Detail::make_unique(config); + std::string getDescriptionImpl( std::true_type ) const { + return T::getDescription(); } + + std::string getDescriptionImpl( std::false_type ) const { + return static_cast(m_defaultDescription); + } + + public: + TypedListenerFactory( StringRef defaultDescription ): + m_defaultDescription( defaultDescription ) {} + + IEventListenerPtr create( IConfig const* config ) const override { + return Detail::make_unique( config ); + } + std::string getDescription() const override { - return std::string(); + return getDescriptionImpl( Detail::has_description{} ); } }; public: - ListenerRegistrar() { - getMutableRegistryHub().registerListener( Detail::make_unique() ); + ListenerRegistrar(StringRef defaultDescription) { + getMutableRegistryHub().registerListener( Detail::make_unique(defaultDescription) ); } }; } @@ -74,7 +101,7 @@ namespace Catch { #define CATCH_REGISTER_LISTENER( listenerType ) \ CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - namespace{ Catch::ListenerRegistrar catch_internal_RegistrarFor##listenerType; } \ + namespace { Catch::ListenerRegistrar catch_internal_RegistrarFor##listenerType(#listenerType); } \ CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION #else // CATCH_CONFIG_DISABLE