implemented single_test_context_with()

This commit is contained in:
jlamothe 2023-11-18 16:38:00 +00:00
parent 6c191d8405
commit bd535791d7
2 changed files with 49 additions and 0 deletions

35
9unit.c
View File

@ -31,6 +31,7 @@ typedef struct RunTestWith RunTestWith;
typedef struct ContextData ContextData; typedef struct ContextData ContextData;
typedef struct SingleTestContext SingleTestContext; typedef struct SingleTestContext SingleTestContext;
typedef struct TestContextWith TestContextWith; typedef struct TestContextWith TestContextWith;
typedef struct SingleTestContextWith SingleTestContextWith;
typedef struct CheckValue CheckValue; typedef struct CheckValue CheckValue;
struct RunTestWith struct RunTestWith
@ -61,6 +62,13 @@ struct TestContextWith
void (*test)(TestState *, void *); // the test function void (*test)(TestState *, void *); // the test function
}; };
struct SingleTestContextWith
{
void *ptr;
TestResult (*test)(TestState *, void *);
void *val;
};
struct CheckValue struct CheckValue
{ {
void *chk_val; // the value being checked void *chk_val; // the value being checked
@ -81,6 +89,7 @@ static void display_context(TestState *);
static void restore_context(TestState *, ContextData *); static void restore_context(TestState *, ContextData *);
static void run_single_test_context(TestState *); static void run_single_test_context(TestState *);
static void test_context_with_test(TestState *); static void test_context_with_test(TestState *);
static TestResult single_test_context_with_test(TestState *);
static void check_value_test(TestState *, void *); static void check_value_test(TestState *, void *);
// Public Functions // Public Functions
@ -230,6 +239,24 @@ test_context_with(
test_context(s, context, test_context_with_test); 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 void
check_value( check_value(
TestState *s, TestState *s,
@ -367,6 +394,14 @@ test_context_with_test(TestState *s)
(*d->test)(s, d->val); (*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 static void
check_value_test(TestState *s, void *ptr) check_value_test(TestState *s, void *ptr)
{ {

14
9unit.h
View File

@ -109,6 +109,20 @@ extern void test_context_with(
void * // the value being passed in 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 // Runs a test to check a value
extern void check_value( extern void check_value(
TestState *, // the current test state TestState *, // the current test state