diff options
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | pom.c | 15 | ||||
-rw-r--r-- | tests/errors.c | 1 | ||||
-rw-r--r-- | tests/location.c | 72 | ||||
-rw-r--r-- | tests/main.c | 1 | ||||
-rw-r--r-- | tests/parsing.c | 1 | ||||
-rw-r--r-- | tests/test.h | 1 |
7 files changed, 91 insertions, 2 deletions
diff --git a/meson.build b/meson.build index 766b96a..ab5080b 100644 --- a/meson.build +++ b/meson.build @@ -4,5 +4,5 @@ project('pom', 'c', library('pom', 'pom.c') static_lib = static_library('pom', 'pom.c') tests = executable('tests', 'tests/main.c', 'tests/parsing.c', - 'tests/errors.c', link_with: [static_lib]) + 'tests/errors.c', 'tests/location.c', link_with: [static_lib]) test('tests', tests) @@ -799,6 +799,7 @@ parse_line(struct parser *parser) { check_if_key_is_valid(parser, current_section); return; } + uint64_t start_line_number = parser->line_number; size_t equals_idx; for (size_t i = 0; ; i++) { if (line[i] == '=') { @@ -847,7 +848,7 @@ parse_line(struct parser *parser) { if (!item) return; item->key = key_idx; item->value = value_idx; - item->line = parser->line_number; + item->line = start_line_number; } static void @@ -1385,11 +1386,23 @@ pom_conf_has(const pom_conf *conf, const char *key) { bool pom_conf_location(const pom_conf *conf, const char *key, const char **file, uint64_t *line) { struct conf_item *item = conf_get_item(conf, key); + bool found; + size_t i; if (item) { if (file) *file = item->file; if (line) *line = item->line; return true; + } else if ((i = conf_binary_search_sections(conf, key, 0, &found)), found) { + // pick an item from this section + const struct conf_section *section = &conf->sections[i]; + if (section->conf.items_count == 0) + goto fail; + const struct conf_item *item = §ion->conf.items[0]; + if (file) *file = item->file; + if (line) *line = item->line; + return true; } else { + fail: if (file) *file = NULL; if (line) *line = 0; return false; diff --git a/tests/errors.c b/tests/errors.c index 8a255ab..2bcf14c 100644 --- a/tests/errors.c +++ b/tests/errors.c @@ -33,6 +33,7 @@ void test_errors(const char *test_dir) { free(conf_path); } } + closedir(dir); free(errors_dir); // pom_conf *conf = pom_load_path("../tests"); } diff --git a/tests/location.c b/tests/location.c new file mode 100644 index 0000000..5694f78 --- /dev/null +++ b/tests/location.c @@ -0,0 +1,72 @@ +#include "test.h" + +#include <dirent.h> +#include <stdlib.h> +#include <string.h> + +void test_location(const char *test_dir) { + char *location_dir = malloc(strlen(test_dir) + 30); + sprintf(location_dir, "%s/location", test_dir); + DIR *dir = opendir(location_dir); + if (!dir) { + test_fail("Couldn't open test directory %s", location_dir); + return; + } + struct dirent *ent; + while ((ent = readdir(dir))) { + const char *name = ent->d_name; + if (strlen(name) >= strlen("x.locations.pom") && + strcmp(name + strlen(name) - strlen(".locations.pom"), ".locations.pom") == 0) { + printf("Testing %s...\n",name); + char *conf_path = malloc(strlen(location_dir) + strlen(name) + 30); + char *loc_path = malloc(strlen(location_dir) + strlen(name) + 30); + sprintf(conf_path, "%s/%.*s.pom", location_dir, + (int)(strlen(name) - strlen(".locations.pom")), name); + sprintf(loc_path, "%s/%s", location_dir, name); + pom_error *error; + pom_conf *conf = pom_load_path(conf_path, &error); + if (error) { + test_fail("Failed to parse %s\n%s", conf_path, + pom_error_to_string(error)); + continue; + } + pom_conf *loc = pom_load_path(loc_path, &error); + if (error) { + test_fail("Failed to parse %s\n%s", loc_path, + pom_error_to_string(error)); + continue; + } + const pom_item *item; + pom_item_iter *iter = NULL; + while ((item = pom_conf_next_item(conf, &iter))) { + if (!pom_conf_has(loc, item->key)) { + test_fail("%s has key '%s' but %s doesn't", + conf_path, item->key, loc_path); + } + } + while ((item = pom_conf_next_item(loc, &iter))) { + const char *file; + uint64_t line; + if (!pom_conf_location(conf, item->key, &file, &line)) { + test_fail("%s has key '%s' but %s doesn't", + loc_path, item->key, conf_path); + continue; + } + if (strcmp(file, conf_path) != 0) { + test_fail("Incorrect file name in location of '%s' (expected %s, got %s)", + item->key, conf_path, file); + } + if ((uint64_t)atol(item->value) != line) { + test_fail("Incorrect line number in location of '%s' (expected %ld, got %ld)", + item->key, atol(item->value), (long)line); + } + } + pom_conf_free(conf); + pom_conf_free(loc); + free(conf_path); + free(loc_path); + } + } + closedir(dir); + free(location_dir); +} diff --git a/tests/main.c b/tests/main.c index ff68df3..2b2bfae 100644 --- a/tests/main.c +++ b/tests/main.c @@ -25,6 +25,7 @@ int main(int argc, char **argv) { const char *test_dir = argc == 2 ? argv[1] : "../tests"; test_parsing(test_dir); test_errors(test_dir); + test_location(test_dir); if (any_failure) { fprintf(stderr, "\x1b[1m\x1b[91mSome tests failed.\x1b[0m\n"); return EXIT_FAILURE; diff --git a/tests/parsing.c b/tests/parsing.c index 3c321c6..f34107e 100644 --- a/tests/parsing.c +++ b/tests/parsing.c @@ -63,5 +63,6 @@ void test_parsing(const char *test_dir) { free(flat_path); } } + closedir(dir); free(parsing_dir); } diff --git a/tests/test.h b/tests/test.h index cd19482..e4123bf 100644 --- a/tests/test.h +++ b/tests/test.h @@ -16,3 +16,4 @@ void test_fail(PRINTF_FORMAT_STRING const char *, ...) ATTRIBUTE_PRINTF(1, 2); void test_parsing(const char *test_dir); void test_errors(const char *test_dir); +void test_location(const char *test_dir); |