Skip to content

Commit

Permalink
TAP appears to be fully supported now
Browse files Browse the repository at this point in the history
  • Loading branch information
nvll committed Mar 25, 2012
1 parent 1bd8ee4 commit 23d6c09
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
13 changes: 8 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,24 @@ int main (int argc, char *argv[])
else
fail++;
} else {
// We're a bit fast and loose with memory since the child process will die anyway
return_t *ret = test->func();
if (ret->value != 0)
printf("not ");
printf("ok %d ", current_test);
if (ret->len > 0)
printf("- %.*s", ret->len, ret->buf);
printf("\n");
printf("ok %d - %s\n", current_test, ret->func);
if (ret->len > 0) {
char *pos, *tmp = strdup(ret->buf);
while ((pos = strsep(&tmp, &(char){'\n'})))
printf(" %s\n", pos);
}
exit(ret->value);
}

current_test++;
}

int total = pass + fail;
printf("%d %s run, %d/%d passed\n", total, (total == 1) ? "test" : "tests", pass, total);
printf("# %d %s run, %d/%d passed\n", total, (total == 1) ? "test" : "tests", pass, total);

return fail;
}
25 changes: 19 additions & 6 deletions tests/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,36 @@
*/
#include "units.h"

static units_test_t test_passing (void)
static units_test_t always_passing (void)
{
int x = 3, y = 7;

if (x + y == 10)
test_pass;

test_fail(1, "mathematics do not work");
test_fail_v("mathematics do not work");
}

static units_test_t test_failing (void)
static units_test_t always_failing (void)
{
test_fail(127, "always fails with code 127");
test_fail;
}

static units_test_t fail_with_output (void)
{
const char *output = "this is a string with\nlinebreaks\nin the failure output";
test_fail_v(output);
}

static units_test_t pass_with_output (void)
{
test_pass_v("everything went better than expected");
}

start_tests
add_test(test_passing)
add_test(test_failing)
add_test(always_passing)
add_test(always_failing)
add_test(fail_with_output)
add_test(pass_with_output)
end_tests

13 changes: 8 additions & 5 deletions units.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ extern int units_test_cnt;
typedef struct {
unsigned int value;
const char *buf;
const char *func;
int len;
} return_t;

Expand All @@ -94,22 +95,24 @@ static inline void _add_test(units_test_t (*func)(void))
units_test_cnt++;
}

static inline return_t *test_result (unsigned int value, const char *buf)
static inline return_t *test_result (unsigned int value, const char *buf, const char *func)
{
return_t *ret = malloc(sizeof(return_t));
ret->value = value;

ret->func = func;
ret->buf = buf;

if (buf)
ret->len = strlen(ret->buf);
else
ret->len = 0;
return ret;
}

#define test_pass return test_result(0, NULL)
#define test_pass_v(string) return test_result(0, string)
#define test_fail(x, y) return test_result(x, y)
#define test_pass return test_result(0, NULL, __func__)
#define test_pass_v(string) return test_result(0, string, __func__)
#define test_fail return test_result(1, NULL, __func__)
#define test_fail_v(x) return test_result(1, x, __func__)

#define start_tests __attribute__((constructor)) static void units_test (void) {
#define add_test(x) _add_test(x); // Just to keep ; usage consistent
Expand Down

0 comments on commit 23d6c09

Please sign in to comment.