Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Add common ReporterBase as parent of the helper bases

This is useful as a centralized place for handling common reporter
problems like handling output streams, and soon also colour impl
handling.
Commit
05d4ec62c8a429752d48c172f368d50c54dc3136
Author
Martin Hořeňovský ·
Parents
4dd5e2eeceaf

Files · Download .tar.gz

Changes

 docs/reporters.md                                  |  2 +-
 src/CMakeLists.txt                                 |  2 +
 .../reporters/catch_reporter_common_base.cpp       | 30 +++++++++++
 .../reporters/catch_reporter_common_base.hpp       | 61 ++++++++++++++++++++++
 .../reporters/catch_reporter_cumulative_base.cpp   | 16 ------
 .../reporters/catch_reporter_cumulative_base.hpp   | 14 ++---
 .../reporters/catch_reporter_streaming_base.cpp    | 16 ------
 .../reporters/catch_reporter_streaming_base.hpp    | 14 ++---
 src/catch2/reporters/catch_reporters_all.hpp       |  1 +
 9 files changed, 101 insertions(+), 55 deletions(-)

diff --git a/docs/reporters.md b/docs/reporters.md
index 14558648..ab6f6453 100644
--- a/docs/reporters.md
+++ b/docs/reporters.md
@@ -70,7 +70,7 @@ out in batch after each runthrough of a test case is finished.
 You can also write your own custom reporter and tell Catch2 to use it.
 When writing your reporter, you have two options:
 
