Store IStream instance owning ptrs in reporter instances

This ended up being a surprisingly large refactoring, motivated
by removing a `const_cast` from `Config`'s handling of reporter
streams, forced by previous commit.
This commit is contained in:
Martin Hořeňovský
2022-04-12 21:22:43 +02:00
parent 4f09f1120b
commit 79d1e82381
38 changed files with 124 additions and 109 deletions

View File

@@ -20,12 +20,13 @@
#include <catch2/reporters/catch_reporter_registrars.hpp>
#include <iostream>
#include <utility>
class CumulativeBenchmarkReporter final : public Catch::CumulativeReporterBase {
public:
CumulativeBenchmarkReporter(Catch::ReporterConfig const& _config) :
CumulativeReporterBase(_config) {
CumulativeBenchmarkReporter(Catch::ReporterConfig&& _config) :
CumulativeReporterBase(std::move(_config)) {
m_preferences.shouldReportAllAssertions = true;
}

View File

@@ -19,11 +19,12 @@
#include <catch2/reporters/catch_reporter_registrars.hpp>
#include <iostream>
#include <utility>
class TestReporter : public Catch::StreamingReporterBase {
public:
TestReporter(Catch::ReporterConfig const& _config):
StreamingReporterBase(_config) {
TestReporter(Catch::ReporterConfig&& _config):
StreamingReporterBase(std::move(_config)) {
std::cout << "TestReporter constructed\n";
}

View File

@@ -16,13 +16,14 @@
#include <catch2/reporters/catch_reporter_registrars.hpp>
#include <iostream>
#include <utility>
namespace {
class TestReporter : public Catch::StreamingReporterBase {
public:
TestReporter(Catch::ReporterConfig const& _config):
StreamingReporterBase(_config) {
TestReporter(Catch::ReporterConfig&& _config):
StreamingReporterBase(std::move(_config)) {
m_preferences.shouldReportAllAssertions = false;
std::cout << "X26 - TestReporter constructed\n";
}

View File

@@ -26,6 +26,7 @@
#include <iostream>
#include <string>
#include <utility>
class TestReporter : public Catch::StreamingReporterBase {
std::string stdOutString( uint64_t iter ){
@@ -36,8 +37,8 @@ class TestReporter : public Catch::StreamingReporterBase {
}
public:
TestReporter( Catch::ReporterConfig const& _config ):
StreamingReporterBase( _config ) {
TestReporter( Catch::ReporterConfig&& _config ):
StreamingReporterBase( std::move(_config) ) {
m_preferences.shouldRedirectStdOut = true;
std::cout << "X27 - TestReporter constructed\n";
}

View File

@@ -19,7 +19,9 @@
#include <catch2/reporters/catch_reporter_event_listener.hpp>
#include <catch2/reporters/catch_reporter_registrars.hpp>
#include <catch2/reporters/catch_reporter_streaming_base.hpp>
#include <iostream>
#include <utility>
namespace {
@@ -54,8 +56,8 @@ namespace {
class TestReporter : public Catch::StreamingReporterBase {
public:
TestReporter( Catch::ReporterConfig const& _config ):
StreamingReporterBase( _config ) {
TestReporter( Catch::ReporterConfig&& _config ):
StreamingReporterBase( std::move(_config) ) {
std::cout << "X28 - TestReporter constructed\n";
}

View File

@@ -25,8 +25,8 @@
class TestReporter : public Catch::StreamingReporterBase {
public:
TestReporter( Catch::ReporterConfig const& _config ):
StreamingReporterBase( _config ) {
TestReporter( Catch::ReporterConfig&& _config ):
StreamingReporterBase( std::move(_config) ) {
std::cout << "X29 - TestReporter constructed\n";
}

View File

@@ -1317,7 +1317,7 @@ CmdLine.tests.cpp:<line number>: passed: config.noThrow == false for: false == f
CmdLine.tests.cpp:<line number>: passed: config.reporterSpecifications.empty() for: true
CmdLine.tests.cpp:<line number>: passed: !(cfg.hasTestFilters()) for: !false
CmdLine.tests.cpp:<line number>: passed: cfg.getReporterSpecs().size() == 1 for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} } for: {?} == {?}
CmdLine.tests.cpp:<line number>: passed: cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} } for: {?} == {?}
CmdLine.tests.cpp:<line number>: passed: result for: {?}
CmdLine.tests.cpp:<line number>: passed: cfg.hasTestFilters() for: true
CmdLine.tests.cpp:<line number>: passed: cfg.testSpec().matches(*fakeTestCase("notIncluded")) == false for: false == false

View File

@@ -1315,7 +1315,7 @@ CmdLine.tests.cpp:<line number>: passed: config.noThrow == false for: false == f
CmdLine.tests.cpp:<line number>: passed: config.reporterSpecifications.empty() for: true
CmdLine.tests.cpp:<line number>: passed: !(cfg.hasTestFilters()) for: !false
CmdLine.tests.cpp:<line number>: passed: cfg.getReporterSpecs().size() == 1 for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} } for: {?} == {?}
CmdLine.tests.cpp:<line number>: passed: cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} } for: {?} == {?}
CmdLine.tests.cpp:<line number>: passed: result for: {?}
CmdLine.tests.cpp:<line number>: passed: cfg.hasTestFilters() for: true
CmdLine.tests.cpp:<line number>: passed: cfg.testSpec().matches(*fakeTestCase("notIncluded")) == false for: false == false

View File

@@ -9438,7 +9438,7 @@ with expansion:
1 == 1
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} } )
CHECK( cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} } )
with expansion:
{?} == {?}

View File

@@ -9436,7 +9436,7 @@ with expansion:
1 == 1
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} } )
CHECK( cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} } )
with expansion:
{?} == {?}

