Add testing utilities
This commit is contained in:
parent
55d0c25c90
commit
59f1c3eb58
48
src/tester/tester.c
Normal file
48
src/tester/tester.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include "tester.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "termcolour/termcolour.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
26
src/tester/tester.h
Normal file
26
src/tester/tester.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef TESTER_H
|
||||||
|
#define TESTER_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#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
|
Loading…
Reference in New Issue
Block a user