From bd535791d749865554403bee0cb5a42e66ad6c58 Mon Sep 17 00:00:00 2001 From: jlamothe Date: Sat, 18 Nov 2023 16:38:00 +0000 Subject: [PATCH] implemented single_test_context_with() --- 9unit.c | 35 +++++++++++++++++++++++++++++++++++ 9unit.h | 14 ++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/9unit.c b/9unit.c index 6f05b80..5acb042 100644 --- a/9unit.c +++ b/9unit.c @@ -31,6 +31,7 @@ typedef struct RunTestWith RunTestWith; typedef struct ContextData ContextData; typedef struct SingleTestContext SingleTestContext; typedef struct TestContextWith TestContextWith; +typedef struct SingleTestContextWith SingleTestContextWith; typedef struct CheckValue CheckValue; struct RunTestWith @@ -61,6 +62,13 @@ struct TestContextWith void (*test)(TestState *, void *); // the test function }; +struct SingleTestContextWith +{ + void *ptr; + TestResult (*test)(TestState *, void *); + void *val; +}; + struct CheckValue { void *chk_val; // the value being checked @@ -81,6 +89,7 @@ static void display_context(TestState *); static void restore_context(TestState *, ContextData *); static void run_single_test_context(TestState *); static void test_context_with_test(TestState *); +static TestResult single_test_context_with_test(TestState *); static void check_value_test(TestState *, void *); // Public Functions @@ -230,6 +239,24 @@ test_context_with( test_context(s, context, test_context_with_test); } +void +single_test_context_with( + TestState *s, + const char *context, + TestResult (*test)(TestState *, void *), + void *val +) +{ + if (!s) return; + if (!test) single_test_context(s, context, 0); + SingleTestContextWith d; + d.ptr = s->ptr; + d.test = test; + d.val = val; + s->ptr = &d; + single_test_context(s, context, single_test_context_with_test); +} + void check_value( TestState *s, @@ -367,6 +394,14 @@ test_context_with_test(TestState *s) (*d->test)(s, d->val); } +static TestResult +single_test_context_with_test(TestState *s) +{ + SingleTestContextWith *d = s->ptr; + s->ptr = d->ptr; + return (*d->test)(s, d->val); +} + static void check_value_test(TestState *s, void *ptr) { diff --git a/9unit.h b/9unit.h index 244815a..96752b7 100644 --- a/9unit.h +++ b/9unit.h @@ -109,6 +109,20 @@ extern void test_context_with( void * // the value being passed in ); +// Runs a single test with a context and input +extern void single_test_context_with( + TestState *, // the state + const char *, // the context + + // the test + TestResult (*)( + TestState *, // the state + void * // the value being passed + ), + + void * // the value being passed +); + // Runs a test to check a value extern void check_value( TestState *, // the current test state