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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#include <algorithm>
#include <Progmasoft/Catch3/TestCaseInfo.hpp>
#include <Progmasoft/Catch3/TestSpec.hpp>
#include <Progmasoft/Catch3/Interfaces/IConfig.hpp>
#include <Progmasoft/Catch3/Interfaces/IRegistryHub.hpp>
#include <Progmasoft/Catch3/Interfaces/ITestCaseRegistry.hpp>
#include <Progmasoft/Catch3/Internal/TestCaseRegistryImpl.hpp>
#include <cmath>
#include <iterator>
#include <map>
#include "Catalog.hpp"
#include "Hash.hpp"
namespace Progmasoft::Catch3::Runner
{
namespace
{
std::optional<double>
ParseSeconds(std::string_view text)
{
if (text.empty())
{
return std::nullopt;
}
double value = 0.0;
double scale = 0.0;
for (const char character : text)
{
if (character == '.' && scale <= 0.0)
{
scale = 1.0;
continue;
}
if (character < '0' || character > '9')
{
return std::nullopt;
}
if (scale > 0.0)
{
scale /= 10.0;
value += (character - '0') * scale;
}
else
{
value = value * 10.0 + (character - '0');
}
}
return value;
}
bool
HasPrefix(std::string_view text, std::string_view prefix)
{
return text.substr(0, prefix.size()) == prefix;
}
Status
ApplyRunnerTag(TestEntry &entry, const std::string &tag)
{
const auto where = [&] {
return "test '" + entry.name + "' at " + entry.file + ":"
+ std::to_string(entry.line);
};
if (tag == "!serial")
{
entry.serial = true;
}
else if (tag == "!isolated")
{
entry.isolated = true;
}
else if (tag == "!quarantine")
{
entry.quarantined = true;
}
else if (HasPrefix(tag, "!resource:"))
{
entry.resources.push_back(tag.substr(10));
}
else if (HasPrefix(tag, "!depends:"))
{
entry.dependsOn.push_back(tag.substr(9));
}
else if (HasPrefix(tag, "!timeout:"))
{
const auto seconds = ParseSeconds(tag.substr(9));
if (!seconds || *seconds > 1e7)
{
return Error{ "[" + tag + "] of " + where()
+ " needs a number of seconds, such as "
"[!timeout:30]" };
}
entry.timeout = std::chrono::milliseconds{
static_cast<long long>(std::llround(*seconds * 1000.0))
};
}
else if (HasPrefix(tag, "!retries:"))
{
const auto count = ParseSeconds(tag.substr(9));
if (!count || *count > 100 || std::floor(*count) < *count)
{
return Error{ "[" + tag + "] of " + where()
+ " needs a whole number up to 100, such as "
"[!retries:3]" };
}
entry.retries = static_cast<unsigned>(*count);
}
return Status::Ok();
}
} // namespace
Expected<Catalog>
Catalog::Load()
{
Catalog catalog;
const auto &handles
= Catch::getRegistryHub().getTestCaseRegistry().getAllTests();
catalog.handles_ = handles;
// Two tests with the same class, name and tags cannot be told apart
// by any filter, history record or report, so they are refused.
std::vector<const Catch::TestCaseInfo *> sorted;
sorted.reserve(handles.size());
for (const auto &handle : handles)
{
sorted.push_back(&handle.getTestCaseInfo());
}
std::stable_sort(sorted.begin(),
sorted.end(),
[](const auto *lhs, const auto *rhs) {
return *lhs < *rhs;
});
const auto duplicate
= std::adjacent_find(sorted.begin(),
sorted.end(),
[](const auto *lhs, const auto *rhs) {
return *lhs == *rhs;
});
if (duplicate != sorted.end())
{
const auto *first = *duplicate;
const auto *again = *std::next(duplicate);
return Error{ "test case \"" + again->name + "\", with tags \""
+ again->tagsAsString() + "\" already defined.\n"
+ "\tFirst seen at "
+ std::string{ first->lineInfo.file } + ":"
+ std::to_string(first->lineInfo.line) + "\n"
+ "\tRedefined at "
+ std::string{ again->lineInfo.file } + ":"
+ std::to_string(again->lineInfo.line) };
}
std::map<std::string, std::size_t> nameCounts;
for (const auto &handle : handles)
{
const auto &info = handle.getTestCaseInfo();
const std::string className{ info.className.data(),
info.className.size() };
++nameCounts[className.empty() ? info.name
: className + "::" + info.name];
}
Sha256 identity;
catalog.tests_.reserve(handles.size());
for (std::size_t index = 0; index < handles.size(); ++index)
{
const auto &info = handles[index].getTestCaseInfo();
TestEntry entry;
entry.index = index;
entry.name = info.name;
entry.className
= std::string{ info.className.data(), info.className.size() };
entry.file = std::string{ info.lineInfo.file };
entry.line = info.lineInfo.line;
entry.hidden = info.isHidden();
entry.mayFail = info.okToFail() && !info.expectedToFail();
entry.shouldFail = info.expectedToFail();
for (const auto &tag : info.tags)
{
entry.tags.emplace_back(tag.original.data(),
tag.original.size());
}
entry.benchmark
= std::find(entry.tags.begin(), entry.tags.end(), "!benchmark")
!= entry.tags.end();
for (const auto &tag : entry.tags)
{
if (!tag.empty() && tag.front() == '!')
{
if (auto status = ApplyRunnerTag(entry, tag); !status)
{
return status.GetError();
}
}
}
const std::string base = entry.className.empty()
? entry.name
: entry.className + "::" + entry.name;
entry.key = base;
if (nameCounts[base] > 1)
{
entry.key += " " + info.tagsAsString();
}
identity.Update(entry.key);
identity.Update("\n");
catalog.tests_.push_back(std::move(entry));
}
catalog.identity_ = identity.HexDigest();
return catalog;
}
Catalog
Catalog::FromEntries(std::vector<TestEntry> entries)
{
Catalog catalog;
Sha256 identity;
for (std::size_t index = 0; index < entries.size(); ++index)
{
TestEntry &entry = entries[index];
entry.index = index;
if (entry.key.empty())
{
entry.key = entry.name;
}
identity.Update(entry.key);
identity.Update("\n");
}
catalog.tests_ = std::move(entries);
catalog.identity_ = identity.HexDigest();
return catalog;
}
const TestEntry *
Catalog::Find(const std::string &keyOrName) const
{
for (const auto &entry : tests_)
{
if (entry.key == keyOrName)
{
return &entry;
}
}
for (const auto &entry : tests_)
{
if (entry.name == keyOrName)
{
return &entry;
}
}
return nullptr;
}
std::vector<std::size_t>
Catalog::Match(const Catch::IConfig &config,
std::vector<std::string> &unmatched) const
{
std::vector<std::size_t> selected;
const auto &spec = config.testSpec();
if (!spec.hasFilters())
{
for (const auto &entry : tests_)
{
if (!entry.hidden
&& Catch::isThrowSafe(handles_[entry.index], config))
{
selected.push_back(entry.index);
}
}
return selected;
}
std::vector<bool> chosen(tests_.size(), false);
for (const auto &match : spec.matchesByFilter(handles_, config))
{
if (match.tests.empty())
{
unmatched.push_back(match.name);
}
for (const auto *handle : match.tests)
{
chosen[static_cast<std::size_t>(handle - handles_.data())]
= true;
}
}
for (std::size_t index = 0; index < chosen.size(); ++index)
{
if (chosen[index])
{
selected.push_back(index);
}
}
return selected;
}
} // namespace Progmasoft::Catch3::Runner
|