unity_config.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef UNITY_CONFIG_H
  2. #define UNITY_CONFIG_H
  3. // This file gets included from unity.h via unity_internals.h
  4. // It is inside #ifdef __cplusplus / extern "C" block, so we can
  5. // only use C features here
  6. // Adapt Unity to our environment, disable FP support
  7. #include <esp_err.h>
  8. #include <sdkconfig.h>
  9. /* Some definitions applicable to Unity running in FreeRTOS */
  10. #define UNITY_FREERTOS_PRIORITY CONFIG_UNITY_FREERTOS_PRIORITY
  11. #define UNITY_FREERTOS_CPU CONFIG_UNITY_FREERTOS_CPU
  12. #define UNITY_FREERTOS_STACK_SIZE CONFIG_UNITY_FREERTOS_STACK_SIZE
  13. #define UNITY_EXCLUDE_FLOAT
  14. #define UNITY_EXCLUDE_DOUBLE
  15. #define UNITY_OUTPUT_CHAR unity_putc
  16. #define UNITY_OUTPUT_FLUSH unity_flush
  17. // Define helpers to register test cases from multiple files
  18. #define UNITY_EXPAND2(a, b) a ## b
  19. #define UNITY_EXPAND(a, b) UNITY_EXPAND2(a, b)
  20. #define UNITY_TEST_UID(what) UNITY_EXPAND(what, __LINE__)
  21. #define UNITY_TEST_REG_HELPER reg_helper ## UNITY_TEST_UID
  22. #define UNITY_TEST_DESC_UID desc ## UNITY_TEST_UID
  23. // get count of __VA_ARGS__
  24. #define PP_NARG(...) \
  25. PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
  26. #define PP_NARG_(...) \
  27. PP_ARG_N(__VA_ARGS__)
  28. #define PP_ARG_N( \
  29. _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
  30. #define PP_RSEQ_N() 9,8,7,6,5,4,3,2,1,0
  31. // support max 5 test func now
  32. #define FN_NAME_SET_1(a) {#a}
  33. #define FN_NAME_SET_2(a, b) {#a, #b}
  34. #define FN_NAME_SET_3(a, b, c) {#a, #b, #c}
  35. #define FN_NAME_SET_4(a, b, c, d) {#a, #b, #c, #d}
  36. #define FN_NAME_SET_5(a, b, c, d, e) {#a, #b, #c, #d, #e}
  37. #define FN_NAME_SET2(n) FN_NAME_SET_##n
  38. #define FN_NAME_SET(n, ...) FN_NAME_SET2(n)(__VA_ARGS__)
  39. #define UNITY_TEST_FN_SET(...) \
  40. static test_func UNITY_TEST_UID(test_functions)[] = {__VA_ARGS__}; \
  41. static const char* UNITY_TEST_UID(test_fn_name)[] = FN_NAME_SET(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  42. typedef void (* test_func)(void);
  43. struct test_desc_t
  44. {
  45. const char* name;
  46. const char* desc;
  47. test_func* fn;
  48. const char* file;
  49. int line;
  50. uint8_t test_fn_count;
  51. const char ** test_fn_name;
  52. struct test_desc_t* next;
  53. };
  54. void unity_testcase_register(struct test_desc_t* desc);
  55. void unity_run_menu() __attribute__((noreturn));
  56. void unity_run_tests_with_filter(const char* filter);
  57. void unity_run_all_tests();
  58. /* Test case macro, a-la CATCH framework.
  59. First argument is a free-form description,
  60. second argument is (by convention) a list of identifiers, each one in square brackets.
  61. Identifiers are used to group related tests, or tests with specific properties.
  62. Use like:
  63. TEST_CASE("Frobnicator forbnicates", "[frobnicator][rom]")
  64. {
  65. // test goes here
  66. }
  67. */
  68. #define TEST_CASE(name_, desc_) \
  69. static void UNITY_TEST_UID(test_func_) (void); \
  70. static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) () \
  71. { \
  72. static test_func test_fn_[] = {&UNITY_TEST_UID(test_func_)}; \
  73. static struct test_desc_t UNITY_TEST_UID(test_desc_) = { \
  74. .name = name_, \
  75. .desc = desc_, \
  76. .fn = test_fn_, \
  77. .file = __FILE__, \
  78. .line = __LINE__, \
  79. .test_fn_count = 1, \
  80. .test_fn_name = NULL, \
  81. .next = NULL \
  82. }; \
  83. unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
  84. }\
  85. static void UNITY_TEST_UID(test_func_) (void)
  86. /*
  87. * Multiple stages test cases will handle the case that test steps are separated by DUT reset.
  88. * e.g: we want to verify some function after SW reset, WDT reset or deep sleep reset.
  89. *
  90. * First argument is a free-form description,
  91. * second argument is (by convention) a list of identifiers, each one in square brackets.
  92. * subsequent arguments are names test functions separated by reset.
  93. * e.g:
  94. * TEST_CASE_MULTIPLE_STAGES("run light sleep after deep sleep","[sleep]", goto_deepsleep, light_sleep_after_deep_sleep_wakeup);
  95. * */
  96. #define TEST_CASE_MULTIPLE_STAGES(name_, desc_, ...) \
  97. UNITY_TEST_FN_SET(__VA_ARGS__); \
  98. static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) () \
  99. { \
  100. static struct test_desc_t UNITY_TEST_UID(test_desc_) = { \
  101. .name = name_, \
  102. .desc = desc_"[multi_stage]", \
  103. .fn = UNITY_TEST_UID(test_functions), \
  104. .file = __FILE__, \
  105. .line = __LINE__, \
  106. .test_fn_count = PP_NARG(__VA_ARGS__), \
  107. .test_fn_name = UNITY_TEST_UID(test_fn_name), \
  108. .next = NULL \
  109. }; \
  110. unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
  111. }
  112. /*
  113. * First argument is a free-form description,
  114. * second argument is (by convention) a list of identifiers, each one in square brackets.
  115. * subsequent arguments are names of test functions for different DUTs
  116. * e.g:
  117. * TEST_CASE_MULTIPLE_DEVICES("master and slave spi","[spi][test_env=UT_T2_1]", master_test, slave_test);
  118. * */
  119. #define TEST_CASE_MULTIPLE_DEVICES(name_, desc_, ...) \
  120. UNITY_TEST_FN_SET(__VA_ARGS__); \
  121. static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) () \
  122. { \
  123. static struct test_desc_t UNITY_TEST_UID(test_desc_) = { \
  124. .name = name_, \
  125. .desc = desc_"[multi_device]", \
  126. .fn = UNITY_TEST_UID(test_functions), \
  127. .file = __FILE__, \
  128. .line = __LINE__, \
  129. .test_fn_count = PP_NARG(__VA_ARGS__), \
  130. .test_fn_name = UNITY_TEST_UID(test_fn_name), \
  131. .next = NULL \
  132. }; \
  133. unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
  134. }
  135. /**
  136. * Note: initialization of test_desc_t fields above has to be done exactly
  137. * in the same order as the fields are declared in the structure.
  138. * Otherwise the initializer will not be valid in C++ (which doesn't
  139. * support designated initializers). G++ can parse the syntax, but
  140. * field names are treated as annotations and don't affect initialization
  141. * order. Also make sure all the fields are initialized.
  142. */
  143. // shorthand to check esp_err_t return code
  144. #define TEST_ESP_OK(rc) TEST_ASSERT_EQUAL_HEX32(ESP_OK, rc)
  145. #define TEST_ESP_ERR(err, rc) TEST_ASSERT_EQUAL_HEX32(err, rc)
  146. #endif //UNITY_CONFIG_H