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

View File

@@ -17,8 +17,21 @@
</$objtype/mkfile
HFILES=../9unit.h util.h initial-state.h run-test.h append-test-log.h
OFILES=util.$O initial-state.$O run-test.$O append-test-log.$O
HFILES=\
../9unit.h\
util.h\
initial-state.h\
run-test.h\
append-test-log.h\
test-context.h
OFILES=\
util.$O\
initial-state.$O\
run-test.$O\
append-test-log.$O\
test-context.$O
LIB=../9unit.a
TEST=tests

150
test/test-context.c Normal file
View File

@@ -0,0 +1,150 @@
/*
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

24
test/test-context.h Normal file
View File

@@ -0,0 +1,24 @@
/*
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/>.
*/
extern void test_test_context(TestState *);
//jl

View File

@@ -26,6 +26,7 @@
#include "initial-state.h"
#include "run-test.h"
#include "append-test-log.h"
#include "test-context.h"
// Internal Prototypes
@@ -55,6 +56,7 @@ tests(TestState *s)
test_initial_state(s);
test_run_test(s);
test_append_test_log(s);
test_context(s, "test_context()", test_test_context);
}
//jl