test test_context() on a state with no initial context

This commit is contained in:
jlamothe 2023-11-21 19:44:35 +00:00
parent bea9a3e34a
commit ebf398fd33

View File

@ -26,7 +26,7 @@
#include "util.h" #include "util.h"
#include "test-context.h" #include "test-context.h"
// Local Types // Internal Types
typedef struct ContextData ContextData; typedef struct ContextData ContextData;
@ -37,21 +37,20 @@ struct ContextData
const char *initial_full_context; const char *initial_full_context;
int initial_depth; int initial_depth;
const char *new_context; const char *new_context;
const char *expected_sub_full_context;
int expected_sub_depth;
const char *sub_context; const char *sub_context;
const char *sub_full_context; const char *sub_full_context;
int sub_depth; int sub_depth;
const char *expected_sub_full_context;
int expected_sub_depth;
}; };
// Local Prototypes // Internal Prototypes
static void no_prior_context(TestState *); static void no_prior_context(TestState *);
static void test_context_common(TestState *, ContextData *);
static void init_ContextData(ContextData *); static void init_ContextData(ContextData *);
static void run_test_context(ContextData *);
static void get_context(TestState *); static void get_context(TestState *);
static void check_results(TestState *, ContextData *); static void check_results(TestState *, ContextData *);
static void check_sub_context(TestState *);
static void clean_up(ContextData *); static void clean_up(ContextData *);
// Public Functions // Public Functions
@ -62,42 +61,64 @@ test_test_context(TestState *s)
test_context(s, "no prior context", no_prior_context); test_context(s, "no prior context", no_prior_context);
} }
// Local Functions // Internal Functions
static void static void
no_prior_context(TestState *s) no_prior_context(TestState *s)
{ {
// set initial state
ContextData cd; ContextData cd;
// set initial state
cd.new_context = "my test"; cd.new_context = "my test";
cd.initial_context = 0; cd.initial_context = 0;
cd.initial_full_context = 0; cd.initial_full_context = 0;
cd.initial_depth = 0; cd.initial_depth = 0;
init_ContextData(&cd);
run_test_context(&cd); // set expectations
cd.expected_sub_full_context = "my test"; cd.expected_sub_full_context = "my test";
cd.expected_sub_depth = 1; cd.expected_sub_depth = 1;
check_results(s, &cd);
clean_up(&cd); test_context_common(s, &cd);
}
static void
test_context_common(
TestState *s,
ContextData *cd
)
{
init_ContextData(cd);
test_context(&cd->state, cd->new_context, get_context);
check_results(s, cd);
clean_up(cd);
} }
static void static void
init_ContextData(ContextData *cd) init_ContextData(ContextData *cd)
{ {
mk_sample_state(&cd->state); mk_sample_state(&cd->state);
cd->state.context = cd->initial_context;
cd->state.full_context = cd->initial_full_context;
cd->state.depth = cd->initial_depth;
}
static void
run_test_context(ContextData *cd)
{
void *old_ptr = cd->state.ptr;
cd->state.ptr = cd; cd->state.ptr = cd;
test_context(&cd->state, cd->new_context, get_context);
cd->state.ptr = old_ptr; // initial context
if (cd->initial_context)
{
cd->state.context =
malloc(strlen(cd->initial_context) + 1);
strcpy(cd->state.context, cd->initial_context);
}
// initial full_context
if (cd->initial_full_context)
{
cd->state.full_context =
malloc(strlen(cd->initial_full_context) + 1);
strcpy(
cd->state.full_context,
cd->initial_full_context
);
}
cd->state.depth = cd->initial_depth;
} }
static void static void
@ -128,23 +149,67 @@ get_context(TestState *s)
} }
static void static void
check_results(TestState *s, ContextData *cd) check_results(
TestState *s,
ContextData *cd
)
{ {
void *old_ptr = s->ptr; // sub context
s->ptr = cd; chk_str_eq(
test_context(s, "sub context", check_sub_context); s,
s->ptr = old_ptr; "sub context",
} cd->sub_context,
cd->new_context
);
static void // sub full_context
check_sub_context(TestState *s) chk_str_eq(
{ s,
ContextData *cd = s->ptr; "sub full_context",
cd->sub_full_context,
cd->expected_sub_full_context
);
// sub depth
chk_int_eq(
s,
"sub depth",
cd->sub_depth,
cd->expected_sub_depth
);
// final context
chk_str_eq(
s,
"final context",
cd->state.context,
cd->initial_context
);
// final full_context
chk_str_eq(
s,
"final full_context",
cd->state.full_context,
cd->initial_full_context
);
// final depth
chk_int_eq(
s,
"final depth",
cd->state.depth,
cd->initial_depth
);
} }
static void static void
clean_up(ContextData *cd) clean_up(ContextData *cd)
{ {
free(cd->state.context);
free(cd->state.full_context);
free(cd->sub_context);
free(cd->sub_full_context);
} }
//jl //jl