unity_runner.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdbool.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include "unity.h"
  20. /* similar to UNITY_PRINT_EOL */
  21. #define UNITY_PRINT_TAB() UNITY_OUTPUT_CHAR('\t')
  22. // Pointers to the head and tail of linked list of test description structs:
  23. static test_desc_t *s_unity_tests_first = NULL;
  24. static test_desc_t *s_unity_tests_last = NULL;
  25. void unity_testcase_register(test_desc_t *desc)
  26. {
  27. if (!s_unity_tests_first) {
  28. s_unity_tests_first = desc;
  29. s_unity_tests_last = desc;
  30. } else {
  31. test_desc_t *temp = s_unity_tests_first;
  32. s_unity_tests_first = desc;
  33. s_unity_tests_first->next = temp;
  34. }
  35. }
  36. /* print the multiple function case name and its sub-menu
  37. * e.g:
  38. * (1) spi master/slave case
  39. * (1)master case
  40. * (2)slave case
  41. * */
  42. static void print_multiple_function_test_menu(const test_desc_t *test_ms)
  43. {
  44. UnityPrint(test_ms->name);
  45. UNITY_PRINT_EOL();
  46. for (int i = 0; i < test_ms->test_fn_count; i++) {
  47. UNITY_PRINT_TAB();
  48. UnityPrint("(");
  49. UnityPrintNumberUnsigned(i + 1);
  50. UnityPrint(")");
  51. UNITY_PRINT_TAB();
  52. UnityPrint("\"");
  53. UnityPrint(test_ms->test_fn_name[i]);
  54. UnityPrint("\"");
  55. UNITY_PRINT_EOL();
  56. }
  57. }
  58. static void multiple_function_option(const test_desc_t *test_ms)
  59. {
  60. int selection;
  61. char cmdline[256] = {0};
  62. print_multiple_function_test_menu(test_ms);
  63. while (strlen(cmdline) == 0) {
  64. unity_gets(cmdline, sizeof(cmdline));
  65. if (strlen(cmdline) == 0) {
  66. /* if input was newline, print a new menu */
  67. print_multiple_function_test_menu(test_ms);
  68. }
  69. }
  70. selection = atoi((const char *) cmdline) - 1;
  71. if (selection >= 0 && selection < test_ms->test_fn_count) {
  72. UnityDefaultTestRun(test_ms->fn[selection], test_ms->name, test_ms->line);
  73. } else {
  74. UnityPrint("Invalid selection, your should input number 1-");
  75. UnityPrintNumber(test_ms->test_fn_count);
  76. UNITY_PRINT_EOL();
  77. }
  78. }
  79. static void unity_run_single_test(const test_desc_t *test)
  80. {
  81. UnityPrint("Running ");
  82. UnityPrint(test->name);
  83. UnityPrint("...");
  84. UNITY_PRINT_EOL();
  85. // Unit test runner expects to see test name before the test starts
  86. UNITY_OUTPUT_FLUSH();
  87. Unity.TestFile = test->file;
  88. Unity.CurrentDetail1 = test->desc;
  89. if (test->test_fn_count == 1) {
  90. UnityDefaultTestRun(test->fn[0], test->name, test->line);
  91. } else {
  92. multiple_function_option(test);
  93. }
  94. }
  95. static void unity_run_single_test_by_index(int index)
  96. {
  97. const test_desc_t *test;
  98. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index) {
  99. ;
  100. }
  101. if (test != NULL) {
  102. unity_run_single_test(test);
  103. }
  104. }
  105. static void unity_run_single_test_by_index_parse(const char *filter, int index_max)
  106. {
  107. int test_index = strtol(filter, NULL, 10);
  108. if (test_index >= 1 && test_index <= index_max) {
  109. UNITY_EXEC_TIME_START();
  110. unity_run_single_test_by_index(test_index - 1);
  111. UNITY_EXEC_TIME_STOP();
  112. UnityPrint("Test ran in ");
  113. UnityPrintNumberUnsigned(UNITY_EXEC_TIME_MS());
  114. UnityPrint("ms");
  115. UNITY_PRINT_EOL();
  116. UNITY_OUTPUT_FLUSH();
  117. }
  118. }
  119. void unity_run_test_by_name(const char *name)
  120. {
  121. for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
  122. if (strcmp(test->name, name) == 0) {
  123. unity_run_single_test(test);
  124. }
  125. }
  126. }
  127. void unity_run_all_tests()
  128. {
  129. for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
  130. unity_run_single_test(test);
  131. }
  132. }
  133. void unity_run_tests_by_tag(const char *tag, bool invert)
  134. {
  135. UnityPrint("Running tests ");
  136. if (invert) {
  137. UnityPrint("NOT ");
  138. }
  139. UnityPrint("matching '");
  140. UnityPrint(tag);
  141. UnityPrint("'...");
  142. UNITY_PRINT_EOL();
  143. for (const test_desc_t *test = s_unity_tests_first; test != NULL; test = test->next) {
  144. if ((strstr(test->desc, tag) != NULL) == !invert) {
  145. unity_run_single_test(test);
  146. }
  147. }
  148. }
  149. static void trim_trailing_space(char *str)
  150. {
  151. char *end = str + strlen(str) - 1;
  152. while (end >= str && isspace((int) *end)) {
  153. *end = 0;
  154. --end;
  155. }
  156. }
  157. static int print_test_menu(void)
  158. {
  159. int test_counter = 0;
  160. UNITY_PRINT_EOL();
  161. UNITY_PRINT_EOL();
  162. UnityPrint("Here's the test menu, pick your combo:");
  163. UNITY_PRINT_EOL();
  164. for (const test_desc_t *test = s_unity_tests_first;
  165. test != NULL;
  166. test = test->next, ++test_counter) {
  167. UnityPrint("(");
  168. UnityPrintNumber(test_counter + 1);
  169. UnityPrint(")");
  170. UNITY_PRINT_TAB();
  171. UnityPrint("\"");
  172. UnityPrint(test->name);
  173. UnityPrint("\" ");
  174. UnityPrint(test->desc);
  175. UNITY_PRINT_EOL();
  176. if (test->test_fn_count > 1) {
  177. for (int i = 0; i < test->test_fn_count; i++) {
  178. UNITY_PRINT_TAB();
  179. UnityPrint("(");
  180. UnityPrintNumber(i + 1);
  181. UnityPrint(")");
  182. UNITY_PRINT_TAB();
  183. UnityPrint("\"");
  184. UnityPrint(test->test_fn_name[i]);
  185. UnityPrint("\"");
  186. UNITY_PRINT_EOL();
  187. }
  188. }
  189. }
  190. UNITY_PRINT_EOL();
  191. UnityPrint("Enter test for running."); /* unit_test.py needs it for finding the end of test menu */
  192. UNITY_PRINT_EOL();
  193. UNITY_OUTPUT_FLUSH();
  194. return test_counter;
  195. }
  196. static int get_test_count(void)
  197. {
  198. int test_counter = 0;
  199. for (const test_desc_t *test = s_unity_tests_first;
  200. test != NULL;
  201. test = test->next) {
  202. ++test_counter;
  203. }
  204. return test_counter;
  205. }
  206. void unity_run_menu()
  207. {
  208. UNITY_PRINT_EOL();
  209. UNITY_PRINT_EOL();
  210. UnityPrint("Press ENTER to see the list of tests.");
  211. UNITY_PRINT_EOL();
  212. int test_count = get_test_count();
  213. while (true) {
  214. char cmdline[256] = { 0 };
  215. while (strlen(cmdline) == 0) {
  216. unity_gets(cmdline, sizeof(cmdline));
  217. trim_trailing_space(cmdline);
  218. if (strlen(cmdline) == 0) {
  219. /* if input was newline, print a new menu */
  220. print_test_menu();
  221. }
  222. }
  223. /*use '-' to show test history. Need to do it before UNITY_BEGIN cleanup history */
  224. if (cmdline[0] == '-') {
  225. UNITY_END();
  226. continue;
  227. }
  228. UNITY_BEGIN();
  229. size_t idx = 0;
  230. bool invert = false;
  231. if (cmdline[idx] == '!') {
  232. invert = true;
  233. ++idx;
  234. }
  235. if (cmdline[idx] == '*') {
  236. unity_run_all_tests();
  237. } else if (cmdline[idx] == '[') {
  238. unity_run_tests_by_tag(cmdline + idx, invert);
  239. } else if (cmdline[idx] == '"') {
  240. char* end = strrchr(cmdline, '"');
  241. if (end > &cmdline[idx]) {
  242. *end = 0;
  243. unity_run_test_by_name(cmdline + idx + 1);
  244. }
  245. } else if (isdigit((unsigned char)cmdline[idx])) {
  246. unity_run_single_test_by_index_parse(cmdline + idx, test_count);
  247. }
  248. UNITY_END();
  249. UnityPrint("Enter next test, or 'enter' to see menu");
  250. UNITY_PRINT_EOL();
  251. UNITY_OUTPUT_FLUSH();
  252. }
  253. }