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
12 namespace Catch
13 {
14 class LocalContext {
15
16 public:
operator ()() const17 TrackerContext& operator()() const {
18 return TrackerContext::instance();
19 }
20 };
21
22 } // namespace Catch
23
24 // -------------------
25
26 #include "catch.hpp"
27
28 using namespace Catch;
29
30 namespace {
makeNAL(std::string const & name)31 Catch::TestCaseTracking::NameAndLocation makeNAL( std::string const& name ) {
32 return Catch::TestCaseTracking::NameAndLocation( name, Catch::SourceLineInfo("",0) );
33 }
34 }
35
36 TEST_CASE( "Tracker" ) {
37
38 TrackerContext ctx;
39 ctx.startRun();
40 ctx.startCycle();
41
42
43 ITracker& testCase = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
44 REQUIRE( testCase.isOpen() );
45
46 ITracker& s1 = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
47 REQUIRE( s1.isOpen() );
48
49 SECTION( "successfully close one section" ) {
50 s1.close();
51 REQUIRE( s1.isSuccessfullyCompleted() );
52 REQUIRE( testCase.isComplete() == false );
53
54 testCase.close();
55 REQUIRE( ctx.completedCycle() );
56 REQUIRE( testCase.isSuccessfullyCompleted() );
57 }
58
59 SECTION( "fail one section" ) {
60 s1.fail();
61 REQUIRE( s1.isComplete() );
62 REQUIRE( s1.isSuccessfullyCompleted() == false );
63 REQUIRE( testCase.isComplete() == false );
64
65 testCase.close();
66 REQUIRE( ctx.completedCycle() );
67 REQUIRE( testCase.isSuccessfullyCompleted() == false );
68
69 SECTION( "re-enter after failed section" ) {
70 ctx.startCycle();
71 ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
72 REQUIRE( testCase2.isOpen() );
73
74 ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
75 REQUIRE( s1b.isOpen() == false );
76
77 testCase2.close();
78 REQUIRE( ctx.completedCycle() );
79 REQUIRE( testCase.isComplete() );
80 REQUIRE( testCase.isSuccessfullyCompleted() );
81 }
82 SECTION( "re-enter after failed section and find next section" ) {
83 ctx.startCycle();
84 ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
85 REQUIRE( testCase2.isOpen() );
86
87 ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
88 REQUIRE( s1b.isOpen() == false );
89
90 ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
91 REQUIRE( s2.isOpen() );
92
93 s2.close();
94 REQUIRE( ctx.completedCycle() );
95
96 testCase2.close();
97 REQUIRE( testCase.isComplete() );
98 REQUIRE( testCase.isSuccessfullyCompleted() );
99 }
100 }
101
102 SECTION( "successfully close one section, then find another" ) {
103 s1.close();
104
105 ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
106 REQUIRE( s2.isOpen() == false );
107
108 testCase.close();
109 REQUIRE( testCase.isComplete() == false );
110
111 SECTION( "Re-enter - skips S1 and enters S2" ) {
112 ctx.startCycle();
113 ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
114 REQUIRE( testCase2.isOpen() );
115
116 ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
117 REQUIRE( s1b.isOpen() == false );
118
119 ITracker& s2b = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
120 REQUIRE( s2b.isOpen() );
121
122 REQUIRE( ctx.completedCycle() == false );
123
124 SECTION ("Successfully close S2") {
125 s2b.close();
126 REQUIRE( ctx.completedCycle() );
127
128 REQUIRE( s2b.isSuccessfullyCompleted() );
129 REQUIRE( testCase2.isComplete() == false );
130
131 testCase2.close();
132 REQUIRE( testCase2.isSuccessfullyCompleted() );
133 }
134 SECTION ("fail S2") {
135 s2b.fail();
136 REQUIRE( ctx.completedCycle() );
137
138 REQUIRE( s2b.isComplete() );
139 REQUIRE( s2b.isSuccessfullyCompleted() == false );
140
141 testCase2.close();
142 REQUIRE( testCase2.isSuccessfullyCompleted() == false );
143
144 // Need a final cycle
145 ctx.startCycle();
146 ITracker& testCase3 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
147 REQUIRE( testCase3.isOpen() );
148
149 ITracker& s1c = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
150 REQUIRE( s1c.isOpen() == false );
151
152 ITracker& s2c = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
153 REQUIRE( s2c.isOpen() == false );
154
155 testCase3.close();
156 REQUIRE( testCase3.isSuccessfullyCompleted() );
157 }
158 }
159 }
160
161 SECTION( "open a nested section" ) {
162 ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
163 REQUIRE( s2.isOpen() );
164
165 s2.close();
166 REQUIRE( s2.isComplete() );
167 REQUIRE( s1.isComplete() == false );
168
169 s1.close();
170 REQUIRE( s1.isComplete() );
171 REQUIRE( testCase.isComplete() == false );
172
173 testCase.close();
174 REQUIRE( testCase.isComplete() );
175 }
176 }
177
178 static bool previouslyRun = false;
179 static bool previouslyRunNested = false;
180
181 TEST_CASE( "#1394", "[.][approvals][tracker]" ) {
182 // -- Don't re-run after specified section is done
183 REQUIRE(previouslyRun == false);
184
185 SECTION( "RunSection" ) {
186 previouslyRun = true;
187 }
188 SECTION( "SkipSection" ) {
189 // cause an error if this section is called because it shouldn't be
190 REQUIRE(1 == 0);
191 }
192 }
193
194 TEST_CASE( "#1394 nested", "[.][approvals][tracker]" ) {
195 REQUIRE(previouslyRunNested == false);
196
197 SECTION( "NestedRunSection" ) {
198 SECTION( "s1" ) {
199 previouslyRunNested = true;
200 }
201 }
202 SECTION( "NestedSkipSection" ) {
203 // cause an error if this section is called because it shouldn't be
204 REQUIRE(1 == 0);
205 }
206 }
207