Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/ExtraTests/X27-CapturedStdoutInTestCaseEvents.cpp
blob: f5072f7ee58670ba0f8fb174666ebe278223ed75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

//              Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
//   (See accompanying file LICENSE.txt or copy at
//        https://www.boost.org/LICENSE_1_0.txt)

// SPDX-License-Identifier: BSL-1.0

/**\file
 * Test that the captured stdout/err in (partial) testCaseEnded events
 * is correct (e.g. that the partial test case event does not get accumulated
 * output).
 *
 * This is done by having a single test case that is entered multiple
 * times through generator, and a capturing listener that knows what it
 * should expect captured from the test case. It counts what differs and
 * prints the count when the run ends.
 */


#include <Progmasoft/Catch3/TestMacros.hpp>
#include <Progmasoft/Catch3/Generators/Generators.hpp>
#include <Progmasoft/Catch3/Generators/Range.hpp>
#include <Progmasoft/Catch3/Reporters/EventListener.hpp>
#include <Progmasoft/Catch3/Reporters/Registrars.hpp>

#include <iostream>
#include <string>

namespace {
class TestListener : public Catch::EventListenerBase {
    std::string stdOutString( uint64_t iter ){
        return "stdout " + std::to_string( iter ) + '\n';
    }
    std::string stdErrString(uint64_t iter) {
        return "stderr " + std::to_string( iter ) + '\n';
    }

    int m_errors = 0;

public:
    TestListener( Catch::IConfig const* config ):
        EventListenerBase( config ) {
        m_preferences.shouldRedirectStdOut = true;
    }

    void testCasePartialEnded( Catch::TestCaseStats const& stats,
                               uint64_t iter ) override {
        if ( stats.stdOut != stdOutString( iter ) ) {
            ++m_errors;
        }
        if ( stats.stdErr != stdErrString( iter ) ) {
            ++m_errors;
        }
    }

    void testCaseEnded( Catch::TestCaseStats const& stats ) override {
        if ( stats.stdOut != "stdout 0\nstdout 1\nstdout 2\nstdout 3\nstdout 4\nstdout 5\n" ) {
            ++m_errors;
        }
        if ( stats.stdErr != "stderr 0\nstderr 1\nstderr 2\nstderr 3\nstderr 4\nstderr 5\n" ) {
            ++m_errors;
        }
    }

    void testRunEnded( Catch::TestRunStats const& ) override {
        std::cerr << "X27 - listener saw " << m_errors << " error(s)\n";
    }
};
} // namespace

CATCH_REGISTER_LISTENER( TestListener )

TEST_CASE( "repeatedly entered test case" ) {
    auto i = GENERATE( range(0, 6) );
    std::cout << "stdout " << i << '\n';
    // Switch between writing to std::cerr and std::clog just to make sure
    // both are properly captured and redirected.
    ( ( i % 2 == 0 ) ? std::cerr : std::clog ) << "stderr " << i << '\n';
}