define and start using single_text_context()

This commit is contained in:
jlamothe 2023-11-16 00:52:01 +00:00
parent ef00063ba3
commit 0ad79ea3b0
4 changed files with 57 additions and 23 deletions

33
9unit.c
View File

@ -30,6 +30,9 @@
// data required by the context management functions
typedef struct ContextData ContextData;
// data required to run a single test with a label
typedef struct SingleTestContext SingleTestContext;
struct ContextData
{
const char *old_c; // previous context
@ -38,6 +41,12 @@ struct ContextData
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
static void init_TestState(TestState *);
@ -48,6 +57,7 @@ static void report(const char *);
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 *);
// Public Functions
@ -147,6 +157,21 @@ test_context(
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
static void
@ -243,4 +268,12 @@ restore_context(TestState *s, ContextData *cd)
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

13
9unit.h
View File

@ -19,6 +19,8 @@
*/
// Types & Structs
// Tracks information about the tests being run.
typedef struct TestState TestState;
@ -57,6 +59,8 @@ typedef enum TestResult
test_pending // the test is pending
} TestResult;
// Functions
// Runs a single test
extern void run_test(
TestState *, // the TestState data
@ -81,6 +85,13 @@ extern void test_context(
TestState *, // the current state
const char *, // a description of the context
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

View File

@ -46,18 +46,17 @@ decl_test(initial_report);
void
test_initial_state(TestState *s)
{
print("initial state\n");
run_test(s, initial_test_count);
run_test(s, initial_pass_count);
run_test(s, initial_fail_count);
run_test(s, initial_pend_count);
run_test(s, initial_first_log);
run_test(s, initial_last_log);
run_test(s, initial_ptr);
run_test(s, initial_context);
run_test(s, initial_full_context);
run_test(s, initial_depth);
run_test(s, initial_report);
single_test_context(s, "run", initial_test_count);
single_test_context(s, "passsed", initial_pass_count);
single_test_context(s, "failed", initial_fail_count);
single_test_context(s, "pending", initial_pend_count);
single_test_context(s, "first_log", initial_first_log);
single_test_context(s, "last_log", initial_last_log);
single_test_context(s, "ptr", initial_ptr);
single_test_context(s, "context", initial_context);
single_test_context(s, "full_context", initial_full_context);
single_test_context(s, "depth", initial_depth);
single_test_context(s, "report()", initial_report);
}
// Internal Functions
@ -65,7 +64,6 @@ test_initial_state(TestState *s)
def_test(initial_test_count, s)
{
TestState *scpy = s->ptr;
print("\trun\n");
if (scpy->run == 0) return test_success;
append_test_log(s, "initial run was nonzero");
return test_failure;
@ -83,7 +81,6 @@ def_test(initial_pass_count, s)
def_test(initial_fail_count, s)
{
TestState *scpy = s->ptr;
print("\tfailed\n");
if (scpy->failed == 0) return test_success;
append_test_log(s, "initial failed was nonzero");
return test_failure;
@ -101,7 +98,6 @@ def_test(initial_pend_count, s)
def_test(initial_first_log, s)
{
TestState *scpy = s->ptr;
print("\tfirst_log\n");
if (scpy->first_log == 0) return test_success;
append_test_log(s, "initial first_log was not null");
return test_failure;
@ -110,7 +106,6 @@ def_test(initial_first_log, s)
def_test(initial_last_log, s)
{
TestState *scpy = s->ptr;
print("\tlast_log\n");
if (scpy->last_log == 0) return test_success;
append_test_log(s, "initial last_log was not null");
return test_failure;
@ -119,7 +114,6 @@ def_test(initial_last_log, s)
def_test(initial_ptr, s)
{
TestState *scpy = s->ptr;
print("\tptr\n");
if (scpy->ptr == 0) return test_success;
append_test_log(s, "initial ptr was not null");
return test_failure;
@ -128,7 +122,6 @@ def_test(initial_ptr, s)
def_test(initial_context, s)
{
TestState *scpy = s->ptr;
print("\tcontext\n");
if (scpy->context == 0) return test_success;
append_test_log(s, "initial context was not null");
return test_failure;
@ -137,7 +130,6 @@ def_test(initial_context, s)
def_test(initial_full_context, s)
{
TestState *scpy = s->ptr;
print("\tfull_context\n");
if (scpy->context == 0) return test_success;
append_test_log(s, "initial full_context was not null");
return test_failure;
@ -146,7 +138,6 @@ def_test(initial_full_context, s)
def_test(initial_depth, s)
{
TestState *scpy = s->ptr;
print("\tdepth\n");
if (scpy->depth == 0) return test_success;
append_test_log(s, "initial depth was not zero");
return test_failure;
@ -155,7 +146,6 @@ def_test(initial_depth, s)
def_test(initial_report, s)
{
TestState *scpy = s->ptr;
print("\treport()\n");
if (scpy->report != 0) return test_success;
append_test_log(s, "initial report was null");
return test_failure;

View File

@ -53,7 +53,7 @@ tests(TestState *s)
memcpy(&scpy, s, sizeof(TestState));
s->ptr = &scpy;
test_initial_state(s);
test_context(s, "initial state", test_initial_state);
test_run_test(s);
test_append_test_log(s);
test_context(s, "test_context()", test_test_context);