bootstrap/test/test_parser.h

85 lines
1.9 KiB
C
Raw Normal View History

#ifndef _BOOTSTRAP_TEST_LOADER
#define _BOOTSTRAP_TEST_LOADER
#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"
/*
A missing `spec.json` file is not an error. Our parsed @cJSON instance should
be set to NULL in this case.
*/
2023-11-24 16:09:18 +00:00
static void test_parse_spec_json_missing() {
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 Config config = {
cwd,
root_dir,
"no_spec_json",
};
cJSON *parsed = 0;
2023-11-24 16:09:18 +00:00
enum SpecParseError retval = parse_spec_json(&config, &parsed);
sput_fail_unless(retval == 0, "no spec.json, success");
sput_fail_unless(parsed == 0, "no spec.json, no parsed");
free(cwd);
free(root_dir);
}
2023-11-24 16:09:18 +00:00
static void test_parse_spec_json_minimal() {
2023-11-23 22:01:58 +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 22:01:58 +00:00
struct Config config = {
cwd,
root_dir,
"minimal_spec_json",
};
cJSON *parsed = 0;
2023-11-24 16:09:18 +00:00
enum SpecParseError retval = parse_spec_json(&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");
free(cwd);
free(root_dir);
}
2023-11-24 16:09:18 +00:00
static void test_parse_spec_json_invalid() {
2023-11-23 22:01:58 +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 22:01:58 +00:00
struct Config config = {
cwd,
root_dir,
"invalid_spec_json",
};
cJSON *parsed = 0;
2023-11-24 16:09:18 +00:00
enum SpecParseError retval = parse_spec_json(&config, &parsed);
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");
free(cwd);
free(root_dir);
}
#endif /* _BOOTSTRAP_TEST_LOADER */