implemented run_test_with()
This commit is contained in:
parent
37ce34b160
commit
6c191d8405
41
9unit.c
41
9unit.c
@ -27,18 +27,19 @@
|
|||||||
|
|
||||||
// Internal Types
|
// Internal Types
|
||||||
|
|
||||||
// data required by the context management functions
|
typedef struct RunTestWith RunTestWith;
|
||||||
typedef struct ContextData ContextData;
|
typedef struct ContextData ContextData;
|
||||||
|
|
||||||
// data required to run a single test with a label
|
|
||||||
typedef struct SingleTestContext SingleTestContext;
|
typedef struct SingleTestContext SingleTestContext;
|
||||||
|
|
||||||
// data required by test_context_with()
|
|
||||||
typedef struct TestContextWith TestContextWith;
|
typedef struct TestContextWith TestContextWith;
|
||||||
|
|
||||||
// data used by check_value()
|
|
||||||
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
|
||||||
@ -74,6 +75,7 @@ static void print_log(TestState *);
|
|||||||
static void clear_log(TestState *);
|
static void clear_log(TestState *);
|
||||||
static void reindex(TestState *);
|
static void reindex(TestState *);
|
||||||
static void report(const char *);
|
static void report(const char *);
|
||||||
|
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 *);
|
||||||
@ -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
|
void
|
||||||
run_tests(void (*tests)(TestState *))
|
run_tests(void (*tests)(TestState *))
|
||||||
{
|
{
|
||||||
@ -286,6 +305,14 @@ 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)
|
||||||
{
|
{
|
||||||
|
7
9unit.h
7
9unit.h
@ -67,6 +67,13 @@ extern void run_test(
|
|||||||
TestResult (*)(TestState *) // the test to run
|
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
|
// Runs multiple tests, displaying a summary at the end
|
||||||
extern void run_tests(
|
extern void run_tests(
|
||||||
// runs the tests and updates a provided TestState
|
// runs the tests and updates a provided TestState
|
||||||
|
Loading…
x
Reference in New Issue
Block a user