unity_config.h 5.9 KB

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