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
View File

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

37
9unit.h
View File

@ -19,34 +19,47 @@
*/
// 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 passed; // number of successful tests
int failed; // number of failed 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
typedef enum TestResult
{
test_success,
test_failure,
test_postponed
test_success, // the test succeeded
test_failure, // the test failed
test_postponed // the test was postponed
} TestResult;
// Initializes a TestState value
extern void initTestState(TestState *);
// Runs a single test
extern void runTest(
extern void run_test(
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
extern void runTests(
// function that runs the tests and updates the provided TestState
extern void run_tests(
// runs the tests and updates a provided TestState
void (*)(TestState *)
);