test: config: add test_color() utility function

This commit is contained in:
Daniel Eklöf 2021-12-29 19:43:01 +01:00
parent a780a8b66f
commit 7938b9e811
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -364,6 +364,40 @@ test_enum(struct context *ctx, bool (*parse_fun)(struct context *ctx),
}
}
static void
test_color(struct context *ctx, bool (*parse_fun)(struct context *ctx),
const char *key, bool alpha_allowed, uint32_t *ptr)
{
ctx->key = key;
const struct {
const char *option_string;
uint32_t color;
bool invalid;
} input[] = {
{"000000", 0},
{"ffffff", 0xffffff},
{"ffffffff", 0xffffffff, !alpha_allowed},
{"unittest-invalid-color", 0, true},
};
for (size_t i = 0; i < ALEN(input); i++) {
ctx->value = input[i].option_string;
if (input[i].invalid) {
if (parse_fun(ctx)) {
BUG("[%s].%s=%s: did not fail to parse as expected",
ctx->section, ctx->key, ctx->value);
}
} else {
if (!parse_fun(ctx)) {
BUG("[%s].%s=%s: failed to parse",
ctx->section, ctx->key, ctx->value);
}
}
}
}
static void
test_section_main(void)
{