1 /*
2  *  Created by Phil on 1/10/2015.
3  *  Copyright 2015 Two Blue Cubes Ltd
4  *
5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 #include "internal/catch_suppress_warnings.h"
9 #include "internal/catch_test_case_tracker.h"
10 
11 #include "catch.hpp"
12 
13 using namespace Catch;
14 
15 namespace {
makeNAL(std::string const & name)16 Catch::TestCaseTracking::NameAndLocation makeNAL( std::string const& name ) {
17     return Catch::TestCaseTracking::NameAndLocation( name, Catch::SourceLineInfo("",0) );
18 }
19 }
20 
21 TEST_CASE( "Tracker" ) {
22 
23     TrackerContext ctx;
24     ctx.startRun();
25     ctx.startCycle();
26 
27 
28     ITracker& testCase = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
29     REQUIRE( testCase.isOpen() );
30 
31     ITracker& s1 = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
32     REQUIRE( s1.isOpen() );
33 
34     SECTION( "successfully close one section" ) {
35         s1.close();
36         REQUIRE( s1.isSuccessfullyCompleted() );
37         REQUIRE( testCase.isComplete() == false );
38 
39         testCase.close();
40         REQUIRE( ctx.completedCycle() );
41         REQUIRE( testCase.isSuccessfullyCompleted() );
42     }
43 
44     SECTION( "fail one section" ) {
45         s1.fail();
46         REQUIRE( s1.isComplete() );
47         REQUIRE( s1.isSuccessfullyCompleted() == false );
48         REQUIRE( testCase.isComplete() == false );
49 
50         testCase.close();
51         REQUIRE( ctx.completedCycle() );
52         REQUIRE( testCase.isSuccessfullyCompleted() == false );
53 
54         SECTION( "re-enter after failed section" ) {
55             ctx.startCycle();
56             ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
57             REQUIRE( testCase2.isOpen() );
58 
59             ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
60             REQUIRE( s1b.isOpen() == false );
61 
62             testCase2.close();
63             REQUIRE( ctx.completedCycle() );
64             REQUIRE( testCase.isComplete() );
65             REQUIRE( testCase.isSuccessfullyCompleted() );
66         }
67         SECTION( "re-enter after failed section and find next section" ) {
68             ctx.startCycle();
69             ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
70             REQUIRE( testCase2.isOpen() );
71 
72             ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
73             REQUIRE( s1b.isOpen() == false );
74 
75             ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
76             REQUIRE( s2.isOpen() );
77 
78             s2.close();
79             REQUIRE( ctx.completedCycle() );
80 
81             testCase2.close();
82             REQUIRE( testCase.isComplete() );
83             REQUIRE( testCase.isSuccessfullyCompleted() );
84         }
85     }
86 
87     SECTION( "successfully close one section, then find another" ) {
88         s1.close();
89 
90         ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
91         REQUIRE( s2.isOpen() == false );
92 
93         testCase.close();
94         REQUIRE( testCase.isComplete() == false );
95 
96         SECTION( "Re-enter - skips S1 and enters S2" ) {
97             ctx.startCycle();
98             ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
99             REQUIRE( testCase2.isOpen() );
100 
101             ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
102             REQUIRE( s1b.isOpen() == false );
103 
104             ITracker& s2b = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
105             REQUIRE( s2b.isOpen() );
106 
107             REQUIRE( ctx.completedCycle() == false );
108 
109             SECTION ("Successfully close S2") {
110                 s2b.close();
111                 REQUIRE( ctx.completedCycle() );
112 
113                 REQUIRE( s2b.isSuccessfullyCompleted() );
114                 REQUIRE( testCase2.isComplete() == false );
115 
116                 testCase2.close();
117                 REQUIRE( testCase2.isSuccessfullyCompleted() );
118             }
119             SECTION ("fail S2") {
120                 s2b.fail();
121                 REQUIRE( ctx.completedCycle() );
122 
123                 REQUIRE( s2b.isComplete() );
124                 REQUIRE( s2b.isSuccessfullyCompleted() == false );
125 
126                 testCase2.close();
127                 REQUIRE( testCase2.isSuccessfullyCompleted() == false );
128 
129                 // Need a final cycle
130                 ctx.startCycle();
131                 ITracker& testCase3 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
132                 REQUIRE( testCase3.isOpen() );
133 
134                 ITracker& s1c = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
135                 REQUIRE( s1c.isOpen() == false );
136 
137                 ITracker& s2c = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
138                 REQUIRE( s2c.isOpen() == false );
139 
140                 testCase3.close();
141                 REQUIRE( testCase3.isSuccessfullyCompleted() );
142             }
143         }
144     }
145 
146     SECTION( "open a nested section" ) {
147         ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
148         REQUIRE( s2.isOpen() );
149 
150         s2.close();
151         REQUIRE( s2.isComplete() );
152         REQUIRE( s1.isComplete() == false );
153 
154         s1.close();
155         REQUIRE( s1.isComplete() );
156         REQUIRE( testCase.isComplete() == false );
157 
158         testCase.close();
159         REQUIRE( testCase.isComplete() );
160     }
161 }
162 
163 static bool previouslyRun = false;
164 static bool previouslyRunNested = false;
165 
166 TEST_CASE( "#1394", "[.][approvals][tracker]" ) {
167     // -- Don't re-run after specified section is done
168     REQUIRE(previouslyRun == false);
169 
170     SECTION( "RunSection" ) {
171         previouslyRun = true;
172     }
173     SECTION( "SkipSection" ) {
174         // cause an error if this section is called because it shouldn't be
175         REQUIRE(1 == 0);
176     }
177 }
178 
179 TEST_CASE( "#1394 nested", "[.][approvals][tracker]" ) {
180     REQUIRE(previouslyRunNested == false);
181 
182     SECTION( "NestedRunSection" ) {
183         SECTION( "s1" ) {
184             previouslyRunNested = true;
185         }
186     }
187     SECTION( "NestedSkipSection" ) {
188         // cause an error if this section is called because it shouldn't be
189         REQUIRE(1 == 0);
190     }
191 }
192 
193 // Selecting a "not last" section inside a test case via -c "section" would
194 // previously only run the first subsection, instead of running all of them.
195 // This allows us to check that `"#1670 regression check" -c A` leads to
196 // 2 successful assertions.
197 TEST_CASE("#1670 regression check", "[.approvals][tracker]") {
198     SECTION("A") {
199         SECTION("1") SUCCEED();
200         SECTION("2") SUCCEED();
201     }
202     SECTION("B") {
203         SECTION("1") SUCCEED();
204         SECTION("2") SUCCEED();
205     }
206 }
207