Debian Codegraph @

3rdparty/catch2/tests/SelfTest/TestRegistrations.cpp

raw symbols SCIP
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_tag_alias_autoregistrar.hpp>
10 #include <catch2/reporters/catch_reporter_event_listener.hpp>
11 #include <catch2/internal/catch_enforce.hpp>
12 #include <catch2/catch_test_case_info.hpp>
13 #include <catch2/reporters/catch_reporter_registrars.hpp>
14
15
16 // Some example tag aliases
17 CATCH_REGISTER_TAG_ALIAS("[@nhf]", "[failing]~[.]")
18 CATCH_REGISTER_TAG_ALIAS("[@tricky]", "[tricky]~[.]")
19
20 #ifdef __clang__
21 # pragma clang diagnostic ignored "-Wpadded"
22 # pragma clang diagnostic ignored "-Wweak-vtables"
23 # pragma clang diagnostic ignored "-Wc++98-compat"
24 #endif
25
26 /**
27 * Event listener that internally counts and validates received events.
28 *
29 * Currently only performs validation by counting received events, rather
30 * than performing full matching. This means that it won't fail if the *Ended
31 * events are provided in wrong order, as long as they come in the right amount
32 * and with the right nesting.
33 */
34 class ValidatingTestListener : public Catch::EventListenerBase {
35 struct EventCounter {
36 int starting = 0;
37 int ended = 0;
38
39 bool hasActiveEvent() const {
40 return starting > ended;
41 }
42 bool hasSingleActiveEvent() const {
43 return starting - 1 == ended;
44 }
45 bool allEventsEnded() const {
46 return starting == ended;
47 }
48 };
49
50 public:
51 static std::string getDescription() {
52 return "Validates ordering of Catch2's listener events";
53 }
54
55 ValidatingTestListener(Catch::IConfig const* config) :
56 EventListenerBase(config) {
57 m_preferences.shouldReportAllAssertions = true;
58 }
59
60 void testRunStarting( Catch::TestRunInfo const& ) override {
61 CATCH_ENFORCE( m_testRunCounter.starting == 0,
62 "Test run can only start once" );
63 ++m_testRunCounter.starting;
64 }
65 void testCaseStarting(Catch::TestCaseInfo const&) override {
66 CATCH_ENFORCE( m_testRunCounter.hasActiveEvent(),
67 "Test case can only be started if the test run has already started" );
68 CATCH_ENFORCE( m_testCaseCounter.allEventsEnded(),
69 "Test case cannot start if there is an unfinished one" );
70
71 ++m_testCaseCounter.starting;
72
73 // Reset the part tracking for partial test case events
74 m_lastSeenPartNumber = uint64_t(-1);
75 }
76
77 void testCasePartialStarting(Catch::TestCaseInfo const&,
78 uint64_t partNumber) override {
79 CATCH_ENFORCE( m_testCaseCounter.hasSingleActiveEvent(),
80 "Test case can only be partially started if the test case has fully started already" );
81 CATCH_ENFORCE( m_lastSeenPartNumber + 1 == partNumber,
82 "Partial test case started out of order" );
83
84 ++m_testCasePartialCounter.starting;
85 m_lastSeenPartNumber = partNumber;
86 }
87
88 void sectionStarting(Catch::SectionInfo const&) override {
89 CATCH_ENFORCE( m_testCaseCounter.hasSingleActiveEvent(),
90 "Section can only start in a test case" );
91 CATCH_ENFORCE( m_testCasePartialCounter.hasSingleActiveEvent(),
92 "Section can only start in a test case" );
93
94 ++m_sectionCounter.starting;
95 }
96
97 void assertionStarting(Catch::AssertionInfo const&) override {
98 CATCH_ENFORCE( m_testCaseCounter.hasSingleActiveEvent(),
99 "Assertion can only start if test case is started" );
100
101 ++m_assertionCounter.starting;
102 }
103 void assertionEnded(Catch::AssertionStats const&) override {
104 // todo:
105 // * Check that assertions are balanced
106 // * Check that assertions has started
107 ++m_assertionCounter.ended;
108 }
109
110 void sectionEnded(Catch::SectionStats const&) override {
111 CATCH_ENFORCE( m_sectionCounter.hasActiveEvent(),
112 "Section ended without corresponding start" );
113 // TODO: Check that all assertions ended
114
115 ++m_sectionCounter.ended;
116 }
117
118
119 void testCasePartialEnded(Catch::TestCaseStats const&,
120 uint64_t partNumber) override {
121 CATCH_ENFORCE( m_lastSeenPartNumber == partNumber,
122 "Partial test case ended out of order" );
123 CATCH_ENFORCE( m_testCasePartialCounter.hasSingleActiveEvent(),
124 "Partial test case ended without corresponding start" );
125 CATCH_ENFORCE( m_sectionCounter.allEventsEnded(),
126 "Partial test case ended with unbalanced sections" );
127 // TODO: Check that all assertions ended
128
129 ++m_testCasePartialCounter.ended;
130 }
131
132
133 void testCaseEnded(Catch::TestCaseStats const&) override {
134 CATCH_ENFORCE( m_testCaseCounter.hasSingleActiveEvent(),
135 "Test case end is not matched with test case start" );
136 CATCH_ENFORCE( m_testCasePartialCounter.allEventsEnded(),
137 "A partial test case has not ended" );
138 CATCH_ENFORCE( m_sectionCounter.allEventsEnded(),
139 "Test case ended with unbalanced sections" );
140
141 // TODO: Check that all assertions ended
142
143 ++m_testCaseCounter.ended;
144 }
145 void testRunEnded( Catch::TestRunStats const& ) override {
146 CATCH_ENFORCE( m_testRunCounter.hasSingleActiveEvent(),
147 "Test run end is not matched with test run start" );
148 CATCH_ENFORCE( m_testRunCounter.ended == 0,
149 "Test run can only end once" );
150
151 ++m_testRunCounter.ended;
152 }
153
154 ~ValidatingTestListener() override;
155
156 private:
157 EventCounter m_testRunCounter;
158 EventCounter m_testCaseCounter;
159 EventCounter m_testCasePartialCounter;
160 uint64_t m_lastSeenPartNumber = 0;
161 EventCounter m_sectionCounter;
162 EventCounter m_assertionCounter;
163 };
164
165
166 ValidatingTestListener::~ValidatingTestListener() {
167 // Throwing from noexcept destructor terminates, but we don't mind
168 // because this is test-only check and we don't need to try and recover
169 // from assumption violation here.
170
171 CATCH_ENFORCE( m_testRunCounter.ended < 2,
172 "Test run should be started at most once" );
173 CATCH_ENFORCE( m_testRunCounter.allEventsEnded(),
174 "The test run has not finished" );
175 CATCH_ENFORCE( m_testCaseCounter.allEventsEnded(),
176 "A test case did not finish" );
177
178 // TODO: other counters being balanced?
179 }
180
181 CATCH_REGISTER_LISTENER( ValidatingTestListener )