reorganized functions and such

This commit is contained in:
jlamothe 2023-11-19 23:48:11 +00:00
parent 55926ec0d8
commit f808eb4cb0
2 changed files with 68 additions and 68 deletions

98
9unit.c
View File

@ -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,

38
9unit.h
View File

@ -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