built and started to test test_context()

This commit is contained in:
jlamothe
2023-11-15 23:46:25 +00:00
parent b9cac45eba
commit ef00063ba3
6 changed files with 272 additions and 2 deletions

74
9unit.c
View File

@@ -25,6 +25,19 @@
#include "9unit.h"
// Internal Types
// data required by the context management functions
typedef struct ContextData ContextData;
struct ContextData
{
const char *old_c; // previous context
const char *old_fc; // previous full context
const char *new_c; // new context
char *new_fc; // new full context
};
// Internal Prototypes
static void init_TestState(TestState *);
@@ -32,6 +45,9 @@ static void print_log(TestState *);
static void clear_log(TestState *);
static void reindex(TestState *);
static void report(const char *);
static void build_new_context(TestState *, ContextData *);
static void display_context(TestState *);
static void restore_context(TestState *, ContextData *);
// Public Functions
@@ -111,6 +127,26 @@ append_test_log(TestState *s, const char *msg)
s->last_log = entry;
}
void
test_context(
TestState *s,
const char *label,
void (*test)(TestState *)
)
{
if (!(s && test)) return;
if (label)
{
ContextData cd;
cd.new_c = label;
build_new_context(s, &cd);
display_context(s);
(*test)(s);
restore_context(s, &cd);
}
else (*test)(s);
}
// Internal Functions
static void
@@ -169,4 +205,42 @@ report(const char *str)
print("%s", str);
}
static void
build_new_context(TestState *s, ContextData *cd)
{
cd->old_c = s->context;
cd->old_fc = s->full_context;
if (s->full_context)
{
cd->new_fc = malloc(strlen(cd->old_fc) + strlen(cd->new_c) + 3);
sprintf(cd->new_fc, "%s: %s", cd->old_fc, cd->new_c);
}
else
{
cd->new_fc = malloc(strlen(cd->new_c) + 1);
strcpy(cd->new_fc, cd->new_c);
}
s->context = cd->new_c;
s->full_context = cd->new_fc;
s->depth++;
}
static void
display_context(TestState *s)
{
for (int i = 1; i < s->depth; i++)
s->report("\t");
s->report(s->context);
s->report("\n");
}
static void
restore_context(TestState *s, ContextData *cd)
{
s->context = cd->old_c;
s->full_context = cd->old_fc;
s->depth--;
free(cd->new_fc);
}
//jl