diff options
author | pommicket <pommicket@gmail.com> | 2025-09-14 22:08:22 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-14 22:08:22 -0400 |
commit | 969bf523b7039c7038b66edde5776278c27941ac (patch) | |
tree | 89a9754ff69046c813f238bff424be7cec722cdb | |
parent | 0302aca4e1aa3eb495970af40a63f8bf4cf95779 (diff) |
Start tests
-rw-r--r-- | meson.build | 4 | ||||
-rw-r--r-- | pom.c | 5 | ||||
-rw-r--r-- | tests/main.c | 26 | ||||
-rw-r--r-- | tests/parsing.c | 68 | ||||
-rw-r--r-- | tests/test.h | 17 |
5 files changed, 116 insertions, 4 deletions
diff --git a/meson.build b/meson.build index 3a09963..5fafc31 100644 --- a/meson.build +++ b/meson.build @@ -2,4 +2,6 @@ project('pom', 'c', # Note: Only C99 is strictly needed default_options: ['c_std=c23']) library('pom', 'pom.c') -static_library('pom', 'pom.c') +static_lib = static_library('pom', 'pom.c') +tests = executable('tests', 'tests/main.c', 'tests/parsing.c', link_with: [static_lib]) +test('tests', tests) @@ -266,15 +266,14 @@ make_error(const char *file, uint64_t line, enum error_id id, ...) { err->message = message; char *string = strchr(message, '\0') + 1; // no, GCC, string will not overlap with message. - #if __GNUC__ >= 4 + #if !__clang__ && __GNUC__ >= 4 #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wunknown-warning-option" #pragma GCC diagnostic ignored "-Wrestrict" #endif sprintf(string, "%s\n%s:%" PRIu64 ": %s\n", get_error_message(ERROR_HEADER), file, line, message); - #if __GNUC__ >= 4 + #if !__clang__ && __GNUC__ >= 4 #pragma GCC diagnostic pop #endif err->string = string; diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..7b791cd --- /dev/null +++ b/tests/main.c @@ -0,0 +1,26 @@ +#include "test.h" + +#include <stdarg.h> +#include <stdio.h> + +static bool any_failure = false; + +void test_fail(const char *fmt, ...) { + any_failure = true; + fprintf(stderr, "\x1b[1m\x1b[91mFailure:\x1b[0m "); + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); +} + +int main(void) { + const char *test_dir = "../tests"; + test_parsing(test_dir); + if (any_failure) { + fprintf(stderr, "\x1b[1m\x1b[91mSome tests failed.\x1b[0m\n"); + } else { + printf("\x1b[1m\x1b[92mAll tests OK\x1b[0m\n"); + } +} diff --git a/tests/parsing.c b/tests/parsing.c new file mode 100644 index 0000000..2b953db --- /dev/null +++ b/tests/parsing.c @@ -0,0 +1,68 @@ +#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", test_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(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(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); + } + } + 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); + } + } + pom_conf_free(conf); + pom_conf_free(flat); + free(conf_path); + free(flat_path); + } + } + free(parsing_dir); +// pom_conf *conf = pom_load_path("../tests"); +} diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..5e0b8e5 --- /dev/null +++ b/tests/test.h @@ -0,0 +1,17 @@ +#include <pom.h> + + +#if __GNUC__ >= 6 +#define ATTRIBUTE_PRINTF(fmt, args) __attribute__ ((format(printf, fmt, args))) +#else +#define ATTRIBUTE_PRINTF(fmt, args) +#endif + +#if _MSC_VER >= 1600 +#define PRINTF_FORMAT_STRING _Printf_format_string_ +#else +#define PRINTF_FORMAT_STRING +#endif + +void test_fail(PRINTF_FORMAT_STRING const char *, ...) ATTRIBUTE_PRINTF(1, 2); +void test_parsing(const char *test_dir); |