1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#pragma once
#include <Progmasoft/Catch3/Internal/LifetimeBound.hpp>
#include <Progmasoft/Catch3/Internal/SourceLineInfo.hpp>
#include <Progmasoft/Catch3/Internal/UniquePtr.hpp>
#include <Progmasoft/Catch3/Internal/StringRef.hpp>
#include <Progmasoft/Catch3/Internal/PathFilter.hpp>
#include <string>
#include <vector>
namespace Catch {
struct PathFilter;
namespace TestCaseTracking {
struct NameAndLocation {
std::string name;
SourceLineInfo location;
NameAndLocation( std::string&& _name, SourceLineInfo const& _location );
friend bool operator==(NameAndLocation const& lhs, NameAndLocation const& rhs) {
// This is a very cheap check that should have a very high hit rate.
// If we get to SourceLineInfo::operator==, we will redo it, but the
// cost of repeating is trivial at that point (we will be paying
// multiple strcmp/memcmps at that point).
if ( lhs.location.line != rhs.location.line ) { return false; }
return lhs.name == rhs.name && lhs.location == rhs.location;
}
friend bool operator!=(NameAndLocation const& lhs,
NameAndLocation const& rhs) {
return !( lhs == rhs );
}
};
/**
* This is a variant of `NameAndLocation` that does not own the name string
*
* This avoids extra allocations when trying to locate a tracker by its
* name and location, as long as we make sure that trackers only keep
* around the owning variant.
*/
struct NameAndLocationRef {
StringRef name;
SourceLineInfo location;
constexpr NameAndLocationRef( StringRef name_ CATCH_ATTR_LIFETIMEBOUND,
SourceLineInfo location_ ):
name( name_ ), location( location_ ) {}
friend bool operator==( NameAndLocation const& lhs,
NameAndLocationRef const& rhs ) {
// This is a very cheap check that should have a very high hit rate.
// If we get to SourceLineInfo::operator==, we will redo it, but the
// cost of repeating is trivial at that point (we will be paying
// multiple strcmp/memcmps at that point).
if ( lhs.location.line != rhs.location.line ) { return false; }
return StringRef( lhs.name ) == rhs.name &&
lhs.location == rhs.location;
}
friend bool operator==( NameAndLocationRef const& lhs,
NameAndLocation const& rhs ) {
return rhs == lhs;
}
};
class ITracker;
using ITrackerPtr = Catch::Detail::unique_ptr<ITracker>;
class ITracker {
NameAndLocation m_nameAndLocation;
using Children = std::vector<ITrackerPtr>;
virtual bool isFilteredImpl() const = 0;
protected:
enum CycleState {
NotStarted,
Executing,
ExecutingChildren,
NeedsAnotherRun,
CompletedSuccessfully,
Failed
};
ITracker* m_parent = nullptr;
Children m_children;
CycleState m_runState = NotStarted;
// Members for path filtering
std::vector<PathFilter> const* m_filterRef = nullptr;
// Note: There are 2 dummy section trackers (root, test-case) before
// the first "real" section tracker can be encountered. We start
// the default tracker at -2, so that the first "real" section
// tracker overflows to index 0.
// Nesting depth of this tracker, used to decide which new-style filter applies.
size_t m_allTrackerDepth = static_cast<size_t>( -2 );
// Nesting depth of sections (inc. this tracker), used for old-style filters.
// Must be updated by the section tracker on its own.
size_t m_sectionOnlyDepth = static_cast<size_t>( -2 );
// Transitory: Remove once we remove backwards compatibility with old-style (v3.x) filters
bool m_newStyleFilters = false;
public:
ITracker( NameAndLocation&& nameAndLoc, ITracker* parent );
// static queries
NameAndLocation const& nameAndLocation() const {
return m_nameAndLocation;
}
ITracker* parent() const {
return m_parent;
}
virtual ~ITracker(); // = default
// dynamic queries
//! Returns true if tracker run to completion (successfully or not)
virtual bool isComplete() const = 0;
//! Returns true if tracker run to completion successfully
bool isSuccessfullyCompleted() const {
return m_runState == CompletedSuccessfully;
}
//! Returns true if tracker has started but hasn't been completed
bool isOpen() const;
//! Returns true iff tracker has started
bool hasStarted() const;
void setFilters( std::vector<PathFilter> const* filters, bool newStyleFilters ) {
m_filterRef = filters;
m_newStyleFilters = newStyleFilters;
}
// actions
virtual void close() = 0; // Successfully complete
virtual void fail() = 0;
void markAsNeedingAnotherRun();
//! Register a nested ITracker
void addChild( ITrackerPtr&& child );
/**
* Returns ptr to specific child if register with this tracker.
*
* Returns nullptr if not found.
*/
ITracker* findChild( NameAndLocationRef const& nameAndLocation );
//! Have any children been added?
bool hasChildren() const {
return !m_children.empty();
}
//! Marks tracker as executing a child, doing se recursively up the tree
void openChild();
/**
* Returns true if the instance is a section tracker
*
* Subclasses should override to true if they are, replaces RTTI
* for internal debug checks.
*/
virtual bool isSectionTracker() const;
/**
* Returns true if the instance is a generator tracker
*
* Subclasses should override to true if they are, replaces RTTI
* for internal debug checks.
*/
virtual bool isGeneratorTracker() const;
/**
* Returns true if the concrete tracker instance has a filter that applies to it.
*/
bool isFiltered() const {
// Fast path: are there even filters for tracker in this position?
const size_t filter_depth =
m_newStyleFilters ? m_allTrackerDepth : m_sectionOnlyDepth;
if ( m_filterRef->size() <= filter_depth ) { return false; }
// Slow path: If there are filters, ask the concrete tracker.
// This handles things like match-all filters for that tracker.
return isFilteredImpl();
}
};
class TrackerContext {
enum RunState {
NotStarted,
Executing,
CompletedCycle
};
ITrackerPtr m_rootTracker;
ITracker* m_currentTracker = nullptr;
RunState m_runState = NotStarted;
public:
ITracker& startRun();
void startCycle() {
m_currentTracker = m_rootTracker.get();
m_runState = Executing;
}
void completeCycle();
bool completedCycle() const;
ITracker& currentTracker() { return *m_currentTracker; }
void setCurrentTracker( ITracker* tracker );
};
class TrackerBase : public ITracker {
protected:
TrackerContext& m_ctx;
public:
TrackerBase( NameAndLocation&& nameAndLocation, TrackerContext& ctx, ITracker* parent );
bool isComplete() const override;
void open();
void close() override;
void fail() override;
private:
void moveToParent();
void moveToThis();
};
class SectionTracker final : public TrackerBase {
// Note that lifetime-wise we piggy back off the name stored in the `ITracker` parent`.
// Currently it allocates owns the name, so this is safe. If it is later refactored
// to not own the name, the name still has to outlive the `ITracker` parent, so
// this should still be safe.
StringRef m_trimmed_name;
bool isFilteredImpl() const override;
public:
SectionTracker( NameAndLocation&& nameAndLocation, TrackerContext& ctx, ITracker* parent );
bool isSectionTracker() const override;
bool isComplete() const override;
static SectionTracker& acquire( TrackerContext& ctx, NameAndLocationRef const& nameAndLocation );
void tryOpen();
//! Returns whitespace-trimmed name of the tracked section
StringRef trimmedName() const;
};
} // namespace TestCaseTracking
using TestCaseTracking::ITracker;
using TestCaseTracking::TrackerContext;
using TestCaseTracking::SectionTracker;
} // namespace Catch
|