bootstrap/test/test_parser.h

84 lines
2.3 KiB
C
Raw Normal View History

2023-11-24 17:27:44 +00:00
#ifndef _BOOTSTRAP_TEST_PARSER
#define _BOOTSTRAP_TEST_PARSER
#include <unistd.h>
#include "cJSON.h"
#include "config.h"
2023-11-24 16:09:18 +00:00
#include "parser.h"
#include "path.h"
#include "sput.h"
struct TestParserFixture {
char *cwd;
char *root_dir;
const char *target;
struct Config config;
};
static struct TestParserFixture *test_parser_setup(const char *target) {
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);
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.
*/
static void test_parser_missing() {
struct TestParserFixture *fixture = test_parser_setup("no_spec_json");
cJSON *parsed = 0;
enum SpecParseError retval = parse_spec_json(&fixture->config, &parsed);
sput_fail_unless(retval == 0, "no spec.json, success");
sput_fail_unless(parsed == 0, "no spec.json, no parsed");
test_parser_teardown(fixture);
}
static void test_parser_minimal() {
struct TestParserFixture *fixture = test_parser_setup("minimal_spec_json");
2023-11-23 22:01:58 +00:00
cJSON *parsed = 0;
enum SpecParseError retval = parse_spec_json(&fixture->config, &parsed);
2023-11-23 22:01:58 +00:00
sput_fail_unless(retval == 0, "minimal spec.json, success");
sput_fail_unless(parsed != 0, "minimal spec.json, parsed");
test_parser_teardown(fixture);
2023-11-23 22:01:58 +00:00
}
static void test_parser_invalid() {
struct TestParserFixture *fixture = test_parser_setup("invalid_spec_json");
2023-11-23 22:01:58 +00:00
cJSON *parsed = 0;
enum SpecParseError retval = parse_spec_json(&fixture->config, &parsed);
2023-11-24 16:09:18 +00:00
sput_fail_unless(
retval == SPE_INVALID_SYNTAX, "invalid spec.json, INVALID_SYNTAX"
);
2023-11-23 22:01:58 +00:00
sput_fail_unless(parsed == 0, "invalid spec.json, not parsed");
test_parser_teardown(fixture);
2023-11-23 22:01:58 +00:00
}
2023-11-24 17:27:44 +00:00
#endif /* _BOOTSTRAP_TEST_PARSER */