bootstrap/test/test_validator.h

113 lines
3.5 KiB
C
Raw Normal View History

2023-11-24 17:27:44 +00:00
#ifndef _BOOTSTRAP_TEST_VALIDATOR
#define _BOOTSTRAP_TEST_VALIDATOR
#include "dyn_array.h"
#include "sput.h"
#include "validator.h"
2023-11-24 19:23:52 +00:00
struct TestValidatorFixture {
const char *json;
struct DynArray *prompts;
cJSON *parsed;
};
static struct TestValidatorFixture *test_validator_setup(const char *json) {
struct TestValidatorFixture *fixture =
malloc(sizeof(struct TestValidatorFixture));
fixture->json = json;
fixture->prompts = 0;
fixture->parsed = cJSON_Parse(json);
return fixture;
}
static void test_validator_teardown(struct TestValidatorFixture *fixture) {
if (fixture->parsed) {
cJSON_Delete(fixture->parsed);
}
free(fixture);
}
static void test_validator_toplevel_not_object() {
2023-11-24 19:23:52 +00:00
struct TestValidatorFixture *fixture = test_validator_setup("[]");
enum SpecValidationError retval =
validate_spec_json(fixture->parsed, &fixture->prompts);
sput_fail_unless(retval == SVE_TOPLEVEL_NOT_OBJECT, "top-level not object");
2023-11-24 17:27:44 +00:00
2023-11-24 19:23:52 +00:00
test_validator_teardown(fixture);
}
static void test_validator_field_not_object() {
2023-11-24 19:23:52 +00:00
struct TestValidatorFixture *fixture =
test_validator_setup("{\"key\": \"$UNKNOWN\"}");
enum SpecValidationError retval =
validate_spec_json(fixture->parsed, &fixture->prompts);
sput_fail_unless(retval == SVE_FIELD_NOT_OBJECT, "field not object");
2023-11-24 19:23:52 +00:00
test_validator_teardown(fixture);
}
static void test_validator_field_type_invalid() {
struct TestValidatorFixture *fixture = test_validator_setup("{"
" \"key\": {"
" \"type\": 2"
" }"
"}");
enum SpecValidationError retval =
validate_spec_json(fixture->parsed, &fixture->prompts);
sput_fail_unless(retval == SVE_FIELD_TYPE_INVALID, "field type invalid");
test_validator_teardown(fixture);
}
static void test_validator_field_type_unknown() {
struct TestValidatorFixture *fixture =
test_validator_setup("{"
" \"key\": {"
" \"type\": \"UNKNOWN\""
" }"
"}");
enum SpecValidationError retval =
validate_spec_json(fixture->parsed, &fixture->prompts);
sput_fail_unless(retval == SVE_FIELD_TYPE_UNKNOWN, "field type unknown");
test_validator_teardown(fixture);
}
static void test_validator_field_prompt_invalid() {
struct TestValidatorFixture *fixture =
test_validator_setup("{"
" \"key\": {"
" \"type\": \"STRING\","
" \"prompt\": 2"
" }"
"}");
enum SpecValidationError retval =
validate_spec_json(fixture->parsed, &fixture->prompts);
sput_fail_unless(retval == SVE_FIELD_PROMPT_INVALID, "field prompt invalid");
test_validator_teardown(fixture);
}
static void test_validator_valid() {
2023-11-24 19:23:52 +00:00
struct TestValidatorFixture *fixture =
test_validator_setup("{"
" \"key\": {"
" \"type\": \"STRING\","
" \"prompt\": \"What value for key?\""
" }"
"}");
2023-11-24 19:23:52 +00:00
enum SpecValidationError retval =
validate_spec_json(fixture->parsed, &fixture->prompts);
sput_fail_unless(retval == 0, "valid");
test_validator_teardown(fixture);
2023-11-24 17:27:44 +00:00
}
#endif /* _BOOTSTRAP_TEST_VALIDATOR */