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

summaryrefslogtreecommitdiffhomepage
path: root/tests/ExtraTests/X38-ThrowingReporter.cpp
blob: 5f6603226404318ea95767e22880b227702868a5 (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

//              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
 * Runs simple test cases with reporter that likes to throw.
 *
 * Used to analyze how Catch2 currently treats throwing reporters/listeners,
 * and what changes make sense for the future.
 */

#include <catch2/catch_test_macros.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#include <catch2/reporters/catch_reporter_registrars.hpp>
#include <catch2/reporters/catch_reporter_streaming_base.hpp>

namespace {

    class ThrowingReporter final : public Catch::StreamingReporterBase {
        bool m_throwInConstructor = false;
        bool m_throwInAssertionEnded = false;

        void processOptions() {
            if ( m_customOptions.find( "Xthrow-in-constructor" ) !=
                 m_customOptions.end() ) {
                m_throwInConstructor = true;
            }
            if ( m_customOptions.find( "Xthrow-in-assertion-ended" ) !=
                 m_customOptions.end() ) {
                m_throwInAssertionEnded = true;
            }
        }

    public:
        ThrowingReporter( Catch::ReporterConfig&& config ):
            StreamingReporterBase( CATCH_MOVE( config ) ) {
            m_preferences.shouldReportAllAssertions = true;
            m_preferences.shouldReportAllAssertionStarts = false;

            Catch::rejectSuperfluousConfigKeys(
                m_customOptions,
                { "Xthrow-in-constructor", "Xthrow-in-assertion-ended" } );
            processOptions();

            if ( m_throwInConstructor ) {
                throw std::runtime_error( "Exception in constructor" );
            }
        }

        void assertionEnded( Catch::AssertionStats const& stats ) override {
            m_stream << "assertionEnded\n";
            if ( m_throwInAssertionEnded ) {
                throw std::runtime_error( "Exception in assertionEnded" );
            }
        }

        static std::string getDescription() {
            using namespace std::string_literals;
            return "Throws for different reporter events"s;
        }
    };

    CATCH_REGISTER_REPORTER( "ThrowingReporter", ThrowingReporter )

} // namespace

TEST_CASE( "REQUIRE - Pass" ) { REQUIRE( true ); }

TEST_CASE( "REQUIRE - Fail" ) { REQUIRE( false ); }

TEST_CASE( "REQUIRE_THROWS - Pass" ) {
    REQUIRE_THROWS( [] {
        throw std::runtime_error( "expected" );
    }() );
}