From a7c489980e71484e0dcfed6d406c99bc011e998e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 29 Dec 2021 19:03:36 +0100 Subject: test: config: add test_enum() utility function --- tests/test-config.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests') diff --git a/tests/test-config.c b/tests/test-config.c index 9c69c054..892b3915 100644 --- a/tests/test-config.c +++ b/tests/test-config.c @@ -337,6 +337,33 @@ test_spawn_template(struct context *ctx, bool (*parse_fun)(struct context *ctx), } } +static void +test_enum(struct context *ctx, bool (*parse_fun)(struct context *ctx), + const char *key, size_t count, const char *enum_strings[static count], + int enum_values[static count], int *ptr) +{ + ctx->key = key; + + for (size_t i = 0; i < count; i++) { + ctx->value = enum_strings[i]; + if (!parse_fun(ctx)) { + BUG("[%s].%s=%s: failed to parse", + ctx->section, ctx->key, ctx->value); + } + + if (*ptr != enum_values[i]) { + BUG("[%s].%s=%s: set value not the expected one: expected %d, got %d", + ctx->section, ctx->key, ctx->value, enum_values[i], *ptr); + } + } + + ctx->value = "invalid-enum-value"; + if (parse_fun(ctx)) { + BUG("[%s].%s=%s: did not fail to parse as expeced", + ctx->section, ctx->key, ctx->value); + } +} + static void test_section_main(void) { -- cgit v1.3