| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#include <catch2/catch_test_macros.hpp> |
| 10 |
|
| 11 |
#include <catch2/catch_test_case_info.hpp> |
| 12 |
#include <catch2/catch_config.hpp> |
| 13 |
#include <catch2/interfaces/catch_interfaces_reporter.hpp> |
| 14 |
#include <catch2/interfaces/catch_interfaces_reporter_factory.hpp> |
| 15 |
#include <catch2/internal/catch_console_colour.hpp> |
| 16 |
#include <catch2/internal/catch_enforce.hpp> |
| 17 |
#include <catch2/internal/catch_list.hpp> |
| 18 |
#include <catch2/internal/catch_reporter_registry.hpp> |
| 19 |
#include <catch2/internal/catch_istream.hpp> |
| 20 |
#include <catch2/matchers/catch_matchers_string.hpp> |
| 21 |
#include <catch2/reporters/catch_reporter_helpers.hpp> |
| 22 |
#include <catch2/reporters/catch_reporter_event_listener.hpp> |
| 23 |
#include <catch2/reporters/catch_reporter_streaming_base.hpp> |
| 24 |
#include <catch2/reporters/catch_reporter_multi.hpp> |
| 25 |
#include <catch2/internal/catch_move_and_forward.hpp> |
| 26 |
|
| 27 |
#include <sstream> |
| 28 |
|
| 29 |
namespace { |
| 30 |
class StringIStream : public Catch::IStream { |
| 31 |
public: |
| 32 |
std::ostream& stream() override { return sstr; } |
| 33 |
std::string str() const { return sstr.str(); } |
| 34 |
private: |
| 35 |
std::stringstream sstr; |
| 36 |
}; |
| 37 |
|
| 38 |
//! config must outlive the function |
| 39 |
Catch::ReporterConfig makeDummyRepConfig( Catch::Config const& config ) { |
| 40 |
return Catch::ReporterConfig{ |
| 41 |
&config, |
| 42 |
Catch::Detail::make_unique<StringIStream>(), |
| 43 |
Catch::ColourMode::None, |
| 44 |
{} }; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
TEST_CASE( "The default listing implementation write to provided stream", |
| 49 |
"[reporters][reporter-helpers]" ) { |
| 50 |
using Catch::Matchers::ContainsSubstring; |
| 51 |
using namespace std::string_literals; |
| 52 |
|
| 53 |
StringIStream sstream; |
| 54 |
SECTION( "Listing tags" ) { |
| 55 |
std::vector<Catch::TagInfo> tags(1); |
| 56 |
tags[0].add("fakeTag"_catch_sr); |
| 57 |
Catch::defaultListTags(sstream.stream(), tags, false); |
| 58 |
|
| 59 |
auto listingString = sstream.str(); |
| 60 |
REQUIRE_THAT(listingString, ContainsSubstring("[fakeTag]"s)); |
| 61 |
} |
| 62 |
SECTION( "Listing reporters" ) { |
| 63 |
std::vector<Catch::ReporterDescription> reporters( |
| 64 |
{ { "fake reporter", "fake description" } } ); |
| 65 |
Catch::defaultListReporters(sstream.stream(), reporters, Catch::Verbosity::Normal); |
| 66 |
|
| 67 |
auto listingString = sstream.str(); |
| 68 |
REQUIRE_THAT( listingString, |
| 69 |
ContainsSubstring( "fake reporter"s ) && |
| 70 |
ContainsSubstring( "fake description"s ) ); |
| 71 |
} |
| 72 |
SECTION( "Listing tests" ) { |
| 73 |
Catch::TestCaseInfo fakeInfo{ |
| 74 |
""s, |
| 75 |
{ "fake test name"_catch_sr, "[fakeTestTag]"_catch_sr }, |
| 76 |
{ "fake-file.cpp", 123456789 } }; |
| 77 |
std::vector<Catch::TestCaseHandle> tests({ {&fakeInfo, nullptr} }); |
| 78 |
auto colour = Catch::makeColourImpl( Catch::ColourMode::None, &sstream); |
| 79 |
Catch::defaultListTests(sstream.stream(), colour.get(), tests, false, Catch::Verbosity::Normal); |
| 80 |
|
| 81 |
auto listingString = sstream.str(); |
| 82 |
REQUIRE_THAT( listingString, |
| 83 |
ContainsSubstring( "fake test name"s ) && |
| 84 |
ContainsSubstring( "fakeTestTag"s ) ); |
| 85 |
} |
| 86 |
SECTION( "Listing listeners" ) { |
| 87 |
std::vector<Catch::ListenerDescription> listeners( |
| 88 |
{ { "fakeListener"_catch_sr, "fake description" } } ); |
| 89 |
|
| 90 |
Catch::defaultListListeners( sstream.stream(), listeners ); |
| 91 |
auto listingString = sstream.str(); |
| 92 |
REQUIRE_THAT( listingString, |
| 93 |
ContainsSubstring( "fakeListener"s ) && |
| 94 |
ContainsSubstring( "fake description"s ) ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
TEST_CASE( "Reporter's write listings to provided stream", "[reporters]" ) { |
| 99 |
using Catch::Matchers::ContainsSubstring; |
| 100 |
using namespace std::string_literals; |
| 101 |
|
| 102 |
auto const& factories = Catch::getRegistryHub().getReporterRegistry().getFactories(); |
| 103 |
// If there are no reporters, the test would pass falsely |
| 104 |
// while there is something obviously broken |
| 105 |
REQUIRE_FALSE(factories.empty()); |
| 106 |
|
| 107 |
for (auto const& factory : factories) { |
| 108 |
INFO("Tested reporter: " << factory.first); |
| 109 |
auto sstream = Catch::Detail::make_unique<StringIStream>(); |
| 110 |
auto& sstreamRef = *sstream; |
| 111 |
|
| 112 |
Catch::ConfigData cfg_data; |
| 113 |
cfg_data.rngSeed = 1234; |
| 114 |
Catch::Config config( cfg_data ); |
| 115 |
auto reporter = factory.second->create( Catch::ReporterConfig{ |
| 116 |
&config, CATCH_MOVE( sstream ), Catch::ColourMode::None, {} } ); |
| 117 |
|
| 118 |
DYNAMIC_SECTION( factory.first << " reporter lists tags" ) { |
| 119 |
std::vector<Catch::TagInfo> tags(1); |
| 120 |
tags[0].add("fakeTag"_catch_sr); |
| 121 |
reporter->listTags(tags); |
| 122 |
|
| 123 |
auto listingString = sstreamRef.str(); |
| 124 |
REQUIRE_THAT(listingString, ContainsSubstring("fakeTag"s)); |
| 125 |
} |
| 126 |
|
| 127 |
DYNAMIC_SECTION( factory.first << " reporter lists reporters" ) { |
| 128 |
std::vector<Catch::ReporterDescription> reporters( |
| 129 |
{ { "fake reporter", "fake description" } } ); |
| 130 |
reporter->listReporters(reporters); |
| 131 |
|
| 132 |
auto listingString = sstreamRef.str(); |
| 133 |
REQUIRE_THAT(listingString, ContainsSubstring("fake reporter"s)); |
| 134 |
} |
| 135 |
|
| 136 |
DYNAMIC_SECTION( factory.first << " reporter lists tests" ) { |
| 137 |
Catch::TestCaseInfo fakeInfo{ |
| 138 |
""s, |
| 139 |
{ "fake test name"_catch_sr, "[fakeTestTag]"_catch_sr }, |
| 140 |
{ "fake-file.cpp", 123456789 } }; |
| 141 |
std::vector<Catch::TestCaseHandle> tests({ {&fakeInfo, nullptr} }); |
| 142 |
reporter->listTests(tests); |
| 143 |
|
| 144 |
auto listingString = sstreamRef.str(); |
| 145 |
REQUIRE_THAT( listingString, |
| 146 |
ContainsSubstring( "fake test name"s ) && |
| 147 |
ContainsSubstring( "fakeTestTag"s ) ); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
TEST_CASE("Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla", "[console-reporter]") { |
| 154 |
SUCCEED(); |
| 155 |
} |
| 156 |
|
| 157 |
namespace { |
| 158 |
// A listener that writes provided string into destination, |
| 159 |
// to record order of testRunStarting invocation. |
| 160 |
class MockListener : public Catch::EventListenerBase { |
| 161 |
std::string m_witness; |
| 162 |
std::vector<std::string>& m_recorder; |
| 163 |
public: |
| 164 |
MockListener( std::string witness, |
| 165 |
std::vector<std::string>& recorder, |
| 166 |
Catch::IConfig const* config ): |
| 167 |
EventListenerBase( config ), |
| 168 |
m_witness( CATCH_MOVE(witness) ), |
| 169 |
m_recorder( recorder ) |
| 170 |
{} |
| 171 |
|
| 172 |
void testRunStarting( Catch::TestRunInfo const& ) override { |
| 173 |
m_recorder.push_back( m_witness ); |
| 174 |
} |
| 175 |
}; |
| 176 |
// A reporter that writes provided string into destination, |
| 177 |
// to record order of testRunStarting invocation. |
| 178 |
class MockReporter : public Catch::StreamingReporterBase { |
| 179 |
std::string m_witness; |
| 180 |
std::vector<std::string>& m_recorder; |
| 181 |
public: |
| 182 |
MockReporter( std::string witness, |
| 183 |
std::vector<std::string>& recorder, |
| 184 |
Catch::ReporterConfig&& config ): |
| 185 |
StreamingReporterBase( CATCH_MOVE(config) ), |
| 186 |
m_witness( CATCH_MOVE(witness) ), |
| 187 |
m_recorder( recorder ) |
| 188 |
{} |
| 189 |
|
| 190 |
void testRunStarting( Catch::TestRunInfo const& ) override { |
| 191 |
m_recorder.push_back( m_witness ); |
| 192 |
} |
| 193 |
}; |
| 194 |
} // namespace |
| 195 |
|
| 196 |
TEST_CASE("Multireporter calls reporters and listeners in correct order", |
| 197 |
"[reporters][multi-reporter]") { |
| 198 |
Catch::Config config( Catch::ConfigData{} ); |
| 199 |
|
| 200 |
// We add reporters before listeners, to check that internally they |
| 201 |
// get sorted properly, and listeners are called first anyway. |
| 202 |
Catch::MultiReporter multiReporter( &config ); |
| 203 |
std::vector<std::string> records; |
| 204 |
multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>( |
| 205 |
"Goodbye", records, makeDummyRepConfig(config) ) ); |
| 206 |
multiReporter.addListener( |
| 207 |
Catch::Detail::make_unique<MockListener>( "Hello", records, &config ) ); |
| 208 |
multiReporter.addListener( |
| 209 |
Catch::Detail::make_unique<MockListener>( "world", records, &config ) ); |
| 210 |
multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>( |
| 211 |
"world", records, makeDummyRepConfig(config) ) ); |
| 212 |
multiReporter.testRunStarting( { "" } ); |
| 213 |
|
| 214 |
std::vector<std::string> expected( { "Hello", "world", "Goodbye", "world" } ); |
| 215 |
REQUIRE( records == expected ); |
| 216 |
} |
| 217 |
|
| 218 |
namespace { |
| 219 |
// A listener that sets it preferences to test that multireporter, |
| 220 |
// properly sets up its own preferences |
| 221 |
class PreferenceListener : public Catch::EventListenerBase { |
| 222 |
public: |
| 223 |
PreferenceListener( bool redirectStdout, |
| 224 |
bool reportAllAssertions, |
| 225 |
Catch::IConfig const* config ): |
| 226 |
EventListenerBase( config ) { |
| 227 |
m_preferences.shouldRedirectStdOut = redirectStdout; |
| 228 |
m_preferences.shouldReportAllAssertions = reportAllAssertions; |
| 229 |
} |
| 230 |
}; |
| 231 |
// A reporter that sets it preferences to test that multireporter, |
| 232 |
// properly sets up its own preferences |
| 233 |
class PreferenceReporter : public Catch::StreamingReporterBase { |
| 234 |
public: |
| 235 |
PreferenceReporter( bool redirectStdout, |
| 236 |
bool reportAllAssertions, |
| 237 |
Catch::ReporterConfig&& config ): |
| 238 |
StreamingReporterBase( CATCH_MOVE(config) ) { |
| 239 |
m_preferences.shouldRedirectStdOut = redirectStdout; |
| 240 |
m_preferences.shouldReportAllAssertions = reportAllAssertions; |
| 241 |
} |
| 242 |
}; |
| 243 |
} // namespace |
| 244 |
|
| 245 |
TEST_CASE("Multireporter updates ReporterPreferences properly", |
| 246 |
"[reporters][multi-reporter]") { |
| 247 |
|
| 248 |
Catch::Config config( Catch::ConfigData{} ); |
| 249 |
Catch::MultiReporter multiReporter( &config ); |
| 250 |
|
| 251 |
// Post init defaults |
| 252 |
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == false ); |
| 253 |
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false ); |
| 254 |
|
| 255 |
SECTION( "Adding listeners" ) { |
| 256 |
multiReporter.addListener( |
| 257 |
Catch::Detail::make_unique<PreferenceListener>( |
| 258 |
true, false, &config ) ); |
| 259 |
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 260 |
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false ); |
| 261 |
|
| 262 |
multiReporter.addListener( |
| 263 |
Catch::Detail::make_unique<PreferenceListener>( |
| 264 |
false, true, &config ) ); |
| 265 |
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 266 |
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true); |
| 267 |
|
| 268 |
multiReporter.addListener( |
| 269 |
Catch::Detail::make_unique<PreferenceListener>( |
| 270 |
false, false, &config ) ); |
| 271 |
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 272 |
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true ); |
| 273 |
} |
| 274 |
SECTION( "Adding reporters" ) { |
| 275 |
multiReporter.addReporter( |
| 276 |
Catch::Detail::make_unique<PreferenceReporter>( |
| 277 |
true, false, makeDummyRepConfig(config) ) ); |
| 278 |
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 279 |
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false ); |
| 280 |
|
| 281 |
multiReporter.addReporter( |
| 282 |
Catch::Detail::make_unique<PreferenceReporter>( |
| 283 |
false, true, makeDummyRepConfig( config ) ) ); |
| 284 |
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 285 |
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true ); |
| 286 |
|
| 287 |
multiReporter.addReporter( |
| 288 |
Catch::Detail::make_unique<PreferenceReporter>( |
| 289 |
false, false, makeDummyRepConfig( config ) ) ); |
| 290 |
REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 291 |
REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true ); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
namespace { |
| 296 |
class TestReporterFactory : public Catch::IReporterFactory { |
| 297 |
Catch::IEventListenerPtr create( Catch::ReporterConfig&& ) const override { |
| 298 |
CATCH_INTERNAL_ERROR( |
| 299 |
"This factory should never create a reporter" ); |
| 300 |
} |
| 301 |
std::string getDescription() const override { |
| 302 |
return "Fake test factory"; |
| 303 |
} |
| 304 |
}; |
| 305 |
} |
| 306 |
|
| 307 |
TEST_CASE("Registering reporter with '::' in name fails", |
| 308 |
"[reporters][registration]") { |
| 309 |
Catch::ReporterRegistry registry; |
| 310 |
|
| 311 |
REQUIRE_THROWS_WITH( registry.registerReporter( |
| 312 |
"with::doublecolons", |
| 313 |
Catch::Detail::make_unique<TestReporterFactory>() ), |
| 314 |
"'::' is not allowed in reporter name: 'with::doublecolons'" ); |
| 315 |
} |
| 316 |
|
| 317 |
TEST_CASE("Registering multiple reporters with the same name fails", |
| 318 |
"[reporters][registration][approvals]") { |
| 319 |
Catch::ReporterRegistry registry; |
| 320 |
|
| 321 |
registry.registerReporter( |
| 322 |
"some-reporter-name", |
| 323 |
Catch::Detail::make_unique<TestReporterFactory>() ); |
| 324 |
|
| 325 |
REQUIRE_THROWS_WITH( |
| 326 |
registry.registerReporter( |
| 327 |
"some-reporter-name", |
| 328 |
Catch::Detail::make_unique<TestReporterFactory>() ), |
| 329 |
"reporter using 'some-reporter-name' as name was already registered" ); |
| 330 |
} |