Compare commits

...

3 Commits

Author SHA1 Message Date
jlamothe cd590339b7 refactored single_test_context() 2023-11-18 20:11:28 +00:00
jlamothe 2011b75b7e refactored run_test_with() 2023-11-18 19:28:40 +00:00
jlamothe bd535791d7 implemented single_test_context_with() 2023-11-18 16:38:00 +00:00
2 changed files with 60 additions and 34 deletions

80
9unit.c
View File

@ -27,19 +27,12 @@
// Internal Types // Internal Types
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
{
void *ptr; // state's prevoius ptr value
TestResult (*test)(TestState *, void *); // the test
void *val; // the value being passed in
};
struct ContextData struct ContextData
{ {
const char *old_c; // previous context const char *old_c; // previous context
@ -61,6 +54,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
@ -79,8 +79,9 @@ static TestResult run_test_with_test(TestState *);
static void build_new_context(TestState *, ContextData *); static void build_new_context(TestState *, ContextData *);
static void display_context(TestState *); 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 single_test_context_test(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
@ -121,14 +122,7 @@ run_test_with(
void *val // the value being passed in void *val // the value being passed in
) )
{ {
if (!s) return; single_test_context_with(s, 0, test, val);
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 void
@ -206,11 +200,11 @@ single_test_context(
) )
{ {
if (!s) return; if (!s) return;
SingleTestContext stc; SingleTestContext d;
stc.ptr = s->ptr; d.ptr = s->ptr;
stc.test = test; d.test = test;
s->ptr = &stc; s->ptr = &d;
test_context(s, label, run_single_test_context); test_context(s, label, single_test_context_test);
} }
void void
@ -230,6 +224,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,
@ -305,14 +317,6 @@ report(const char *str)
print("%s", 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 static void
build_new_context(TestState *s, ContextData *cd) build_new_context(TestState *s, ContextData *cd)
{ {
@ -352,11 +356,11 @@ restore_context(TestState *s, ContextData *cd)
} }
static void static void
run_single_test_context(TestState *s) single_test_context_test(TestState *s)
{ {
SingleTestContext *stc = s->ptr; SingleTestContext *d = s->ptr;
s->ptr = stc->ptr; s->ptr = d->ptr;
run_test(s, stc->test); run_test(s, d->test);
} }
static void static void
@ -367,6 +371,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