Skip to content

Commit

Permalink
Pass the whole IStream wrapper into reporter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
horenmar committed Mar 6, 2022
1 parent 9b01c40 commit 61d0f7a
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/catch2/catch_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ namespace Catch {
return m_data.reporterSpecifications;
}

std::ostream& Config::getReporterOutputStream(std::size_t reporterIdx) const {
return m_reporterStreams.at(reporterIdx)->stream();
IStream const* Config::getReporterOutputStream(std::size_t reporterIdx) const {
return m_reporterStreams.at(reporterIdx).get();
}

TestSpec const& Config::testSpec() const { return m_testSpec; }
Expand All @@ -118,7 +118,7 @@ namespace Catch {

// IConfig interface
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; }
bool Config::includeSuccessfulResults() const { return m_data.showSuccessfulTests; }
bool Config::warnAboutMissingAssertions() const {
Expand Down
6 changes: 3 additions & 3 deletions src/catch2/catch_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace Catch {

struct IStream;
class IStream;

struct ConfigData {
struct ReporterAndFile {
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace Catch {
bool listReporters() 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& getSectionsToRun() const override;
Expand All @@ -103,7 +103,7 @@ namespace Catch {

// IConfig interface
bool allowThrows() const override;
std::ostream& defaultStream() const override;
IStream const* defaultStream() const override;
StringRef name() const override;
bool includeSuccessfulResults() const override;
bool warnAboutMissingAssertions() const override;
Expand Down
4 changes: 2 additions & 2 deletions src/catch2/catch_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Catch {
IStreamingReporterPtr makeReporter(Config const* config) {
if (Catch::getRegistryHub().getReporterRegistry().getListeners().empty()
&& config->getReportersAndOutputFiles().size() == 1) {
auto& stream = config->getReporterOutputStream(0);
auto stream = config->getReporterOutputStream(0);
return createReporter(config->getReportersAndOutputFiles()[0].reporterName, ReporterConfig(config, stream));
}

Expand All @@ -57,7 +57,7 @@ namespace Catch {

std::size_t reporterIdx = 0;
for (auto const& reporterAndFile : config->getReportersAndOutputFiles()) {
auto& stream = config->getReporterOutputStream(reporterIdx);
auto stream = config->getReporterOutputStream(reporterIdx);
multi->addReporter(createReporter(reporterAndFile.reporterName, ReporterConfig(config, stream)));
reporterIdx++;
}
Expand Down
3 changes: 2 additions & 1 deletion src/catch2/interfaces/catch_interfaces_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ namespace Catch {
}; };

class TestSpec;
class IStream;

struct IConfig : Detail::NonCopyable {

virtual ~IConfig();

virtual bool allowThrows() const = 0;
virtual std::ostream& defaultStream() const = 0;
virtual IStream const* defaultStream() const = 0;
virtual StringRef name() const = 0;
virtual bool includeSuccessfulResults() const = 0;
virtual bool shouldDebugBreak() const = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/catch2/interfaces/catch_interfaces_reporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

namespace Catch {

ReporterConfig::ReporterConfig( IConfig const* _fullConfig, std::ostream& _stream )
: m_stream( &_stream ), m_fullConfig( _fullConfig ) {}
ReporterConfig::ReporterConfig( IConfig const* _fullConfig, IStream const* _stream )
: 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; }

AssertionStats::AssertionStats( AssertionResult const& _assertionResult,
Expand Down
7 changes: 4 additions & 3 deletions src/catch2/interfaces/catch_interfaces_reporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ namespace Catch {
struct TestCaseInfo;
class TestCaseHandle;
struct IConfig;
class IStream;

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;

private:
std::ostream* m_stream;
IStream const* m_stream;
IConfig const* m_fullConfig;
};

Expand Down
2 changes: 1 addition & 1 deletion src/catch2/internal/catch_console_colour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace {
void setColour( const char* _escapeCode ) {
// The escape sequence must be flushed to console, otherwise if
// stdin and stderr are intermixed, we'd get accidentally coloured output.
getCurrentContext().getConfig()->defaultStream()
getCurrentContext().getConfig()->defaultStream()->stream()
<< '\033' << _escapeCode << std::flush;
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/catch2/internal/catch_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace Catch {
std::ostream& cerr();
std::ostream& clog();

struct IStream {
class IStream {
public:
virtual ~IStream(); // = default
virtual std::ostream& stream() const = 0;
// Win32 colour supports requires us to identify whether a stream
Expand Down
11 changes: 9 additions & 2 deletions src/catch2/reporters/catch_reporter_common_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define CATCH_REPORTER_COMMON_BASE_HPP_INCLUDED

#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_stream.hpp>

namespace Catch {
/**
Expand All @@ -23,13 +24,19 @@ namespace Catch {
*/
class ReporterBase : public IEventListener {
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;



public:
ReporterBase( ReporterConfig const& config ):
IEventListener( config.fullConfig() ),
m_stream( config.stream() ) {}
m_wrapped_stream( config.stream() ),
m_stream( m_wrapped_stream->stream() ) {}


/**
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/reporters/catch_reporter_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class TablePrinter {

ConsoleReporter::ConsoleReporter(ReporterConfig const& config)
: StreamingReporterBase(config),
m_tablePrinter(Detail::make_unique<TablePrinter>(config.stream(),
m_tablePrinter(Detail::make_unique<TablePrinter>(m_stream,
[&config]() -> std::vector<ColumnInfo> {
if (config.fullConfig()->benchmarkNoAnalysis())
{
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/reporters/catch_reporter_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Catch {
XmlReporter::XmlReporter( ReporterConfig const& _config )
: StreamingReporterBase( _config ),
m_xml(_config.stream())
m_xml(m_stream)
{
m_preferences.shouldRedirectStdOut = true;
m_preferences.shouldReportAllAssertions = true;
Expand Down
23 changes: 17 additions & 6 deletions tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_list.hpp>
#include <catch2/internal/catch_reporter_registry.hpp>
#include <catch2/internal/catch_stream.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#include <catch2/reporters/catch_reporter_event_listener.hpp>
Expand All @@ -24,6 +25,16 @@

#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",
"[reporters][reporter-helpers]" ) {
using Catch::Matchers::ContainsSubstring;
Expand Down Expand Up @@ -72,11 +83,11 @@ TEST_CASE( "Reporter's write listings to provided stream", "[reporters]" ) {

for (auto const& factory : factories) {
INFO("Tested reporter: " << factory.first);
std::stringstream sstream;
StringIStream sstream;

Catch::ConfigData 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 );

DYNAMIC_SECTION( factory.first << " reporter lists tags" ) {
Expand Down Expand Up @@ -162,8 +173,8 @@ TEST_CASE("Multireporter calls reporters and listeners in correct order",

Catch::ConfigData config_data;
Catch::Config config( config_data );
std::stringstream sstream;
Catch::ReporterConfig rep_config( &config, sstream );
StringIStream sstream;
Catch::ReporterConfig rep_config( &config, &sstream );

// We add reporters before listeners, to check that internally they
// get sorted properly, and listeners are called first anyway.
Expand Down Expand Up @@ -215,8 +226,8 @@ TEST_CASE("Multireporter updates ReporterPreferences properly",

Catch::ConfigData config_data;
Catch::Config config( config_data );
std::stringstream sstream;
Catch::ReporterConfig rep_config( &config, sstream );
StringIStream sstream;
Catch::ReporterConfig rep_config( &config, &sstream );
Catch::MultiReporter multiReporter( &config );

// Post init defaults
Expand Down

0 comments on commit 61d0f7a

Please sign in to comment.