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.
This commit is contained in:
Martin Hořeňovský 2022-03-14 15:04:42 +01:00
parent 18c58667d7
commit 4acc520f76
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
15 changed files with 36 additions and 24 deletions

View File

@ -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<EventListenerFactory> factory ) override {
m_reporterRegistry.registerListener( CATCH_MOVE(factory) );
}
void registerTest( Detail::unique_ptr<TestCaseInfo>&& testInfo, Detail::unique_ptr<ITestInvoker>&& invoker ) override {

View File

@ -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;

View File

@ -86,4 +86,5 @@ namespace Catch {
namespace Catch {
IReporterFactory::~IReporterFactory() = default;
EventListenerFactory::~EventListenerFactory() = default;
}

View File

@ -27,6 +27,7 @@ namespace Catch {
struct SourceLineInfo;
class StartupExceptionRegistry;
class EventListenerFactory;
using IReporterFactoryPtr = Detail::unique_ptr<IReporterFactory>;
@ -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<EventListenerFactory> factory ) = 0;
virtual void registerTest(Detail::unique_ptr<TestCaseInfo>&& testInfo, Detail::unique_ptr<ITestInvoker>&& invoker) = 0;
virtual void registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator ) = 0;
virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0;

View File

@ -15,6 +15,7 @@
namespace Catch {
struct ReporterConfig;
struct IConfig;
class IEventListener;
using IStreamingReporterPtr = Detail::unique_ptr<IEventListener>;
@ -27,6 +28,13 @@ namespace Catch {
virtual std::string getDescription() const = 0;
};
using IReporterFactoryPtr = Detail::unique_ptr<IReporterFactory>;
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

View File

@ -24,10 +24,11 @@ namespace Catch {
struct IReporterFactory;
using IReporterFactoryPtr = Detail::unique_ptr<IReporterFactory>;
struct ReporterConfig;
class EventListenerFactory;
struct IReporterRegistry {
using FactoryMap = std::map<std::string, IReporterFactoryPtr, Detail::CaseInsensitiveLess>;
using Listeners = std::vector<IReporterFactoryPtr>;
using Listeners = std::vector<Detail::unique_ptr<EventListenerFactory>>;
virtual ~IReporterRegistry(); // = default
virtual IStreamingReporterPtr create( std::string const& name, ReporterConfig const& config ) const = 0;

View File

@ -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<EventListenerFactory> factory ) {
m_listeners.push_back( CATCH_MOVE(factory) );
}

View File

@ -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<EventListenerFactory> factory );
FactoryMap const& getFactories() const override;
Listeners const& getListeners() const override;

View File

@ -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;

View File

@ -43,9 +43,10 @@ namespace Catch {
template<typename T>
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<T>(config);
}
std::string getDescription() const override {
@ -56,7 +57,7 @@ namespace Catch {
public:
ListenerRegistrar() {
getMutableRegistryHub().registerListener( Detail::make_unique<ListenerFactory>() );
getMutableRegistryHub().registerListener( Detail::make_unique<TypedListenerFactory>() );
}
};
}

View File

@ -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";

View File

@ -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";

View File

@ -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";
}

View File

@ -143,7 +143,7 @@ namespace {
public:
MockListener( std::string witness,
std::vector<std::string>& 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<std::string> records;
multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>(
"Goodbye", records, rep_config ) );
multiReporter.addListener( Catch::Detail::make_unique<MockListener>(
"Hello", records, rep_config ) );
multiReporter.addListener( Catch::Detail::make_unique<MockListener>(
"world", records, rep_config ) );
multiReporter.addListener(
Catch::Detail::make_unique<MockListener>( "Hello", records, &config ) );
multiReporter.addListener(
Catch::Detail::make_unique<MockListener>( "world", records, &config ) );
multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>(
"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<PreferenceListener>(
true, false, rep_config ) );
true, false, &config ) );
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true );
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false );
multiReporter.addListener(
Catch::Detail::make_unique<PreferenceListener>(
false, true, rep_config ) );
false, true, &config ) );
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true );
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true);
multiReporter.addListener(
Catch::Detail::make_unique<PreferenceListener>(
false, false, rep_config ) );
false, false, &config ) );
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true );
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true );
}

View File

@ -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;
}