View File

@@ -2481,7 +2481,7 @@ ok {test-number} - !(cfg.hasTestFilters()) for: !false
# Process can be configured on command line
ok {test-number} - cfg.getReporterSpecs().size() == 1 for: 1 == 1
# Process can be configured on command line
ok {test-number} - cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} } for: {?} == {?}
ok {test-number} - cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} } for: {?} == {?}
# Process can be configured on command line
ok {test-number} - result for: {?}
# Process can be configured on command line

View File

@@ -2479,7 +2479,7 @@ ok {test-number} - !(cfg.hasTestFilters()) for: !false
# Process can be configured on command line
ok {test-number} - cfg.getReporterSpecs().size() == 1 for: 1 == 1
# Process can be configured on command line
ok {test-number} - cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} } for: {?} == {?}
ok {test-number} - cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} } for: {?} == {?}
# Process can be configured on command line
ok {test-number} - result for: {?}
# Process can be configured on command line

View File

@@ -11476,7 +11476,7 @@ C
</Expression>
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} }
cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} }
</Original>
<Expanded>
{?} == {?}

View File

@@ -11476,7 +11476,7 @@ C
</Expression>
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, {}, {}, {} }
cfg.getReporterSpecs()[0] == Catch::ReporterSpec{ expectedReporter, std::string{}, Catch::ColourMode::PlatformDefault, {} }
</Original>
<Expanded>
{?} == {?}

View File

