diff --git a/9unit.c b/9unit.c index 2bd2ab5..958f3a1 100644 --- a/9unit.c +++ b/9unit.c @@ -87,11 +87,11 @@ static void print_log(TestState *); static void clear_log(TestState *); static void reindex(TestState *); static void report(const char *); -static TestResult run_test_with_test(TestState *); -static TestResult run_test_compare_test(TestState *, void *); static void build_new_context(TestState *, ContextData *); static void display_context(TestState *); static void restore_context(TestState *, ContextData *); +static TestResult run_test_with_test(TestState *); +static TestResult run_test_compare_test(TestState *, void *); static void single_test_context_test(TestState *, void *); static void test_context_with_test(TestState *); static void single_test_context_with_test(TestState *, void *); @@ -99,6 +99,53 @@ static void check_value_test(TestState *, void *); // Public Functions +void +run_tests(void (*tests)(TestState *)) +{ + if (!tests) return; + TestState s; + memset(&s, 0, sizeof(TestState)); + s.report = report; + (*tests)(&s); + print_log(&s); + printf("Tests run: %d\n", s.run); + printf("Tests passed: %d\n", s.passed); + printf("Tests failed: %d\n", s.failed); + printf("Tests pending: %d\n", s.pending); + clear_log(&s); + if (s.failed) exits("test(s) failed"); +} + +void +append_test_log(TestState *s, const char *msg) +{ + if (!(s && msg)) return; + + // build a new entry: + TestLogEntry *entry = malloc(sizeof(TestLogEntry)); + entry->text = malloc(strlen(msg) + 1); + strcpy(entry->text, msg); + entry->next = 0; + + // add it to the list: + if (!s->last_log) + { + if (s->first_log) // no last entry but we have a first? + { + reindex(s); + s->last_log->next = entry; + } + else s->first_log = entry; + } + else // there's already a last entry + { + if (!s->first_log) // no first entry but we have a last? + reindex(s); // do our best to fix that + s->last_log->next = entry; + } + s->last_log = entry; +} + void run_test(TestState *s, TestResult (*t)(TestState *)) { @@ -164,53 +211,6 @@ run_test_compare( run_test_with(s, run_test_compare_test, &d); } -void -run_tests(void (*tests)(TestState *)) -{ - if (!tests) return; - TestState s; - memset(&s, 0, sizeof(TestState)); - s.report = report; - (*tests)(&s); - print_log(&s); - printf("Tests run: %d\n", s.run); - printf("Tests passed: %d\n", s.passed); - printf("Tests failed: %d\n", s.failed); - printf("Tests pending: %d\n", s.pending); - clear_log(&s); - if (s.failed) exits("test(s) failed"); -} - -void -append_test_log(TestState *s, const char *msg) -{ - if (!(s && msg)) return; - - // build a new entry: - TestLogEntry *entry = malloc(sizeof(TestLogEntry)); - entry->text = malloc(strlen(msg) + 1); - strcpy(entry->text, msg); - entry->next = 0; - - // add it to the list: - if (!s->last_log) - { - if (s->first_log) // no last entry but we have a first? - { - reindex(s); - s->last_log->next = entry; - } - else s->first_log = entry; - } - else // there's already a last entry - { - if (!s->first_log) // no first entry but we have a last? - reindex(s); // do our best to fix that - s->last_log->next = entry; - } - s->last_log = entry; -} - void test_context( TestState *s, diff --git a/9unit.h b/9unit.h index 3816498..0e5fd37 100644 --- a/9unit.h +++ b/9unit.h @@ -25,6 +25,14 @@ // testing framework. You shouldn't need to concern yourself with // them. +// Possible results of running a single test +typedef enum TestResult +{ + test_success, // the test succeeded + test_failure, // the test failed + test_pending // the test is pending +} TestResult; + typedef struct TestState TestState; typedef struct TestLogEntry TestLogEntry; @@ -51,16 +59,19 @@ struct TestLogEntry TestLogEntry *next; // points to the next entry }; -// Possible results of running a single test -typedef enum TestResult -{ - test_success, // the test succeeded - test_failure, // the test failed - test_pending // the test is pending -} TestResult; - // Functions +// Creates an initial TestState, passes it to the supplied function, +// and displays the resulting log and summary +extern void run_tests(void (*)(TestState *)); + +// Adds an entry to the log that is displayed after the tests have +// completed +extern void append_test_log( + TestState *, // the current state + const char * // the message to append +); + // Runs a single test extern void run_test( TestState *, // the TestState data @@ -89,17 +100,6 @@ extern void run_test_compare( void * // the second value ); -// Creates an initial TestState, passes it to the supplied function, -// and displays the resulting log and summary -extern void run_tests(void (*)(TestState *)); - -// Adds an entry to the log that is displayed after the tests have -// completed -extern void append_test_log( - TestState *, // the current state - const char * // the message to append -); - // Gives additional context for a test extern void test_context( TestState *, // the current state