restructured library to hide struct initialization

This commit is contained in:
jlamothe 2023-11-06 23:23:18 +00:00
parent e3037f3f3f
commit 870f1b619b
2 changed files with 51 additions and 25 deletions

39
9unit.c

@ -19,26 +19,23 @@
*/ */
#include <u.h> #include <u.h>
#include <libc.h>
#include <stdio.h> #include <stdio.h>
#include "9unit.h" #include "9unit.h"
void // Internal Prototypes
initTestState(TestState *s)
{ static void init_TestState(TestState *s);
if (!s) return;
s->run = 0; // Public Functions
s->passed = 0;
s->failed = 0;
s->postponed = 0;
}
void void
runTest(TestState *s, TestResult (*t)(void)) run_test(TestState *s, TestResult (*t)(TestState *))
{ {
if (!(s && t)) return; if (!(s && t)) return;
s->run++; s->run++;
switch ((*t)()) switch ((*t)(s))
{ {
case test_success: case test_success:
s->passed++; s->passed++;
@ -49,15 +46,17 @@ runTest(TestState *s, TestResult (*t)(void))
case test_postponed: case test_postponed:
s->postponed++; s->postponed++;
break; break;
default:
exits("test returned an invalid response");
} }
} }
void void
runTests(void (*tests)(TestState *)) run_tests(void (*tests)(TestState *))
{ {
if(!tests) return; if(!tests) return;
TestState s; TestState s;
initTestState(&s); init_TestState(&s);
(*tests)(&s); (*tests)(&s);
printf("Tests run: %d\n", s.run); printf("Tests run: %d\n", s.run);
printf("Tests passed: %d\n", s.passed); printf("Tests passed: %d\n", s.passed);
@ -65,4 +64,18 @@ runTests(void (*tests)(TestState *))
printf("Tests postponed: %d\n", s.postponed); printf("Tests postponed: %d\n", s.postponed);
} }
// Internal Functions
static void
init_TestState(TestState *s)
{
if (!s) return;
s->run = 0;
s->passed = 0;
s->failed = 0;
s->postponed = 0;
s->first_log = 0;
s->last_log = 0;
}
//jl //jl

37
9unit.h

@ -19,34 +19,47 @@
*/ */
// Tracks information about the tests being run. // Tracks information about the tests being run.
typedef struct TestState typedef struct TestState TestState;
// Defines a log entry in a TestState struct
typedef struct TestLogEntry TestLogEntry;
// The following structures will typically be maintained by the
// testing framework. You shouldn't need to concern yourself with
// them.
struct TestState
{ {
int run; // number of tests run int run; // number of tests run
int passed; // number of successful tests int passed; // number of successful tests
int failed; // number of failed tests int failed; // number of failed tests
int postponed; // number of postponed tests int postponed; // number of postponed tests
} TestState; TestLogEntry *first_log; // the first log entry
TestLogEntry *last_log; //the last log entry
};
struct TestLogEntry
{
char *text; // the entry text
TestLogEntry *next; // points to the next entry
};
// Possible results of running a single test // Possible results of running a single test
typedef enum TestResult typedef enum TestResult
{ {
test_success, test_success, // the test succeeded
test_failure, test_failure, // the test failed
test_postponed test_postponed // the test was postponed
} TestResult; } TestResult;
// Initializes a TestState value
extern void initTestState(TestState *);
// Runs a single test // Runs a single test
extern void runTest( extern void run_test(
TestState *, // the TestState data TestState *, // the TestState data
TestResult (*)(void) // the test to run TestResult (*)(TestState *) // the test to run
); );
// Runs multiple tests, displaying a summary at the end // Runs multiple tests, displaying a summary at the end
extern void runTests( extern void run_tests(
// function that runs the tests and updates the provided TestState // runs the tests and updates a provided TestState
void (*)(TestState *) void (*)(TestState *)
); );