From 59f1c3eb583303c0f93e6316d23818ce940d3984 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 2 Jun 2024 23:34:53 +0100 Subject: [PATCH] Add testing utilities --- src/tester/tester.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/tester/tester.h | 26 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/tester/tester.c create mode 100644 src/tester/tester.h diff --git a/src/tester/tester.c b/src/tester/tester.c new file mode 100644 index 0000000..629b091 --- /dev/null +++ b/src/tester/tester.c @@ -0,0 +1,48 @@ +#include "tester.h" +#include "aliases.h" +#include "termcolour/termcolour.h" +#include +#include +#include + +internal void print_test_result(TestFuncResult result); + +void run_tests(TestFunc *func1, ...) { + printf("\n"); + + print_test_result(func1()); + + va_list args; + va_start(args, func1); + + TestFunc *func = NULL; + + while ((func = va_arg(args, TestFunc *))) { + TestFuncResult result = func(); + print_test_result(result); + + if (!result.success) { + exit(EXIT_FAILURE); + } + } + + va_end(args); + + printf("\n"); +} + +internal void print_test_result(TestFuncResult result) { + const char *colour; + const char *result_text; + + if (result.success) { + colour = TERM_COLOUR_FG_BR_GREEN; + result_text = "PASSED"; + } else { + colour = TERM_COLOUR_FG_BR_RED; + result_text = "FAILED"; + } + + printf("[%s%s%s%s] %s\n", colour, TERM_COLOUR_BOLD, result_text, + TERM_COLOUR_CLEAR, result.name); +} diff --git a/src/tester/tester.h b/src/tester/tester.h new file mode 100644 index 0000000..b48084d --- /dev/null +++ b/src/tester/tester.h @@ -0,0 +1,26 @@ +#ifndef TESTER_H +#define TESTER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define TEST_RESULT(BOOL) ((TestFuncResult){.name = __func__, .success = BOOL}) + +typedef struct test_func_result TestFuncResult; +struct test_func_result { + const char *name; + bool success; +}; + +typedef TestFuncResult(TestFunc)(void); + +void run_tests(TestFunc *func1, ...); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // !TESTER_H