From 8138f59fef628ad2dc7a5574775e763c18f273cd Mon Sep 17 00:00:00 2001 From: jlamothe Date: Sun, 19 Nov 2023 01:48:14 +0000 Subject: [PATCH] refactored run_test_with() --- 9unit.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/9unit.c b/9unit.c index 4914c96..64e5cea 100644 --- a/9unit.c +++ b/9unit.c @@ -27,12 +27,21 @@ // Internal Types +typedef struct RunTestWith RunTestWith; typedef struct ContextData ContextData; typedef struct SingleTestContext SingleTestContext; typedef struct TestContextWith TestContextWith; typedef struct SingleTestContextWith SingleTestContextWith; 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 { const char *old_c; // previous context @@ -122,7 +131,18 @@ run_test_with( 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 @@ -355,6 +375,14 @@ restore_context(TestState *s, ContextData *cd) 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 single_test_context_test(TestState *s) {