Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2
diff options
context:
space:
mode:
authorMartin Hořeňovský2022-05-10 20:00:36 +0200
committerMartin Hořeňovský2022-05-10 20:57:59 +0200
commit5a49285e9cf203dd20b00ac7488a6a3233d60fff (patch)
tree0a232d7ec81e8f34d37d7fbbe7ccc2f212671eb2 /src/catch2
parentae475a3c19e9b770e66ea3a2ce3eaa2debecd4c6 (diff)
downloadcatch3-5a49285e9cf203dd20b00ac7488a6a3233d60fff.tar.gz
Add --list-listeners option
Diffstat (limited to 'src/catch2')
-rw-r--r--src/catch2/catch_config.cpp1
-rw-r--r--src/catch2/catch_config.hpp2
-rw-r--r--src/catch2/interfaces/catch_interfaces_reporter.hpp4
-rw-r--r--src/catch2/interfaces/catch_interfaces_reporter_factory.hpp4
-rw-r--r--src/catch2/internal/catch_commandline.cpp5
-rw-r--r--src/catch2/internal/catch_list.cpp17
-rw-r--r--src/catch2/internal/catch_list.hpp4
-rw-r--r--src/catch2/reporters/catch_reporter_combined_tu.cpp29
-rw-r--r--src/catch2/reporters/catch_reporter_common_base.cpp5
-rw-r--r--src/catch2/reporters/catch_reporter_common_base.hpp8
-rw-r--r--src/catch2/reporters/catch_reporter_event_listener.hpp2
-rw-r--r--src/catch2/reporters/catch_reporter_helpers.hpp7
-rw-r--r--src/catch2/reporters/catch_reporter_multi.cpp7
-rw-r--r--src/catch2/reporters/catch_reporter_multi.hpp1
-rw-r--r--src/catch2/reporters/catch_reporter_registrars.hpp17
15 files changed, 104 insertions, 9 deletions
diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp
index e3ce6b65..670cc4f7 100644
--- a/src/catch2/catch_config.cpp
+++ b/src/catch2/catch_config.cpp
@@ -111,6 +111,7 @@ namespace Catch {
bool Config::listTests() const { return m_data.listTests; }
bool Config::listTags() const { return m_data.listTags; }
bool Config::listReporters() const { return m_data.listReporters; }
+ bool Config::listListeners() const { return m_data.listListeners; }
std::vector<std::string> const& Config::getTestsOrTags() const { return m_data.testsOrTags; }
std::vector<std::string> const& Config::getSectionsToRun() const { return m_data.sectionsToRun; }
diff --git a/src/catch2/catch_config.hpp b/src/catch2/catch_config.hpp
index a3dc010b..10df4d64 100644
--- a/src/catch2/catch_config.hpp
+++ b/src/catch2/catch_config.hpp
@@ -48,6 +48,7 @@ namespace Catch {
bool listTests = false;
bool listTags = false;
bool listReporters = false;
+ bool listListeners = false;
bool showSuccessfulTests = false;
bool shouldDebugBreak = false;
@@ -99,6 +100,7 @@ namespace Catch {
bool listTests() const;
bool listTags() const;
bool listReporters() const;
+ bool listListeners() const;
std::vector<ReporterSpec> const& getReporterSpecs() const;
std::vector<ProcessedReporterSpec> const&
diff --git a/src/catch2/interfaces/catch_interfaces_reporter.hpp b/src/catch2/interfaces/catch_interfaces_reporter.hpp
index 1a2736f7..42192438 100644
--- a/src/catch2/interfaces/catch_interfaces_reporter.hpp
+++ b/src/catch2/interfaces/catch_interfaces_reporter.hpp
@@ -27,6 +27,7 @@
namespace Catch {
struct ReporterDescription;
+ struct ListenerDescription;
struct TagInfo;
struct TestCaseInfo;
class TestCaseHandle;
@@ -249,11 +250,12 @@ namespace Catch {
//! Writes out information about provided reporters using reporter-specific format
virtual void listReporters(std::vector<ReporterDescription> const& descriptions) = 0;
+ //! Writes out the provided listeners descriptions using reporter-specific format
+ virtual void listListeners(std::vector<ListenerDescription> const& descriptions) = 0;
//! Writes out information about provided tests using reporter-specific format
virtual void listTests(std::vector<TestCaseHandle> const& tests) = 0;
//! Writes out information about the provided tags using reporter-specific format
virtual void listTags(std::vector<TagInfo> const& tags) = 0;
-
};
using IEventListenerPtr = Detail::unique_ptr<IEventListener>;
diff --git a/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp b/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp
index 76dce504..323edde2 100644
--- a/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp
+++ b/src/catch2/interfaces/catch_interfaces_reporter_factory.hpp
@@ -9,6 +9,7 @@
#define CATCH_INTERFACES_REPORTER_FACTORY_HPP_INCLUDED
#include <catch2/internal/catch_unique_ptr.hpp>
+#include <catch2/internal/catch_stringref.hpp>
#include <string>
@@ -34,6 +35,9 @@ namespace Catch {
public:
virtual ~EventListenerFactory(); // = default
virtual IEventListenerPtr create( IConfig const* config ) const = 0;
+ //! Return a meaningful name for the listener, e.g. its type name
+ virtual StringRef getName() const = 0;
+ //! Return listener's description if available
virtual std::string getDescription() const = 0;
};
} // namespace Catch
diff --git a/src/catch2/internal/catch_commandline.cpp b/src/catch2/internal/catch_commandline.cpp
index f2f638bc..fda4b2bd 100644
--- a/src/catch2/internal/catch_commandline.cpp
+++ b/src/catch2/internal/catch_commandline.cpp
@@ -280,7 +280,10 @@ namespace Catch {
( "list all/matching tags" )
| Opt( config.listReporters )
["--list-reporters"]
- ( "list all reporters" )
+ ( "list all available reporters" )
+ | Opt( config.listListeners )
+ ["--list-listeners"]
+ ( "list all listeners" )
| Opt( setTestOrder, "decl|lex|rand" )
["--order"]
( "test case order (defaults to decl)" )
diff --git a/src/catch2/internal/catch_list.cpp b/src/catch2/internal/catch_list.cpp
index 609d459c..2c22745c 100644
--- a/src/catch2/internal/catch_list.cpp
+++ b/src/catch2/internal/catch_list.cpp
@@ -63,6 +63,19 @@ namespace Catch {
reporter.listReporters(descriptions);
}
+ void listListeners(IEventListener& reporter) {
+ std::vector<ListenerDescription> descriptions;
+
+ auto const& factories =
+ getRegistryHub().getReporterRegistry().getListeners();
+ descriptions.reserve( factories.size() );
+ for ( auto const& fac : factories ) {
+ descriptions.push_back( { fac->getName(), fac->getDescription() } );
+ }
+
+ reporter.listListeners( descriptions );
+ }
+
} // end anonymous namespace
void TagInfo::add( StringRef spelling ) {
@@ -100,6 +113,10 @@ namespace Catch {
listed = true;
listReporters(reporter);
}
+ if ( config.listListeners() ) {
+ listed = true;
+ listListeners( reporter );
+ }
return listed;
}
diff --git a/src/catch2/internal/catch_list.hpp b/src/catch2/internal/catch_list.hpp
index 9caf4cf6..6fd759ea 100644
--- a/src/catch2/internal/catch_list.hpp
+++ b/src/catch2/internal/catch_list.hpp
@@ -23,6 +23,10 @@ namespace Catch {
struct ReporterDescription {
std::string name, description;
};
+ struct ListenerDescription {
+ StringRef name;
+ std::string description;
+ };
struct TagInfo {
void add(StringRef spelling);
diff --git a/src/catch2/reporters/catch_reporter_combined_tu.cpp b/src/catch2/reporters/catch_reporter_combined_tu.cpp
index 77d2b52d..06259433 100644
--- a/src/catch2/reporters/catch_reporter_combined_tu.cpp
+++ b/src/catch2/reporters/catch_reporter_combined_tu.cpp
@@ -152,6 +152,33 @@ namespace Catch {
out << '\n' << std::flush;
}
+ void defaultListListeners( std::ostream& out,
+ std::vector<ListenerDescription> const& descriptions ) {
+ const auto maxNameLen =
+ std::max_element( descriptions.begin(),
+ descriptions.end(),
+ []( ListenerDescription const& lhs,
+ ListenerDescription const& rhs ) {
+ return lhs.name.size() < rhs.name.size();
+ } )
+ ->name.size();
+
+ out << "Registered listeners:\n";
+ for ( auto const& desc : descriptions ) {
+ out << TextFlow::Column( static_cast<std::string>( desc.name ) +
+ ':' )
+ .indent( 2 )
+ .width( maxNameLen + 5 ) +
+ TextFlow::Column( desc.description )
+ .initialIndent( 0 )
+ .indent( 2 )
+ .width( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen - 8 )
+ << '\n';
+ }
+
+ out << '\n' << std::flush;
+ }
+
void defaultListTags( std::ostream& out,
std::vector<TagInfo> const& tags,
bool isFiltered ) {
@@ -233,6 +260,8 @@ namespace Catch {
void EventListenerBase::assertionEnded( AssertionStats const& ) {}
void EventListenerBase::listReporters(
std::vector<ReporterDescription> const& ) {}
+ void EventListenerBase::listListeners(
+ std::vector<ListenerDescription> const& ) {}
void EventListenerBase::listTests( std::vector<TestCaseHandle> const& ) {}
void EventListenerBase::listTags( std::vector<TagInfo> const& ) {}
void EventListenerBase::noMatchingTestCases( StringRef ) {}
diff --git a/src/catch2/reporters/catch_reporter_common_base.cpp b/src/catch2/reporters/catch_reporter_common_base.cpp
index f4736ab2..698435d0 100644
--- a/src/catch2/reporters/catch_reporter_common_base.cpp
+++ b/src/catch2/reporters/catch_reporter_common_base.cpp
@@ -29,6 +29,11 @@ namespace Catch {
defaultListReporters(m_stream, descriptions, m_config->verbosity());
}
+ void ReporterBase::listListeners(
+ std::vector<ListenerDescription> const& descriptions ) {
+ defaultListListeners( m_stream, descriptions );
+ }
+
void ReporterBase::listTests(std::vector<TestCaseHandle> const& tests) {
defaultListTests(m_stream,
m_colour.get(),
diff --git a/src/catch2/reporters/catch_reporter_common_base.hpp b/src/catch2/reporters/catch_reporter_common_base.hpp
index 5d80ca29..e6888be1 100644
--- a/src/catch2/reporters/catch_reporter_common_base.hpp
+++ b/src/catch2/reporters/catch_reporter_common_base.hpp
@@ -51,6 +51,14 @@ namespace Catch {
void listReporters(
std::vector<ReporterDescription> const& descriptions ) override;
/**
+ * Provides a simple default listing of listeners
+ *
+ * Looks similarly to listing of reporters, but with listener type
+ * instead of reporter name.
+ */
+ void listListeners(
+ std::vector<ListenerDescription> const& descriptions ) override;
+ /**
* Provides a simple default listing of tests.
*
* Should look roughly like the test listing in v2 and earlier versions
diff --git a/src/catch2/reporters/catch_reporter_event_listener.hpp b/src/catch2/reporters/catch_reporter_event_listener.hpp
index 383b080a..3a6d822c 100644
--- a/src/catch2/reporters/catch_reporter_event_listener.hpp
+++ b/src/catch2/reporters/catch_reporter_event_listener.hpp
@@ -36,6 +36,8 @@ namespace Catch {
void listReporters(
std::vector<ReporterDescription> const& descriptions ) override;
+ void listListeners(
+ std::vector<ListenerDescription> const& descriptions ) override;
void listTests( std::vector<TestCaseHandle> const& tests ) override;
void listTags( std::vector<TagInfo> const& tagInfos ) override;
diff --git a/src/catch2/reporters/catch_reporter_helpers.hpp b/src/catch2/reporters/catch_reporter_helpers.hpp
index 28ab966c..ef43534c 100644
--- a/src/catch2/reporters/catch_reporter_helpers.hpp
+++ b/src/catch2/reporters/catch_reporter_helpers.hpp
@@ -50,6 +50,13 @@ namespace Catch {
Verbosity verbosity );
/**
+ * Lists listeners descriptions to the provided stream in user-friendly
+ * format
+ */
+ void defaultListListeners( std::ostream& out,
+ std::vector<ListenerDescription> const& descriptions );
+
+ /**
* Lists tag information to the provided stream in user-friendly format
*
* Used as the default listing implementation by the first party reporter
diff --git a/src/catch2/reporters/catch_reporter_multi.cpp b/src/catch2/reporters/catch_reporter_multi.cpp
index e7ba7e5d..cf18cb81 100644
--- a/src/catch2/reporters/catch_reporter_multi.cpp
+++ b/src/catch2/reporters/catch_reporter_multi.cpp
@@ -176,6 +176,13 @@ namespace Catch {
}
}
+ void MultiReporter::listListeners(
+ std::vector<ListenerDescription> const& descriptions ) {
+ for ( auto& reporterish : m_reporterLikes ) {
+ reporterish->listListeners( descriptions );
+ }
+ }
+
void MultiReporter::listTests(std::vector<TestCaseHandle> const& tests) {
for (auto& reporterish : m_reporterLikes) {
reporterish->listTests(tests);
diff --git a/src/catch2/reporters/catch_reporter_multi.hpp b/src/catch2/reporters/catch_reporter_multi.hpp
index 29f2693f..e35ea504 100644
--- a/src/catch2/reporters/catch_reporter_multi.hpp
+++ b/src/catch2/reporters/catch_reporter_multi.hpp
@@ -60,6 +60,7 @@ namespace Catch {
void skipTest( TestCaseInfo const& testInfo ) override;
void listReporters(std::vector<ReporterDescription> const& descriptions) override;
+ void listListeners(std::vector<ListenerDescription> const& descriptions) override;
void listTests(std::vector<TestCaseHandle> const& tests) override;
void listTags(std::vector<TagInfo> const& tags) override;
diff --git a/src/catch2/reporters/catch_reporter_registrars.hpp b/src/catch2/reporters/catch_reporter_registrars.hpp
index 19d97b3b..e0922524 100644
--- a/src/catch2/reporters/catch_reporter_registrars.hpp
+++ b/src/catch2/reporters/catch_reporter_registrars.hpp
@@ -59,33 +59,36 @@ namespace Catch {
class ListenerRegistrar {
class TypedListenerFactory : public EventListenerFactory {
- StringRef m_defaultDescription;
+ StringRef m_listenerName;
std::string getDescriptionImpl( std::true_type ) const {
return T::getDescription();
}
std::string getDescriptionImpl( std::false_type ) const {
- return static_cast<std::string>(m_defaultDescription);
+ return "(No description provided)";
}
public:
- TypedListenerFactory( StringRef defaultDescription ):
- m_defaultDescription( defaultDescription ) {}
+ TypedListenerFactory( StringRef listenerName ):
+ m_listenerName( listenerName ) {}
IEventListenerPtr create( IConfig const* config ) const override {
return Detail::make_unique<T>( config );
}
+ StringRef getName() const override {
+ return m_listenerName;
+ }
+
std::string getDescription() const override {
return getDescriptionImpl( Detail::has_description<T>{} );
}
};
public:
-
- ListenerRegistrar(StringRef defaultDescription) {
- getMutableRegistryHub().registerListener( Detail::make_unique<TypedListenerFactory>(defaultDescription) );
+ ListenerRegistrar(StringRef listenerName) {
+ getMutableRegistryHub().registerListener( Detail::make_unique<TypedListenerFactory>(listenerName) );
}
};
}