implemented run_test_with()

This commit is contained in:
jlamothe 2023-11-17 23:35:02 +00:00
parent 37ce34b160
commit 6c191d8405
2 changed files with 41 additions and 7 deletions

41
9unit.c
View File

@ -27,18 +27,19 @@
// Internal Types
// data required by the context management functions
typedef struct RunTestWith RunTestWith;
typedef struct ContextData ContextData;
// data required to run a single test with a label
typedef struct SingleTestContext SingleTestContext;
// data required by test_context_with()
typedef struct TestContextWith TestContextWith;
// data used by check_value()
typedef struct CheckValue CheckValue;
struct RunTestWith
{
void *ptr; // state's prevoius ptr value
TestResult (*test)(TestState *, void *); // the test
void *val; // the value being passed in
};
struct ContextData
{
const char *old_c; // previous context
@ -74,6 +75,7 @@ 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 void build_new_context(TestState *, ContextData *);
static void display_context(TestState *);
static void restore_context(TestState *, ContextData *);
@ -112,6 +114,23 @@ run_test(TestState *s, TestResult (*t)(TestState *))
}
}
void
run_test_with(
TestState *s, // the state
TestResult (*test)(TestState *, void *), // the test
void *val // the value being passed in
)
{
if (!s) return;
if (!test) run_test(s, 0);
RunTestWith d;
d.ptr = s->ptr;
d.test = test;
d.val = val;
s->ptr = &d;
run_test(s, run_test_with_test);
}
void
run_tests(void (*tests)(TestState *))
{
@ -286,6 +305,14 @@ report(const char *str)
print("%s", str);
}
static TestResult
run_test_with_test(TestState *s)
{
RunTestWith *d = s->ptr;
s->ptr = d->ptr;
return (*d->test)(s, d->val);
}
static void
build_new_context(TestState *s, ContextData *cd)
{

View File

@ -67,6 +67,13 @@ extern void run_test(
TestResult (*)(TestState *) // the test to run
);
// Runs a single test with an arbitrary input
extern void run_test_with(
TestState *, // the state
TestResult (*)(TestState *, void *), // the test
void * // the value to pass in
);
// Runs multiple tests, displaying a summary at the end
extern void run_tests(
// runs the tests and updates a provided TestState