| 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.get(); |
| 111 | |
| 112 | Catch::Config config( Catch::ConfigData{} ); |
| 113 | auto reporter = factory.second->create( Catch::ReporterConfig{ |
| 114 | &config, CATCH_MOVE( sstream ), Catch::ColourMode::None, {} } ); |
| 115 | |
| 116 | DYNAMIC_SECTION( factory.first << " reporter lists tags" ) { |
| 117 | std::vector<Catch::TagInfo> tags(1); |
| 118 | tags[0].add("fakeTag"_catch_sr); |
| 119 | reporter->listTags(tags); |
| 120 | |
| 121 | auto listingString = sstreamRef.str(); |
| 122 | REQUIRE_THAT(listingString, ContainsSubstring("fakeTag"s)); |
| 123 | } |
| 124 | |
| 125 | DYNAMIC_SECTION( factory.first << " reporter lists reporters" ) { |
| 126 | std::vector<Catch::ReporterDescription> reporters( |
| 127 | { { "fake reporter", "fake description" } } ); |
| 128 | reporter->listReporters(reporters); |
| 129 | |
| 130 | auto listingString = sstreamRef.str(); |
| 131 | REQUIRE_THAT(listingString, ContainsSubstring("fake reporter"s)); |
| 132 | } |
| 133 | |
| 134 | DYNAMIC_SECTION( factory.first << " reporter lists tests" ) { |
| 135 | Catch::TestCaseInfo fakeInfo{ |
| 136 | ""s, |
| 137 | { "fake test name"_catch_sr, "[fakeTestTag]"_catch_sr }, |
| 138 | { "fake-file.cpp", 123456789 } }; |
| 139 | std::vector<Catch::TestCaseHandle> tests({ {&fakeInfo, nullptr} }); |
| 140 | reporter->listTests(tests); |
| 141 | |
| 142 | auto listingString = sstreamRef.str(); |
| 143 | REQUIRE_THAT( listingString, |
| 144 | ContainsSubstring( "fake test name"s ) && |
| 145 | ContainsSubstring( "fakeTestTag"s ) ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | TEST_CASE("Reproducer for #2309 - a very long description past 80 chars (default console width) with a late colon : blablabla", "[console-reporter]") { |
| 152 | SUCCEED(); |
| 153 | } |
| 154 | |
| 155 | namespace { |
| 156 | // A listener that writes provided string into destination, |
| 157 | // to record order of testRunStarting invocation. |
| 158 | class MockListener : public Catch::EventListenerBase { |
| 159 | std::string m_witness; |
| 160 | std::vector<std::string>& m_recorder; |
| 161 | public: |
| 162 | MockListener( std::string witness, |
| 163 | std::vector<std::string>& recorder, |
| 164 | Catch::IConfig const* config ): |
| 165 | EventListenerBase( config ), |
| 166 | m_witness( CATCH_MOVE(witness) ), |
| 167 | m_recorder( recorder ) |
| 168 | {} |
| 169 | |
| 170 | void testRunStarting( Catch::TestRunInfo const& ) override { |
| 171 | m_recorder.push_back( m_witness ); |
| 172 | } |
| 173 | }; |
| 174 | // A reporter that writes provided string into destination, |
| 175 | // to record order of testRunStarting invocation. |
| 176 | class MockReporter : public Catch::StreamingReporterBase { |
| 177 | std::string m_witness; |
| 178 | std::vector<std::string>& m_recorder; |
| 179 | public: |
| 180 | MockReporter( std::string witness, |
| 181 | std::vector<std::string>& recorder, |
| 182 | Catch::ReporterConfig&& config ): |
| 183 | StreamingReporterBase( CATCH_MOVE(config) ), |
| 184 | m_witness( CATCH_MOVE(witness) ), |
| 185 | m_recorder( recorder ) |
| 186 | {} |
| 187 | |
| 188 | void testRunStarting( Catch::TestRunInfo const& ) override { |
| 189 | m_recorder.push_back( m_witness ); |
| 190 | } |
| 191 | }; |
| 192 | } // namespace |
| 193 | |
| 194 | TEST_CASE("Multireporter calls reporters and listeners in correct order", |
| 195 | "[reporters][multi-reporter]") { |
| 196 | Catch::Config config( Catch::ConfigData{} ); |
| 197 | |
| 198 | // We add reporters before listeners, to check that internally they |
| 199 | // get sorted properly, and listeners are called first anyway. |
| 200 | Catch::MultiReporter multiReporter( &config ); |
| 201 | std::vector<std::string> records; |
| 202 | multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>( |
| 203 | "Goodbye", records, makeDummyRepConfig(config) ) ); |
| 204 | multiReporter.addListener( |
| 205 | Catch::Detail::make_unique<MockListener>( "Hello", records, &config ) ); |
| 206 | multiReporter.addListener( |
| 207 | Catch::Detail::make_unique<MockListener>( "world", records, &config ) ); |
| 208 | multiReporter.addReporter( Catch::Detail::make_unique<MockReporter>( |
| 209 | "world", records, makeDummyRepConfig(config) ) ); |
| 210 | multiReporter.testRunStarting( { "" } ); |
| 211 | |
| 212 | std::vector<std::string> expected( { "Hello", "world", "Goodbye", "world" } ); |
| 213 | REQUIRE( records == expected ); |
| 214 | } |
| 215 | |
| 216 | namespace { |
| 217 | // A listener that sets it preferences to test that multireporter, |
| 218 | // properly sets up its own preferences |
| 219 | class PreferenceListener : public Catch::EventListenerBase { |
| 220 | public: |
| 221 | PreferenceListener( bool redirectStdout, |
| 222 | bool reportAllAssertions, |
| 223 | Catch::IConfig const* config ): |
| 224 | EventListenerBase( config ) { |
| 225 | m_preferences.shouldRedirectStdOut = redirectStdout; |
| 226 | m_preferences.shouldReportAllAssertions = reportAllAssertions; |
| 227 | } |
| 228 | }; |
| 229 | // A reporter that sets it preferences to test that multireporter, |
| 230 | // properly sets up its own preferences |
| 231 | class PreferenceReporter : public Catch::StreamingReporterBase { |
| 232 | public: |
| 233 | PreferenceReporter( bool redirectStdout, |
| 234 | bool reportAllAssertions, |
| 235 | Catch::ReporterConfig&& config ): |
| 236 | StreamingReporterBase( CATCH_MOVE(config) ) { |
| 237 | m_preferences.shouldRedirectStdOut = redirectStdout; |
| 238 | m_preferences.shouldReportAllAssertions = reportAllAssertions; |
| 239 | } |
| 240 | }; |
| 241 | } // namespace |
| 242 | |
| 243 | TEST_CASE("Multireporter updates ReporterPreferences properly", |
| 244 | "[reporters][multi-reporter]") { |
| 245 | |
| 246 | Catch::Config config( Catch::ConfigData{} ); |
| 247 | Catch::MultiReporter multiReporter( &config ); |
| 248 | |
| 249 | // Post init defaults |
| 250 | REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == false ); |
| 251 | REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false ); |
| 252 | |
| 253 | SECTION( "Adding listeners" ) { |
| 254 | multiReporter.addListener( |
| 255 | Catch::Detail::make_unique<PreferenceListener>( |
| 256 | true, false, &config ) ); |
| 257 | REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 258 | REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false ); |
| 259 | |
| 260 | multiReporter.addListener( |
| 261 | Catch::Detail::make_unique<PreferenceListener>( |
| 262 | false, true, &config ) ); |
| 263 | REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 264 | REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true); |
| 265 | |
| 266 | multiReporter.addListener( |
| 267 | Catch::Detail::make_unique<PreferenceListener>( |
| 268 | false, false, &config ) ); |
| 269 | REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 270 | REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true ); |
| 271 | } |
| 272 | SECTION( "Adding reporters" ) { |
| 273 | multiReporter.addReporter( |
| 274 | Catch::Detail::make_unique<PreferenceReporter>( |
| 275 | true, false, makeDummyRepConfig(config) ) ); |
| 276 | REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 277 | REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == false ); |
| 278 | |
| 279 | multiReporter.addReporter( |
| 280 | Catch::Detail::make_unique<PreferenceReporter>( |
| 281 | false, true, makeDummyRepConfig( config ) ) ); |
| 282 | REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 283 | REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true ); |
| 284 | |
| 285 | multiReporter.addReporter( |
| 286 | Catch::Detail::make_unique<PreferenceReporter>( |
| 287 | false, false, makeDummyRepConfig( config ) ) ); |
| 288 | REQUIRE( multiReporter.getPreferences().shouldRedirectStdOut == true ); |
| 289 | REQUIRE( multiReporter.getPreferences().shouldReportAllAssertions == true ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | namespace { |
| 294 | class TestReporterFactory : public Catch::IReporterFactory { |
| 295 | Catch::IEventListenerPtr create( Catch::ReporterConfig&& ) const override { |
| 296 | CATCH_INTERNAL_ERROR( |
| 297 | "This factory should never create a reporter" ); |
| 298 | } |
| 299 | std::string getDescription() const override { |
| 300 | return "Fake test factory"; |
| 301 | } |
| 302 | }; |
| 303 | } |
| 304 | |
| 305 | TEST_CASE("Registering reporter with '::' in name fails", |
| 306 | "[reporters][registration]") { |
| 307 | Catch::ReporterRegistry registry; |
| 308 | |
| 309 | REQUIRE_THROWS_WITH( registry.registerReporter( |
| 310 | "with::doublecolons", |
| 311 | Catch::Detail::make_unique<TestReporterFactory>() ), |
| 312 | "'::' is not allowed in reporter name: 'with::doublecolons'" ); |
| 313 | } |
| 314 | |
| 315 | TEST_CASE("Registering multiple reporters with the same name fails", |
| 316 | "[reporters][registration][approvals]") { |
| 317 | Catch::ReporterRegistry registry; |
| 318 | |
| 319 | registry.registerReporter( |
| 320 | "some-reporter-name", |
| 321 | Catch::Detail::make_unique<TestReporterFactory>() ); |
| 322 | |
| 323 | REQUIRE_THROWS_WITH( |
| 324 | registry.registerReporter( |
| 325 | "some-reporter-name", |
| 326 | Catch::Detail::make_unique<TestReporterFactory>() ), |
| 327 | "reporter using 'some-reporter-name' as name was already registered" ); |
| 328 | } |