1 // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <stdint.h>
17 #include <stdbool.h>
18 
19 // This file gets included from unity.h via unity_internals.h via unity_config.h
20 // It is inside #ifdef __cplusplus / extern "C" block, so we can
21 // only use C features here
22 
23 // Define helpers to register test cases from multiple files
24 #define UNITY_EXPAND2(a, b) a ## b
25 #define UNITY_EXPAND(a, b) UNITY_EXPAND2(a, b)
26 #define UNITY_TEST_UID(what) UNITY_EXPAND(what, __LINE__)
27 
28 #define UNITY_TEST_REG_HELPER reg_helper ## UNITY_TEST_UID
29 #define UNITY_TEST_DESC_UID desc ## UNITY_TEST_UID
30 
31 
32 // get count of __VA_ARGS__
33 #define PP_NARG(...) \
34          PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
35 #define PP_NARG_(...) \
36          PP_ARG_N(__VA_ARGS__)
37 #define PP_ARG_N( \
38           _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
39 #define PP_RSEQ_N() 9,8,7,6,5,4,3,2,1,0
40 
41 // support max 5 test func now
42 #define FN_NAME_SET_1(a)                {#a}
43 #define FN_NAME_SET_2(a, b)             {#a, #b}
44 #define FN_NAME_SET_3(a, b, c)          {#a, #b, #c}
45 #define FN_NAME_SET_4(a, b, c, d)       {#a, #b, #c, #d}
46 #define FN_NAME_SET_5(a, b, c, d, e)    {#a, #b, #c, #d, #e}
47 
48 #define FN_NAME_SET2(n) FN_NAME_SET_##n
49 #define FN_NAME_SET(n, ...) FN_NAME_SET2(n)(__VA_ARGS__)
50 
51 #define UNITY_TEST_FN_SET(...)  \
52     static test_func UNITY_TEST_UID(test_functions)[] = {__VA_ARGS__}; \
53     static const char* UNITY_TEST_UID(test_fn_name)[] = FN_NAME_SET(PP_NARG(__VA_ARGS__), __VA_ARGS__)
54 
55 
56 typedef void (* test_func)(void);
57 
58 typedef struct test_desc_t
59 {
60     const char* name;
61     const char* desc;
62     test_func* fn;
63     const char* file;
64     int line;
65     uint8_t test_fn_count;
66     const char ** test_fn_name;
67     struct test_desc_t* next;
68 } test_desc_t;
69 
70 void unity_testcase_register(test_desc_t* desc);
71 
72 
73 /*  Test case macro, a-la CATCH framework.
74     First argument is a free-form description,
75     second argument is (by convention) a list of identifiers, each one in square brackets.
76     Identifiers are used to group related tests, or tests with specific properties.
77     Use like:
78 
79     TEST_CASE("Frobnicator forbnicates", "[frobnicator][rom]")
80     {
81         // test goes here
82     }
83 */
84 
85 #define TEST_CASE(name_, desc_) \
86     static void UNITY_TEST_UID(test_func_) (void); \
87     static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) (void) \
88     { \
89         static test_func test_fn_[] = {&UNITY_TEST_UID(test_func_)}; \
90         static test_desc_t UNITY_TEST_UID(test_desc_) = { \
91             .name = name_, \
92             .desc = desc_, \
93             .fn = test_fn_, \
94             .file = __FILE__, \
95             .line = __LINE__, \
96             .test_fn_count = 1, \
97             .test_fn_name = NULL, \
98             .next = NULL \
99         }; \
100         unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
101     }\
102     static void UNITY_TEST_UID(test_func_) (void)
103 
104 
105 /*
106  * Multiple stages test cases will handle the case that test steps are separated by DUT reset.
107  * e.g: we want to verify some function after SW reset, WDT reset or deep sleep reset.
108  *
109  * First argument is a free-form description,
110  * second argument is (by convention) a list of identifiers, each one in square brackets.
111  * subsequent arguments are names test functions separated by reset.
112  * e.g:
113  * TEST_CASE_MULTIPLE_STAGES("run light sleep after deep sleep","[sleep]", goto_deepsleep, light_sleep_after_deep_sleep_wakeup);
114  * */
115 
116 #define TEST_CASE_MULTIPLE_STAGES(name_, desc_, ...) \
117     UNITY_TEST_FN_SET(__VA_ARGS__); \
118     static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) (void) \
119     { \
120         static test_desc_t UNITY_TEST_UID(test_desc_) = { \
121             .name = name_, \
122             .desc = desc_"[multi_stage]", \
123             .fn = UNITY_TEST_UID(test_functions), \
124             .file = __FILE__, \
125             .line = __LINE__, \
126             .test_fn_count = PP_NARG(__VA_ARGS__), \
127             .test_fn_name = UNITY_TEST_UID(test_fn_name), \
128             .next = NULL \
129         }; \
130         unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
131     }
132 
133 
134 
135 /*
136  * First argument is a free-form description,
137  * second argument is (by convention) a list of identifiers, each one in square brackets.
138  * subsequent arguments are names of test functions for different DUTs
139  * e.g:
140  * TEST_CASE_MULTIPLE_DEVICES("master and slave spi","[spi][test_env=UT_T2_1]", master_test, slave_test);
141  * */
142 
143 #define TEST_CASE_MULTIPLE_DEVICES(name_, desc_, ...) \
144     UNITY_TEST_FN_SET(__VA_ARGS__); \
145     static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) (void) \
146     { \
147         static test_desc_t UNITY_TEST_UID(test_desc_) = { \
148             .name = name_, \
149             .desc = desc_"[multi_device]", \
150             .fn = UNITY_TEST_UID(test_functions), \
151             .file = __FILE__, \
152             .line = __LINE__, \
153             .test_fn_count = PP_NARG(__VA_ARGS__), \
154             .test_fn_name = UNITY_TEST_UID(test_fn_name), \
155             .next = NULL \
156         }; \
157         unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
158     }
159 
160 /**
161  * Note: initialization of test_desc_t fields above has to be done exactly
162  * in the same order as the fields are declared in the structure.
163  * Otherwise the initializer will not be valid in C++ (which doesn't
164  * support designated initializers). G++ can parse the syntax, but
165  * field names are treated as annotations and don't affect initialization
166  * order. Also make sure all the fields are initialized.
167  */
168 
169 void unity_run_test_by_name(const char *name);
170 
171 void unity_run_tests_by_tag(const char *tag, bool invert);
172 
173 void unity_run_all_tests(void);
174 
175 void unity_run_menu(void);
176 
177 #include "sdkconfig.h" //to get IDF_TARGET_xxx
178 
179 #define CONFIG_IDF_TARGET_NA   0
180 
181 /*
182  * This macro is to disable those tests and their callees that cannot be built or run temporarily
183  * (needs update or runners).
184  *
185  * Usage:
186  * ```
187  * #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S2)
188  * TEST_CASE("only for esp32", "")
189  * {
190  * }
191  * #endif
192  * ```
193  */
194 #define TEMPORARY_DISABLED_FOR_TARGETS(...)    (_UNITY_DFT_10(__VA_ARGS__, NA, NA, NA, NA, NA, NA, NA, NA, NA))
195 
196 /*
197  * This macro is to disable those tests and their callees that is totally impossible to run on the
198  * specific targets. Usage same as TEMPORARY_DISABLED_FOR_TARGETS.
199  */
200 #define DISABLED_FOR_TARGETS(...)    TEMPORARY_DISABLED_FOR_TARGETS(__VA_ARGS__)
201 
202 #define _UNITY_DFT_10(TARGET, ...)  (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_9(__VA_ARGS__))
203 #define _UNITY_DFT_9(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_8(__VA_ARGS__))
204 #define _UNITY_DFT_8(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_7(__VA_ARGS__))
205 #define _UNITY_DFT_7(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_6(__VA_ARGS__))
206 #define _UNITY_DFT_6(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_5(__VA_ARGS__))
207 #define _UNITY_DFT_5(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_4(__VA_ARGS__))
208 #define _UNITY_DFT_4(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_3(__VA_ARGS__))
209 #define _UNITY_DFT_3(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_2(__VA_ARGS__))
210 #define _UNITY_DFT_2(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_1(__VA_ARGS__))
211 #define _UNITY_DFT_1(TARGET, ...)   (CONFIG_IDF_TARGET_##TARGET)
212