From 6c191d84054003058953db432b7051c00fe71183 Mon Sep 17 00:00:00 2001 From: jlamothe Date: Fri, 17 Nov 2023 23:35:02 +0000 Subject: [PATCH] implemented run_test_with() --- 9unit.c | 41 ++++++++++++++++++++++++++++++++++------- 9unit.h | 7 +++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/9unit.c b/9unit.c index 012fdba..6f05b80 100644 --- a/9unit.c +++ b/9unit.c @@ -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) { diff --git a/9unit.h b/9unit.h index c3639d4..244815a 100644 --- a/9unit.h +++ b/9unit.h @@ -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