define and start using single_text_context()
This commit is contained in:
parent
ef00063ba3
commit
0ad79ea3b0
33
9unit.c
33
9unit.c
|
@ -30,6 +30,9 @@
|
||||||
// data required by the context management functions
|
// data required by the context management functions
|
||||||
typedef struct ContextData ContextData;
|
typedef struct ContextData ContextData;
|
||||||
|
|
||||||
|
// data required to run a single test with a label
|
||||||
|
typedef struct SingleTestContext SingleTestContext;
|
||||||
|
|
||||||
struct ContextData
|
struct ContextData
|
||||||
{
|
{
|
||||||
const char *old_c; // previous context
|
const char *old_c; // previous context
|
||||||
|
@ -38,6 +41,12 @@ struct ContextData
|
||||||
char *new_fc; // new full context
|
char *new_fc; // new full context
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct SingleTestContext
|
||||||
|
{
|
||||||
|
void *ptr; // the state's previous ptr value
|
||||||
|
TestResult (*test)(TestState *); // the test to run
|
||||||
|
};
|
||||||
|
|
||||||
// Internal Prototypes
|
// Internal Prototypes
|
||||||
|
|
||||||
static void init_TestState(TestState *);
|
static void init_TestState(TestState *);
|
||||||
|
@ -48,6 +57,7 @@ static void report(const char *);
|
||||||
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 *);
|
||||||
|
|
||||||
// Public Functions
|
// Public Functions
|
||||||
|
|
||||||
|
@ -147,6 +157,21 @@ test_context(
|
||||||
else (*test)(s);
|
else (*test)(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
single_test_context(
|
||||||
|
TestState *s,
|
||||||
|
const char *label,
|
||||||
|
TestResult (*test)(TestState *)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (!s) return;
|
||||||
|
SingleTestContext stc;
|
||||||
|
stc.ptr = s->ptr;
|
||||||
|
stc.test = test;
|
||||||
|
s->ptr = &stc;
|
||||||
|
test_context(s, label, run_single_test_context);
|
||||||
|
}
|
||||||
|
|
||||||
// Internal Functions
|
// Internal Functions
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -243,4 +268,12 @@ restore_context(TestState *s, ContextData *cd)
|
||||||
free(cd->new_fc);
|
free(cd->new_fc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
run_single_test_context(TestState *s)
|
||||||
|
{
|
||||||
|
SingleTestContext *stc = s->ptr;
|
||||||
|
s->ptr = stc->ptr;
|
||||||
|
run_test(s, stc->test);
|
||||||
|
}
|
||||||
|
|
||||||
//jl
|
//jl
|
||||||
|
|
13
9unit.h
13
9unit.h
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Types & Structs
|
||||||
|
|
||||||
// Tracks information about the tests being run.
|
// Tracks information about the tests being run.
|
||||||
typedef struct TestState TestState;
|
typedef struct TestState TestState;
|
||||||
|
|
||||||
|
@ -57,6 +59,8 @@ typedef enum TestResult
|
||||||
test_pending // the test is pending
|
test_pending // the test is pending
|
||||||
} TestResult;
|
} TestResult;
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
// Runs a single test
|
// Runs a single test
|
||||||
extern void run_test(
|
extern void run_test(
|
||||||
TestState *, // the TestState data
|
TestState *, // the TestState data
|
||||||
|
@ -81,6 +85,13 @@ extern void test_context(
|
||||||
TestState *, // the current state
|
TestState *, // the current state
|
||||||
const char *, // a description of the context
|
const char *, // a description of the context
|
||||||
void (*)(TestState *) // the actual test
|
void (*)(TestState *) // the actual test
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Runs a single test with a context label
|
||||||
|
extern void single_test_context(
|
||||||
|
TestState *, // the current state
|
||||||
|
const char *, // a description of the context
|
||||||
|
TestResult (*)(TestState *) // the actual test
|
||||||
|
);
|
||||||
|
|
||||||
//jl
|
//jl
|
||||||
|
|
|
@ -46,18 +46,17 @@ decl_test(initial_report);
|
||||||
void
|
void
|
||||||
test_initial_state(TestState *s)
|
test_initial_state(TestState *s)
|
||||||
{
|
{
|
||||||
print("initial state\n");
|
single_test_context(s, "run", initial_test_count);
|
||||||
run_test(s, initial_test_count);
|
single_test_context(s, "passsed", initial_pass_count);
|
||||||
run_test(s, initial_pass_count);
|
single_test_context(s, "failed", initial_fail_count);
|
||||||
run_test(s, initial_fail_count);
|
single_test_context(s, "pending", initial_pend_count);
|
||||||
run_test(s, initial_pend_count);
|
single_test_context(s, "first_log", initial_first_log);
|
||||||
run_test(s, initial_first_log);
|
single_test_context(s, "last_log", initial_last_log);
|
||||||
run_test(s, initial_last_log);
|
single_test_context(s, "ptr", initial_ptr);
|
||||||
run_test(s, initial_ptr);
|
single_test_context(s, "context", initial_context);
|
||||||
run_test(s, initial_context);
|
single_test_context(s, "full_context", initial_full_context);
|
||||||
run_test(s, initial_full_context);
|
single_test_context(s, "depth", initial_depth);
|
||||||
run_test(s, initial_depth);
|
single_test_context(s, "report()", initial_report);
|
||||||
run_test(s, initial_report);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal Functions
|
// Internal Functions
|
||||||
|
@ -65,7 +64,6 @@ test_initial_state(TestState *s)
|
||||||
def_test(initial_test_count, s)
|
def_test(initial_test_count, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\trun\n");
|
|
||||||
if (scpy->run == 0) return test_success;
|
if (scpy->run == 0) return test_success;
|
||||||
append_test_log(s, "initial run was nonzero");
|
append_test_log(s, "initial run was nonzero");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -83,7 +81,6 @@ def_test(initial_pass_count, s)
|
||||||
def_test(initial_fail_count, s)
|
def_test(initial_fail_count, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\tfailed\n");
|
|
||||||
if (scpy->failed == 0) return test_success;
|
if (scpy->failed == 0) return test_success;
|
||||||
append_test_log(s, "initial failed was nonzero");
|
append_test_log(s, "initial failed was nonzero");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -101,7 +98,6 @@ def_test(initial_pend_count, s)
|
||||||
def_test(initial_first_log, s)
|
def_test(initial_first_log, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\tfirst_log\n");
|
|
||||||
if (scpy->first_log == 0) return test_success;
|
if (scpy->first_log == 0) return test_success;
|
||||||
append_test_log(s, "initial first_log was not null");
|
append_test_log(s, "initial first_log was not null");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -110,7 +106,6 @@ def_test(initial_first_log, s)
|
||||||
def_test(initial_last_log, s)
|
def_test(initial_last_log, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\tlast_log\n");
|
|
||||||
if (scpy->last_log == 0) return test_success;
|
if (scpy->last_log == 0) return test_success;
|
||||||
append_test_log(s, "initial last_log was not null");
|
append_test_log(s, "initial last_log was not null");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -119,7 +114,6 @@ def_test(initial_last_log, s)
|
||||||
def_test(initial_ptr, s)
|
def_test(initial_ptr, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\tptr\n");
|
|
||||||
if (scpy->ptr == 0) return test_success;
|
if (scpy->ptr == 0) return test_success;
|
||||||
append_test_log(s, "initial ptr was not null");
|
append_test_log(s, "initial ptr was not null");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -128,7 +122,6 @@ def_test(initial_ptr, s)
|
||||||
def_test(initial_context, s)
|
def_test(initial_context, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\tcontext\n");
|
|
||||||
if (scpy->context == 0) return test_success;
|
if (scpy->context == 0) return test_success;
|
||||||
append_test_log(s, "initial context was not null");
|
append_test_log(s, "initial context was not null");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -137,7 +130,6 @@ def_test(initial_context, s)
|
||||||
def_test(initial_full_context, s)
|
def_test(initial_full_context, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\tfull_context\n");
|
|
||||||
if (scpy->context == 0) return test_success;
|
if (scpy->context == 0) return test_success;
|
||||||
append_test_log(s, "initial full_context was not null");
|
append_test_log(s, "initial full_context was not null");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -146,7 +138,6 @@ def_test(initial_full_context, s)
|
||||||
def_test(initial_depth, s)
|
def_test(initial_depth, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\tdepth\n");
|
|
||||||
if (scpy->depth == 0) return test_success;
|
if (scpy->depth == 0) return test_success;
|
||||||
append_test_log(s, "initial depth was not zero");
|
append_test_log(s, "initial depth was not zero");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
@ -155,7 +146,6 @@ def_test(initial_depth, s)
|
||||||
def_test(initial_report, s)
|
def_test(initial_report, s)
|
||||||
{
|
{
|
||||||
TestState *scpy = s->ptr;
|
TestState *scpy = s->ptr;
|
||||||
print("\treport()\n");
|
|
||||||
if (scpy->report != 0) return test_success;
|
if (scpy->report != 0) return test_success;
|
||||||
append_test_log(s, "initial report was null");
|
append_test_log(s, "initial report was null");
|
||||||
return test_failure;
|
return test_failure;
|
||||||
|
|
|
@ -53,7 +53,7 @@ tests(TestState *s)
|
||||||
memcpy(&scpy, s, sizeof(TestState));
|
memcpy(&scpy, s, sizeof(TestState));
|
||||||
s->ptr = &scpy;
|
s->ptr = &scpy;
|
||||||
|
|
||||||
test_initial_state(s);
|
test_context(s, "initial state", test_initial_state);
|
||||||
test_run_test(s);
|
test_run_test(s);
|
||||||
test_append_test_log(s);
|
test_append_test_log(s);
|
||||||
test_context(s, "test_context()", test_test_context);
|
test_context(s, "test_context()", test_test_context);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user