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);
|
|
|
|
}
|
|
|
|
|
2023-11-25 02:29:14 +00:00
|
|
|
static void test_validator_toplevel_not_object() {
|
2023-11-24 19:23:52 +00:00
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup("[]");
|
|
|
|
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(
|
|
|
|
error->code == ERROR_VALIDATOR_TOP_LEVEL_NOT_OBJECT, "top-level not object"
|
|
|
|
);
|
|
|
|
error_free(error);
|
2023-11-24 17:27:44 +00:00
|
|
|
|
2023-11-24 19:23:52 +00:00
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
2023-11-25 02:29:14 +00:00
|
|
|
static void test_validator_field_not_object() {
|
2023-11-24 19:23:52 +00:00
|
|
|
struct TestValidatorFixture *fixture =
|
|
|
|
test_validator_setup("{\"key\": \"$UNKNOWN\"}");
|
|
|
|
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(
|
|
|
|
error->code == ERROR_VALIDATOR_FIELD_NOT_OBJECT, "field not object"
|
|
|
|
);
|
|
|
|
error_free(error);
|
2023-11-24 19:23:52 +00:00
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
2023-11-25 22:58:13 +00:00
|
|
|
static void test_validator_field_name_leading_digit() {
|
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup(
|
|
|
|
"{"
|
|
|
|
" \"1abc\": {"
|
|
|
|
" \"type\": \"text\""
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
|
|
|
|
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(
|
|
|
|
error->code == ERROR_VALIDATOR_FIELD_NAME_INVALID,
|
|
|
|
"field name leading digit"
|
|
|
|
);
|
|
|
|
error_free(error);
|
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_validator_field_name_non_alnum() {
|
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup(
|
|
|
|
"{"
|
|
|
|
" \"a~bc\": {"
|
|
|
|
" \"type\": \"text\""
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
|
|
|
|
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(
|
|
|
|
error->code == ERROR_VALIDATOR_FIELD_NAME_INVALID, "field name non alnum"
|
|
|
|
);
|
|
|
|
error_free(error);
|
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
2023-11-25 02:29:14 +00:00
|
|
|
static void test_validator_field_type_invalid() {
|
2023-11-25 02:35:22 +00:00
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup(
|
|
|
|
"{"
|
|
|
|
" \"key\": {"
|
|
|
|
" \"type\": 2"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
2023-11-25 02:29:14 +00:00
|
|
|
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(
|
|
|
|
error->code == ERROR_VALIDATOR_FIELD_TYPE_INVALID, "field type invalid"
|
|
|
|
);
|
|
|
|
error_free(error);
|
2023-11-25 02:29:14 +00:00
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_validator_field_type_unknown() {
|
2023-11-25 02:35:22 +00:00
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup(
|
|
|
|
"{"
|
|
|
|
" \"key\": {"
|
|
|
|
" \"type\": \"UNKNOWN\""
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
2023-11-25 02:29:14 +00:00
|
|
|
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(
|
|
|
|
error->code == ERROR_VALIDATOR_FIELD_TYPE_UNKNOWN, "field type unknown"
|
|
|
|
);
|
|
|
|
error_free(error);
|
2023-11-25 02:29:14 +00:00
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
2023-11-25 22:47:47 +00:00
|
|
|
static void test_validator_valid_type_ci() {
|
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup(
|
|
|
|
"{"
|
|
|
|
" \"key\": {"
|
|
|
|
" \"type\": \"tExT\","
|
|
|
|
" \"prompt\": \"What value for key?\""
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
|
|
|
|
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(error == 0, "valid");
|
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
2023-11-25 02:29:14 +00:00
|
|
|
static void test_validator_field_prompt_invalid() {
|
2023-11-25 02:35:22 +00:00
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup(
|
|
|
|
"{"
|
|
|
|
" \"key\": {"
|
2023-11-25 22:47:47 +00:00
|
|
|
" \"type\": \"text\","
|
2023-11-25 02:35:22 +00:00
|
|
|
" \"prompt\": 2"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
2023-11-25 02:29:14 +00:00
|
|
|
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(
|
|
|
|
error->code == ERROR_VALIDATOR_FIELD_PROMPT_INVALID, "field prompt invalid"
|
|
|
|
);
|
|
|
|
error_free(error);
|
2023-11-25 02:29:14 +00:00
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_validator_valid() {
|
2023-11-25 02:35:22 +00:00
|
|
|
struct TestValidatorFixture *fixture = test_validator_setup(
|
|
|
|
"{"
|
|
|
|
" \"key\": {"
|
2023-11-25 22:47:47 +00:00
|
|
|
" \"type\": \"text\","
|
2023-11-25 02:35:22 +00:00
|
|
|
" \"prompt\": \"What value for key?\""
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
2023-11-24 19:23:52 +00:00
|
|
|
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = validate_spec_json(fixture->parsed, &fixture->prompts);
|
|
|
|
sput_fail_unless(error == 0, "valid");
|
2023-11-24 19:23:52 +00:00
|
|
|
|
|
|
|
test_validator_teardown(fixture);
|
2023-11-24 17:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _BOOTSTRAP_TEST_VALIDATOR */
|