refactored run_test_with()

This commit is contained in:
jlamothe 2023-11-19 01:48:14 +00:00
parent cd590339b7
commit 8138f59fef

30
9unit.c
View File

@ -27,12 +27,21 @@
// Internal Types // Internal Types
typedef struct RunTestWith RunTestWith;
typedef struct ContextData ContextData; typedef struct ContextData ContextData;
typedef struct SingleTestContext SingleTestContext; typedef struct SingleTestContext SingleTestContext;
typedef struct TestContextWith TestContextWith; typedef struct TestContextWith TestContextWith;
typedef struct SingleTestContextWith SingleTestContextWith; typedef struct SingleTestContextWith SingleTestContextWith;
typedef struct CheckValue CheckValue; typedef struct CheckValue CheckValue;
// data required by run_test_with()
struct RunTestWith
{
void *ptr; // the state's previous ptr value
TestResult (*test)(TestState *, void *); // the test
void *val; // the value passed in
};
struct ContextData struct ContextData
{ {
const char *old_c; // previous context const char *old_c; // previous context
@ -122,7 +131,18 @@ run_test_with(
void *val // the value being passed in void *val // the value being passed in
) )
{ {
single_test_context_with(s, 0, test, val); if (!s) return;
if (!test)
{
run_test(s, 0);
return;
}
RunTestWith d;
d.ptr = s->ptr;
d.test = test;
d.val = val;
s->ptr = &d;
run_test(s, run_test_with_test);
} }
void void
@ -355,6 +375,14 @@ restore_context(TestState *s, ContextData *cd)
free(cd->new_fc); free(cd->new_fc);
} }
static TestResult
run_test_with_test(TestState *s)
{
RunTestWith *d = s->ptr;
s->ptr = d->ptr;
return (*d->test)(s, d->val);
}
static void static void
single_test_context_test(TestState *s) single_test_context_test(TestState *s)
{ {