9unit/test/test-context.c

151 lines
3.1 KiB
C
Raw Normal View History

/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
#include "../9unit.h"
#include "util.h"
#include "test-context.h"
// Local Types
typedef struct ContextData ContextData;
struct ContextData
{
TestState state;
const char *initial_context;
const char *initial_full_context;
int initial_depth;
const char *new_context;
const char *sub_context;
const char *sub_full_context;
int sub_depth;
const char *expected_sub_full_context;
int expected_sub_depth;
};
// Local Prototypes
static void no_prior_context(TestState *);
static void init_ContextData(ContextData *);
static void run_test_context(ContextData *);
static void get_context(TestState *);
static void check_results(TestState *, ContextData *);
static void check_sub_context(TestState *);
static void clean_up(ContextData *);
// Public Functions
void
test_test_context(TestState *s)
{
test_context(s, "no prior context", no_prior_context);
}
// Local Functions
static void
no_prior_context(TestState *s)
{
// set initial state
ContextData cd;
cd.new_context = "my test";
cd.initial_context = 0;
cd.initial_full_context = 0;
cd.initial_depth = 0;
init_ContextData(&cd);
run_test_context(&cd);
cd.expected_sub_full_context = "my test";
cd.expected_sub_depth = 1;
check_results(s, &cd);
clean_up(&cd);
}
static void
init_ContextData(ContextData *cd)
{
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;
test_context(&cd->state, cd->new_context, get_context);
cd->state.ptr = old_ptr;
}
static void
get_context(TestState *s)
{
ContextData *cd = s->ptr;
// get the context
if (s->context)
{
char *c = malloc(strlen(s->context) + 1);
strcpy(c, s->context);
cd->sub_context = c;
}
else cd->sub_context = 0;
// get the full context
if (s->full_context)
{
char *fc = malloc(strlen(s->full_context) + 1);
strcpy(fc, s->full_context);
cd->sub_full_context = fc;
}
else cd->sub_full_context = 0;
// get the depth
cd->sub_depth = s->depth;
}
static void
check_results(TestState *s, ContextData *cd)
{
void *old_ptr = s->ptr;
s->ptr = cd;
test_context(s, "sub context", check_sub_context);
s->ptr = old_ptr;
}
static void
check_sub_context(TestState *s)
{
ContextData *cd = s->ptr;
}
static void
clean_up(ContextData *cd)
{
}
//jl