unity_platform.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include "unity.h"
  6. #include "rom/ets_sys.h"
  7. #include "rom/uart.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_log.h"
  11. #include "soc/cpu.h"
  12. #define unity_printf ets_printf
  13. // Pointers to the head and tail of linked list of test description structs:
  14. static struct test_desc_t* s_unity_tests_first = NULL;
  15. static struct test_desc_t* s_unity_tests_last = NULL;
  16. // Inverse of the filter
  17. static bool s_invert = false;
  18. void unity_putc(int c)
  19. {
  20. if (c == '\n')
  21. {
  22. uart_tx_one_char('\r');
  23. uart_tx_one_char('\n');
  24. }
  25. else if (c == '\r')
  26. {
  27. }
  28. else
  29. {
  30. uart_tx_one_char(c);
  31. }
  32. }
  33. void unity_flush()
  34. {
  35. uart_tx_wait_idle(0); // assume that output goes to UART0
  36. }
  37. void unity_testcase_register(struct test_desc_t* desc)
  38. {
  39. if (!s_unity_tests_first)
  40. {
  41. s_unity_tests_first = desc;
  42. s_unity_tests_last = desc;
  43. }
  44. else
  45. {
  46. struct test_desc_t* temp = s_unity_tests_first;
  47. s_unity_tests_first = desc;
  48. s_unity_tests_first->next = temp;
  49. }
  50. }
  51. static void unity_run_single_test(const struct test_desc_t* test)
  52. {
  53. printf("Running %s...\n", test->name);
  54. Unity.TestFile = test->file;
  55. Unity.CurrentDetail1 = test->desc;
  56. UnityDefaultTestRun(test->fn, test->name, test->line);
  57. }
  58. static void unity_run_single_test_by_index(int index)
  59. {
  60. const struct test_desc_t* test;
  61. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
  62. {
  63. }
  64. if (test != NULL)
  65. {
  66. unity_run_single_test(test);
  67. }
  68. }
  69. static void unity_run_single_test_by_index_parse(const char* filter, int index_max)
  70. {
  71. if (s_invert)
  72. {
  73. printf("Inverse is not supported for that kind of filter\n");
  74. return;
  75. }
  76. int test_index = strtol(filter, NULL, 10);
  77. if (test_index >= 1 && test_index <= index_max)
  78. {
  79. uint32_t start;
  80. RSR(CCOUNT, start);
  81. unity_run_single_test_by_index(test_index - 1);
  82. uint32_t end;
  83. RSR(CCOUNT, end);
  84. uint32_t ms = (end - start) / (XT_CLOCK_FREQ / 1000);
  85. printf("Test ran in %dms\n", ms);
  86. }
  87. }
  88. static void unity_run_single_test_by_name(const char* filter)
  89. {
  90. if (s_invert)
  91. {
  92. printf("Inverse is not supported for that kind of filter\n");
  93. return;
  94. }
  95. char tmp[256];
  96. strncpy(tmp, filter + 1, sizeof(tmp) - 1);
  97. tmp[strlen(filter) - 2] = 0;
  98. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  99. {
  100. if (strcmp(test->name, tmp) == 0)
  101. {
  102. unity_run_single_test(test);
  103. }
  104. }
  105. }
  106. void unity_run_all_tests()
  107. {
  108. if (s_invert)
  109. {
  110. printf("Inverse is not supported for that kind of filter\n");
  111. return;
  112. }
  113. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  114. {
  115. unity_run_single_test(test);
  116. }
  117. }
  118. void unity_run_tests_with_filter(const char* filter)
  119. {
  120. if (s_invert)
  121. {
  122. ++filter;
  123. }
  124. printf("Running tests %smatching '%s'...\n", s_invert ? "NOT " : "", filter);
  125. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  126. {
  127. if ((strstr(test->desc, filter) != NULL) == !s_invert)
  128. {
  129. unity_run_single_test(test);
  130. }
  131. }
  132. }
  133. static void trim_trailing_space(char* str)
  134. {
  135. char* end = str + strlen(str) - 1;
  136. while (end >= str && isspace((int) *end))
  137. {
  138. *end = 0;
  139. --end;
  140. }
  141. }
  142. static int print_test_menu(void)
  143. {
  144. int test_counter = 0;
  145. unity_printf("\n\nHere's the test menu, pick your combo:\n");
  146. for (const struct test_desc_t* test = s_unity_tests_first;
  147. test != NULL;
  148. test = test->next, ++test_counter)
  149. {
  150. unity_printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
  151. }
  152. return test_counter;
  153. }
  154. void unity_run_menu()
  155. {
  156. int test_count = print_test_menu();
  157. while (true)
  158. {
  159. char cmdline[256] = { 0 };
  160. while(strlen(cmdline) == 0)
  161. {
  162. /* Flush anything already in the RX buffer */
  163. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  164. }
  165. /* Read input */
  166. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  167. trim_trailing_space(cmdline);
  168. if(strlen(cmdline) == 0) {
  169. /* if input was newline, print a new menu */
  170. print_test_menu();
  171. }
  172. }
  173. UNITY_BEGIN();
  174. size_t idx = 0;
  175. if (cmdline[idx] == '!')
  176. {
  177. s_invert = true;
  178. ++idx;
  179. }
  180. else
  181. {
  182. s_invert = false;
  183. }
  184. if (cmdline[idx] == '*')
  185. {
  186. unity_run_all_tests();
  187. }
  188. else if (cmdline[idx] =='[')
  189. {
  190. unity_run_tests_with_filter(cmdline + idx);
  191. }
  192. else if (cmdline[idx] =='"')
  193. {
  194. unity_run_single_test_by_name(cmdline + idx);
  195. }
  196. else if (isdigit((unsigned char)cmdline[idx]))
  197. {
  198. unity_run_single_test_by_index_parse(cmdline + idx, test_count);
  199. }
  200. UNITY_END();
  201. printf("Enter next test, or 'enter' to see menu\n");
  202. }
  203. }