Improve tests.h just a little
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul Brinkmeier 2022-10-26 20:56:33 +02:00
parent afe0276961
commit db29cbb20c
2 changed files with 24 additions and 14 deletions

View File

@ -2,4 +2,4 @@
# CI Playground # CI Playground
This is where is play around with [Drone](https://ci.pbrinkmeier.de). This is where I play around with [Drone](https://ci.pbrinkmeier.de).

View File

@ -6,9 +6,14 @@
#include <limits.h> #include <limits.h>
// globals. // globals.
int tests_passed = 0; struct tests_state {
int passed_tests;
int run_tests;
int total_tests;
char *current_label; char *current_label;
int current_num; };
struct tests_state tests_state = { 0, 0, 0, NULL };
/* Basic idea: /* Basic idea:
@ -30,23 +35,28 @@ DONE
*/ */
#define TEST(LABEL, BLOCK) \ #define TEST(LABEL, BLOCK) \
current_label = LABEL; \ tests_state.current_label = LABEL; \
current_num = __COUNTER__ + 1; \ tests_state.run_tests++; \
tests_state.total_tests = __COUNTER__; \
BLOCK; \ BLOCK; \
fprintf(stderr, "[%3d] \x1b[32m%s ✓\x1b[0m\n", current_num, current_label); \ fprintf(stderr, "[%3d] \x1b[32m%s ✓\x1b[0m\n", tests_state.run_tests, tests_state.current_label); \
tests_passed++; tests_state.passed_tests++;
#define ASSERT(EXPR) \ #define ASSERT(EXPR) \
if (!(EXPR)) { \ if (!(EXPR)) { \
fprintf(stderr, "[%3d] \x1b[31m%s ❌\x1b[0m\n", current_num, current_label); \ fprintf(stderr, "[%3d] \x1b[31m%s ❌\x1b[0m\n", tests_state.run_tests, tests_state.current_label); \
fprintf(stderr, " Assertion failed in %s:%d:\n", __FILE__, __LINE__); \ fprintf(stderr, " Assertion failed in %s:%d:\n", __FILE__, __LINE__); \
fprintf(stderr, " " #EXPR "\n"); \ fprintf(stderr, " " #EXPR "\n"); \
goto after_tests; \ goto tests_bail; \
} }
#define DONE \ #define DONE \
after_tests: { \ tests_bail: { \
int tests_total = __COUNTER__; \ tests_state.total_tests = __COUNTER__; \
fprintf(stderr, "[\x1b[%dm***\x1b[0m] %d/%d tests passed.\n", tests_passed == tests_total ? 32 : 31, tests_passed, tests_total); \ fprintf(stderr, "[\x1b[%dm***\x1b[0m] %d/%d tests passed", tests_state.passed_tests == tests_state.total_tests ? 32 : 31, tests_state.passed_tests, tests_state.total_tests); \
if (tests_passed != tests_total) exit(1); \ if (tests_state.total_tests > tests_state.run_tests) { \
fprintf(stderr, ", %d skipped", tests_state.total_tests - tests_state.run_tests); \
} \
fprintf(stderr, ".\n"); \
if (tests_state.passed_tests != tests_state.total_tests) exit(1); \
} }