unity_platform.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include "unity.h"
  5. #include "rom/ets_sys.h"
  6. #define unity_printf ets_printf
  7. // Functions which are defined in ROM, linker script provides addresses for these:
  8. int uart_tx_one_char(uint8_t c);
  9. void uart_tx_wait_idle(uint8_t uart_no);
  10. int UartRxString(uint8_t* dst, uint8_t max_length);
  11. // Pointers to the head and tail of linked list of test description structs:
  12. static struct test_desc_t* s_unity_tests_first = NULL;
  13. static struct test_desc_t* s_unity_tests_last = NULL;
  14. void unity_putc(int c)
  15. {
  16. if (c == '\n')
  17. {
  18. uart_tx_one_char('\n');
  19. uart_tx_one_char('\r');
  20. }
  21. else if (c == '\r')
  22. {
  23. }
  24. else
  25. {
  26. uart_tx_one_char(c);
  27. }
  28. }
  29. void unity_flush()
  30. {
  31. uart_tx_wait_idle(0); // assume that output goes to UART0
  32. }
  33. void unity_testcase_register(struct test_desc_t* desc)
  34. {
  35. if (!s_unity_tests_first)
  36. {
  37. s_unity_tests_first = desc;
  38. }
  39. else
  40. {
  41. s_unity_tests_last->next = desc;
  42. }
  43. s_unity_tests_last = desc;
  44. desc->next = NULL;
  45. }
  46. static void unity_run_single_test(const struct test_desc_t* test)
  47. {
  48. Unity.TestFile = test->file;
  49. Unity.CurrentDetail1 = test->desc;
  50. UnityDefaultTestRun(test->fn, test->name, test->line);
  51. }
  52. static void unity_run_single_test_by_index(int index)
  53. {
  54. const struct test_desc_t* test;
  55. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
  56. {
  57. }
  58. if (test != NULL)
  59. {
  60. unity_run_single_test(test);
  61. }
  62. }
  63. static void unity_run_single_test_by_name(const char* filter)
  64. {
  65. char tmp[256];
  66. strncpy(tmp, filter + 1, sizeof(tmp) - 1);
  67. tmp[strlen(filter) - 2] = 0;
  68. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  69. {
  70. if (strstr(test->name, tmp) != NULL)
  71. {
  72. unity_run_single_test(test);
  73. }
  74. }
  75. }
  76. void unity_run_all_tests()
  77. {
  78. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  79. {
  80. unity_run_single_test(test);
  81. }
  82. }
  83. void unity_run_tests_with_filter(const char* filter)
  84. {
  85. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  86. {
  87. if (strstr(test->desc, filter) != NULL)
  88. {
  89. unity_run_single_test(test);
  90. }
  91. }
  92. }
  93. static void trim_trailing_space(char* str)
  94. {
  95. char* end = str + strlen(str) - 1;
  96. while (end >= str && isspace((int) *end))
  97. {
  98. *end = 0;
  99. --end;
  100. }
  101. }
  102. void unity_run_menu()
  103. {
  104. while (true)
  105. {
  106. int test_counter = 0;
  107. unity_printf("\n\nHere's the test menu, pick your combo:\n");
  108. for (const struct test_desc_t* test = s_unity_tests_first;
  109. test != NULL;
  110. test = test->next, ++test_counter)
  111. {
  112. unity_printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
  113. }
  114. char cmdline[256];
  115. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  116. trim_trailing_space(cmdline);
  117. if (strlen(cmdline) == 0)
  118. {
  119. continue;
  120. }
  121. UNITY_BEGIN();
  122. if (cmdline[0] == '*')
  123. {
  124. unity_run_all_tests();
  125. }
  126. else if (cmdline[0] =='[')
  127. {
  128. unity_run_tests_with_filter(cmdline);
  129. }
  130. else if (cmdline[0] =='"')
  131. {
  132. unity_run_single_test_by_name(cmdline);
  133. }
  134. else
  135. {
  136. int test_index = strtol(cmdline, NULL, 10);
  137. if (test_index >= 1 && test_index <= test_counter)
  138. {
  139. unity_run_single_test_by_index(test_index - 1);
  140. }
  141. }
  142. UNITY_END();
  143. }
  144. }