diff options
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | doc/example.c | 7 | ||||
-rw-r--r-- | simple-test.h | 147 |
3 files changed, 133 insertions, 30 deletions
@@ -30,6 +30,13 @@ the following example file is available as [example.c](doc/example.c), which you can compile and play with yourself. ```C +/* SIMPLE_TEST_PRINT_FUN may be redefined like so. default is `printf` */ +#define stderr_redirect(...) fprintf(stderr, __VA_ARGS__) +#define SIMPLE_TEST_PRINT_FUN stderr_redirect + +/* colour output is on by default, but can be disabled */ +/* #define SIMPLE_TEST_USE_COLOUR false */ + #include "../simple-test.h" /* global variables, functions, and includes must come before BEGIN_TEST */ @@ -173,6 +180,8 @@ simple-test's interface consists of a series of preprocessor macros: | **NAME** | **DESCRIPTION** | |---------:|:----------------| +| **SIMPLE_TEST_USE_COLOUR** | may be defined, before the header is included, as either a `true` or `false` resolving expression. default is `true` | +| **SIMPLE_TEST_PRINT_FUN** | may be defined, before the header is included, as a function or macro which takes `printf` style arguments. default is `printf` | | **BEGIN_TEST** | must be included once, after includes and global declarations and before the first `TEST` statement | | **END_TEST** | must be included once, after the final `TEST` statement | | **TEST(description)**{} | declare a test, described by `description`, which consists of statements between the {} | diff --git a/doc/example.c b/doc/example.c index 6341a1f..0229d2a 100644 --- a/doc/example.c +++ b/doc/example.c @@ -1,3 +1,10 @@ +/* SIMPLE_TEST_PRINT_FUN may be redefined like so. default is `printf` */ +#define stderr_redirect(...) fprintf(stderr, __VA_ARGS__) +#define SIMPLE_TEST_PRINT_FUN stderr_redirect + +/* colour output is on by default, but can be disabled */ +/* #define SIMPLE_TEST_USE_COLOUR false */ + #include "../simple-test.h" /* global variables, functions, and includes must come before BEGIN_TEST */ 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 <stdarg.h> #include <string.h> +/************************** + * 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) |