Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_test_case_tracker.cpp
Go to the documentation of this file.
1/*
2 * Created by Martin on 19/07/2017
3 *
4 * Distributed under the Boost Software License, Version 1.0. (See accompanying
5 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 */
7
9
10#include "catch_enforce.h"
11
12#include <algorithm>
13#include <cassert>
14#include <stdexcept>
15#include <memory>
16#include <sstream>
17
18#if defined(__clang__)
19# pragma clang diagnostic push
20# pragma clang diagnostic ignored "-Wexit-time-destructors"
21#endif
22
23namespace Catch {
24namespace TestCaseTracking {
25
26 NameAndLocation::NameAndLocation( std::string const& _name, SourceLineInfo const& _location )
27 : name( _name ),
28 location( _location )
29 {}
30
31
32 ITracker::~ITracker() = default;
33
34
36 m_rootTracker = std::make_shared<SectionTracker>( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, nullptr );
37 m_currentTracker = nullptr;
38 m_runState = Executing;
39 return *m_rootTracker;
40 }
41
43 m_rootTracker.reset();
44 m_currentTracker = nullptr;
45 m_runState = NotStarted;
46 }
47
49 m_currentTracker = m_rootTracker.get();
50 m_runState = Executing;
51 }
53 m_runState = CompletedCycle;
54 }
55
57 return m_runState == CompletedCycle;
58 }
60 return *m_currentTracker;
61 }
63 m_currentTracker = tracker;
64 }
65
66
67 TrackerBase::TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent )
68 : m_nameAndLocation( nameAndLocation ),
69 m_ctx( ctx ),
70 m_parent( parent )
71 {}
72
82 bool TrackerBase::isOpen() const {
83 return m_runState != NotStarted && !isComplete();
84 }
86 return !m_children.empty();
87 }
88
89
90 void TrackerBase::addChild( ITrackerPtr const& child ) {
91 m_children.push_back( child );
92 }
93
95 auto it = std::find_if( m_children.begin(), m_children.end(),
96 [&nameAndLocation]( ITrackerPtr const& tracker ){
97 return
98 tracker->nameAndLocation().location == nameAndLocation.location &&
99 tracker->nameAndLocation().name == nameAndLocation.name;
100 } );
101 return( it != m_children.end() )
102 ? *it
103 : nullptr;
104 }
106 assert( m_parent ); // Should always be non-null except for root
107 return *m_parent;
108 }
109
113 if( m_parent )
115 }
116 }
117
118 bool TrackerBase::isSectionTracker() const { return false; }
119 bool TrackerBase::isGeneratorTracker() const { return false; }
120
123 moveToThis();
124 if( m_parent )
126 }
127
129
130 // Close any still open children (e.g. generators)
131 while( &m_ctx.currentTracker() != this )
133
134 switch( m_runState ) {
135 case NeedsAnotherRun:
136 break;
137
138 case Executing:
140 break;
142 if( m_children.empty() || m_children.back()->isComplete() )
144 break;
145
146 case NotStarted:
148 case Failed:
149 CATCH_INTERNAL_ERROR( "Illogical state: " << m_runState );
150
151 default:
152 CATCH_INTERNAL_ERROR( "Unknown state: " << m_runState );
153 }
154 moveToParent();
156 }
159 if( m_parent )
161 moveToParent();
163 }
167
168 void TrackerBase::moveToParent() {
169 assert( m_parent );
171 }
172 void TrackerBase::moveToThis() {
173 m_ctx.setCurrentTracker( this );
174 }
175
177 : TrackerBase( nameAndLocation, ctx, parent )
178 {
179 if( parent ) {
180 while( !parent->isSectionTracker() )
181 parent = &parent->parent();
182
183 SectionTracker& parentSection = static_cast<SectionTracker&>( *parent );
184 addNextFilters( parentSection.m_filters );
185 }
186 }
187
189 bool complete = true;
190
191 if ((m_filters.empty() || m_filters[0] == "") ||
192 std::find(m_filters.begin(), m_filters.end(),
193 m_nameAndLocation.name) != m_filters.end())
194 complete = TrackerBase::isComplete();
195 return complete;
196
197 }
198
199 bool SectionTracker::isSectionTracker() const { return true; }
200
202 std::shared_ptr<SectionTracker> section;
203
204 ITracker& currentTracker = ctx.currentTracker();
205 if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) {
206 assert( childTracker );
207 assert( childTracker->isSectionTracker() );
208 section = std::static_pointer_cast<SectionTracker>( childTracker );
209 }
210 else {
211 section = std::make_shared<SectionTracker>( nameAndLocation, ctx, &currentTracker );
212 currentTracker.addChild( section );
213 }
214 if( !ctx.completedCycle() )
215 section->tryOpen();
216 return *section;
217 }
218
220 if( !isComplete() && (m_filters.empty() || m_filters[0].empty() || m_filters[0] == m_nameAndLocation.name ) )
221 open();
222 }
223
224 void SectionTracker::addInitialFilters( std::vector<std::string> const& filters ) {
225 if( !filters.empty() ) {
226 m_filters.push_back(""); // Root - should never be consulted
227 m_filters.push_back(""); // Test Case - not a section filter
228 m_filters.insert( m_filters.end(), filters.begin(), filters.end() );
229 }
230 }
231 void SectionTracker::addNextFilters( std::vector<std::string> const& filters ) {
232 if( filters.size() > 1 )
233 m_filters.insert( m_filters.end(), ++filters.begin(), filters.end() );
234 }
235
236} // namespace TestCaseTracking
237
241
242} // namespace Catch
243
244#if defined(__clang__)
245# pragma clang diagnostic pop
246#endif
#define CATCH_INTERNAL_LINEINFO
#define CATCH_INTERNAL_ERROR(msg)
std::string name
void addNextFilters(std::vector< std::string > const &filters)
void addInitialFilters(std::vector< std::string > const &filters)
static SectionTracker & acquire(TrackerContext &ctx, NameAndLocation const &nameAndLocation)
SectionTracker(NameAndLocation const &nameAndLocation, TrackerContext &ctx, ITracker *parent)
ITrackerPtr findChild(NameAndLocation const &nameAndLocation) override
void addChild(ITrackerPtr const &child) override
NameAndLocation const & nameAndLocation() const override
TrackerBase(NameAndLocation const &nameAndLocation, TrackerContext &ctx, ITracker *parent)
std::shared_ptr< ITracker > ITrackerPtr
virtual void markAsNeedingAnotherRun()=0
virtual ITrackerPtr findChild(NameAndLocation const &nameAndLocation)=0
virtual void addChild(ITrackerPtr const &child)=0
NameAndLocation(std::string const &_name, SourceLineInfo const &_location)