test: print test success rate

This commit is contained in:
Benedek László 2024-04-16 09:58:54 +02:00
parent 1f0a022cc7
commit 947117e061
2 changed files with 21 additions and 6 deletions

View File

@ -1,19 +1,21 @@
#ifndef CEX_TEST_ASSERT_H #ifndef CEX_TEST_ASSERT_H
#define CEX_TEST_ASSERT_H #define CEX_TEST_ASSERT_H
#include <assert.h>
#include <cex/ansi/codes.h> #include <cex/ansi/codes.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
void _print_assert(bool success, const char* label, const char* file, size_t line); #define INFO_BAR_WIDTH 50
typedef struct { typedef struct {
size_t test_count; size_t test_count;
size_t fail_count; size_t fail_count;
} test_result_t; } test_result_t;
void _print_assert(bool success, const char* label, const char* file, size_t line);
void _print_project_info(test_result_t final_result);
#define TEST(label) \ #define TEST(label) \
test_result_t test_##label() { \ test_result_t test_##label() { \
test_result_t result = {.test_count = 0, .fail_count = 0}; \ test_result_t result = {.test_count = 0, .fail_count = 0}; \
@ -30,8 +32,7 @@ typedef struct {
fprintf(stderr, ANSI_BOLD ANSI_UNDERLINE "project: %s\n" ANSI_RESET, label); fprintf(stderr, ANSI_BOLD ANSI_UNDERLINE "project: %s\n" ANSI_RESET, label);
#define END_PROJECT \ #define END_PROJECT \
fprintf(stderr, "%ld / %ld " ANSI_FG_GREEN " succesful" ANSI_RESET ", %ld " ANSI_FG_RED "failed\n" ANSI_RESET, \ _print_project_info(final_result); \
final_result.test_count - final_result.fail_count, final_result.test_count, final_result.fail_count); \
return final_result.fail_count != 0; \ return final_result.fail_count != 0; \
} }

View File

@ -1,4 +1,6 @@
#include <cex/ansi/codes.h>
#include <cex/test/assert.h> #include <cex/test/assert.h>
#include <stdio.h>
void _print_assert(bool success, const char* label, const char* file, size_t line) { void _print_assert(bool success, const char* label, const char* file, size_t line) {
fprintf(stderr, "%20s: \t%s" ANSI_RESET, label, success ? ANSI_FG_GREEN "OK" : ANSI_FG_RED "FAILED"); fprintf(stderr, "%20s: \t%s" ANSI_RESET, label, success ? ANSI_FG_GREEN "OK" : ANSI_FG_RED "FAILED");
@ -6,3 +8,15 @@ void _print_assert(bool success, const char* label, const char* file, size_t lin
fprintf(stderr, " in %s on line %ld", file, line); fprintf(stderr, " in %s on line %ld", file, line);
putc('\n', stderr); putc('\n', stderr);
} }
void _print_project_info(test_result_t final_result) {
int succes_len = INFO_BAR_WIDTH * (final_result.test_count - final_result.fail_count) / final_result.test_count;
fprintf(stderr, "[");
for (int i = 0; i < INFO_BAR_WIDTH; i++) {
fprintf(stderr, "%s ", succes_len > i ? ANSI_BG_GREEN : ANSI_BG_RED);
}
fprintf(stderr, ANSI_RESET "]\n");
fprintf(stderr, "%ld / %ld " ANSI_FG_GREEN " succesful" ANSI_RESET ", %ld " ANSI_FG_RED "failed\n" ANSI_RESET,
final_result.test_count - final_result.fail_count, final_result.test_count, final_result.fail_count);
}