-* Derive from `Catch::IStreamingReporter`. When doing this, you will have
+* Derive from `Catch::ReporterBase`. When doing this, you will have
   to provide handling for all [reporter events](reporter-events.md#top).
 * Derive from one of the provided [utility reporter bases in
   Catch2](#utility-reporter-bases).
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4d0eb295..4aa755a4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -213,6 +213,7 @@ set(INTERNAL_FILES ${IMPL_SOURCES} ${INTERNAL_HEADERS})
 set(REPORTER_HEADERS
     ${SOURCES_DIR}/reporters/catch_reporters_all.hpp
     ${SOURCES_DIR}/reporters/catch_reporter_automake.hpp
+    ${SOURCES_DIR}/reporters/catch_reporter_common_base.hpp
     ${SOURCES_DIR}/reporters/catch_reporter_compact.hpp
     ${SOURCES_DIR}/reporters/catch_reporter_console.hpp
     ${SOURCES_DIR}/reporters/catch_reporter_cumulative_base.hpp
@@ -229,6 +230,7 @@ set(REPORTER_HEADERS
 set(REPORTER_SOURCES
     ${SOURCES_DIR}/reporters/catch_reporter_automake.cpp
     ${SOURCES_DIR}/reporters/catch_reporter_combined_tu.cpp
+    ${SOURCES_DIR}/reporters/catch_reporter_common_base.cpp
     ${SOURCES_DIR}/reporters/catch_reporter_compact.cpp
     ${SOURCES_DIR}/reporters/catch_reporter_console.cpp
     ${SOURCES_DIR}/reporters/catch_reporter_cumulative_base.cpp
diff --git a/src/catch2/reporters/catch_reporter_common_base.cpp b/src/catch2/reporters/catch_reporter_common_base.cpp
new file mode 100644
index 00000000..18a014fc
--- /dev/null
+++ b/src/catch2/reporters/catch_reporter_common_base.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_common_base.hpp>
+
+#include <catch2/reporters/catch_reporter_helpers.hpp>
+
+namespace Catch {
+
+    void ReporterBase::listReporters(std::vector<ReporterDescription> const& descriptions) {
+        defaultListReporters(m_stream, descriptions, m_config->verbosity());
+    }
+
+    void ReporterBase::listTests(std::vector<TestCaseHandle> const& tests) {
+        defaultListTests(m_stream,
+                         tests,
+                         m_config->hasTestFilters(),
+                         m_config->verbosity());
+    }
+
+    void ReporterBase::listTags(std::vector<TagInfo> const& tags) {
+        defaultListTags( m_stream, tags, m_config->hasTestFilters() );
+    }
+
+} // namespace Catch
diff --git a/src/catch2/reporters/catch_reporter_common_base.hpp b/src/catch2/reporters/catch_reporter_common_base.hpp
new file mode 100644
index 00000000..235c2606
--- /dev/null
+++ b/src/catch2/reporters/catch_reporter_common_base.hpp
@@ -0,0 +1,61 @@
+
+//              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
+#ifndef CATCH_REPORTER_COMMON_BASE_HPP_INCLUDED
+#define CATCH_REPORTER_COMMON_BASE_HPP_INCLUDED
+
+#include <catch2/interfaces/catch_interfaces_reporter.hpp>
+
+namespace Catch {
+    /**
+     * This is the base class for all reporters.
+     *
+     * If are writing a reporter, you must derive from this type, or one
+     * of the helper reporter bases that are derived from this type.
+     *
+     * ReporterBase centralizes handling of various common tasks in reporters,
+     * like storing the right stream for the reporters to write to, and
+     * providing the default implementation of the different listing events.
+     */
+    class ReporterBase : public IEventListener {
+    protected:
+        //! Stream to write the output to
+        std::ostream& m_stream;
+
+    public:
+        ReporterBase( ReporterConfig const& config ):
+            IEventListener( config.fullConfig() ),
+            m_stream( config.stream() ) {}
+
+
+        /**
+         * Provides a simple default listing of reporters.
+         *
+         * Should look roughly like the reporter listing in v2 and earlier
+         * versions of Catch2.
+         */
+        void listReporters(
+            std::vector<ReporterDescription> const& descriptions ) override;
+        /**
+         * Provides a simple default listing of tests.
+         *
+         * Should look roughly like the test listing in v2 and earlier versions
+         * of Catch2. Especially supports low-verbosity listing that mimics the
+         * old `--list-test-names-only` output.
+         */
+        void listTests( std::vector<TestCaseHandle> const& tests ) override;
+        /**
+         * Provides a simple default listing of tags.
+         *
+         * Should look roughly like the tag listing in v2 and earlier versions
+         * of Catch2.
+         */
+        void listTags( std::vector<TagInfo> const& tags ) override;
+    };
+} // namespace Catch
+
+#endif // CATCH_REPORTER_COMMON_BASE_HPP_INCLUDED
diff --git a/src/catch2/reporters/catch_reporter_cumulative_base.cpp b/src/catch2/reporters/catch_reporter_cumulative_base.cpp
index 9dd1eb78..b52ba7ef 100644
--- a/src/catch2/reporters/catch_reporter_cumulative_base.cpp
+++ b/src/catch2/reporters/catch_reporter_cumulative_base.cpp
@@ -6,7 +6,6 @@
 
 // SPDX-License-Identifier: BSL-1.0
 #include <catch2/reporters/catch_reporter_cumulative_base.hpp>
-#include <catch2/reporters/catch_reporter_helpers.hpp>
 
 #include <algorithm>
 #include <cassert>
@@ -145,21 +144,6 @@ namespace Catch {
         testRunEndedCumulative();
     }
 
-    void CumulativeReporterBase::listReporters(std::vector<ReporterDescription> const& descriptions) {
-        defaultListReporters(m_stream, descriptions, m_config->verbosity());
-    }
-
-    void CumulativeReporterBase::listTests(std::vector<TestCaseHandle> const& tests) {
-        defaultListTests(m_stream,
-                         tests,
-                         m_config->hasTestFilters(),
-                         m_config->verbosity());
-    }
-
-    void CumulativeReporterBase::listTags(std::vector<TagInfo> const& tags) {
-        defaultListTags( m_stream, tags, m_config->hasTestFilters() );
-    }
-
     bool CumulativeReporterBase::SectionNode::hasAnyAssertions() const {
         return std::any_of(
             assertionsAndBenchmarks.begin(),
diff --git a/src/catch2/reporters/catch_reporter_cumulative_base.hpp b/src/catch2/reporters/catch_reporter_cumulative_base.hpp
index 974797c5..fd7a5045 100644
--- a/src/catch2/reporters/catch_reporter_cumulative_base.hpp
+++ b/src/catch2/reporters/catch_reporter_cumulative_base.hpp
@@ -8,7 +8,7 @@
 #ifndef CATCH_REPORTER_CUMULATIVE_BASE_HPP_INCLUDED
 #define CATCH_REPORTER_CUMULATIVE_BASE_HPP_INCLUDED
 
-#include <catch2/interfaces/catch_interfaces_reporter.hpp>
+#include <catch2/reporters/catch_reporter_common_base.hpp>
 #include <catch2/internal/catch_unique_ptr.hpp>
 #include <catch2/internal/catch_optional.hpp>
 
@@ -59,7 +59,7 @@ namespace Catch {
      * performance. **Accessing the assertion expansions if it wasn't stored is
      * UB.**
      */
-    class CumulativeReporterBase : public IEventListener {
+    class CumulativeReporterBase : public ReporterBase {
     public:
         template<typename T, typename ChildNodeT>
         struct Node {
@@ -90,8 +90,7 @@ namespace Catch {
         using TestRunNode = Node<TestRunStats, TestCaseNode>;
 
         CumulativeReporterBase( ReporterConfig const& _config ):
-            IEventListener( _config.fullConfig() ),
-            m_stream( _config.stream() ) {}
+            ReporterBase( _config ) {}
         ~CumulativeReporterBase() override;
 
         void benchmarkPreparing( StringRef ) override {}
@@ -121,19 +120,12 @@ namespace Catch {
 
         void skipTest(TestCaseInfo const&) override {}
 
-        void listReporters( std::vector<ReporterDescription> const& descriptions ) override;
-        void listTests( std::vector<TestCaseHandle> const& tests ) override;
-        void listTags( std::vector<TagInfo> const& tags ) override;
-
     protected:
         //! Should the cumulative base store the assertion expansion for succesful assertions?
         bool m_shouldStoreSuccesfulAssertions = true;
         //! Should the cumulative base store the assertion expansion for failed assertions?
         bool m_shouldStoreFailedAssertions = true;
 
-        //! Stream to write the output to
-        std::ostream& m_stream;
-
         // We need lazy construction here. We should probably refactor it
         // later, after the events are redone.
         //! The root node of the test run tree.
diff --git a/src/catch2/reporters/catch_reporter_streaming_base.cpp b/src/catch2/reporters/catch_reporter_streaming_base.cpp
index 893415fe..a9c898ed 100644
--- a/src/catch2/reporters/catch_reporter_streaming_base.cpp
+++ b/src/catch2/reporters/catch_reporter_streaming_base.cpp
@@ -6,7 +6,6 @@
 
 // SPDX-License-Identifier: BSL-1.0
 #include <catch2/reporters/catch_reporter_streaming_base.hpp>
-#include <catch2/reporters/catch_reporter_helpers.hpp>
 
 namespace Catch {
 
@@ -21,19 +20,4 @@ namespace Catch {
         currentTestCaseInfo = nullptr;
     }
 
-    void StreamingReporterBase::listReporters(std::vector<ReporterDescription> const& descriptions) {
-        defaultListReporters( m_stream, descriptions, m_config->verbosity() );
-    }
-
-    void StreamingReporterBase::listTests(std::vector<TestCaseHandle> const& tests) {
-        defaultListTests(m_stream,
-                         tests,
-                         m_config->hasTestFilters(),
-                         m_config->verbosity());
-    }
-
-    void StreamingReporterBase::listTags(std::vector<TagInfo> const& tags) {
-        defaultListTags( m_stream, tags, m_config->hasTestFilters() );
-    }
-
 } // end namespace Catch
diff --git a/src/catch2/reporters/catch_reporter_streaming_base.hpp b/src/catch2/reporters/catch_reporter_streaming_base.hpp
index 1ca50f0a..990610fa 100644
--- a/src/catch2/reporters/catch_reporter_streaming_base.hpp
+++ b/src/catch2/reporters/catch_reporter_streaming_base.hpp
@@ -8,7 +8,7 @@
 #ifndef CATCH_REPORTER_STREAMING_BASE_HPP_INCLUDED
 #define CATCH_REPORTER_STREAMING_BASE_HPP_INCLUDED
 
-#include <catch2/interfaces/catch_interfaces_reporter.hpp>
+#include <catch2/reporters/catch_reporter_common_base.hpp>
 
 #include <iosfwd>
 #include <string>
@@ -16,11 +16,10 @@
 
 namespace Catch {
 
-    class StreamingReporterBase : public IEventListener {
+    class StreamingReporterBase : public ReporterBase {
     public:
         StreamingReporterBase( ReporterConfig const& _config ):
-            IEventListener( _config.fullConfig() ),
-            m_stream( _config.stream() ) {}
+            ReporterBase( _config ) {}
 
 
         ~StreamingReporterBase() override;
@@ -61,14 +60,7 @@ namespace Catch {
             // It can optionally be overridden in the derived class.
         }
 
-        void listReporters( std::vector<ReporterDescription> const& descriptions ) override;
-        void listTests( std::vector<TestCaseHandle> const& tests ) override;
-        void listTags( std::vector<TagInfo> const& tags ) override;
-
     protected:
-        //! Stream that the reporter output should be written to
-        std::ostream& m_stream;
-
         TestRunInfo currentTestRunInfo{ "test run has not started yet"_sr };
         TestCaseInfo const* currentTestCaseInfo = nullptr;
 
diff --git a/src/catch2/reporters/catch_reporters_all.hpp b/src/catch2/reporters/catch_reporters_all.hpp
index 8c6208dc..d3a59eb7 100644
--- a/src/catch2/reporters/catch_reporters_all.hpp
+++ b/src/catch2/reporters/catch_reporters_all.hpp
@@ -22,6 +22,7 @@
 #define CATCH_REPORTERS_ALL_HPP_INCLUDED
 
 #include <catch2/reporters/catch_reporter_automake.hpp>
+#include <catch2/reporters/catch_reporter_common_base.hpp>
 #include <catch2/reporters/catch_reporter_compact.hpp>
 #include <catch2/reporters/catch_reporter_console.hpp>
 #include <catch2/reporters/catch_reporter_cumulative_base.hpp>