Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/PartTracker.tests.cpp

raw
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_test_macros.hpp>
10 #include <catch2/generators/catch_generators.hpp>
11 #include <catch2/internal/catch_test_case_tracker.hpp>
12
13
14 using namespace Catch;
15
16 namespace {
17 Catch::TestCaseTracking::NameAndLocationRef makeNAL( StringRef name ) {
18 return Catch::TestCaseTracking::NameAndLocationRef( name, Catch::SourceLineInfo("",0) );
19 }
20 }
21
22 TEST_CASE( "Tracker" ) {
23
24 TrackerContext ctx;
25 ctx.startRun();
26 ctx.startCycle();
27
28
29 ITracker& testCase = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
30 REQUIRE( testCase.isOpen() );
31
32 ITracker& s1 = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
33 REQUIRE( s1.isOpen() );
34
35 SECTION( "successfully close one section" ) {
36 s1.close();
37 REQUIRE( s1.isSuccessfullyCompleted() );
38 REQUIRE( testCase.isComplete() == false );
39
40 testCase.close();
41 REQUIRE( ctx.completedCycle() );
42 REQUIRE( testCase.isSuccessfullyCompleted() );
43 }
44
45 SECTION( "fail one section" ) {
46 s1.fail();
47 REQUIRE( s1.isComplete() );
48 REQUIRE( s1.isSuccessfullyCompleted() == false );
49 REQUIRE( testCase.isComplete() == false );
50
51 testCase.close();
52 REQUIRE( ctx.completedCycle() );
53 REQUIRE( testCase.isSuccessfullyCompleted() == false );
54
55 SECTION( "re-enter after failed section" ) {
56 ctx.startCycle();
57 ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
58 REQUIRE( testCase2.isOpen() );
59
60 ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
61 REQUIRE( s1b.isOpen() == false );
62
63 testCase2.close();
64 REQUIRE( ctx.completedCycle() );
65 REQUIRE( testCase.isComplete() );
66 REQUIRE( testCase.isSuccessfullyCompleted() );
67 }
68 SECTION( "re-enter after failed section and find next section" ) {
69 ctx.startCycle();
70 ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
71 REQUIRE( testCase2.isOpen() );
72
73 ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
74 REQUIRE( s1b.isOpen() == false );
75
76 ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
77 REQUIRE( s2.isOpen() );
78
79 s2.close();
80 REQUIRE( ctx.completedCycle() );
81
82 testCase2.close();
83 REQUIRE( testCase.isComplete() );
84 REQUIRE( testCase.isSuccessfullyCompleted() );
85 }
86 }
87
88 SECTION( "successfully close one section, then find another" ) {
89 s1.close();
90
91 ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
92 REQUIRE( s2.isOpen() == false );
93
94 testCase.close();
95 REQUIRE( testCase.isComplete() == false );
96
97 SECTION( "Re-enter - skips S1 and enters S2" ) {
98 ctx.startCycle();
99 ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
100 REQUIRE( testCase2.isOpen() );
101
102 ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
103 REQUIRE( s1b.isOpen() == false );
104
105 ITracker& s2b = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
106 REQUIRE( s2b.isOpen() );
107
108 REQUIRE( ctx.completedCycle() == false );
109
110 SECTION ("Successfully close S2") {
111 s2b.close();
112 REQUIRE( ctx.completedCycle() );
113
114 REQUIRE( s2b.isSuccessfullyCompleted() );
115 REQUIRE( testCase2.isComplete() == false );
116
117 testCase2.close();
118 REQUIRE( testCase2.isSuccessfullyCompleted() );
119 }
120 SECTION ("fail S2") {
121 s2b.fail();
122 REQUIRE( ctx.completedCycle() );
123
124 REQUIRE( s2b.isComplete() );
125 REQUIRE( s2b.isSuccessfullyCompleted() == false );
126
127 testCase2.close();
128 REQUIRE( testCase2.isSuccessfullyCompleted() == false );
129
130 // Need a final cycle
131 ctx.startCycle();
132 ITracker& testCase3 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
133 REQUIRE( testCase3.isOpen() );
134
135 ITracker& s1c = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
136 REQUIRE( s1c.isOpen() == false );
137
138 ITracker& s2c = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
139 REQUIRE( s2c.isOpen() == false );
140
141 testCase3.close();
142 REQUIRE( testCase3.isSuccessfullyCompleted() );
143 }
144 }
145 }
146
147 SECTION( "open a nested section" ) {
148 ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
149 REQUIRE( s2.isOpen() );
150
151 s2.close();
152 REQUIRE( s2.isComplete() );
153 REQUIRE( s1.isComplete() == false );
154
155 s1.close();
156 REQUIRE( s1.isComplete() );
157 REQUIRE( testCase.isComplete() == false );
158
159 testCase.close();
160 REQUIRE( testCase.isComplete() );
161 }
162 }
163
164 static bool previouslyRun = false;
165 static bool previouslyRunNested = false;
166
167 TEST_CASE( "#1394", "[.][approvals][tracker]" ) {
168 // -- Don't re-run after specified section is done
169 REQUIRE(previouslyRun == false);
170
171 SECTION( "RunSection" ) {
172 previouslyRun = true;
173 }
174 SECTION( "SkipSection" ) {
175 // cause an error if this section is called because it shouldn't be
176 REQUIRE(1 == 0);
177 }
178 }
179
180 TEST_CASE( "#1394 nested", "[.][approvals][tracker]" ) {
181 REQUIRE(previouslyRunNested == false);
182
183 SECTION( "NestedRunSection" ) {
184 SECTION( "s1" ) {
185 previouslyRunNested = true;
186 }
187 }
188 SECTION( "NestedSkipSection" ) {
189 // cause an error if this section is called because it shouldn't be
190 REQUIRE(1 == 0);
191 }
192 }
193
194 // Selecting a "not last" section inside a test case via -c "section" would
195 // previously only run the first subsection, instead of running all of them.
196 // This allows us to check that `"#1670 regression check" -c A` leads to
197 // 2 successful assertions.
198 TEST_CASE("#1670 regression check", "[.approvals][tracker]") {
199 SECTION("A") {
200 SECTION("1") SUCCEED();
201 SECTION("2") SUCCEED();
202 }
203 SECTION("B") {
204 SECTION("1") SUCCEED();
205 SECTION("2") SUCCEED();
206 }
207 }
208
209 // #1938 required a rework on how generator tracking works, so that `GENERATE`
210 // supports being sandwiched between two `SECTION`s. The following tests check
211 // various other scenarios through checking output in approval tests.
212 TEST_CASE("#1938 - GENERATE after a section", "[.][regression][generators]") {
213 SECTION("A") {
214 SUCCEED("A");
215 }
216 auto m = GENERATE(1, 2, 3);
217 SECTION("B") {
218 REQUIRE(m);
219 }
220 }
221
222 TEST_CASE("#1938 - flat generate", "[.][regression][generators]") {
223 auto m = GENERATE(1, 2, 3);
224 REQUIRE(m);
225 }
226
227 TEST_CASE("#1938 - nested generate", "[.][regression][generators]") {
228 auto m = GENERATE(1, 2, 3);
229 auto n = GENERATE(1, 2, 3);
230 REQUIRE(m);
231 REQUIRE(n);
232 }
233
234 TEST_CASE("#1938 - mixed sections and generates", "[.][regression][generators]") {
235 auto i = GENERATE(1, 2);
236 SECTION("A") {
237 SUCCEED("A");
238 }
239 auto j = GENERATE(3, 4);
240 SECTION("B") {
241 SUCCEED("B");
242 }
243 auto k = GENERATE(5, 6);
244 CAPTURE(i, j, k);
245 SUCCEED();
246 }
247
248 TEST_CASE("#1938 - Section followed by flat generate", "[.][regression][generators]") {
249 SECTION("A") {
250 REQUIRE(1);
251 }
252 auto m = GENERATE(2, 3);
253 REQUIRE(m);
254 }