-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
Copy pathunity_runner.c
335 lines (305 loc) · 9.3 KB
/
unity_runner.c
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include "unity.h"
#include "esp_system.h"
/* similar to UNITY_PRINT_EOL */
#define UNITY_PRINT_TAB() UNITY_OUTPUT_CHAR('\t')
// Pointers to the head and tail of linked list of test description structs:
static test_desc_t *s_unity_tests_first = NULL;
static test_desc_t *s_unity_tests_last = NULL;
void unity_testcase_register(test_desc_t *desc)
{
if (!s_unity_tests_first) {
s_unity_tests_first = desc;
s_unity_tests_last = desc;
} else {
test_desc_t *temp = s_unity_tests_first;
s_unity_tests_first = desc;
s_unity_tests_first->next = temp;
}
}
/* print the multiple function case name and its sub-menu
* e.g:
* (1) spi master/slave case
* (1)master case
* (2)slave case
* */
static void print_multiple_function_test_menu(const test_desc_t *test_ms)
{
UnityPrint(test_ms->name);
UNITY_PRINT_EOL();
for (int i = 0; i < test_ms->test_fn_count; i++) {
UNITY_PRINT_TAB();
UnityPrint("(");
UnityPrintNumberUnsigned(i + 1);
UnityPrint(")");
UNITY_PRINT_TAB();
UnityPrint("\"");
UnityPrint(test_ms->test_fn_name[i]);
UnityPrint("\"");
UNITY_PRINT_EOL();
}
}
/*
* This function looks like UnityDefaultTestRun function only without UNITY_CLR_DETAILS.
* void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
* was moved from `components/unity/unity/src/unity.c` to here.
*/
static void unity_default_test_run(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
{
Unity.CurrentTestName = FuncName;
Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
Unity.NumberOfTests++;
if (TEST_PROTECT())
{
setUp();
Func();
}
if (TEST_PROTECT())
{
tearDown();
}
UnityConcludeTest();
}
static int multiple_function_option(const test_desc_t *test_ms)
{
int selection;
char cmdline[256] = {0};
print_multiple_function_test_menu(test_ms);
while (strlen(cmdline) == 0) {
unity_gets(cmdline, sizeof(cmdline));
if (strlen(cmdline) == 0) {
/* if input was newline, print a new menu */
print_multiple_function_test_menu(test_ms);
}
}
selection = atoi((const char *) cmdline) - 1;
if (selection >= 0 && selection < test_ms->test_fn_count) {
UnityPrint("Running stage ");
UnityPrintNumber(selection + 1);
UnityPrint("...");
UNITY_PRINT_EOL();
// Unit test runner expects to see test name before the test starts
UNITY_OUTPUT_FLUSH();
unity_default_test_run(test_ms->fn[selection], test_ms->name, test_ms->line);
} else {
UnityPrint("Invalid selection, your should input number 1-");
UnityPrintNumber(test_ms->test_fn_count);
UNITY_PRINT_EOL();
}
return selection;
}
static void unity_run_single_test(const test_desc_t *test)
{
UnityPrint("Running ");
UnityPrint(test->name);
UnityPrint("...");
UNITY_PRINT_EOL();
// Unit test runner expects to see test name before the test starts
UNITY_OUTPUT_FLUSH();
Unity.TestFile = test->file;
Unity.CurrentDetail1 = test->desc;
bool reset_after_test = strstr(Unity.CurrentDetail1, "[leaks") != NULL;
bool multi_device = strstr(Unity.CurrentDetail1, "[multi_device]") != NULL;
if (test->test_fn_count == 1) {
unity_default_test_run(test->fn[0], test->name, test->line);
} else {
int selection = multiple_function_option(test);
if (reset_after_test && multi_device == false) {
if (selection != (test->test_fn_count - 1)) {
// to do a reset for all stages except the last stage.
esp_restart();
}
}
}
if (reset_after_test) {
// print a result of test before to do reset for the last stage.
UNITY_END();
UnityPrint("Enter next test, or 'enter' to see menu");
UNITY_PRINT_EOL();
UNITY_OUTPUT_FLUSH();
esp_restart();
}
}
void unity_run_test_by_index(int index)
{
const test_desc_t *test;
for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index) {
;
}
if (test != NULL) {
unity_run_single_test(test);
}
}
static void unity_run_single_test_by_index_parse(const char *filter, int index_max)
{
int test_index = strtol(filter, NULL, 10);
if (test_index >= 1 && test_index <= index_max) {
UNITY_EXEC_TIME_START();
unity_run_test_by_index(test_index - 1);
UNITY_EXEC_TIME_STOP();
UnityPrint("Test ran in ");
UnityPrintNumberUnsigned(UNITY_EXEC_TIME_MS());
UnityPrint("ms");
UNITY_PRINT_EOL();
UNITY_OUTPUT_FLUSH();
}
}
void unity_run_test_by_name(const char *name)
{
for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
if (strcmp(test->name, name) == 0) {
unity_run_single_test(test);
}
}
}
void unity_run_all_tests(void)
{
for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
unity_run_single_test(test);
}
}
void unity_run_tests_by_tag(const char *tag, bool invert)
{
UnityPrint("Running tests ");
if (invert) {
UnityPrint("NOT ");
}
UnityPrint("matching '");
UnityPrint(tag);
UnityPrint("'...");
UNITY_PRINT_EOL();
for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
if ((strstr(test->desc, tag) != NULL) == !invert) {
unity_run_single_test(test);
}
}
}
static void trim_trailing_space(char *str)
{
char *end = str + strlen(str) - 1;
while (end >= str && isspace((int) *end)) {
*end = 0;
--end;
}
}
static int print_test_menu(void)
{
int test_counter = 0;
UNITY_PRINT_EOL();
UNITY_PRINT_EOL();
UnityPrint("Here's the test menu, pick your combo:");
UNITY_PRINT_EOL();
for (const test_desc_t *test = s_unity_tests_first;
test != NULL;
test = test->next, ++test_counter) {
UnityPrint("(");
UnityPrintNumber(test_counter + 1);
UnityPrint(")");
UNITY_PRINT_TAB();
UnityPrint("\"");
UnityPrint(test->name);
UnityPrint("\" ");
UnityPrint(test->desc);
UNITY_PRINT_EOL();
if (test->test_fn_count > 1) {
for (int i = 0; i < test->test_fn_count; i++) {
UNITY_PRINT_TAB();
UnityPrint("(");
UnityPrintNumber(i + 1);
UnityPrint(")");
UNITY_PRINT_TAB();
UnityPrint("\"");
UnityPrint(test->test_fn_name[i]);
UnityPrint("\"");
UNITY_PRINT_EOL();
}
}
}
UNITY_PRINT_EOL();
UnityPrint("Enter test for running."); /* unit_test.py needs it for finding the end of test menu */
UNITY_PRINT_EOL();
UNITY_OUTPUT_FLUSH();
return test_counter;
}
int unity_get_test_count(void)
{
int test_counter = 0;
for (const test_desc_t *test = s_unity_tests_first;
test != NULL;
test = test->next) {
++test_counter;
}
return test_counter;
}
void unity_run_menu(void)
{
UNITY_PRINT_EOL();
UNITY_PRINT_EOL();
UnityPrint("Press ENTER to see the list of tests.");
UNITY_PRINT_EOL();
int test_count = unity_get_test_count();
while (true) {
char cmdline[256] = { 0 };
while (strlen(cmdline) == 0) {
unity_gets(cmdline, sizeof(cmdline));
trim_trailing_space(cmdline);
if (strlen(cmdline) == 0) {
/* if input was newline, print a new menu */
print_test_menu();
}
}
/*use '-' to show test history. Need to do it before UNITY_BEGIN cleanup history */
if (cmdline[0] == '-') {
UNITY_END();
continue;
}
UNITY_BEGIN();
size_t idx = 0;
bool invert = false;
if (cmdline[idx] == '!') {
invert = true;
++idx;
}
if (cmdline[idx] == '*') {
unity_run_all_tests();
} else if (cmdline[idx] == '[') {
unity_run_tests_by_tag(cmdline + idx, invert);
} else if (cmdline[idx] == '"') {
char* end = strrchr(cmdline, '"');
if (end > &cmdline[idx]) {
*end = 0;
unity_run_test_by_name(cmdline + idx + 1);
}
} else if (isdigit((unsigned char)cmdline[idx])) {
unity_run_single_test_by_index_parse(cmdline + idx, test_count);
}
UNITY_END();
UnityPrint("Enter next test, or 'enter' to see menu");
UNITY_PRINT_EOL();
UNITY_OUTPUT_FLUSH();
}
}
bool unity_get_test_info(int test_index, test_desc_t* out_info)
{
if (test_index < 0) {
return false;
}
const test_desc_t *test;
for (test = s_unity_tests_first; test != NULL && test_index != 0; test = test->next, --test_index) {
;
}
if (test == NULL) {
return false;
}
*out_info = *test;
return true;
}