unity_platform.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. void unity_putc(int c)
  17. {
  18. if (c == '\n')
  19. {
  20. uart_tx_one_char('\n');
  21. uart_tx_one_char('\r');
  22. }
  23. else if (c == '\r')
  24. {
  25. }
  26. else
  27. {
  28. uart_tx_one_char(c);
  29. }
  30. }
  31. void unity_flush()
  32. {
  33. uart_tx_wait_idle(0); // assume that output goes to UART0
  34. }
  35. void unity_testcase_register(struct test_desc_t* desc)
  36. {
  37. if (!s_unity_tests_first)
  38. {
  39. s_unity_tests_first = desc;
  40. s_unity_tests_last = desc;
  41. }
  42. else
  43. {
  44. struct test_desc_t* temp = s_unity_tests_first;
  45. s_unity_tests_first = desc;
  46. s_unity_tests_first->next = temp;
  47. }
  48. }
  49. static void unity_run_single_test(const struct test_desc_t* test)
  50. {
  51. Unity.TestFile = test->file;
  52. Unity.CurrentDetail1 = test->desc;
  53. UnityDefaultTestRun(test->fn, test->name, test->line);
  54. }
  55. static void unity_run_single_test_by_index(int index)
  56. {
  57. const struct test_desc_t* test;
  58. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
  59. {
  60. }
  61. if (test != NULL)
  62. {
  63. unity_run_single_test(test);
  64. }
  65. }
  66. static void unity_run_single_test_by_name(const char* filter)
  67. {
  68. char tmp[256];
  69. strncpy(tmp, filter + 1, sizeof(tmp) - 1);
  70. tmp[strlen(filter) - 2] = 0;
  71. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  72. {
  73. if (strstr(test->name, tmp) != NULL)
  74. {
  75. unity_run_single_test(test);
  76. }
  77. }
  78. }
  79. void unity_run_all_tests()
  80. {
  81. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  82. {
  83. unity_run_single_test(test);
  84. }
  85. }
  86. void unity_run_tests_with_filter(const char* filter)
  87. {
  88. bool invert = filter[0] == '!';
  89. if (invert) {
  90. filter++;
  91. }
  92. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  93. {
  94. if ((strstr(test->desc, filter) != NULL) == !invert)
  95. {
  96. unity_run_single_test(test);
  97. }
  98. }
  99. }
  100. static void trim_trailing_space(char* str)
  101. {
  102. char* end = str + strlen(str) - 1;
  103. while (end >= str && isspace((int) *end))
  104. {
  105. *end = 0;
  106. --end;
  107. }
  108. }
  109. static int print_test_menu(void)
  110. {
  111. int test_counter = 0;
  112. unity_printf("\n\nHere's the test menu, pick your combo:\n");
  113. for (const struct test_desc_t* test = s_unity_tests_first;
  114. test != NULL;
  115. test = test->next, ++test_counter)
  116. {
  117. unity_printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
  118. }
  119. return test_counter;
  120. }
  121. void unity_run_menu()
  122. {
  123. int test_count = print_test_menu();
  124. while (true)
  125. {
  126. char cmdline[256] = { 0 };
  127. while(strlen(cmdline) == 0)
  128. {
  129. /* Flush anything already in the RX buffer */
  130. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  131. }
  132. /* Read input */
  133. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  134. trim_trailing_space(cmdline);
  135. if(strlen(cmdline) == 0) {
  136. /* if input was newline, print a new menu */
  137. print_test_menu();
  138. }
  139. }
  140. UNITY_BEGIN();
  141. if (cmdline[0] == '*')
  142. {
  143. unity_run_all_tests();
  144. }
  145. else if (cmdline[0] =='[')
  146. {
  147. unity_run_tests_with_filter(cmdline);
  148. }
  149. else if (cmdline[0] =='"')
  150. {
  151. unity_run_single_test_by_name(cmdline);
  152. }
  153. else
  154. {
  155. int test_index = strtol(cmdline, NULL, 10);
  156. if (test_index >= 1 && test_index <= test_count)
  157. {
  158. uint32_t start;
  159. RSR(CCOUNT, start);
  160. unity_run_single_test_by_index(test_index - 1);
  161. uint32_t end;
  162. RSR(CCOUNT, end);
  163. uint32_t ms = (end - start) / (XT_CLOCK_FREQ / 1000);
  164. printf("Test ran in %dms\n", ms);
  165. }
  166. }
  167. UNITY_END();
  168. printf("Enter next test, or 'enter' to see menu\n");
  169. }
  170. }