Compare commits
No commits in common. "e5cad7cd20030a712e48d5f8fdb3706d8942a2bc" and "57e4d2ba49746e4b276d96ba9bb7231f46112a07" have entirely different histories.
e5cad7cd20
...
57e4d2ba49
15
9unit.c
15
9unit.c
|
@ -119,10 +119,10 @@ run_tests(void (*tests)(TestState *))
|
|||
s.report = report;
|
||||
(*tests)(&s);
|
||||
print_log(&s);
|
||||
print("\n\nTests run: %d\n", s.run);
|
||||
print("Tests passed: %d\n", s.passed);
|
||||
print("Tests failed: %d\n", s.failed);
|
||||
print("Tests pending: %d\n", s.pending);
|
||||
printf("Tests run: %d\n", s.run);
|
||||
printf("Tests passed: %d\n", s.passed);
|
||||
printf("Tests failed: %d\n", s.failed);
|
||||
printf("Tests pending: %d\n", s.pending);
|
||||
clear_log(&s);
|
||||
if (s.failed) exits("test(s) failed");
|
||||
}
|
||||
|
@ -182,15 +182,12 @@ run_test(TestState *s, TestResult (*t)(TestState *))
|
|||
{
|
||||
case test_success:
|
||||
s->passed++;
|
||||
s->report(" [PASS]");
|
||||
break;
|
||||
case test_failure:
|
||||
s->failed++;
|
||||
s->report(" [FAIL]");
|
||||
break;
|
||||
case test_pending:
|
||||
s->pending++;
|
||||
s->report(" [PEND]");
|
||||
break;
|
||||
default:
|
||||
exits("test returned an invalid response");
|
||||
|
@ -350,7 +347,7 @@ print_log(TestState *s)
|
|||
TestLogEntry *e = s->first_log;
|
||||
while(e)
|
||||
{
|
||||
if(e->text) print("%s\n", e->text);
|
||||
if(e->text) printf("%s\n", e->text);
|
||||
else print("(empty message)\n");
|
||||
e = e->next;
|
||||
}
|
||||
|
@ -422,10 +419,10 @@ build_new_context(TestState *s, ContextData *cd)
|
|||
static void
|
||||
display_context(TestState *s)
|
||||
{
|
||||
s->report("\n");
|
||||
for (int i = 1; i < s->depth; i++)
|
||||
s->report("\t");
|
||||
s->report(s->context);
|
||||
s->report("\n");
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
@ -136,9 +136,8 @@ static void
|
|||
chk_last(TestState *s, void *ptr)
|
||||
{
|
||||
LogData *ld = ptr;
|
||||
chk_ptr_chg(s, 0, ld->state.last_log, &ld->second);
|
||||
chk_str_eq(s, "text", ld->state.last_log->text, "baz");
|
||||
chk_ptr_eq(s, "next", ld->state.last_log->next, 0);
|
||||
chk_ptr_chg(s, "last_log", ld->state.last_log, &ld->second);
|
||||
chk_str_eq(s, "last_log->text", ld->state.last_log->text, "baz");
|
||||
free(ld->state.last_log->text);
|
||||
free(ld->state.last_log);
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#include "util.h"
|
||||
#include "test-context.h"
|
||||
|
||||
// Internal Types
|
||||
// Local Types
|
||||
|
||||
typedef struct ContextData ContextData;
|
||||
|
||||
|
@ -37,21 +37,21 @@ struct ContextData
|
|||
const char *initial_full_context;
|
||||
int initial_depth;
|
||||
const char *new_context;
|
||||
const char *expected_sub_full_context;
|
||||
int expected_sub_depth;
|
||||
const char *sub_context;
|
||||
const char *sub_full_context;
|
||||
int sub_depth;
|
||||
const char *expected_sub_full_context;
|
||||
int expected_sub_depth;
|
||||
};
|
||||
|
||||
// Internal Prototypes
|
||||
// Local Prototypes
|
||||
|
||||
static void no_prior_context(TestState *);
|
||||
static void prior_context(TestState *);
|
||||
static void test_context_common(TestState *, ContextData *);
|
||||
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
|
||||
|
@ -60,87 +60,46 @@ void
|
|||
test_test_context(TestState *s)
|
||||
{
|
||||
test_context(s, "no prior context", no_prior_context);
|
||||
test_context(s, "prior context", prior_context);
|
||||
}
|
||||
|
||||
// Internal Functions
|
||||
// Local Functions
|
||||
|
||||
static void
|
||||
no_prior_context(TestState *s)
|
||||
{
|
||||
ContextData cd;
|
||||
|
||||
// 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);
|
||||
|
||||
// set expectations
|
||||
run_test_context(&cd);
|
||||
cd.expected_sub_full_context = "my test";
|
||||
cd.expected_sub_depth = 1;
|
||||
|
||||
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;
|
||||
|
||||
test_context_common(s, &cd);
|
||||
}
|
||||
|
||||
static void
|
||||
test_context_common(
|
||||
TestState *s,
|
||||
ContextData *cd
|
||||
)
|
||||
{
|
||||
init_ContextData(cd);
|
||||
test_context(&cd->state, cd->new_context, get_context);
|
||||
check_results(s, cd);
|
||||
clean_up(cd);
|
||||
check_results(s, &cd);
|
||||
clean_up(&cd);
|
||||
}
|
||||
|
||||
static void
|
||||
init_ContextData(ContextData *cd)
|
||||
{
|
||||
mk_sample_state(&cd->state);
|
||||
cd->state.ptr = cd;
|
||||
|
||||
// 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.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)
|
||||
{
|
||||
|
@ -169,67 +128,23 @@ get_context(TestState *s)
|
|||
}
|
||||
|
||||
static void
|
||||
check_results(
|
||||
TestState *s,
|
||||
ContextData *cd
|
||||
)
|
||||
check_results(TestState *s, ContextData *cd)
|
||||
{
|
||||
// sub context
|
||||
chk_str_eq(
|
||||
s,
|
||||
"sub context",
|
||||
cd->sub_context,
|
||||
cd->new_context
|
||||
);
|
||||
void *old_ptr = s->ptr;
|
||||
s->ptr = cd;
|
||||
test_context(s, "sub context", check_sub_context);
|
||||
s->ptr = old_ptr;
|
||||
}
|
||||
|
||||
// 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
|
||||
);
|
||||
static void
|
||||
check_sub_context(TestState *s)
|
||||
{
|
||||
ContextData *cd = s->ptr;
|
||||
}
|
||||
|
||||
static void
|
||||
clean_up(ContextData *cd)
|
||||
{
|
||||
free(cd->state.context);
|
||||
free(cd->state.full_context);
|
||||
free(cd->sub_context);
|
||||
free(cd->sub_full_context);
|
||||
}
|
||||
|
||||
//jl
|
||||
|
|
Loading…
Reference in New Issue
Block a user