2023-11-24 17:27:44 +00:00
|
|
|
#ifndef _BOOTSTRAP_TEST_PARSER
|
|
|
|
#define _BOOTSTRAP_TEST_PARSER
|
2023-11-23 20:31:54 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "cJSON.h"
|
|
|
|
#include "config.h"
|
2023-11-24 16:09:18 +00:00
|
|
|
#include "parser.h"
|
2023-11-23 20:31:54 +00:00
|
|
|
#include "path.h"
|
|
|
|
#include "sput.h"
|
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
struct TestParserFixture {
|
|
|
|
char *cwd;
|
|
|
|
char *root_dir;
|
|
|
|
const char *target;
|
|
|
|
struct Config config;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct TestParserFixture *test_parser_setup(const char *target) {
|
2023-11-23 20:31:54 +00:00
|
|
|
char *cwd = getcwd(0, 0);
|
|
|
|
const char *segments[] = {cwd, "test", "specs"};
|
2023-11-24 16:09:18 +00:00
|
|
|
char *root_dir =
|
|
|
|
join_path_segments(sizeof(segments) / sizeof(char *), segments);
|
2023-11-23 20:31:54 +00:00
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
struct TestParserFixture *fixture = malloc(sizeof(struct TestParserFixture));
|
|
|
|
fixture->cwd = getcwd(0, 0);
|
|
|
|
fixture->root_dir = root_dir;
|
|
|
|
fixture->target = target;
|
|
|
|
|
|
|
|
// Reproduce in `Config` instance for convenience.
|
|
|
|
fixture->config.cwd = fixture->cwd;
|
|
|
|
fixture->config.root_dir = fixture->root_dir;
|
|
|
|
fixture->config.target = fixture->target;
|
|
|
|
|
|
|
|
return fixture;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_parser_teardown(struct TestParserFixture *fixture) {
|
|
|
|
free(fixture->cwd);
|
|
|
|
free(fixture->root_dir);
|
|
|
|
free(fixture);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
A missing `spec.json` file is not an error. Our parsed @cJSON instance should
|
|
|
|
be set to NULL in this case.
|
|
|
|
*/
|
2023-11-25 02:29:14 +00:00
|
|
|
static void test_parser_missing() {
|
2023-11-24 18:47:23 +00:00
|
|
|
struct TestParserFixture *fixture = test_parser_setup("no_spec_json");
|
2023-11-23 20:31:54 +00:00
|
|
|
|
|
|
|
cJSON *parsed = 0;
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = parse_spec_json(&fixture->config, &parsed);
|
|
|
|
sput_fail_unless(error == 0, "no spec.json, success");
|
2023-11-23 20:31:54 +00:00
|
|
|
sput_fail_unless(parsed == 0, "no spec.json, no parsed");
|
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
test_parser_teardown(fixture);
|
2023-11-23 20:31:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 02:29:14 +00:00
|
|
|
static void test_parser_minimal() {
|
2023-11-24 18:47:23 +00:00
|
|
|
struct TestParserFixture *fixture = test_parser_setup("minimal_spec_json");
|
2023-11-23 22:01:58 +00:00
|
|
|
|
|
|
|
cJSON *parsed = 0;
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = parse_spec_json(&fixture->config, &parsed);
|
|
|
|
sput_fail_unless(error == 0, "minimal spec.json, success");
|
2023-11-23 22:01:58 +00:00
|
|
|
sput_fail_unless(parsed != 0, "minimal spec.json, parsed");
|
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
test_parser_teardown(fixture);
|
2023-11-23 22:01:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 02:29:14 +00:00
|
|
|
static void test_parser_invalid() {
|
2023-11-24 18:47:23 +00:00
|
|
|
struct TestParserFixture *fixture = test_parser_setup("invalid_spec_json");
|
2023-11-23 22:01:58 +00:00
|
|
|
|
|
|
|
cJSON *parsed = 0;
|
2023-11-25 16:15:30 +00:00
|
|
|
struct Error *error = parse_spec_json(&fixture->config, &parsed);
|
2023-11-24 16:09:18 +00:00
|
|
|
sput_fail_unless(
|
2023-11-25 16:15:30 +00:00
|
|
|
error->code == ERROR_PARSER_SPEC_JSON_INVALID_SYNTAX,
|
|
|
|
"invalid spec.json, INVALID_SYNTAX"
|
2023-11-24 16:09:18 +00:00
|
|
|
);
|
2023-11-25 16:15:30 +00:00
|
|
|
error_free(error);
|
2023-11-23 22:01:58 +00:00
|
|
|
sput_fail_unless(parsed == 0, "invalid spec.json, not parsed");
|
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
test_parser_teardown(fixture);
|
2023-11-23 22:01:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 17:27:44 +00:00
|
|
|
#endif /* _BOOTSTRAP_TEST_PARSER */
|