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

summaryrefslogtreecommitdiffhomepage
path: root/tests/ExtraTests/X21-PartialTestCaseEvents.cpp
blob: d98d07b365fbfa52138d59aa930fda8129a38980 (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

//              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
 * Registers a listener that records the testCase* events and prints them
 * when the test run ends, outside any test, where the runner passes the
 * output on.
 *
 * The resulting executable can then be used by an external Python script
 * to verify that testCase{Starting,Ended} and testCasePartial{Starting,Ended}
 * events are properly nested.
 */

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

#include <iostream>
#include <string>

using Catch::TestCaseInfo;
using Catch::TestCaseStats;

namespace {
class PartialListener : public Catch::EventListenerBase {
public:
    using EventListenerBase::EventListenerBase;

    //! Called _once_ for each TEST_CASE, no matter how many times it is entered
    void testCaseStarting(TestCaseInfo const& testInfo) override {
        m_events += "TestCaseStarting: " + testInfo.name + '\n';
    }
    //! Called _every time_ a TEST_CASE is entered, including repeats (due to sections)
    void testCasePartialStarting(TestCaseInfo const& testInfo, uint64_t partNumber) override {
        m_events += "TestCaseStartingPartial: " + testInfo.name + '#' + std::to_string(partNumber) + '\n';
    }

    //! Called _every time_ a TEST_CASE is entered, including repeats (due to sections)
    void testCasePartialEnded(TestCaseStats const& testCaseStats, uint64_t partNumber) override {
        m_events += "TestCasePartialEnded: " + testCaseStats.testInfo->name + '#' + std::to_string(partNumber) + '\n';
    }

    //! Called _once_ for each TEST_CASE, no matter how many times it is entered
    void testCaseEnded(TestCaseStats const& testCaseStats) override {
        m_events += "TestCaseEnded: " + testCaseStats.testInfo->name + '\n';
    }

    void testRunEnded( Catch::TestRunStats const& ) override {
        std::cout << "BEGIN EVENTS\n" << m_events << "END EVENTS\n" << std::flush;
    }

private:
    std::string m_events;
};
} // namespace

CATCH_REGISTER_LISTENER(PartialListener)

TEST_CASE("section") {
    SECTION("A") {}
    SECTION("B") {}
    SECTION("C") {}
    SECTION("D") {}
}

TEST_CASE("generator") {
    auto _ = GENERATE(1, 2, 3, 4);
    (void)_;
}