implemented test_context_with()

This commit is contained in:
jlamothe 2023-11-17 21:30:52 +00:00
parent 7818e3d646
commit 5d67b7aeb8
2 changed files with 46 additions and 1 deletions

39
9unit.c
View File

@ -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)
{

View File

@ -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