1 // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdbool.h>
18 #include <ctype.h>
19 #include <stdio.h>
20 #include "unity.h"
21 #include "esp_system.h"
22 
23 /* similar to UNITY_PRINT_EOL */
24 #define UNITY_PRINT_TAB() UNITY_OUTPUT_CHAR('\t')
25 
26 // Pointers to the head and tail of linked list of test description structs:
27 static test_desc_t *s_unity_tests_first = NULL;
28 static test_desc_t *s_unity_tests_last = NULL;
29 
unity_testcase_register(test_desc_t * desc)30 void unity_testcase_register(test_desc_t *desc)
31 {
32     if (!s_unity_tests_first) {
33         s_unity_tests_first = desc;
34         s_unity_tests_last = desc;
35     } else {
36         test_desc_t *temp = s_unity_tests_first;
37         s_unity_tests_first = desc;
38         s_unity_tests_first->next = temp;
39     }
40 }
41 
42 /* print the multiple function case name and its sub-menu
43  * e.g:
44  * (1) spi master/slave case
45  *       (1)master case
46  *       (2)slave case
47  * */
print_multiple_function_test_menu(const test_desc_t * test_ms)48 static void print_multiple_function_test_menu(const test_desc_t *test_ms)
49 {
50     UnityPrint(test_ms->name);
51     UNITY_PRINT_EOL();
52     for (int i = 0; i < test_ms->test_fn_count; i++) {
53         UNITY_PRINT_TAB();
54         UnityPrint("(");
55         UnityPrintNumberUnsigned(i + 1);
56         UnityPrint(")");
57         UNITY_PRINT_TAB();
58         UnityPrint("\"");
59         UnityPrint(test_ms->test_fn_name[i]);
60         UnityPrint("\"");
61         UNITY_PRINT_EOL();
62     }
63 }
64 
65 /*
66  * This function looks like UnityDefaultTestRun function only without UNITY_CLR_DETAILS.
67  * void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
68  * was moved from `components/unity/unity/src/unity.c` to here.
69 */
unity_default_test_run(UnityTestFunction Func,const char * FuncName,const int FuncLineNum)70 static void unity_default_test_run(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
71 {
72     Unity.CurrentTestName = FuncName;
73     Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
74     Unity.NumberOfTests++;
75     if (TEST_PROTECT())
76     {
77         setUp();
78         Func();
79     }
80     if (TEST_PROTECT())
81     {
82         tearDown();
83     }
84     UnityConcludeTest();
85 }
86 
multiple_function_option(const test_desc_t * test_ms)87 static int multiple_function_option(const test_desc_t *test_ms)
88 {
89     int selection;
90     char cmdline[256] = {0};
91 
92     print_multiple_function_test_menu(test_ms);
93     while (strlen(cmdline) == 0) {
94         unity_gets(cmdline, sizeof(cmdline));
95         if (strlen(cmdline) == 0) {
96             /* if input was newline, print a new menu */
97             print_multiple_function_test_menu(test_ms);
98         }
99     }
100     selection = atoi((const char *) cmdline) - 1;
101     if (selection >= 0 && selection < test_ms->test_fn_count) {
102         unity_default_test_run(test_ms->fn[selection], test_ms->name, test_ms->line);
103     } else {
104         UnityPrint("Invalid selection, your should input number 1-");
105         UnityPrintNumber(test_ms->test_fn_count);
106         UNITY_PRINT_EOL();
107     }
108     return selection;
109 }
110 
unity_run_single_test(const test_desc_t * test)111 static void unity_run_single_test(const test_desc_t *test)
112 {
113     UnityPrint("Running ");
114     UnityPrint(test->name);
115     UnityPrint("...");
116     UNITY_PRINT_EOL();
117     // Unit test runner expects to see test name before the test starts
118     UNITY_OUTPUT_FLUSH();
119 
120     Unity.TestFile = test->file;
121     Unity.CurrentDetail1 = test->desc;
122     bool reset_after_test = strstr(Unity.CurrentDetail1, "[leaks") != NULL;
123     bool multi_device = strstr(Unity.CurrentDetail1, "[multi_device]") != NULL;
124     if (test->test_fn_count == 1) {
125         unity_default_test_run(test->fn[0], test->name, test->line);
126     } else {
127         int selection = multiple_function_option(test);
128         if (reset_after_test && multi_device == false) {
129             if (selection != (test->test_fn_count - 1)) {
130                 // to do a reset for all stages except the last stage.
131                 esp_restart();
132             }
133         }
134     }
135 
136     if (reset_after_test) {
137         // print a result of test before to do reset for the last stage.
138         UNITY_END();
139         UnityPrint("Enter next test, or 'enter' to see menu");
140         UNITY_PRINT_EOL();
141         UNITY_OUTPUT_FLUSH();
142         esp_restart();
143     }
144 }
145 
unity_run_single_test_by_index(int index)146 static void unity_run_single_test_by_index(int index)
147 {
148     const test_desc_t *test;
149     for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index) {
150         ;
151     }
152     if (test != NULL) {
153         unity_run_single_test(test);
154     }
155 }
156 
unity_run_single_test_by_index_parse(const char * filter,int index_max)157 static void unity_run_single_test_by_index_parse(const char *filter, int index_max)
158 {
159     int test_index = strtol(filter, NULL, 10);
160     if (test_index >= 1 && test_index <= index_max) {
161         UNITY_EXEC_TIME_START();
162         unity_run_single_test_by_index(test_index - 1);
163         UNITY_EXEC_TIME_STOP();
164         UnityPrint("Test ran in ");
165         UnityPrintNumberUnsigned(UNITY_EXEC_TIME_MS());
166         UnityPrint("ms");
167         UNITY_PRINT_EOL();
168         UNITY_OUTPUT_FLUSH();
169     }
170 }
171 
unity_run_test_by_name(const char * name)172 void unity_run_test_by_name(const char *name)
173 {
174     for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
175         if (strcmp(test->name, name) == 0) {
176             unity_run_single_test(test);
177         }
178     }
179 }
180 
unity_run_all_tests(void)181 void unity_run_all_tests(void)
182 {
183     for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
184         unity_run_single_test(test);
185     }
186 }
187 
unity_run_tests_by_tag(const char * tag,bool invert)188 void unity_run_tests_by_tag(const char *tag, bool invert)
189 {
190     UnityPrint("Running tests ");
191     if (invert) {
192         UnityPrint("NOT ");
193     }
194     UnityPrint("matching '");
195     UnityPrint(tag);
196     UnityPrint("'...");
197     UNITY_PRINT_EOL();
198 
199     for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
200         if ((strstr(test->desc, tag) != NULL) == !invert) {
201             unity_run_single_test(test);
202         }
203     }
204 }
205 
trim_trailing_space(char * str)206 static void trim_trailing_space(char *str)
207 {
208     char *end = str + strlen(str) - 1;
209     while (end >= str && isspace((int) *end)) {
210         *end = 0;
211         --end;
212     }
213 }
214 
print_test_menu(void)215 static int print_test_menu(void)
216 {
217     int test_counter = 0;
218     UNITY_PRINT_EOL();
219     UNITY_PRINT_EOL();
220     UnityPrint("Here's the test menu, pick your combo:");
221     UNITY_PRINT_EOL();
222     for (const test_desc_t *test = s_unity_tests_first;
223             test != NULL;
224             test = test->next, ++test_counter) {
225 
226         UnityPrint("(");
227         UnityPrintNumber(test_counter + 1);
228         UnityPrint(")");
229         UNITY_PRINT_TAB();
230         UnityPrint("\"");
231         UnityPrint(test->name);
232         UnityPrint("\" ");
233         UnityPrint(test->desc);
234         UNITY_PRINT_EOL();
235 
236         if (test->test_fn_count > 1) {
237             for (int i = 0; i < test->test_fn_count; i++) {
238                 UNITY_PRINT_TAB();
239                 UnityPrint("(");
240                 UnityPrintNumber(i + 1);
241                 UnityPrint(")");
242                 UNITY_PRINT_TAB();
243                 UnityPrint("\"");
244                 UnityPrint(test->test_fn_name[i]);
245                 UnityPrint("\"");
246                 UNITY_PRINT_EOL();
247             }
248         }
249     }
250     UNITY_PRINT_EOL();
251     UnityPrint("Enter test for running."); /* unit_test.py needs it for finding the end of test menu */
252     UNITY_PRINT_EOL();
253     UNITY_OUTPUT_FLUSH();
254     return test_counter;
255 }
256 
get_test_count(void)257 static int get_test_count(void)
258 {
259     int test_counter = 0;
260     for (const test_desc_t *test = s_unity_tests_first;
261             test != NULL;
262             test = test->next) {
263         ++test_counter;
264     }
265     return test_counter;
266 }
267 
unity_run_menu(void)268 void unity_run_menu(void)
269 {
270     UNITY_PRINT_EOL();
271     UNITY_PRINT_EOL();
272     UnityPrint("Press ENTER to see the list of tests.");
273     UNITY_PRINT_EOL();
274     int test_count = get_test_count();
275     while (true) {
276         char cmdline[256] = { 0 };
277         while (strlen(cmdline) == 0) {
278             unity_gets(cmdline, sizeof(cmdline));
279             trim_trailing_space(cmdline);
280             if (strlen(cmdline) == 0) {
281                 /* if input was newline, print a new menu */
282                 print_test_menu();
283             }
284         }
285         /*use '-' to show test history. Need to do it before UNITY_BEGIN cleanup history */
286         if (cmdline[0] == '-') {
287             UNITY_END();
288             continue;
289         }
290 
291         UNITY_BEGIN();
292 
293         size_t idx = 0;
294         bool invert = false;
295         if (cmdline[idx] == '!') {
296             invert = true;
297             ++idx;
298         }
299 
300         if (cmdline[idx] == '*') {
301             unity_run_all_tests();
302         } else if (cmdline[idx] == '[') {
303             unity_run_tests_by_tag(cmdline + idx, invert);
304         } else if (cmdline[idx] == '"') {
305             char* end = strrchr(cmdline, '"');
306             if (end > &cmdline[idx]) {
307                 *end = 0;
308                 unity_run_test_by_name(cmdline + idx + 1);
309             }
310         } else if (isdigit((unsigned char)cmdline[idx])) {
311             unity_run_single_test_by_index_parse(cmdline + idx, test_count);
312         }
313 
314         UNITY_END();
315 
316         UnityPrint("Enter next test, or 'enter' to see menu");
317         UNITY_PRINT_EOL();
318         UNITY_OUTPUT_FLUSH();
319     }
320 }
321