diff options
Diffstat (limited to 'tests/location.c')
-rw-r--r-- | tests/location.c | 103 |
1 files changed, 44 insertions, 59 deletions
diff --git a/tests/location.c b/tests/location.c index 372d506..3aa975f 100644 --- a/tests/location.c +++ b/tests/location.c @@ -1,72 +1,57 @@ #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(NULL, conf_path, &error); - if (error) { - test_fail("Failed to parse %s\n%s", conf_path, - pom_error_to_string(error)); - continue; +void test_location(void) { + char **listing = list_dir("location", ".locations.pom"); + for (size_t i = 0; listing[i]; i++) { + const char *loc_path = listing[i]; + printf("Testing %s...\n",loc_path); + char *conf_path = malloc(strlen(loc_path)); + sprintf(conf_path, "%.*s.pom", + (int)(strlen(loc_path) - strlen(".locations.pom")), loc_path); + pom_error *error; + pom_conf *conf = pom_load_path(NULL, 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(NULL, 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); } - pom_conf *loc = pom_load_path(NULL, loc_path, &error); - if (error) { - test_fail("Failed to parse %s\n%s", loc_path, - pom_error_to_string(error)); + } + 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; } - 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); - } + if (strcmp(file, conf_path) != 0) { + test_fail("Incorrect file name in location of '%s' (expected %s, got %s)", + item->key, conf_path, file); } - 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); - } + 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); } + pom_conf_free(conf); + pom_conf_free(loc); + free(conf_path); } - closedir(dir); - free(location_dir); + free_listing(listing); } |