Pass the whole IStream wrapper into reporter

This will become useful when reworking colour support, because
Win32 colour support requires checking whether the output is
stdout, which is done through the `IStream` wrapper.
This commit is contained in:
Martin Hořeňovský 2022-02-23 23:46:45 +01:00
parent 9b01c404f5
commit 61d0f7a9af
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
12 changed files with 48 additions and 27 deletions

View File

@ -107,8 +107,8 @@ namespace Catch {
return m_data.reporterSpecifications; return m_data.reporterSpecifications;
} }
std::ostream& Config::getReporterOutputStream(std::size_t reporterIdx) const { IStream const* Config::getReporterOutputStream(std::size_t reporterIdx) const {
return m_reporterStreams.at(reporterIdx)->stream(); return m_reporterStreams.at(reporterIdx).get();
} }
TestSpec const& Config::testSpec() const { return m_testSpec; } TestSpec const& Config::testSpec() const { return m_testSpec; }
@ -118,7 +118,7 @@ namespace Catch {
// IConfig interface // IConfig interface
bool Config::allowThrows() const { return !m_data.noThrow; } bool Config::allowThrows() const { return !m_data.noThrow; }
std::ostream& Config::defaultStream() const { return m_defaultStream->stream(); } IStream const* Config::defaultStream() const { return m_defaultStream.get(); }
StringRef Config::name() const { return m_data.name.empty() ? m_data.processName : m_data.name; } StringRef Config::name() const { return m_data.name.empty() ? m_data.processName : m_data.name; }
bool Config::includeSuccessfulResults() const { return m_data.showSuccessfulTests; } bool Config::includeSuccessfulResults() const { return m_data.showSuccessfulTests; }
bool Config::warnAboutMissingAssertions() const { bool Config::warnAboutMissingAssertions() const {

View File

@ -21,7 +21,7 @@
namespace Catch { namespace Catch {
struct IStream; class IStream;
struct ConfigData { struct ConfigData {
struct ReporterAndFile { struct ReporterAndFile {
@ -91,7 +91,7 @@ namespace Catch {
bool listReporters() const; bool listReporters() const;
std::vector<ConfigData::ReporterAndFile> const& getReportersAndOutputFiles() const; std::vector<ConfigData::ReporterAndFile> const& getReportersAndOutputFiles() const;
std::ostream& getReporterOutputStream(std::size_t reporterIdx) const; IStream const* getReporterOutputStream(std::size_t reporterIdx) const;
std::vector<std::string> const& getTestsOrTags() const override; std::vector<std::string> const& getTestsOrTags() const override;
std::vector<std::string> const& getSectionsToRun() const override; std::vector<std::string> const& getSectionsToRun() const override;
@ -103,7 +103,7 @@ namespace Catch {
// IConfig interface // IConfig interface
bool allowThrows() const override; bool allowThrows() const override;
std::ostream& defaultStream() const override; IStream const* defaultStream() const override;
StringRef name() const override; StringRef name() const override;
bool includeSuccessfulResults() const override; bool includeSuccessfulResults() const override;
bool warnAboutMissingAssertions() const override; bool warnAboutMissingAssertions() const override;

View File

@ -44,7 +44,7 @@ namespace Catch {
IStreamingReporterPtr makeReporter(Config const* config) { IStreamingReporterPtr makeReporter(Config const* config) {
if (Catch::getRegistryHub().getReporterRegistry().getListeners().empty() if (Catch::getRegistryHub().getReporterRegistry().getListeners().empty()
&& config->getReportersAndOutputFiles().size() == 1) { && config->getReportersAndOutputFiles().size() == 1) {
auto& stream = config->getReporterOutputStream(0); auto stream = config->getReporterOutputStream(0);
return createReporter(config->getReportersAndOutputFiles()[0].reporterName, ReporterConfig(config, stream)); return createReporter(config->getReportersAndOutputFiles()[0].reporterName, ReporterConfig(config, stream));
} }
@ -57,7 +57,7 @@ namespace Catch {
std::size_t reporterIdx = 0; std::size_t reporterIdx = 0;
for (auto const& reporterAndFile : config->getReportersAndOutputFiles()) { for (auto const& reporterAndFile : config->getReportersAndOutputFiles()) {
auto& stream = config->getReporterOutputStream(reporterIdx); auto stream = config->getReporterOutputStream(reporterIdx);
multi->addReporter(createReporter(reporterAndFile.reporterName, ReporterConfig(config, stream))); multi->addReporter(createReporter(reporterAndFile.reporterName, ReporterConfig(config, stream)));
reporterIdx++; reporterIdx++;
} }

View File

@ -55,13 +55,14 @@ namespace Catch {
}; }; }; };
class TestSpec; class TestSpec;
class IStream;
struct IConfig : Detail::NonCopyable { struct IConfig : Detail::NonCopyable {
virtual ~IConfig(); virtual ~IConfig();
virtual bool allowThrows() const = 0; virtual bool allowThrows() const = 0;
virtual std::ostream& defaultStream() const = 0; virtual IStream const* defaultStream() const = 0;
virtual StringRef name() const = 0; virtual StringRef name() const = 0;
virtual bool includeSuccessfulResults() const = 0; virtual bool includeSuccessfulResults() const = 0;
virtual bool shouldDebugBreak() const = 0; virtual bool shouldDebugBreak() const = 0;

View File

@ -20,10 +20,10 @@
namespace Catch { namespace Catch {
ReporterConfig::ReporterConfig( IConfig const* _fullConfig, std::ostream& _stream ) ReporterConfig::ReporterConfig( IConfig const* _fullConfig, IStream const* _stream )
: m_stream( &_stream ), m_fullConfig( _fullConfig ) {} : m_stream( _stream ), m_fullConfig( _fullConfig ) {}
std::ostream& ReporterConfig::stream() const { return *m_stream; } IStream const* ReporterConfig::stream() const { return m_stream; }
IConfig const * ReporterConfig::fullConfig() const { return m_fullConfig; } IConfig const * ReporterConfig::fullConfig() const { return m_fullConfig; }
AssertionStats::AssertionStats( AssertionResult const& _assertionResult, AssertionStats::AssertionStats( AssertionResult const& _assertionResult,

View File

@ -30,15 +30,16 @@ namespace Catch {
struct TestCaseInfo; struct TestCaseInfo;
class TestCaseHandle; class TestCaseHandle;
struct IConfig; struct IConfig;
class IStream;
struct ReporterConfig { struct ReporterConfig {
ReporterConfig( IConfig const* _fullConfig, std::ostream& _stream ); ReporterConfig( IConfig const* _fullConfig, IStream const* _stream );
std::ostream& stream() const; IStream const* stream() const;
IConfig const* fullConfig() const; IConfig const* fullConfig() const;
private: private:
std::ostream* m_stream; IStream const* m_stream;
IConfig const* m_fullConfig; IConfig const* m_fullConfig;
}; };

View File

@ -160,7 +160,7 @@ namespace {
void setColour( const char* _escapeCode ) { void setColour( const char* _escapeCode ) {
// The escape sequence must be flushed to console, otherwise if // The escape sequence must be flushed to console, otherwise if
// stdin and stderr are intermixed, we'd get accidentally coloured output. // stdin and stderr are intermixed, we'd get accidentally coloured output.
getCurrentContext().getConfig()->defaultStream() getCurrentContext().getConfig()->defaultStream()->stream()
<< '\033' << _escapeCode << std::flush; << '\033' << _escapeCode << std::flush;
} }
}; };

View File

@ -22,7 +22,8 @@ namespace Catch {
std::ostream& cerr(); std::ostream& cerr();
std::ostream& clog(); std::ostream& clog();
struct IStream { class IStream {
public:
virtual ~IStream(); // = default virtual ~IStream(); // = default
virtual std::ostream& stream() const = 0; virtual std::ostream& stream() const = 0;
// Win32 colour supports requires us to identify whether a stream // Win32 colour supports requires us to identify whether a stream

View File

@ -9,6 +9,7 @@
#define CATCH_REPORTER_COMMON_BASE_HPP_INCLUDED #define CATCH_REPORTER_COMMON_BASE_HPP_INCLUDED
#include <catch2/interfaces/catch_interfaces_reporter.hpp> #include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_stream.hpp>
namespace Catch { namespace Catch {
/** /**
@ -23,13 +24,19 @@ namespace Catch {
*/ */
class ReporterBase : public IEventListener { class ReporterBase : public IEventListener {
protected: protected:
//! Stream to write the output to //! The stream wrapper as passed to us by outside code
IStream const* m_wrapped_stream;
//! Cached output stream from `m_wrapped_stream` to reduce
//! number of indirect calls needed to write output.
std::ostream& m_stream; std::ostream& m_stream;
public: public:
ReporterBase( ReporterConfig const& config ): ReporterBase( ReporterConfig const& config ):
IEventListener( config.fullConfig() ), IEventListener( config.fullConfig() ),
m_stream( config.stream() ) {} m_wrapped_stream( config.stream() ),
m_stream( m_wrapped_stream->stream() ) {}
/** /**

View File

@ -355,7 +355,7 @@ public:
ConsoleReporter::ConsoleReporter(ReporterConfig const& config) ConsoleReporter::ConsoleReporter(ReporterConfig const& config)
: StreamingReporterBase(config), : StreamingReporterBase(config),
m_tablePrinter(Detail::make_unique<TablePrinter>(config.stream(), m_tablePrinter(Detail::make_unique<TablePrinter>(m_stream,
[&config]() -> std::vector<ColumnInfo> { [&config]() -> std::vector<ColumnInfo> {
if (config.fullConfig()->benchmarkNoAnalysis()) if (config.fullConfig()->benchmarkNoAnalysis())
{ {

View File

@ -24,7 +24,7 @@
namespace Catch { namespace Catch {
XmlReporter::XmlReporter( ReporterConfig const& _config ) XmlReporter::XmlReporter( ReporterConfig const& _config )
: StreamingReporterBase( _config ), : StreamingReporterBase( _config ),
m_xml(_config.stream()) m_xml(m_stream)
{ {
m_preferences.shouldRedirectStdOut = true; m_preferences.shouldRedirectStdOut = true;
m_preferences.shouldReportAllAssertions = true; m_preferences.shouldReportAllAssertions = true;

View File

@ -16,6 +16,7 @@
#include <catch2/internal/catch_enforce.hpp> #include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_list.hpp> #include <catch2/internal/catch_list.hpp>
#include <catch2/internal/catch_reporter_registry.hpp> #include <catch2/internal/catch_reporter_registry.hpp>
#include <catch2/internal/catch_stream.hpp>
#include <catch2/matchers/catch_matchers_string.hpp> #include <catch2/matchers/catch_matchers_string.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp> #include <catch2/reporters/catch_reporter_helpers.hpp>
#include <catch2/reporters/catch_reporter_event_listener.hpp> #include <catch2/reporters/catch_reporter_event_listener.hpp>
@ -24,6 +25,16 @@
#include <sstream> #include <sstream>
namespace {
class StringIStream : public Catch::IStream {
public:
std::ostream& stream() const override { return sstr; }
std::string str() const { return sstr.str(); }
private:
mutable std::stringstream sstr;
};
}
TEST_CASE( "The default listing implementation write to provided stream", TEST_CASE( "The default listing implementation write to provided stream",
"[reporters][reporter-helpers]" ) { "[reporters][reporter-helpers]" ) {
using Catch::Matchers::ContainsSubstring; using Catch::Matchers::ContainsSubstring;
@ -72,11 +83,11 @@ TEST_CASE( "Reporter's write listings to provided stream", "[reporters]" ) {
for (auto const& factory : factories) { for (auto const& factory : factories) {
INFO("Tested reporter: " << factory.first); INFO("Tested reporter: " << factory.first);
std::stringstream sstream; StringIStream sstream;
Catch::ConfigData config_data; Catch::ConfigData config_data;
Catch::Config config( config_data ); Catch::Config config( config_data );
Catch::ReporterConfig rep_config( &config, sstream ); Catch::ReporterConfig rep_config( &config, &sstream );
auto reporter = factory.second->create( rep_config ); auto reporter = factory.second->create( rep_config );
DYNAMIC_SECTION( factory.first << " reporter lists tags" ) { DYNAMIC_SECTION( factory.first << " reporter lists tags" ) {
@ -162,8 +173,8 @@ TEST_CASE("Multireporter calls reporters and listeners in correct order",
Catch::ConfigData config_data; Catch::ConfigData config_data;
Catch::Config config( config_data ); Catch::Config config( config_data );
std::stringstream sstream; StringIStream sstream;
Catch::ReporterConfig rep_config( &config, sstream ); Catch::ReporterConfig rep_config( &config, &sstream );
// We add reporters before listeners, to check that internally they // We add reporters before listeners, to check that internally they
// get sorted properly, and listeners are called first anyway. // get sorted properly, and listeners are called first anyway.
@ -215,8 +226,8 @@ TEST_CASE("Multireporter updates ReporterPreferences properly",
Catch::ConfigData config_data; Catch::ConfigData config_data;
Catch::Config config( config_data ); Catch::Config config( config_data );
std::stringstream sstream; StringIStream sstream;
Catch::ReporterConfig rep_config( &config, sstream ); Catch::ReporterConfig rep_config( &config, &sstream );
Catch::MultiReporter multiReporter( &config ); Catch::MultiReporter multiReporter( &config );
// Post init defaults // Post init defaults