From cae280b3246a51cd485cb01c8ec73a205ceac7e8 Mon Sep 17 00:00:00 2001 From: katherine Date: Fri, 14 Jun 2019 18:48:19 -0700 Subject: add macro settings for colour and print fun --- simple-test.h | 147 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 117 insertions(+), 30 deletions(-) (limited to 'simple-test.h') diff --git a/simple-test.h b/simple-test.h index 1b91fa9..c6abebf 100644 --- a/simple-test.h +++ b/simple-test.h @@ -9,6 +9,18 @@ #include #include +/************************** + * CONFIGURATION MACROS * + **************************/ + +#ifndef SIMPLE_TEST_USE_COLOUR +#define SIMPLE_TEST_USE_COLOUR true +#endif + +#ifndef SIMPLE_TEST_PRINT_FUN +#define SIMPLE_TEST_PRINT_FUN printf +#endif + /************************************************** * INTERNAL FUNCTIONALITY. DO NOT CALL DIRECTLY * @@ -250,42 +262,70 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1, #define SIMPLE_TEST_FAIL1(...) \ do { \ - printf("\x1B[1m%*c :: at line %d, \x1B[m\x1B[1;31mfail: \x1B[m", \ - simple_test_pad_width, ' ', __LINE__); \ - printf(__VA_ARGS__); \ - printf("\n"); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN( \ + "\x1B[1m%*c :: at line %d, \x1B[m\x1B[1;31mfail: \x1B[m", \ + simple_test_pad_width, ' ', __LINE__); \ + } else { \ + SIMPLE_TEST_PRINT_FUN( \ + "%*c :: at line %d, fail: ", \ + simple_test_pad_width, ' ', __LINE__); \ + } \ + SIMPLE_TEST_PRINT_FUN( \ + __VA_ARGS__); \ + SIMPLE_TEST_PRINT_FUN( \ + "\n"); \ } while (0) #define SIMPLE_TEST_PRINT_VAL(w, s, t) \ do { \ - printf("\x1B[1m%*c :: ", simple_test_pad_width, ' '); \ - printf("`%s` \x1B[m== \x1B[1m", s); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN( \ + "\x1B[1m%*c :: ", simple_test_pad_width, ' '); \ + SIMPLE_TEST_PRINT_FUN( \ + "`%s` \x1B[m== \x1B[1m", s); \ + } else { \ + SIMPLE_TEST_PRINT_FUN( \ + "%*c :: ", simple_test_pad_width, ' '); \ + SIMPLE_TEST_PRINT_FUN( \ + "`%s` == ", s); \ + } \ switch (t) { \ case SIMPLE_TEST_BOOL: \ - printf("%s", (w ? simple_test_il : simple_test_ir) \ + SIMPLE_TEST_PRINT_FUN( \ + "%s", (w ? simple_test_il : simple_test_ir) \ ? "true" : "false"); \ break; \ case SIMPLE_TEST_CHAR: \ - printf("%c", w ? (char)simple_test_il : (char)simple_test_ir); \ + SIMPLE_TEST_PRINT_FUN( \ + "%c", w ? (char)simple_test_il : (char)simple_test_ir); \ break; \ case SIMPLE_TEST_INT: \ - printf("%" PRIdMAX, w ? simple_test_il : simple_test_ir); \ + SIMPLE_TEST_PRINT_FUN( \ + "%" PRIdMAX, w ? simple_test_il : simple_test_ir); \ break; \ case SIMPLE_TEST_UNSIGNED: \ - printf("%" PRIuMAX, w ? simple_test_ul : simple_test_ur); \ + SIMPLE_TEST_PRINT_FUN( \ + "%" PRIuMAX, w ? simple_test_ul : simple_test_ur); \ break; \ case SIMPLE_TEST_STRING: \ - printf(((w ? simple_test_sl : simple_test_sr) \ + SIMPLE_TEST_PRINT_FUN( \ + ((w ? simple_test_sl : simple_test_sr) \ ? "\"%s\"" : "%s"), w \ ? simple_test_sl : simple_test_sr); \ break; \ case SIMPLE_TEST_POINTER: \ - printf("%p", w ? simple_test_pl : simple_test_pr); \ + SIMPLE_TEST_PRINT_FUN( \ + "%p", w ? simple_test_pl : simple_test_pr); \ break; \ default: \ break; \ } \ - printf("\x1B[m\n"); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN("\x1B[m\n"); \ + } else { \ + SIMPLE_TEST_PRINT_FUN("\n"); \ + } \ } while (0) #define SIMPLE_TEST_FAIL2 \ @@ -296,11 +336,22 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1, #define SIMPLE_TEST_ERR(...) \ do { \ - printf("\x1B[1m%*c :: at line %d, \x1B[m\x1B[1;31merr: \x1B[m", \ - simple_test_pad_width, ' ', __LINE__); \ - printf(__VA_ARGS__); \ - printf("\n"); \ - printf("\x1B[1;31mtesting aborted\x1B[m\n"); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN( \ + "\x1B[1m%*c :: at line %d, \x1B[m\x1B[1;31merr: \x1B[m", \ + simple_test_pad_width, ' ', __LINE__); \ + } else { \ + SIMPLE_TEST_PRINT_FUN( \ + "%*c :: at line %d, err: ", \ + simple_test_pad_width, ' ', __LINE__); \ + } \ + SIMPLE_TEST_PRINT_FUN(__VA_ARGS__); \ + SIMPLE_TEST_PRINT_FUN("\n"); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN("\x1B[1;31mtesting aborted\x1B[m\n"); \ + } else { \ + SIMPLE_TEST_PRINT_FUN("testing aborted\n"); \ + } \ exit(1); \ } while (0) @@ -355,7 +406,6 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1, } \ } while (0) - #define USE_TEARDOWN \ do { \ if (simple_test_teardown == NULL) \ @@ -380,7 +430,13 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1, do { \ simple_test_test_current_at = 0; \ if (simple_test_pass_number == 0) { \ - printf("\x1B[1mstarting tests in " __FILE__ "...\n"); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN( \ + "\x1B[1mstarting tests in " __FILE__ "...\n"); \ + } else { \ + SIMPLE_TEST_PRINT_FUN( \ + "starting tests in " __FILE__ "...\n"); \ + } \ } else { \ simple_test_pad_width = sprintf(simple_test_print_buf, \ "%d", simple_test_test_count) + 1; \ @@ -389,7 +445,6 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1, simple_test_iterator < 1; simple_test_iterator++) { \ (void)0; \ - #define TEST(description) \ } \ if (simple_test_do_teardown) { \ @@ -401,9 +456,18 @@ static enum simple_test_type simple_test_type_resolve(enum simple_test_type t1, simple_test_test_count++; \ } else if (simple_test_pass_number \ == simple_test_test_current_at) { \ - printf("\x1B[m%*d \x1B[1m:: \x1B[m\x1B[33m%s\x1B[m\n", simple_test_pad_width, \ - simple_test_test_current++, description); \ - + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN( \ + "\x1B[m%*d \x1B[1m:: \x1B[m\x1B[33m%s\x1B[m\n", \ + simple_test_pad_width, \ + simple_test_test_current++, description); \ + } else { \ + SIMPLE_TEST_PRINT_FUN( \ + "%*d :: %s\n", \ + simple_test_pad_width, \ + simple_test_test_current++, description); \ + } \ + /* must appear after all tests */ #define END_TEST \ } \ @@ -418,20 +482,43 @@ simple_test_loop_end: \ (void)0; \ } while (simple_test_pass_number++ < simple_test_test_count); \ if (simple_test_fail_count) { \ - printf("\x1B[1;31m%d of %d tests failed\x1B[m\n", \ - simple_test_fail_count, simple_test_test_count); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN( \ + "\x1B[1;31m%d of %d tests failed\x1B[m\n", \ + simple_test_fail_count, simple_test_test_count); \ + } else { \ + SIMPLE_TEST_PRINT_FUN( \ + "%d of %d tests failed\n", \ + simple_test_fail_count, simple_test_test_count); \ + } \ return 1; \ } else { \ - printf("\x1B[1;32mall tests passed!\x1B[m\n"); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN("\x1B[1;32mall tests passed!\x1B[m\n"); \ + } else { \ + SIMPLE_TEST_PRINT_FUN("all tests passed!\n"); \ + } \ return 0; \ } \ } #define ECHO(...) \ do { \ - printf("\x1B[1m%*c :: \x1B[m\x1B[34m", simple_test_pad_width, ' '); \ - printf(__VA_ARGS__); \ - printf("...\x1B[m\n"); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN( \ + "\x1B[1m%*c :: \x1B[m\x1B[34m", \ + simple_test_pad_width, ' '); \ + } else { \ + SIMPLE_TEST_PRINT_FUN( \ + "%*c :: ", \ + simple_test_pad_width, ' '); \ + } \ + SIMPLE_TEST_PRINT_FUN(__VA_ARGS__); \ + if (SIMPLE_TEST_USE_COLOUR) { \ + SIMPLE_TEST_PRINT_FUN("...\x1B[m\n"); \ + } else { \ + SIMPLE_TEST_PRINT_FUN("...\n"); \ + } \ } while (0) -- cgit v1.2.3