unity_platform.c 4.2 KB

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