diff --git a/9unit.c b/9unit.c index defe2aa..39b4398 100644 --- a/9unit.c +++ b/9unit.c @@ -33,6 +33,9 @@ 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 CheckValueData CheckValueData; @@ -50,6 +53,13 @@ struct SingleTestContext TestResult (*test)(TestState *); // the test to run }; +struct TestContextWith +{ + void *ptr; // state's original ptr value + void *val; // the value being passed in + void (*test)(TestState *, void *); // the test function +}; + struct CheckValueData { void *ptr; // state's original ptr value @@ -69,6 +79,7 @@ static void build_new_context(TestState *, ContextData *); 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 void check_value_test(TestState *); // Public Functions @@ -184,7 +195,25 @@ single_test_context( test_context(s, label, run_single_test_context); } -void check_value( +void +test_context_with( + TestState *s, + const char *context, + void (*test)(TestState *, void *), + void *val +) +{ + if (!(s && test)) return; + TestContextWith d; + d.ptr = s->ptr; + d.val = val; + d.test = test; + s->ptr = &d; + test_context(s, context, test_context_with_test); +} + +void +check_value( TestState *s, const char *context, void (*test)(TestState *, void *, void *), @@ -306,6 +335,14 @@ run_single_test_context(TestState *s) run_test(s, stc->test); } +static void +test_context_with_test(TestState *s) +{ + TestContextWith *d = s->ptr; + s->ptr = d->ptr; + (*d->test)(s, d->val); +} + static void check_value_test(TestState *s) { diff --git a/9unit.h b/9unit.h index b69f54f..c3639d4 100644 --- a/9unit.h +++ b/9unit.h @@ -94,6 +94,14 @@ extern void single_test_context( TestResult (*)(TestState *) // the actual test ); +// Runs a test with a context and an additional input +extern void test_context_with( + TestState *, // the current state + const char *, // a description of the context + void (*)(TestState *, void *), // the test function + void * // the value being passed in +); + // Runs a test to check a value extern void check_value( TestState *, // the current test state