From 757c8963101a0bce5dbb5ad389acdcf7ceb2ecc4 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sun, 27 Jun 2021 10:42:57 -0300 Subject: src/remembering-c.c: Add unit tests for get_profile_file() Those tests led me to handling the case where $XDG_DATA_HOME was set, but as an empty string. This is the equivalent of the "[ -z ".." ]" test. I also parameterized get_profile_file() to receive the stream where it should write to in case of failing to getenv("HOME"). Even though this makes the arguments of functions that print longer, it looks like an overall good pattern. It is the equivalent of every sh function printing to STDOUT, and let the caller decide if it should go to STDOUT or STDERR. --- src/unit-test.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/unit-test.h (limited to 'src/unit-test.h') diff --git a/src/unit-test.h b/src/unit-test.h new file mode 100644 index 0000000..1dcd53c --- /dev/null +++ b/src/unit-test.h @@ -0,0 +1,23 @@ +#ifndef UNIT_TEST_H +#define UNIT_TEST_H + +#include + +#define COLOUR_RESET "\033[0m" +#define COLOUR_RED "\033[0;31m" +#define COLOUR_GREEN "\033[0;32m" +#define COLOUR_YELLOW "\033[0;33m" + +void testing(const char *const message) { + fprintf(stderr, "testing: " COLOUR_YELLOW "%s" COLOUR_RESET "...", message); +} + +void test_ok() { + fprintf(stderr, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n"); +} + +#define ASSERT_MSG "\n" COLOUR_RED "ERROR" COLOUR_RESET ": " +#define assertf(A, M, ...) if (!(A)) { fprintf(stderr, (ASSERT_MSG M "\n"), __VA_ARGS__); assert(A); } +#define asserte(A) if (!(A)) { fprintf(stderr, ASSERT_MSG "\n"); assert(A); } + +#endif -- cgit v1.2.3