Compare commits

...

2 Commits

Author SHA1 Message Date
jlamothe 0ad79ea3b0 define and start using single_text_context() 2023-11-16 00:52:01 +00:00
jlamothe ef00063ba3 built and started to test test_context() 2023-11-15 23:46:25 +00:00
7 changed files with 328 additions and 24 deletions

107
9unit.c
View File

@ -25,6 +25,28 @@
#include "9unit.h"
// Internal Types
// data required by the context management functions
typedef struct ContextData ContextData;
// data required to run a single test with a label
typedef struct SingleTestContext SingleTestContext;
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
};
struct SingleTestContext
{
void *ptr; // the state's previous ptr value
TestResult (*test)(TestState *); // the test to run
};
// Internal Prototypes
static void init_TestState(TestState *);
@ -32,6 +54,10 @@ 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 *);
static void run_single_test_context(TestState *);
// Public Functions
@ -111,6 +137,41 @@ 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);
}
void
single_test_context(
TestState *s,
const char *label,
TestResult (*test)(TestState *)
)
{
if (!s) return;
SingleTestContext stc;
stc.ptr = s->ptr;
stc.test = test;
s->ptr = &stc;
test_context(s, label, run_single_test_context);
}
// Internal Functions
static void
@ -169,4 +230,50 @@ 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);
}
static void
run_single_test_context(TestState *s)
{
SingleTestContext *stc = s->ptr;
s->ptr = stc->ptr;
run_test(s, stc->test);
}
//jl

18
9unit.h
View File

@ -19,6 +19,8 @@
*/
// Types & Structs
// Tracks information about the tests being run.
typedef struct TestState TestState;
@ -57,6 +59,8 @@ typedef enum TestResult
test_pending // the test is pending
} TestResult;
// Functions
// Runs a single test
extern void run_test(
TestState *, // the TestState data
@ -76,4 +80,18 @@ extern void append_test_log(
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
);
// Runs a single test with a context label
extern void single_test_context(
TestState *, // the current state
const char *, // a description of the context
TestResult (*)(TestState *) // the actual test
);
//jl

View File

@ -46,18 +46,17 @@ decl_test(initial_report);
void
test_initial_state(TestState *s)
{
print("initial state\n");
run_test(s, initial_test_count);
run_test(s, initial_pass_count);
run_test(s, initial_fail_count);
run_test(s, initial_pend_count);
run_test(s, initial_first_log);
run_test(s, initial_last_log);
run_test(s, initial_ptr);
run_test(s, initial_context);
run_test(s, initial_full_context);
run_test(s, initial_depth);
run_test(s, initial_report);
single_test_context(s, "run", initial_test_count);
single_test_context(s, "passsed", initial_pass_count);
single_test_context(s, "failed", initial_fail_count);
single_test_context(s, "pending", initial_pend_count);
single_test_context(s, "first_log", initial_first_log);
single_test_context(s, "last_log", initial_last_log);
single_test_context(s, "ptr", initial_ptr);
single_test_context(s, "context", initial_context);
single_test_context(s, "full_context", initial_full_context);
single_test_context(s, "depth", initial_depth);
single_test_context(s, "report()", initial_report);
}
// Internal Functions
@ -65,7 +64,6 @@ test_initial_state(TestState *s)
def_test(initial_test_count, s)
{
TestState *scpy = s->ptr;
print("\trun\n");
if (scpy->run == 0) return test_success;
append_test_log(s, "initial run was nonzero");
return test_failure;
@ -83,7 +81,6 @@ def_test(initial_pass_count, s)
def_test(initial_fail_count, s)
{
TestState *scpy = s->ptr;
print("\tfailed\n");
if (scpy->failed == 0) return test_success;
append_test_log(s, "initial failed was nonzero");
return test_failure;
@ -101,7 +98,6 @@ def_test(initial_pend_count, s)
def_test(initial_first_log, s)
{
TestState *scpy = s->ptr;
print("\tfirst_log\n");
if (scpy->first_log == 0) return test_success;
append_test_log(s, "initial first_log was not null");
return test_failure;
@ -110,7 +106,6 @@ def_test(initial_first_log, s)
def_test(initial_last_log, s)
{
TestState *scpy = s->ptr;
print("\tlast_log\n");
if (scpy->last_log == 0) return test_success;
append_test_log(s, "initial last_log was not null");
return test_failure;
@ -119,7 +114,6 @@ def_test(initial_last_log, s)
def_test(initial_ptr, s)
{
TestState *scpy = s->ptr;
print("\tptr\n");
if (scpy->ptr == 0) return test_success;
append_test_log(s, "initial ptr was not null");
return test_failure;
@ -128,7 +122,6 @@ def_test(initial_ptr, s)
def_test(initial_context, s)
{
TestState *scpy = s->ptr;
print("\tcontext\n");
if (scpy->context == 0) return test_success;
append_test_log(s, "initial context was not null");
return test_failure;
@ -137,7 +130,6 @@ def_test(initial_context, s)
def_test(initial_full_context, s)
{
TestState *scpy = s->ptr;
print("\tfull_context\n");
if (scpy->context == 0) return test_success;
append_test_log(s, "initial full_context was not null");
return test_failure;
@ -146,7 +138,6 @@ def_test(initial_full_context, s)
def_test(initial_depth, s)
{
TestState *scpy = s->ptr;
print("\tdepth\n");
if (scpy->depth == 0) return test_success;
append_test_log(s, "initial depth was not zero");
return test_failure;
@ -155,7 +146,6 @@ def_test(initial_depth, s)
def_test(initial_report, s)
{
TestState *scpy = s->ptr;
print("\treport()\n");
if (scpy->report != 0) return test_success;
append_test_log(s, "initial report was null");
return test_failure;

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
@ -52,9 +53,10 @@ tests(TestState *s)
memcpy(&scpy, s, sizeof(TestState));
s->ptr = &scpy;
test_initial_state(s);
test_context(s, "initial state", test_initial_state);
test_run_test(s);
test_append_test_log(s);
test_context(s, "test_context()", test_test_context);
}
//jl