built and started to test test_context()
This commit is contained in:
parent
b9cac45eba
commit
ef00063ba3
74
9unit.c
74
9unit.c
|
@ -25,6 +25,19 @@
|
||||||
|
|
||||||
#include "9unit.h"
|
#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
|
// Internal Prototypes
|
||||||
|
|
||||||
static void init_TestState(TestState *);
|
static void init_TestState(TestState *);
|
||||||
|
@ -32,6 +45,9 @@ static void print_log(TestState *);
|
||||||
static void clear_log(TestState *);
|
static void clear_log(TestState *);
|
||||||
static void reindex(TestState *);
|
static void reindex(TestState *);
|
||||||
static void report(const char *);
|
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
|
// Public Functions
|
||||||
|
|
||||||
|
@ -111,6 +127,26 @@ append_test_log(TestState *s, const char *msg)
|
||||||
s->last_log = entry;
|
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
|
// Internal Functions
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -169,4 +205,42 @@ report(const char *str)
|
||||||
print("%s", 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
|
//jl
|
||||||
|
|
7
9unit.h
7
9unit.h
|
@ -76,4 +76,11 @@ extern void append_test_log(
|
||||||
const char * // the message to append
|
const char * // the message to append
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Gives additional context for a test
|
||||||
|
extern void test_context(
|
||||||
|
TestState *, // the current state
|
||||||
|
const char *, // a description of the context
|
||||||
|
void (*)(TestState *) // the actual test
|
||||||
|
);
|
||||||
|
|
||||||
//jl
|
//jl
|
||||||
|
|
17
test/mkfile
17
test/mkfile
|
@ -17,8 +17,21 @@
|
||||||
|
|
||||||
</$objtype/mkfile
|
</$objtype/mkfile
|
||||||
|
|
||||||
HFILES=../9unit.h util.h initial-state.h run-test.h append-test-log.h
|
HFILES=\
|
||||||
OFILES=util.$O initial-state.$O run-test.$O append-test-log.$O
|
../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
|
LIB=../9unit.a
|
||||||
TEST=tests
|
TEST=tests
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -26,6 +26,7 @@
|
||||||
#include "initial-state.h"
|
#include "initial-state.h"
|
||||||
#include "run-test.h"
|
#include "run-test.h"
|
||||||
#include "append-test-log.h"
|
#include "append-test-log.h"
|
||||||
|
#include "test-context.h"
|
||||||
|
|
||||||
// Internal Prototypes
|
// Internal Prototypes
|
||||||
|
|
||||||
|
@ -55,6 +56,7 @@ tests(TestState *s)
|
||||||
test_initial_state(s);
|
test_initial_state(s);
|
||||||
test_run_test(s);
|
test_run_test(s);
|
||||||
test_append_test_log(s);
|
test_append_test_log(s);
|
||||||
|
test_context(s, "test_context()", test_test_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
//jl
|
//jl
|
||||||
|
|
Loading…
Reference in New Issue
Block a user