2023-11-15 18:46:25 -05:00
|
|
|
/*
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2023-11-21 14:44:35 -05:00
|
|
|
// Internal Types
|
2023-11-15 18:46:25 -05:00
|
|
|
|
|
|
|
typedef struct ContextData ContextData;
|
|
|
|
|
|
|
|
struct ContextData
|
|
|
|
{
|
|
|
|
TestState state;
|
|
|
|
const char *initial_context;
|
|
|
|
const char *initial_full_context;
|
|
|
|
int initial_depth;
|
|
|
|
const char *new_context;
|
2023-11-21 14:44:35 -05:00
|
|
|
const char *expected_sub_full_context;
|
|
|
|
int expected_sub_depth;
|
2023-11-15 18:46:25 -05:00
|
|
|
const char *sub_context;
|
|
|
|
const char *sub_full_context;
|
|
|
|
int sub_depth;
|
|
|
|
};
|
|
|
|
|
2023-11-21 14:44:35 -05:00
|
|
|
// Internal Prototypes
|
2023-11-15 18:46:25 -05:00
|
|
|
|
|
|
|
static void no_prior_context(TestState *);
|
2023-11-21 14:49:19 -05:00
|
|
|
static void prior_context(TestState *);
|
2023-11-21 14:44:35 -05:00
|
|
|
static void test_context_common(TestState *, ContextData *);
|
2023-11-15 18:46:25 -05:00
|
|
|
static void init_ContextData(ContextData *);
|
|
|
|
static void get_context(TestState *);
|
|
|
|
static void check_results(TestState *, ContextData *);
|
|
|
|
static void clean_up(ContextData *);
|
|
|
|
|
|
|
|
// Public Functions
|
|
|
|
|
|
|
|
void
|
|
|
|
test_test_context(TestState *s)
|
|
|
|
{
|
|
|
|
test_context(s, "no prior context", no_prior_context);
|
2023-11-21 14:49:19 -05:00
|
|
|
test_context(s, "prior context", prior_context);
|
2023-11-15 18:46:25 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 14:44:35 -05:00
|
|
|
// Internal Functions
|
2023-11-15 18:46:25 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
no_prior_context(TestState *s)
|
|
|
|
{
|
|
|
|
ContextData cd;
|
2023-11-21 14:44:35 -05:00
|
|
|
|
|
|
|
// set initial state
|
2023-11-15 18:46:25 -05:00
|
|
|
cd.new_context = "my test";
|
|
|
|
cd.initial_context = 0;
|
|
|
|
cd.initial_full_context = 0;
|
|
|
|
cd.initial_depth = 0;
|
|
|
|
|
2023-11-21 14:44:35 -05:00
|
|
|
// set expectations
|
2023-11-15 18:46:25 -05:00
|
|
|
cd.expected_sub_full_context = "my test";
|
|
|
|
cd.expected_sub_depth = 1;
|
2023-11-21 14:44:35 -05:00
|
|
|
|
2023-11-21 14:49:19 -05:00
|
|
|
test_context_common(s, &cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prior_context(TestState *s)
|
|
|
|
{
|
|
|
|
ContextData cd;
|
|
|
|
|
|
|
|
// set initial state
|
|
|
|
cd.new_context = "my other test";
|
|
|
|
cd.initial_context = "bar";
|
|
|
|
cd.initial_full_context = "foo: bar";
|
|
|
|
cd.initial_depth = 2;
|
|
|
|
|
|
|
|
// set expectations
|
|
|
|
cd.expected_sub_full_context = "foo: bar: my other test";
|
|
|
|
cd.expected_sub_depth = 3;
|
|
|
|
|
2023-11-21 14:44:35 -05:00
|
|
|
test_context_common(s, &cd);
|
2023-11-15 18:46:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-11-21 14:44:35 -05:00
|
|
|
test_context_common(
|
|
|
|
TestState *s,
|
|
|
|
ContextData *cd
|
|
|
|
)
|
2023-11-15 18:46:25 -05:00
|
|
|
{
|
2023-11-21 14:44:35 -05:00
|
|
|
init_ContextData(cd);
|
|
|
|
test_context(&cd->state, cd->new_context, get_context);
|
|
|
|
check_results(s, cd);
|
|
|
|
clean_up(cd);
|
2023-11-15 18:46:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-11-21 14:44:35 -05:00
|
|
|
init_ContextData(ContextData *cd)
|
2023-11-15 18:46:25 -05:00
|
|
|
{
|
2023-11-21 14:44:35 -05:00
|
|
|
mk_sample_state(&cd->state);
|
2023-11-15 18:46:25 -05:00
|
|
|
cd->state.ptr = cd;
|
2023-11-21 14:44:35 -05:00
|
|
|
|
|
|
|
// 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;
|
2023-11-15 18:46:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-11-21 14:44:35 -05:00
|
|
|
check_results(
|
|
|
|
TestState *s,
|
|
|
|
ContextData *cd
|
|
|
|
)
|
2023-11-15 18:46:25 -05:00
|
|
|
{
|
2023-11-21 14:44:35 -05:00
|
|
|
// sub context
|
|
|
|
chk_str_eq(
|
|
|
|
s,
|
|
|
|
"sub context",
|
|
|
|
cd->sub_context,
|
|
|
|
cd->new_context
|
|
|
|
);
|
|
|
|
|
|
|
|
// sub full_context
|
|
|
|
chk_str_eq(
|
|
|
|
s,
|
|
|
|
"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
|
|
|
|
);
|
2023-11-15 18:46:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clean_up(ContextData *cd)
|
|
|
|
{
|
2023-11-21 14:44:35 -05:00
|
|
|
free(cd->state.context);
|
|
|
|
free(cd->state.full_context);
|
|
|
|
free(cd->sub_context);
|
|
|
|
free(cd->sub_full_context);
|
2023-11-15 18:46:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//jl
|