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