unity_platform.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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('\n');
  23. uart_tx_one_char('\r');
  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. Unity.TestFile = test->file;
  54. Unity.CurrentDetail1 = test->desc;
  55. UnityDefaultTestRun(test->fn, test->name, test->line);
  56. }
  57. static void unity_run_single_test_by_index(int index)
  58. {
  59. const struct test_desc_t* test;
  60. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
  61. {
  62. }
  63. if (test != NULL)
  64. {
  65. unity_run_single_test(test);
  66. }
  67. }
  68. static void unity_run_single_test_by_index_parse(const char* filter, int index_max)
  69. {
  70. if (s_invert)
  71. {
  72. printf("Inverse is not supported for that kind of filter\n");
  73. return;
  74. }
  75. int test_index = strtol(filter, NULL, 10);
  76. if (test_index >= 1 && test_index <= index_max)
  77. {
  78. uint32_t start;
  79. RSR(CCOUNT, start);
  80. unity_run_single_test_by_index(test_index - 1);
  81. uint32_t end;
  82. RSR(CCOUNT, end);
  83. uint32_t ms = (end - start) / (XT_CLOCK_FREQ / 1000);
  84. printf("Test ran in %dms\n", ms);
  85. }
  86. }
  87. static void unity_run_single_test_by_name(const char* filter)
  88. {
  89. if (s_invert)
  90. {
  91. printf("Inverse is not supported for that kind of filter\n");
  92. return;
  93. }
  94. char tmp[256];
  95. strncpy(tmp, filter + 1, sizeof(tmp) - 1);
  96. tmp[strlen(filter) - 2] = 0;
  97. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  98. {
  99. if (strstr(test->name, tmp) != NULL)
  100. {
  101. unity_run_single_test(test);
  102. }
  103. }
  104. }
  105. void unity_run_all_tests()
  106. {
  107. if (s_invert)
  108. {
  109. printf("Inverse is not supported for that kind of filter\n");
  110. return;
  111. }
  112. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  113. {
  114. unity_run_single_test(test);
  115. }
  116. }
  117. void unity_run_tests_with_filter(const char* filter)
  118. {
  119. if (s_invert)
  120. {
  121. ++filter;
  122. }
  123. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  124. {
  125. if ((strstr(test->desc, filter) != NULL) == !s_invert)
  126. {
  127. unity_run_single_test(test);
  128. }
  129. }
  130. }
  131. static void trim_trailing_space(char* str)
  132. {
  133. char* end = str + strlen(str) - 1;
  134. while (end >= str && isspace((int) *end))
  135. {
  136. *end = 0;
  137. --end;
  138. }
  139. }
  140. static int print_test_menu(void)
  141. {
  142. int test_counter = 0;
  143. unity_printf("\n\nHere's the test menu, pick your combo:\n");
  144. for (const struct test_desc_t* test = s_unity_tests_first;
  145. test != NULL;
  146. test = test->next, ++test_counter)
  147. {
  148. unity_printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
  149. }
  150. return test_counter;
  151. }
  152. void unity_run_menu()
  153. {
  154. int test_count = print_test_menu();
  155. while (true)
  156. {
  157. char cmdline[256] = { 0 };
  158. while(strlen(cmdline) == 0)
  159. {
  160. /* Flush anything already in the RX buffer */
  161. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  162. }
  163. /* Read input */
  164. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  165. trim_trailing_space(cmdline);
  166. if(strlen(cmdline) == 0) {
  167. /* if input was newline, print a new menu */
  168. print_test_menu();
  169. }
  170. }
  171. UNITY_BEGIN();
  172. size_t idx = 0;
  173. if (cmdline[idx] == '!')
  174. {
  175. s_invert = true;
  176. ++idx;
  177. }
  178. else
  179. {
  180. s_invert = false;
  181. }
  182. if (cmdline[idx] == '*')
  183. {
  184. unity_run_all_tests();
  185. }
  186. else if (cmdline[idx] =='[')
  187. {
  188. unity_run_tests_with_filter(cmdline + idx);
  189. }
  190. else if (cmdline[idx] =='"')
  191. {
  192. unity_run_single_test_by_name(cmdline + idx);
  193. }
  194. else if (isdigit((unsigned char)cmdline[idx]))
  195. {
  196. unity_run_single_test_by_index_parse(cmdline + idx, test_count);
  197. }
  198. UNITY_END();
  199. printf("Enter next test, or 'enter' to see menu\n");
  200. }
  201. }