diff --git a/clib.json b/clib.json index b5c4c64..3fb6ed2 100644 --- a/clib.json +++ b/clib.json @@ -1,15 +1,12 @@ { "name": "libtap", - "version": "0.0.4", + "version": "0.0.5", "author": "Matthew Zito", "repo": "exbotanical/libtap", "license": "MIT", "description": "An implementation of TAP testing", "keywords": ["test anything protocol", "testing", "unit test"], "src": ["include/libtap.h", "src/tap.c"], - "dependencies": { - "exbotanical/libutil": "*" - }, "development": { "exbotanical/print-assert": "0.0.1" } diff --git a/include/libtap.h b/include/libtap.h index e941d10..29fe6cd 100644 --- a/include/libtap.h +++ b/include/libtap.h @@ -1,6 +1,8 @@ #ifndef LIBTAP_H #define LIBTAP_H +#include +#include #include #include #include @@ -15,6 +17,27 @@ extern "C" { // TODO: parser for gh actions, etc +char* +__s_fmt__ (char* fmt, ...) { + va_list args, args_cp; + va_start(args, fmt); + va_copy(args_cp, args); + + // Pass length of zero first to determine number of bytes needed + unsigned int n = vsnprintf(NULL, 0, fmt, args) + 1; + char* buf = (char*)malloc(n); + if (!buf) { + return NULL; + } + + vsnprintf(buf, n, fmt, args_cp); + + va_end(args); + va_end(args_cp); + + return buf; +} + unsigned int __ok( unsigned int ok, const char* fn_name, @@ -40,7 +63,7 @@ unsigned int exit_status(void); unsigned int bail_out(const char* fmt, ...); #define ok(test, ...) \ - __ok(test ? 1 : 0, __func__, __FILE__, __LINE__, s_fmt(__VA_ARGS__)) + __ok(test ? 1 : 0, __func__, __FILE__, __LINE__, __s_fmt__(__VA_ARGS__)) #define is(actual, expected, ...) \ __ok( \ @@ -51,21 +74,21 @@ unsigned int bail_out(const char* fmt, ...); __func__, \ __FILE__, \ __LINE__, \ - s_fmt(__VA_ARGS__) \ + __s_fmt__(__VA_ARGS__) \ ); -#define skip_start(cond, num_skips, ...) \ - do { \ - if (cond) { \ - __skip(num_skips, s_fmt(__VA_ARGS__)); \ - break; \ +#define skip_start(cond, num_skips, ...) \ + do { \ + if (cond) { \ + __skip(num_skips, __s_fmt__(__VA_ARGS__)); \ + break; \ } #define skip_end() \ } \ while (0) -#define skip(test, ...) __skip(1, s_fmt(__VA_ARGS__)); +#define skip(test, ...) __skip(1, __s_fmt__(__VA_ARGS__)); #define done_testing() return exit_status() @@ -106,7 +129,7 @@ unsigned int bail_out(const char* fmt, ...); __func__, \ __FILE__, \ __LINE__, \ - s_fmt(__VA_ARGS__) \ + __s_fmt__(__VA_ARGS__) \ ); \ } while (0) diff --git a/src/tap.c b/src/tap.c index 0c0ecd7..dfff21e 100644 --- a/src/tap.c +++ b/src/tap.c @@ -40,8 +40,7 @@ static unsigned int is_todo_block = 0; static char* todo_msg = NULL; noreturn void -panic (const unsigned int errcode, const char* fmt, ...) -{ +panic (const unsigned int errcode, const char* fmt, ...) { va_list args; va_start(args, fmt); vfprintf(stderr, fmt, args); @@ -51,16 +50,14 @@ panic (const unsigned int errcode, const char* fmt, ...) } static void -diagv (const char* fmt, va_list ap) -{ +diagv (const char* fmt, va_list ap) { fprintf(stdout, "# "); vfprintf(stdout, fmt, ap); fprintf(stdout, "\n"); } static void -cleanup (void) -{ +cleanup (void) { // Fast exit if we've forked if (getpid() != test_runner_pid) { return; @@ -109,8 +106,7 @@ __ok ( const char* file, const unsigned int line, char* msg -) -{ +) { lock(); num_ran_tests++; @@ -155,8 +151,7 @@ __ok ( } void -__skip (unsigned int num_skips, char* msg) -{ +__skip (unsigned int num_skips, char* msg) { with_lock({ while (num_skips--) { fprintf(stdout, "ok %d # SKIP %s", ++num_ran_tests, msg); @@ -167,8 +162,7 @@ __skip (unsigned int num_skips, char* msg) } int -__write_shared_mem (int status) -{ +__write_shared_mem (int status) { static int* test_died = NULL; int prev; @@ -190,8 +184,7 @@ __write_shared_mem (int status) } void -todo_start (const char* fmt, ...) -{ +todo_start (const char* fmt, ...) { va_list ap; with_lock({ @@ -206,8 +199,7 @@ todo_start (const char* fmt, ...) } void -todo_end (void) -{ +todo_end (void) { with_lock({ is_todo_block = 0; free(todo_msg); @@ -216,8 +208,7 @@ todo_end (void) } void -diag (const char* fmt, ...) -{ +diag (const char* fmt, ...) { va_list ap; va_start(ap, fmt); diagv(fmt, ap); @@ -225,8 +216,7 @@ diag (const char* fmt, ...) } void -plan (unsigned int num_ran_tests) -{ +plan (unsigned int num_ran_tests) { lock(); static unsigned int singleton = 0; @@ -262,8 +252,7 @@ plan (unsigned int num_ran_tests) } unsigned int -exit_status (void) -{ +exit_status (void) { unsigned int retval; with_lock({ @@ -282,8 +271,7 @@ exit_status (void) } unsigned int -bail_out (const char* fmt, ...) -{ +bail_out (const char* fmt, ...) { va_list args; va_start(args, fmt); diff --git a/t/main.c b/t/main.c index 5938354..5b73762 100644 --- a/t/main.c +++ b/t/main.c @@ -74,41 +74,29 @@ main () { todo_end(); - lives( - { - will_succeed(); - printf("test\n"); - }, - "doesn't die" - ); + lives({ will_succeed(); }, "doesn't die"); assert(pa_match_stdout("ok 9 - doesn't die\n") == 1); - dies( - { - will_fail(); - printf("test\n"); - }, - "dies" - ); + dies({ will_fail(); }, "dies"); assert( pa_match_stdout("not ok 10 - dies\n" - "# \tFailed test (t/main.c:main at line 87)\n") + "# \tFailed test (t/main.c:main at line 81)\n") == 1 ); - char *x = s_fmt("x"); - char *y = s_fmt("y"); - char *a = s_fmt("a"); - char *a2 = s_fmt("a"); - char *b = s_fmt("b"); + char *x = strdup("x"); + char *y = strdup("y"); + char *a = strdup("a"); + char *a2 = strdup("a"); + char *b = strdup("b"); char *n = NULL; is(a, b, "not equal"); assert( pa_match_stdout("not ok 11 - not equal\n" - "# \tFailed test (t/main.c:main at line 108)\n") + "# \tFailed test (t/main.c:main at line 96)\n") == 1 );