From 4acc520f763f7a104d11d57afb8d3bab0bfa6ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 14 Mar 2022 15:04:42 +0100 Subject: [PATCH] Event listeners no longer take reporter config in constructor This also required splitting out Listener factory from the reporter factory hierarchy. In return, the listener factories only need to take in `IConfig const*`, which opens up further refactorings down the road in the colour selection and implementation. --- src/catch2/catch_registry_hub.cpp | 2 +- src/catch2/catch_session.cpp | 2 +- .../catch_interfaces_combined_tu.cpp | 1 + .../catch_interfaces_registry_hub.hpp | 3 ++- .../catch_interfaces_reporter_factory.hpp | 8 ++++++++ .../catch_interfaces_reporter_registry.hpp | 3 ++- .../internal/catch_reporter_registry.cpp | 3 ++- .../internal/catch_reporter_registry.hpp | 2 +- .../catch_reporter_event_listener.hpp | 3 +-- .../reporters/catch_reporter_registrars.hpp | 7 ++++--- ...24-ListenerStdoutCaptureInMultireporter.cpp | 2 +- .../X25-ListenerCanAskForCapturedStdout.cpp | 2 +- .../X28-ListenersGetEventsBeforeReporters.cpp | 2 +- .../IntrospectiveTests/Reporters.tests.cpp | 18 +++++++++--------- tests/SelfTest/TestRegistrations.cpp | 2 +- 15 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/catch2/catch_registry_hub.cpp b/src/catch2/catch_registry_hub.cpp index edf9b2b5..1fca78fb 100644 --- a/src/catch2/catch_registry_hub.cpp +++ b/src/catch2/catch_registry_hub.cpp @@ -51,7 +51,7 @@ namespace Catch { void registerReporter( std::string const& name, IReporterFactoryPtr factory ) override { m_reporterRegistry.registerReporter( name, CATCH_MOVE(factory) ); } - void registerListener( IReporterFactoryPtr factory ) override { + void registerListener( Detail::unique_ptr factory ) override { m_reporterRegistry.registerListener( CATCH_MOVE(factory) ); } void registerTest( Detail::unique_ptr&& testInfo, Detail::unique_ptr&& invoker ) override { diff --git a/src/catch2/catch_session.cpp b/src/catch2/catch_session.cpp index fc9b44c5..3a47974d 100644 --- a/src/catch2/catch_session.cpp +++ b/src/catch2/catch_session.cpp @@ -52,7 +52,7 @@ namespace Catch { auto const& listeners = Catch::getRegistryHub().getReporterRegistry().getListeners(); for (auto const& listener : listeners) { - multi->addListener(listener->create(Catch::ReporterConfig(config, config->defaultStream()))); + multi->addListener(listener->create(config)); } std::size_t reporterIdx = 0; diff --git a/src/catch2/interfaces/catch_interfaces_combined_tu.cpp b/src/catch2/interfaces/catch_interfaces_combined_tu.cpp index b1ce0928..2e126a46 100644 --- a/src/catch2/interfaces/catch_interfaces_combined_tu.cpp +++ b/src/catch2/interfaces/catch_interfaces_combined_tu.cpp @@ -86,4 +86,5 @@ namespace Catch { namespace Catch { IReporterFactory::~IReporterFactory() = default; + EventListenerFactory::~EventListenerFactory() = default; } diff --git a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp index aeefeedb..1c40f2de 100644 --- a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp +++ b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp @@ -27,6 +27,7 @@ namespace Catch { struct SourceLineInfo; class StartupExceptionRegistry; + class EventListenerFactory; using IReporterFactoryPtr = Detail::unique_ptr; @@ -45,7 +46,7 @@ namespace Catch { struct IMutableRegistryHub { virtual ~IMutableRegistryHub(); // = default virtual void registerReporter( std::string const& name, IReporterFactoryPtr factory ) = 0; - virtual void registerListener( IReporterFactoryPtr factory ) = 0; + virtual void registerListener( Detail::unique_ptr factory ) = 0; virtual void registerTest(Detail::unique_ptr&& testInfo, Detail::unique_ptr&& invoker) = 0; virtual void registerTranslator( Detail::unique_ptr&& translator ) = 0; virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; diff --git a/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp b/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp index eb107c20..0f9b90cd 100644 --- a/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp +++ b/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp @@ -15,6 +15,7 @@ namespace Catch { struct ReporterConfig; + struct IConfig; class IEventListener; using IStreamingReporterPtr = Detail::unique_ptr; @@ -27,6 +28,13 @@ namespace Catch { virtual std::string getDescription() const = 0; }; using IReporterFactoryPtr = Detail::unique_ptr; + + class EventListenerFactory { + public: + virtual ~EventListenerFactory(); // = default + virtual IStreamingReporterPtr create( IConfig const* config ) const = 0; + virtual std::string getDescription() const = 0; + }; } // namespace Catch #endif // CATCH_INTERFACES_REPORTER_FACTORY_HPP_INCLUDED diff --git a/src/catch2/interfaces/catch_interfaces_reporter_registry.hpp b/src/catch2/interfaces/catch_interfaces_reporter_registry.hpp index 904ffd03..7a7b275d 100644 --- a/src/catch2/interfaces/catch_interfaces_reporter_registry.hpp +++ b/src/catch2/interfaces/catch_interfaces_reporter_registry.hpp @@ -24,10 +24,11 @@ namespace Catch { struct IReporterFactory; using IReporterFactoryPtr = Detail::unique_ptr; struct ReporterConfig; + class EventListenerFactory; struct IReporterRegistry { using FactoryMap = std::map; - using Listeners = std::vector; + using Listeners = std::vector>; virtual ~IReporterRegistry(); // = default virtual IStreamingReporterPtr create( std::string const& name, ReporterConfig const& config ) const = 0; diff --git a/src/catch2/internal/catch_reporter_registry.cpp b/src/catch2/internal/catch_reporter_registry.cpp index 67687d1b..7ac3a606 100644 --- a/src/catch2/internal/catch_reporter_registry.cpp +++ b/src/catch2/internal/catch_reporter_registry.cpp @@ -49,7 +49,8 @@ namespace Catch { "'::' is not allowed in reporter name: '" + name + '\'' ); m_factories.emplace(name, CATCH_MOVE(factory)); } - void ReporterRegistry::registerListener( IReporterFactoryPtr factory ) { + void ReporterRegistry::registerListener( + Detail::unique_ptr factory ) { m_listeners.push_back( CATCH_MOVE(factory) ); } diff --git a/src/catch2/internal/catch_reporter_registry.hpp b/src/catch2/internal/catch_reporter_registry.hpp index a761e7cf..aaa99ba0 100644 --- a/src/catch2/internal/catch_reporter_registry.hpp +++ b/src/catch2/internal/catch_reporter_registry.hpp @@ -24,7 +24,7 @@ namespace Catch { IStreamingReporterPtr create( std::string const& name, ReporterConfig const& config ) const override; void registerReporter( std::string const& name, IReporterFactoryPtr factory ); - void registerListener( IReporterFactoryPtr factory ); + void registerListener( Detail::unique_ptr factory ); FactoryMap const& getFactories() const override; Listeners const& getListeners() const override; diff --git a/src/catch2/reporters/catch_reporter_event_listener.hpp b/src/catch2/reporters/catch_reporter_event_listener.hpp index bf007c63..383b080a 100644 --- a/src/catch2/reporters/catch_reporter_event_listener.hpp +++ b/src/catch2/reporters/catch_reporter_event_listener.hpp @@ -21,8 +21,7 @@ namespace Catch { */ class EventListenerBase : public IEventListener { public: - EventListenerBase( ReporterConfig const& config ): - IEventListener( config.fullConfig() ) {} + using IEventListener::IEventListener; void reportInvalidTestSpec( StringRef unmatchedSpec ) override; void fatalErrorEncountered( StringRef error ) override; diff --git a/src/catch2/reporters/catch_reporter_registrars.hpp b/src/catch2/reporters/catch_reporter_registrars.hpp index 8b02e61f..e607c368 100644 --- a/src/catch2/reporters/catch_reporter_registrars.hpp +++ b/src/catch2/reporters/catch_reporter_registrars.hpp @@ -43,9 +43,10 @@ namespace Catch { template class ListenerRegistrar { - class ListenerFactory : public IReporterFactory { + class TypedListenerFactory : public EventListenerFactory { - IStreamingReporterPtr create( ReporterConfig const& config ) const override { + IStreamingReporterPtr + create( IConfig const* config ) const override { return Detail::make_unique(config); } std::string getDescription() const override { @@ -56,7 +57,7 @@ namespace Catch { public: ListenerRegistrar() { - getMutableRegistryHub().registerListener( Detail::make_unique() ); + getMutableRegistryHub().registerListener( Detail::make_unique() ); } }; } diff --git a/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp b/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp index 4c6148fd..60545851 100644 --- a/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp +++ b/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp @@ -25,7 +25,7 @@ namespace { class NonCapturingListener : public Catch::EventListenerBase { public: - NonCapturingListener( Catch::ReporterConfig const& config ): + NonCapturingListener( Catch::IConfig const* config ): EventListenerBase( config ) { m_preferences.shouldRedirectStdOut = false; std::cerr << "X24 - NonCapturingListener initialized.\n"; diff --git a/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp b/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp index b36a307a..a4a55ad7 100644 --- a/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp +++ b/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp @@ -25,7 +25,7 @@ namespace { class CapturingListener : public Catch::EventListenerBase { public: - CapturingListener( Catch::ReporterConfig const& config ): + CapturingListener( Catch::IConfig const* config ): EventListenerBase( config ) { m_preferences.shouldRedirectStdOut = true; std::cerr << "CapturingListener initialized\n"; diff --git a/tests/ExtraTests/X28-ListenersGetEventsBeforeReporters.cpp b/tests/ExtraTests/X28-ListenersGetEventsBeforeReporters.cpp index 37d14928..ce2d31eb 100644 --- a/tests/ExtraTests/X28-ListenersGetEventsBeforeReporters.cpp +++ b/tests/ExtraTests/X28-ListenersGetEventsBeforeReporters.cpp @@ -30,7 +30,7 @@ namespace { class TestListener : public Catch::EventListenerBase { public: - TestListener( Catch::ReporterConfig const& config ): + TestListener( Catch::IConfig const* config ): EventListenerBase( config ) { std::cout << "X28 - TestListener constructed.\n"; } diff --git a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp index dcb186b6..34afc1a2 100644 --- a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp @@ -143,7 +143,7 @@ namespace { public: MockListener( std::string witness, std::vector& recorder, - Catch::ReporterConfig const& config ): + Catch::IConfig const* config ): EventListenerBase( config ), m_witness( witness ), m_recorder( recorder ) @@ -187,10 +187,10 @@ TEST_CASE("Multireporter calls reporters and listeners in correct order", std::vector records; multiReporter.addReporter( Catch::Detail::make_unique( "Goodbye", records, rep_config ) ); - multiReporter.addListener( Catch::Detail::make_unique( - "Hello", records, rep_config ) ); - multiReporter.addListener( Catch::Detail::make_unique( - "world", records, rep_config ) ); + multiReporter.addListener( + Catch::Detail::make_unique( "Hello", records, &config ) ); + multiReporter.addListener( + Catch::Detail::make_unique( "world", records, &config ) ); multiReporter.addReporter( Catch::Detail::make_unique( "world", records, rep_config ) ); multiReporter.testRunStarting( { "" } ); @@ -206,7 +206,7 @@ namespace { public: PreferenceListener( bool redirectStdout, bool reportAllAssertions, - Catch::ReporterConfig const& config ): + Catch::IConfig const* config ): EventListenerBase( config ) { m_preferences.shouldRedirectStdOut = redirectStdout; m_preferences.shouldReportAllAssertions = reportAllAssertions; @@ -242,19 +242,19 @@ TEST_CASE("Multireporter updates ReporterPreferences properly", SECTION( "Adding listeners" ) { multiReporter.addListener( Catch::Detail::make_unique( - true, false, rep_config ) ); + true, false, &config ) ); REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false ); multiReporter.addListener( Catch::Detail::make_unique( - false, true, rep_config ) ); + false, true, &config ) ); REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true); multiReporter.addListener( Catch::Detail::make_unique( - false, false, rep_config ) ); + false, false, &config ) ); REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true ); } diff --git a/tests/SelfTest/TestRegistrations.cpp b/tests/SelfTest/TestRegistrations.cpp index 2b28c379..2586ffab 100644 --- a/tests/SelfTest/TestRegistrations.cpp +++ b/tests/SelfTest/TestRegistrations.cpp @@ -48,7 +48,7 @@ class ValidatingTestListener : public Catch::EventListenerBase { }; public: - ValidatingTestListener(Catch::ReporterConfig const& config) : + ValidatingTestListener(Catch::IConfig const* config) : EventListenerBase(config) { m_preferences.shouldReportAllAssertions = true; }