diff options
author | EuAndreh <eu@euandre.org> | 2022-01-28 07:33:30 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-01-01 12:35:01 -0300 |
commit | 9f554a72b01705ebf6be66143c5e69b09b3c1372 (patch) | |
tree | fadf04038b2d5df32fd4b5bc93ba291269c322a0 /src/testing.c | |
download | pindaiba-9f554a72b01705ebf6be66143c5e69b09b3c1372.tar.gz pindaiba-9f554a72b01705ebf6be66143c5e69b09b3c1372.tar.xz |
Init project: copy files and skeletons from others
Diffstat (limited to 'src/testing.c')
-rw-r--r-- | src/testing.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/testing.c b/src/testing.c new file mode 100644 index 0000000..028e48f --- /dev/null +++ b/src/testing.c @@ -0,0 +1,114 @@ +#include "config.h" + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +#include "testing.h" + + +#define COLOUR_RESET "\033[0m" +#define COLOUR_GREEN "\033[0;32m" +#define COLOUR_YELLOW "\033[0;33m" + +static const char *const +ENVVAR_NAME = "NO_COLOR"; + + +static bool +show_colour(void) { + const char *const no_colour = getenv(ENVVAR_NAME); + return !no_colour || no_colour[0] == '\0'; +} + + +/** + * @tags infallible + */ +void +test_start(const char *const name) { + (void)fprintf(stderr, "%s:\n", name); + + return; +} + + +/** + * @tags infallible + */ +void +testing(const char *const message) { + if (show_colour()) { + (void)fprintf( + stderr, + COLOUR_YELLOW "testing" COLOUR_RESET ": %s...", + message + ); + } else { + (void)fprintf( + stderr, + "testing: %s...", + message + ); + } + + return; +} + + +/** + * @tags infallible + */ +void +test_ok(void) { + if (show_colour()) { + (void)fprintf(stderr, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n"); + } else { + (void)fprintf(stderr, " OK.\n"); + } + + return; +} + +#ifdef TEST +int +main(void) { + int rc = 0; + + test_start("testing.c"); + const int should_overwrite = 1; + + if (unsetenv(ENVVAR_NAME)) { + perror("unsetenv(\"NO_COLOR\")"); + rc = -1; + goto out; + } + { + testing("unset NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"\", 1)"); + rc = -1; + goto out; + } + { + testing("empty NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "something", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"something\", 1)"); + rc = -1; + goto out; + } + { + testing("defined NO_COLOR"); + test_ok(); + } + +out: + return !!rc; +} +#endif |