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.
This commit is contained in:
Martin Hořeňovský 2022-03-06 16:08:31 +01:00
parent 4dd5e2eece
commit 05d4ec62c8
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
9 changed files with 101 additions and 55 deletions

View File

@ -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. You can also write your own custom reporter and tell Catch2 to use it.
When writing your reporter, you have two options: 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). to provide handling for all [reporter events](reporter-events.md#top).
* Derive from one of the provided [utility reporter bases in * Derive from one of the provided [utility reporter bases in
Catch2](#utility-reporter-bases). Catch2](#utility-reporter-bases).

View File

@ -213,6 +213,7 @@ set(INTERNAL_FILES ${IMPL_SOURCES} ${INTERNAL_HEADERS})
set(REPORTER_HEADERS set(REPORTER_HEADERS
${SOURCES_DIR}/reporters/catch_reporters_all.hpp ${SOURCES_DIR}/reporters/catch_reporters_all.hpp
${SOURCES_DIR}/reporters/catch_reporter_automake.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_compact.hpp
${SOURCES_DIR}/reporters/catch_reporter_console.hpp ${SOURCES_DIR}/reporters/catch_reporter_console.hpp
${SOURCES_DIR}/reporters/catch_reporter_cumulative_base.hpp ${SOURCES_DIR}/reporters/catch_reporter_cumulative_base.hpp
@ -229,6 +230,7 @@ set(REPORTER_HEADERS
set(REPORTER_SOURCES set(REPORTER_SOURCES
${SOURCES_DIR}/reporters/catch_reporter_automake.cpp ${SOURCES_DIR}/reporters/catch_reporter_automake.cpp
${SOURCES_DIR}/reporters/catch_reporter_combined_tu.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_compact.cpp
${SOURCES_DIR}/reporters/catch_reporter_console.cpp ${SOURCES_DIR}/reporters/catch_reporter_console.cpp
${SOURCES_DIR}/reporters/catch_reporter_cumulative_base.cpp ${SOURCES_DIR}/reporters/catch_reporter_cumulative_base.cpp

View File

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

View File

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

View File

@ -6,7 +6,6 @@
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
#include <catch2/reporters/catch_reporter_cumulative_base.hpp> #include <catch2/reporters/catch_reporter_cumulative_base.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
@ -145,21 +144,6 @@ namespace Catch {
testRunEndedCumulative(); 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 { bool CumulativeReporterBase::SectionNode::hasAnyAssertions() const {
return std::any_of( return std::any_of(
assertionsAndBenchmarks.begin(), assertionsAndBenchmarks.begin(),

View File

@ -8,7 +8,7 @@
#ifndef CATCH_REPORTER_CUMULATIVE_BASE_HPP_INCLUDED #ifndef CATCH_REPORTER_CUMULATIVE_BASE_HPP_INCLUDED
#define 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_unique_ptr.hpp>
#include <catch2/internal/catch_optional.hpp> #include <catch2/internal/catch_optional.hpp>
@ -59,7 +59,7 @@ namespace Catch {
* performance. **Accessing the assertion expansions if it wasn't stored is * performance. **Accessing the assertion expansions if it wasn't stored is
* UB.** * UB.**
*/ */
class CumulativeReporterBase : public IEventListener { class CumulativeReporterBase : public ReporterBase {
public: public:
template<typename T, typename ChildNodeT> template<typename T, typename ChildNodeT>
struct Node { struct Node {
@ -90,8 +90,7 @@ namespace Catch {
using TestRunNode = Node<TestRunStats, TestCaseNode>; using TestRunNode = Node<TestRunStats, TestCaseNode>;
CumulativeReporterBase( ReporterConfig const& _config ): CumulativeReporterBase( ReporterConfig const& _config ):
IEventListener( _config.fullConfig() ), ReporterBase( _config ) {}
m_stream( _config.stream() ) {}
~CumulativeReporterBase() override; ~CumulativeReporterBase() override;
void benchmarkPreparing( StringRef ) override {} void benchmarkPreparing( StringRef ) override {}
@ -121,19 +120,12 @@ namespace Catch {
void skipTest(TestCaseInfo const&) override {} 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: protected:
//! Should the cumulative base store the assertion expansion for succesful assertions? //! Should the cumulative base store the assertion expansion for succesful assertions?
bool m_shouldStoreSuccesfulAssertions = true; bool m_shouldStoreSuccesfulAssertions = true;
//! Should the cumulative base store the assertion expansion for failed assertions? //! Should the cumulative base store the assertion expansion for failed assertions?
bool m_shouldStoreFailedAssertions = true; bool m_shouldStoreFailedAssertions = true;
//! Stream to write the output to
std::ostream& m_stream;
// We need lazy construction here. We should probably refactor it // We need lazy construction here. We should probably refactor it
// later, after the events are redone. // later, after the events are redone.
//! The root node of the test run tree. //! The root node of the test run tree.

View File

@ -6,7 +6,6 @@
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
#include <catch2/reporters/catch_reporter_streaming_base.hpp> #include <catch2/reporters/catch_reporter_streaming_base.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
namespace Catch { namespace Catch {
@ -21,19 +20,4 @@ namespace Catch {
currentTestCaseInfo = nullptr; 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 } // end namespace Catch

View File

@ -8,7 +8,7 @@
#ifndef CATCH_REPORTER_STREAMING_BASE_HPP_INCLUDED #ifndef CATCH_REPORTER_STREAMING_BASE_HPP_INCLUDED
#define 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 <iosfwd>
#include <string> #include <string>
@ -16,11 +16,10 @@
namespace Catch { namespace Catch {
class StreamingReporterBase : public IEventListener { class StreamingReporterBase : public ReporterBase {
public: public:
StreamingReporterBase( ReporterConfig const& _config ): StreamingReporterBase( ReporterConfig const& _config ):
IEventListener( _config.fullConfig() ), ReporterBase( _config ) {}
m_stream( _config.stream() ) {}
~StreamingReporterBase() override; ~StreamingReporterBase() override;
@ -61,14 +60,7 @@ namespace Catch {
// It can optionally be overridden in the derived class. // 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: protected:
//! Stream that the reporter output should be written to
std::ostream& m_stream;
TestRunInfo currentTestRunInfo{ "test run has not started yet"_sr }; TestRunInfo currentTestRunInfo{ "test run has not started yet"_sr };
TestCaseInfo const* currentTestCaseInfo = nullptr; TestCaseInfo const* currentTestCaseInfo = nullptr;

View File

@ -22,6 +22,7 @@
#define CATCH_REPORTERS_ALL_HPP_INCLUDED #define CATCH_REPORTERS_ALL_HPP_INCLUDED
#include <catch2/reporters/catch_reporter_automake.hpp> #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_compact.hpp>
#include <catch2/reporters/catch_reporter_console.hpp> #include <catch2/reporters/catch_reporter_console.hpp>
#include <catch2/reporters/catch_reporter_cumulative_base.hpp> #include <catch2/reporters/catch_reporter_cumulative_base.hpp>