tests: rework test macros to not take code as parameters
C macros are nasty. We use them, but we try to be conservative with them. In particular passing literal, complex code blocks as argument is icky, because of "," handling of C, and also because it's quite a challange for most code highlighters and similar. Hence, let's avoid that. Using macros for genreating functions is OK but if so, the parameters should be simple words, not full code blocks. hence, rework DEFINE_CUSTOM_TEST_MAIN() to take a function name instead of code block as argument. As side-effect this also fixes a bunch of cases where we might end up returning a negative value from main(). Some uses of DEFINE_CUSTOM_TEST_MAIN() inserted local variables into the main() functions, these are replaced by static variables, and their destructors by the static destructor logic. This doesn't fix any bugs or so, it's just supposed to make the code easier to work with and improve it easthetically. Or in other words: let's use macros where it really makes sense, but let's not go overboard with it. (And yes, FOREACH_DIRENT() is another one of those macros that take code, and I dislike that too and regret I ever added that.)
This commit is contained in:
committed by
Yu Watanabe
parent
faa1b3c6c4
commit
99839c7ebd
+19
-6
@@ -6,6 +6,7 @@
|
||||
#include "sd-daemon.h"
|
||||
|
||||
#include "macro.h"
|
||||
#include "static-destruct.h"
|
||||
#include "util.h"
|
||||
|
||||
static inline bool manager_errno_skip_test(int r) {
|
||||
@@ -109,15 +110,27 @@ static inline int run_test_table(void) {
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline int test_nop(void) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
#define DEFINE_CUSTOM_TEST_MAIN(log_level, intro, outro) \
|
||||
int main(int argc, char *argv[]) { \
|
||||
int _r = EXIT_SUCCESS; \
|
||||
int _r, _q; \
|
||||
test_setup_logging(log_level); \
|
||||
save_argc_argv(argc, argv); \
|
||||
intro; \
|
||||
_r = run_test_table(); \
|
||||
outro; \
|
||||
return _r; \
|
||||
_r = intro(); \
|
||||
if (_r == EXIT_SUCCESS) \
|
||||
_r = run_test_table(); \
|
||||
_q = outro(); \
|
||||
static_destruct(); \
|
||||
if (_r < 0) \
|
||||
return EXIT_FAILURE; \
|
||||
if (_r != EXIT_SUCCESS) \
|
||||
return _r; \
|
||||
if (_q < 0) \
|
||||
return EXIT_FAILURE; \
|
||||
return _q; \
|
||||
}
|
||||
|
||||
#define DEFINE_TEST_MAIN(log_level) DEFINE_CUSTOM_TEST_MAIN(log_level, , )
|
||||
#define DEFINE_TEST_MAIN(log_level) DEFINE_CUSTOM_TEST_MAIN(log_level, test_nop, test_nop)
|
||||
|
||||
Reference in New Issue
Block a user