diff --git a/src/util-munit.c b/src/util-munit.c index 5405dcb..696ee1d 100644 --- a/src/util-munit.c +++ b/src/util-munit.c @@ -34,23 +34,20 @@ * __start and __stop point to the start and end of that section. See the * __attribute__(section) documentation. */ -extern const struct test_function __start_test_functions_section, __stop_test_functions_section; +DECLARE_TEST_SECTION(); int munit_tests_run(int argc, char **argv) { size_t count = 1; /* For NULL-terminated entry */ - for (const struct test_function *t = &__start_test_functions_section; - t < &__stop_test_functions_section; - t++) + foreach_test(t) { count++; + } _cleanup_free_ MunitTest *tests = calloc(count, sizeof(*tests)); size_t idx = 0; - for (const struct test_function *t = &__start_test_functions_section; - t < &__stop_test_functions_section; - t++) { + foreach_test(t) { MunitTest test = { .name = xaprintf("%s", t->name), .test = t->func, diff --git a/src/util-munit.h b/src/util-munit.h index 003a949..3d5de85 100644 --- a/src/util-munit.h +++ b/src/util-munit.h @@ -41,6 +41,20 @@ #include +/** + * Put at the top of the file somewhere, declares the start/stop for the test section we need. + */ +#define DECLARE_TEST_SECTION() \ + extern const struct test_function __start_test_functions_section, __stop_test_functions_section + +/** + * Helper to loop through each test. + */ +#define foreach_test(t_) \ + for (const struct test_function *t_ = &__start_test_functions_section; \ + t_ < &__stop_test_functions_section; \ + t_++) + typedef MunitResult (*munit_test_func_t)(const MunitParameter params[], void *user_data); struct test_function {