// vim:fileencoding=utf-8:foldmethod=marker

#include "tester.h"
#include "../../common/aliases/aliases.h"
#include "../../os/shell/termcolour/termcolour.h"
#include "../../base/strings/str8/str8.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

wp_intern void handleTestResult(WpTestFuncResult result);

void _runTests(WpTestFunc *func1, ...) {
	printf("\n");

	handleTestResult(func1());

	va_list args;
	va_start(args, func1);

	WpTestFunc *func = va_arg(args, WpTestFunc *);

	while (func) {
		WpTestFuncResult result = func();
		handleTestResult(result);

		func = va_arg(args, WpTestFunc *);
	}

	va_end(args);

	printf("\n");
}

wp_intern void handleTestResult(WpTestFuncResult result) {
	WpTerminalColour colour;
	WpStr8 result_text = wpStr8Buf(64);

	if (result.passed) {
		colour = WP_TERM_COLOUR_FG_BR_GREEN;
		wpStr8CopyCstrCapped(&result_text, "PASSED");
	} else {
		colour = WP_TERM_COLOUR_FG_BR_RED;
		wpStr8CopyCstrCapped(&result_text, "FAILED");
	}

	printf("[");
	wpShellTermcolourPrintText(&result_text, colour);
	wpShellTermcolourClearColour();
	printf("] " WP_STR8_SPEC "\n", wpStr8Varg(result.name));

	if (!result.passed) {
		exit(EXIT_FAILURE);
	}
}
