-
Notifications
You must be signed in to change notification settings - Fork 16
/
TestRunner.cpp
304 lines (259 loc) · 8.86 KB
/
TestRunner.cpp
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
294
295
296
297
298
299
300
301
302
303
304
/*
MIT License
Copyright (c) 2018 Brian T. Park
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if EPOXY_DUINO
#include <stdio.h>
#endif
#include <Arduino.h> // 'Serial' or SERIAL_PORT_MONITOR
#include <string.h>
#include <stdint.h>
#include "FCString.h"
#include "Compare.h"
#include "Printer.h"
#include "Verbosity.h"
#include "Test.h"
#include "TestRunner.h"
#include "string_util.h"
namespace aunit {
// Use a function static singleton to avoid the static initialization ordering
// problem. It's probably not an issue right now, since TestRunner is expected
// to be called only after all static initialization, but future refactoring
// could change that so this is defensive programming.
TestRunner* TestRunner::getRunner() {
static TestRunner singletonRunner;
return &singletonRunner;
}
void TestRunner::setPrinter(Print* printer) {
Printer::setPrinter(printer);
}
void TestRunner::setLifeCycleMatchingPattern(const char* pattern,
uint8_t lifeCycle) {
// Do an implicit excludeAll() if the first filter is an include().
if (!hasBeenFiltered && lifeCycle == Test::kLifeCycleNew) {
excludeAll();
}
hasBeenFiltered = true;
size_t length = strlen(pattern);
if (length > 0 && pattern[length - 1] == '*') {
// prefix match
length--;
} else {
// exact match
length++;
}
for (Test** p = Test::getRoot(); *p != nullptr; p = (*p)->getNext()) {
if ((*p)->getName().compareToN(pattern, length) == 0) {
(*p)->setLifeCycle(lifeCycle);
}
}
}
void TestRunner::setLifeCycleMatchingPattern(const char* testClass,
const char* pattern, uint8_t lifeCycle) {
// Do an implicit excludeAll() if the first filter is an include().
if (!hasBeenFiltered && lifeCycle == Test::kLifeCycleNew) {
excludeAll();
}
hasBeenFiltered = true;
// The effective pattern is the join of testClass and pattern with a '_'
// delimiter. This must match the algorithm used by testF() and testingF().
// We use string_join() instead of String so that AUnit can avoid a direct
// dependency on the String class. AUnit thus avoids allocating any memory on
// the heap.
char fullPattern[kMaxPatternLength];
bool status = internal::string_join(fullPattern, kMaxPatternLength, '_',
testClass, pattern);
if (!status) return;
setLifeCycleMatchingPattern(fullPattern, lifeCycle);
}
void TestRunner::setLifeCycleMatchingSubstring(
const char* substring, uint8_t lifeCycle) {
// Do an implicit excludeAll() if the first filter is an include().
if (!hasBeenFiltered && lifeCycle == Test::kLifeCycleNew) {
excludeAll();
}
hasBeenFiltered = true;
for (Test** p = Test::getRoot(); *p != nullptr; p = (*p)->getNext()) {
if ((*p)->getName().hasSubstring(substring)) {
(*p)->setLifeCycle(lifeCycle);
}
}
}
void TestRunner::excludeAll() {
for (Test** p = Test::getRoot(); *p != nullptr; p = (*p)->getNext()) {
(*p)->setLifeCycle(Test::kLifeCycleExcluded);
}
}
// Count the number of tests in TestRunner instead of Test::insert() to avoid
// another C++ static initialization ordering problem.
uint16_t TestRunner::countTests() {
uint16_t count = 0;
for (Test** p = Test::getRoot(); *p != nullptr; p = (*p)->getNext()) {
count++;
}
return count;
}
namespace {
/**
* Print the timeMillis as floating point seconds, without using floating point
* math. This is the equivalent of 'printer->print((float) timeMillis / 1000)',
* but saves 1400-1600 bytes of flash memory and 12 bytes of static memory.
*/
void printSeconds(Print* printer, unsigned long timeMillis) {
int s = timeMillis / 1000;
int ms = timeMillis % 1000;
printer->print(s);
printer->print('.');
if (ms < 100) printer->print('0');
if (ms < 10) printer->print('0');
printer->print(ms);
}
}
void TestRunner::printStartRunner() const {
if (!isVerbosity(Verbosity::kTestRunSummary)) return;
Print* printer = Printer::getPrinter();
printer->print(F("TestRunner started on "));
printer->print(mCount);
printer->println(F(" test(s)."));
}
void TestRunner::resolveRun() const {
if (!isVerbosity(Verbosity::kTestRunSummary)) return;
Print* printer = Printer::getPrinter();
unsigned long elapsedTime = mEndTime - mStartTime;
printer->print(F("TestRunner duration: "));
printSeconds(printer, elapsedTime);
printer->println(" seconds.");
printer->print(F("TestRunner summary: "));
printer->print(mPassedCount);
printer->print(F(" passed, "));
printer->print(mFailedCount);
printer->print(F(" failed, "));
printer->print(mSkippedCount);
printer->print(F(" skipped, "));
printer->print(mExpiredCount);
printer->print(F(" timed out, out of "));
printer->print(mCount);
printer->println(F(" test(s)."));
}
void TestRunner::setRunnerTimeout(TimeoutType timeout) {
mTimeout = timeout;
}
//----------------------------------------------------------------------------
// Command line argument processing on EpoxyDuino
//----------------------------------------------------------------------------
#if EPOXY_DUINO
static void shift(int& argc, const char* const*& argv) {
argc--;
argv++;
}
static bool argEquals(const char* s, const char* t) {
return strcmp(s, t) == 0;
}
static void usageAndExit(int status) {
fprintf(
stderr,
"Usage: %s [--help|-h]\n"
" [--include pattern,...] [--exclude pattern,...]\n"
" [--includesub substring,...] [--excludesub substring,...]\n"
" [--] [substring ...]\n",
epoxy_argv[0]
);
exit(status);
}
void TestRunner::processCommaList(
const char* const commaList, FilterType filterType) {
const int kArgumentSize = 64;
const char* list = commaList;
// Loop for each comma-separate list of words. Call the filtering command
// defined by the filterType.
char argument[kArgumentSize];
while (*list != '\0') {
const char* comma = strchr(list, ',');
int length = (comma) ? (comma - list) : strlen(list);
// Copy each word of the comma-separated list.
if (length >= kArgumentSize - 1) {
length = kArgumentSize - 1;
}
memcpy(argument, list, length);
argument[length] = '\0';
switch (filterType) {
case FilterType::kInclude:
include(argument);
break;
case FilterType::kExclude:
exclude(argument);
break;
case FilterType::kIncludeSub:
includesub(argument);
break;
case FilterType::kExcludeSub:
excludesub(argument);
break;
}
list += (comma) ? length + 1 : length;
}
}
/**
* Parse command line flags.
* Returns the index of the first argument after the flags.
*/
int TestRunner::parseFlags(int argc, const char* const* argv) {
int argc_original = argc;
shift(argc, argv);
while (argc > 0) {
if (argEquals(argv[0], "--include")) {
shift(argc, argv);
if (argc == 0) usageAndExit(1);
processCommaList(argv[0], FilterType::kInclude);
} else if (argEquals(argv[0], "--exclude")) {
shift(argc, argv);
if (argc == 0) usageAndExit(1);
processCommaList(argv[0], FilterType::kExclude);
} else if (argEquals(argv[0], "--includesub")) {
shift(argc, argv);
if (argc == 0) usageAndExit(1);
processCommaList(argv[0], FilterType::kIncludeSub);
} else if (argEquals(argv[0], "--excludesub")) {
shift(argc, argv);
if (argc == 0) usageAndExit(1);
processCommaList(argv[0], FilterType::kExcludeSub);
} else if (argEquals(argv[0], "--")) {
shift(argc, argv);
break;
} else if (argEquals(argv[0], "--help") || argEquals(argv[0], "-h")) {
usageAndExit(0);
break;
} else if (argv[0][0] == '-') {
fprintf(stderr, "Unknonwn flag '%s'\n", argv[0]);
usageAndExit(1);
} else {
break;
}
shift(argc, argv);
}
return argc_original - argc;
}
void TestRunner::processCommandLine() {
int args = parseFlags(epoxy_argc, epoxy_argv);
// Process any remaining *space*-separated arguments using includesub().
for (int i = args; i < epoxy_argc; i++) {
includesub(epoxy_argv[i]);
}
}
#endif
}