@@ -363,7 +363,10 @@ TEST_CASE( "Process can be configured on command line", "[config][command-line]"
CHECK( cfg.getReporterSpecs().size() == 1 );
CHECK( cfg.getReporterSpecs()[0] ==
Catch::ReporterSpec{ expectedReporter, {}, {}, {} } );
Catch::ReporterSpec{ expectedReporter,
std::string{},
Catch::ColourMode::PlatformDefault,
{} } );
}
SECTION("test lists") {

View File

@@ -23,6 +23,7 @@
#include <catch2/reporters/catch_reporter_event_listener.hpp>
#include <catch2/reporters/catch_reporter_streaming_base.hpp>
#include <catch2/reporters/catch_reporter_multi.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <sstream>
@@ -34,6 +35,15 @@ namespace {
private:
std::stringstream sstr;
};
//! config must outlive the function
Catch::ReporterConfig makeDummyRepConfig( Catch::Config const& config ) {
return Catch::ReporterConfig{
&config,
Catch::Detail::make_unique<StringIStream>(),
Catch::ColourMode::None,
{} };
}
}
TEST_CASE( "The default listing implementation write to provided stream",
@@ -85,20 +95,19 @@ TEST_CASE( "Reporter's write listings to provided stream", "[reporters]" ) {
for (auto const& factory : factories) {
INFO("Tested reporter: " << factory.first);
StringIStream sstream;
auto sstream = Catch::Detail::make_unique<StringIStream>();
auto& sstreamRef = *sstream.get();
Catch::ConfigData config_data;
Catch::Config config( config_data );
Catch::ReporterConfig rep_config(
&config, &sstream, Catch::ColourMode::None, {} );
auto reporter = factory.second->create( rep_config );
Catch::Config config( Catch::ConfigData{} );
auto reporter = factory.second->create( Catch::ReporterConfig{
&config, CATCH_MOVE( sstream ), Catch::ColourMode::None, {} } );
DYNAMIC_SECTION( factory.first << " reporter lists tags" ) {
std::vector<Catch::TagInfo> tags(1);
tags[0].add("fakeTag"_catch_sr);
reporter->listTags(tags);
auto listingString = sstream.str();
auto listingString = sstreamRef.str();
REQUIRE_THAT(listingString, ContainsSubstring("fakeTag"s));
}
@@ -107,7 +116,7 @@ TEST_CASE( "Reporter's write listings to provided stream", "[reporters]" ) {
{ { "fake reporter", "fake description" } } );
reporter->listReporters(reporters);
auto listingString = sstream.str();
auto listingString = sstreamRef.str();
REQUIRE_THAT(listingString, ContainsSubstring("fake reporter"s));
}
@@ -119,7 +128,7 @@ TEST_CASE( "Reporter's write listings to provided stream", "[reporters]" ) {
std::vector<Catch::TestCaseHandle> tests({ {&fakeInfo, nullptr} });
reporter->listTests(tests);
auto listingString = sstream.str();
auto listingString = sstreamRef.str();
REQUIRE_THAT( listingString,
ContainsSubstring( "fake test name"s ) &&
ContainsSubstring( "fakeTestTag"s ) );
@@ -159,8 +168,8 @@ namespace {
public:
MockReporter( std::string witness,
std::vector<std::string>& recorder,
Catch::ReporterConfig const& config ):
StreamingReporterBase( config ),
Catch::ReporterConfig&& config ):
StreamingReporterBase( CATCH_MOVE(config) ),
m_witness( witness ),
m_recorder( recorder )
{}
@@ -173,25 +182,20 @@ namespace {
TEST_CASE("Multireporter calls reporters and listeners in correct order",
"[reporters][multi-reporter]") {
Catch::ConfigData config_data;
Catch::Config config( config_data );
StringIStream sstream;
Catch::ReporterConfig rep_config(
&config, &sstream, Catch::ColourMode::None, {} );
Catch::Config config( Catch::ConfigData{} );
// We add reporters before listeners, to check that internally they
// get sorted properly, and listeners are called first anyway.
Catch::MultiReporter multiReporter( &config );
std::vector<std::string> records;
multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>(
"Goodbye", records, rep_config ) );
"Goodbye", records, makeDummyRepConfig(config) ) );
multiReporter.addListener(
Catch::Detail::make_unique<MockListener>( "Hello", records, &config ) );
multiReporter.addListener(
Catch::Detail::make_unique<MockListener>( "world", records, &config ) );
multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>(
"world", records, rep_config ) );
"world", records, makeDummyRepConfig(config) ) );
multiReporter.testRunStarting( { "" } );
std::vector<std::string> expected( { "Hello", "world", "Goodbye", "world" } );
@@ -217,8 +221,8 @@ namespace {
public:
PreferenceReporter( bool redirectStdout,
bool reportAllAssertions,
Catch::ReporterConfig const& config ):
StreamingReporterBase( config ) {
Catch::ReporterConfig&& config ):
StreamingReporterBase( CATCH_MOVE(config) ) {
m_preferences.shouldRedirectStdOut = redirectStdout;
m_preferences.shouldReportAllAssertions = reportAllAssertions;
}
@@ -228,11 +232,7 @@ namespace {
TEST_CASE("Multireporter updates ReporterPreferences properly",
"[reporters][multi-reporter]") {
Catch::ConfigData config_data;
Catch::Config config( config_data );
StringIStream sstream;
Catch::ReporterConfig rep_config(
&config, &sstream, Catch::ColourMode::None, {} );
Catch::Config config( Catch::ConfigData{} );
Catch::MultiReporter multiReporter( &config );
// Post init defaults
@@ -261,19 +261,19 @@ TEST_CASE("Multireporter updates ReporterPreferences properly",
SECTION( "Adding reporters" ) {
multiReporter.addReporter(
Catch::Detail::make_unique<PreferenceReporter>(
true, false, rep_config ) );
true, false, makeDummyRepConfig(config) ) );
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true );
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false );
multiReporter.addReporter(
Catch::Detail::make_unique<PreferenceReporter>(
false, true, rep_config ) );
false, true, makeDummyRepConfig( config ) ) );
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true );
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true );
multiReporter.addReporter(
Catch::Detail::make_unique<PreferenceReporter>(
false, false, rep_config ) );
false, false, makeDummyRepConfig( config ) ) );
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true );
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true );
}
@@ -281,7 +281,7 @@ TEST_CASE("Multireporter updates ReporterPreferences properly",
namespace {
class TestReporterFactory : public Catch::IReporterFactory {
Catch::IEventListenerPtr create( Catch::ReporterConfig const& ) const override {
Catch::IEventListenerPtr create( Catch::ReporterConfig&& ) const override {
CATCH_INTERNAL_ERROR(
"This factory should never create a reporter" );
}