| 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 | /**\file |
| 10 | * Test that the captured stdout/err in (partial) testCaseEnded events |
| 11 | * is correct (e.g. that the partial test case event does not get accumulated |
| 12 | * output). |
| 13 | * |
| 14 | * This is done by having a single test case that is entered multiple |
| 15 | * times through generator, and a custom capturing reporter that knows |
| 16 | * what it should expect captured from the test case. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | #include <catch2/catch_test_macros.hpp> |
| 21 | #include <catch2/generators/catch_generators.hpp> |
| 22 | #include <catch2/generators/catch_generators_range.hpp> |
| 23 | #include <catch2/reporters/catch_reporter_streaming_base.hpp> |
| 24 | #include <catch2/reporters/catch_reporter_registrars.hpp> |
| 25 | |
| 26 | |
| 27 | #include <iostream> |
| 28 | #include <string> |
| 29 | #include <utility> |
| 30 | |
| 31 | class TestReporter : public Catch::StreamingReporterBase { |
| 32 | std::string stdOutString( uint64_t iter ){ |
| 33 | return "stdout " + std::to_string( iter ) + '\n'; |
| 34 | } |
| 35 | std::string stdErrString(uint64_t iter) { |
| 36 | return "stderr " + std::to_string( iter ) + '\n'; |
| 37 | } |
| 38 | |
| 39 | public: |
| 40 | TestReporter( Catch::ReporterConfig&& _config ): |
| 41 | StreamingReporterBase( std::move(_config) ) { |
| 42 | m_preferences.shouldRedirectStdOut = true; |
| 43 | std::cout << "X27 - TestReporter constructed\n"; |
| 44 | } |
| 45 | |
| 46 | static std::string getDescription() { |
| 47 | return "X27 test reporter"; |
| 48 | } |
| 49 | |
| 50 | void testCasePartialEnded( Catch::TestCaseStats const& stats, |
| 51 | uint64_t iter ) override { |
| 52 | if ( stats.stdOut != stdOutString( iter ) ) { |
| 53 | std::cerr << "X27 ERROR in partial stdout\n" << stats.stdOut; |
| 54 | } |
| 55 | if ( stats.stdErr != stdErrString( iter ) ) { |
| 56 | std::cerr << "X27 ERROR in partial stderr\n" << stats.stdErr; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void testCaseEnded( Catch::TestCaseStats const& stats ) override { |
| 61 | if ( stats.stdOut != "stdout 0\nstdout 1\nstdout 2\nstdout 3\nstdout 4\nstdout 5\n" ) { |
| 62 | std::cerr << "X27 ERROR in full stdout\n" << stats.stdOut; |
| 63 | } |
| 64 | if ( stats.stdErr != "stderr 0\nstderr 1\nstderr 2\nstderr 3\nstderr 4\nstderr 5\n" ) { |
| 65 | std::cerr << "X27 ERROR in full stderr\n" << stats.stdErr; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | ~TestReporter() override; |
| 70 | }; |
| 71 | |
| 72 | TestReporter::~TestReporter() = default; |
| 73 | |
| 74 | CATCH_REGISTER_REPORTER( "test-reporter", TestReporter ) |
| 75 | |
| 76 | TEST_CASE( "repeatedly entered test case" ) { |
| 77 | auto i = GENERATE( range(0, 6) ); |
| 78 | std::cout << "stdout " << i << '\n'; |
| 79 | // Switch between writing to std::cerr and std::clog just to make sure |
| 80 | // both are properly captured and redirected. |
| 81 | ( ( i % 2 == 0 ) ? std::cerr : std::clog ) << "stderr " << i << '\n'; |
| 82 | } |