diff options
author | pommicket <pommicket@gmail.com> | 2025-09-15 17:16:48 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-15 17:16:48 -0400 |
commit | 2b519f42fec913763abd0d967301916e763e770a (patch) | |
tree | c7268e64d6c0f3d7db7ac7134d20398a54ff8e36 | |
parent | dc989291eb6b9b20a25a78f35d6f04968a88ed0b (diff) |
Better testing framework
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | tests/errors.c | 43 | ||||
-rw-r--r-- | tests/location.c | 103 | ||||
-rw-r--r-- | tests/main.c | 47 | ||||
-rw-r--r-- | tests/parsing.c | 99 | ||||
-rw-r--r-- | tests/test.h | 9 |
7 files changed, 152 insertions, 157 deletions
@@ -1,6 +1,6 @@ /doc -/debug -/release +/Debug +/Release /.cache build* compile_commands.json @@ -1,8 +1,8 @@ -PROFILE ?= release +PROFILE ?= Release BUILD_DIR ?= $(PROFILE) __build: mkdir -p $(BUILD_DIR) - P=`pwd` && cd $(BUILD_DIR) && cmake $$P + P=`pwd` && cd $(BUILD_DIR) && cmake -DCMAKE_BUILD_TYPE=$(PROFILE) -DCMAKE_EXPORT_COMPILE_COMMANDS=1 $$P make -j16 -C $(BUILD_DIR) .PHONY: __build diff --git a/tests/errors.c b/tests/errors.c index d1dec96..1411d0e 100644 --- a/tests/errors.c +++ b/tests/errors.c @@ -1,39 +1,24 @@ #include "test.h" -#include <dirent.h> #include <stdlib.h> #include <string.h> -void test_errors(const char *test_dir) { - char *errors_dir = malloc(strlen(test_dir) + 30); - sprintf(errors_dir, "%s/errors", test_dir); - DIR *dir = opendir(errors_dir); - if (!dir) { - test_fail("Couldn't open test directory %s", errors_dir); +void test_errors(void) { + char **listing = list_dir("errors", ".pom"); + if (!listing) return; - } - struct dirent *ent; - while ((ent = readdir(dir))) { - const char *name = ent->d_name; - if (strlen(name) >= strlen("x.pom") && - strcmp(name + strlen(name) - strlen(".pom"), ".pom") == 0) { - printf("Testing %s...\n",name); - char *conf_path = malloc(strlen(errors_dir) + strlen(name) + 30); - sprintf(conf_path, "%s/errors/%s", test_dir, name); - pom_error *error; - pom_conf *conf = pom_load_path(NULL, conf_path, &error); - if (error) { - free(error); - free(conf_path); - continue; - } - test_fail("Parsing %s didn't produce an error but it should have.", - conf_path); - pom_conf_free(conf); - free(conf_path); + for (size_t i = 0; listing[i]; i++) { + const char *conf_path = listing[i]; + printf("Testing %s...\n",conf_path); + pom_error *error; + pom_conf *conf = pom_load_path(NULL, conf_path, &error); + if (error) { + free(error); + continue; } + test_fail("Parsing %s didn't produce an error but it should have.", conf_path); + pom_conf_free(conf); } - closedir(dir); - free(errors_dir); + free_listing(listing); // pom_conf *conf = pom_load_path("../tests"); } 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); } diff --git a/tests/main.c b/tests/main.c index 2b2bfae..c34a79d 100644 --- a/tests/main.c +++ b/tests/main.c @@ -17,15 +17,54 @@ void test_fail(const char *fmt, ...) { fprintf(stderr, "\n"); } +static const char *test_dir; + +#include <dirent.h> +char **list_dir(const char *dir_name, const char *suffix) { + char *dir_path = malloc(strlen(test_dir) + strlen(dir_name) + 8); + sprintf(dir_path, "%s/%s", test_dir, dir_name); + DIR *dir = opendir(dir_path); + if (!dir) { + test_fail("Couldn't open test directory %s", dir_path); + return NULL; + } + struct dirent *ent; + size_t entries = 0; + while ((ent = readdir(dir))) entries++; + rewinddir(dir); + char **listing = calloc(entries + 1, sizeof (char *)); + size_t i = 0; + while ((ent = readdir(dir))) { + const char *name = ent->d_name; + if (strlen(name) < strlen(suffix) || strcmp(name+strlen(name)-strlen(suffix), suffix) != 0 + || i >= entries) { + continue; + } + char *path = malloc(strlen(dir_path) + strlen(name) + 8); + sprintf(path, "%s/%s", dir_path, name); + listing[i++] = path; + } + closedir(dir); + free(dir_path); + return listing; +} + +void free_listing(char **listing) { + for (size_t i = 0; listing[i]; i++) { + free(listing[i]); + } + free(listing); +} + int main(int argc, char **argv) { if (argc > 2 || (argc == 2 && strcmp(argv[1], "--help") == 0)) { printf("usage: tests [TEST DIRECTORY]\n"); return EXIT_FAILURE; } - const char *test_dir = argc == 2 ? argv[1] : "../tests"; - test_parsing(test_dir); - test_errors(test_dir); - test_location(test_dir); + test_dir = argc == 2 ? argv[1] : "../tests"; + test_parsing(); + test_errors(); + test_location(); 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 c28df0d..670361a 100644 --- a/tests/parsing.c +++ b/tests/parsing.c @@ -1,68 +1,53 @@ #include "test.h" -#include <dirent.h> #include <stdlib.h> #include <string.h> -void test_parsing(const char *test_dir) { - char *parsing_dir = malloc(strlen(test_dir) + 30); - sprintf(parsing_dir, "%s/parsing", test_dir); - DIR *dir = opendir(parsing_dir); - if (!dir) { - test_fail("Couldn't open test directory %s", parsing_dir); - return; - } - struct dirent *ent; - while ((ent = readdir(dir))) { - const char *name = ent->d_name; - if (strlen(name) >= strlen("x.flat.pom") && - strcmp(name + strlen(name) - strlen(".flat.pom"), ".flat.pom") == 0) { - printf("Testing %s...\n",name); - char *conf_path = malloc(strlen(parsing_dir) + strlen(name) + 30); - char *flat_path = malloc(strlen(parsing_dir) + strlen(name) + 30); - sprintf(conf_path, "%s/parsing/%.*s.pom", test_dir, - (int)(strlen(name) - strlen(".flat.pom")), name); - sprintf(flat_path, "%s/parsing/%s", test_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; - } - pom_conf *flat = pom_load_path(NULL, flat_path, &error); - if (error) { - test_fail("Failed to parse %s\n%s", flat_path, - pom_error_to_string(error)); - continue; +void test_parsing(void) { + char **listing = list_dir("parsing", ".flat.pom"); + for (size_t i = 0; listing[i]; i++) { + const char *flat_path = listing[i]; + printf("Testing %s...\n",flat_path); + char *conf_path = malloc(strlen(flat_path)); + sprintf(conf_path, "%.*s.pom", + (int)(strlen(flat_path) - strlen(".flat.pom")), flat_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 *flat = pom_load_path(NULL, flat_path, &error); + if (error) { + test_fail("Failed to parse %s\n%s", flat_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(flat, item->key)) { + test_fail("%s has key '%s' but %s doesn't", + conf_path, item->key, flat_path); } - const pom_item *item; - pom_item_iter *iter = NULL; - while ((item = pom_conf_next_item(conf, &iter))) { - if (!pom_conf_has(flat, item->key)) { - test_fail("%s has key '%s' but %s doesn't", - conf_path, item->key, flat_path); - } + } + while ((item = pom_conf_next_item(flat, &iter))) { + const char *conf_value = pom_conf_get(conf, item->key); + if (!conf_value) { + test_fail("%s has key '%s' but %s doesn't", + flat_path, item->key, conf_path); } - while ((item = pom_conf_next_item(flat, &iter))) { - const char *conf_value = pom_conf_get(conf, item->key); - if (!conf_value) { - test_fail("%s has key '%s' but %s doesn't", - flat_path, item->key, conf_path); - } - if (strcmp(conf_value, item->value) != 0) { - test_fail("Mismatch between %s and %s at key '%s'\n" - "expected: %s\n" - " got: %s\n", - flat_path, conf_path, item->key, item->value, conf_value); - } + if (strcmp(conf_value, item->value) != 0) { + test_fail("Mismatch between %s and %s at key '%s'\n" + "expected: %s\n" + " got: %s\n", + flat_path, conf_path, item->key, item->value, conf_value); } - pom_conf_free(conf); - pom_conf_free(flat); - free(conf_path); - free(flat_path); } + pom_conf_free(conf); + pom_conf_free(flat); + free(conf_path); } - closedir(dir); - free(parsing_dir); + free_listing(listing); } diff --git a/tests/test.h b/tests/test.h index e4123bf..59807d1 100644 --- a/tests/test.h +++ b/tests/test.h @@ -1,6 +1,5 @@ #include <pom.h> - #if __GNUC__ >= 6 #define ATTRIBUTE_PRINTF(fmt, args) __attribute__ ((format(printf, fmt, args))) #else @@ -14,6 +13,8 @@ #endif 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); +void test_parsing(void); +void test_errors(void); +void test_location(void); +char **list_dir(const char *dir, const char *suffix); +void free_listing(char **listing); |