Compare commits

...

3 Commits

Author SHA1 Message Date
jlamothe b9cac45eba report function should ignore formatting 2023-11-15 00:04:35 +00:00
jlamothe cead3f67d0 created overridable report() function to be used in place of print() 2023-11-14 23:53:43 +00:00
jlamothe 85a6d49b38 added context info to TestState 2023-11-14 23:08:19 +00:00
5 changed files with 176 additions and 26 deletions

23
9unit.c
View File

@ -31,6 +31,7 @@ static void init_TestState(TestState *);
static void print_log(TestState *);
static void clear_log(TestState *);
static void reindex(TestState *);
static void report(const char *);
// Public Functions
@ -68,7 +69,8 @@ run_tests(void (*tests)(TestState *))
{
if (!tests) return;
TestState s;
init_TestState(&s);
memset(&s, 0, sizeof(TestState));
s.report = report;
(*tests)(&s);
print_log(&s);
printf("Tests run: %d\n", s.run);
@ -111,19 +113,6 @@ append_test_log(TestState *s, const char *msg)
// Internal Functions
static void
init_TestState(TestState *s)
{
if (!s) return;
s->run = 0;
s->passed = 0;
s->failed = 0;
s->pending = 0;
s->first_log = 0;
s->last_log = 0;
s->ptr = 0;
}
static void
print_log(TestState *s)
{
@ -174,4 +163,10 @@ reindex(TestState *s)
}
}
static void
report(const char *str)
{
print("%s", str);
}
//jl

View File

@ -37,6 +37,10 @@ struct TestState
TestLogEntry *first_log; // the first log entry
TestLogEntry *last_log; //the last log entry
void *ptr; // used for passing data between tests
const char *context; // immediate context of current test
const char *full_context; // full context of current test
int depth; // how many tests "deep" are we?
void (*report)(const char *); // prints a string immediately
};
struct TestLogEntry

View File

@ -36,6 +36,10 @@ decl_test(initial_pend_count);
decl_test(initial_first_log);
decl_test(initial_last_log);
decl_test(initial_ptr);
decl_test(initial_context);
decl_test(initial_full_context);
decl_test(initial_depth);
decl_test(initial_report);
// Public Functions
@ -50,6 +54,10 @@ test_initial_state(TestState *s)
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);
}
// Internal Functions
@ -117,4 +125,40 @@ def_test(initial_ptr, s)
return test_failure;
}
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;
}
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;
}
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;
}
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;
}
//jl

View File

@ -59,10 +59,8 @@ test_pass(TestState *s)
print("\tpassing\n");
// expected result
memset(&expected, 0, sizeof(TestState));
mk_sample_state(&expected);
expected.passed = 2;
expected.failed = 2;
expected.pending = 3;
expected.run = 7;
// actual result
@ -85,10 +83,8 @@ test_fail(TestState *s)
print("\tfailing\n");
// expected result
memset(&expected, 0, sizeof(TestState));
expected.passed = 1;
mk_sample_state(&expected);
expected.failed = 3;
expected.pending = 3;
expected.run = 7;
// actual result
@ -111,9 +107,7 @@ test_pend(TestState *s)
print("\tpending\n");
// expected result
memset(&expected, 0, sizeof(TestState));
expected.passed = 1;
expected.failed = 2;
mk_sample_state(&expected);
expected.pending = 4;
expected.run = 7;
@ -144,9 +138,7 @@ null_test(TestState *s)
print("\tnull test\n");
// expected value
memset(&expected, 0, sizeof(TestState));
expected.passed = 1;
expected.failed = 2;
mk_sample_state(&expected);
expected.pending = 4;
expected.run = 7;

View File

@ -110,6 +110,45 @@ static void chk_TestState_ptr_eq(
const TestState *
);
// compare the context value of two states
static void chk_TestState_context_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the full_context value of two states
static void chk_TestState_full_context_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the depth value of two states
static void chk_TestState_depth_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the report pointer of two states
static void chk_TestState_report_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// discard report data
static void report(const char *);
decl_test(chk_int_eq_test); // ensure ints are equal
decl_test(chk_ptr_eq_test); // ensure pointers are equal
decl_test(chk_ptr_ne_test); // ensure pointers are not equal
@ -125,6 +164,7 @@ mk_sample_state(TestState *s)
s->failed = 2;
s->pending = 3;
s->run = 6;
s->report = report;
}
void
@ -143,6 +183,10 @@ chk_TestState_eq(
chk_TestState_first_log_eq(s, prefix, context, actual, expected);
chk_TestState_last_log_eq(s, prefix, context, actual, expected);
chk_TestState_ptr_eq(s, prefix, context, actual, expected);
chk_TestState_context_eq(s, prefix, context, actual, expected);
chk_TestState_full_context_eq(s, prefix, context, actual, expected);
chk_TestState_depth_eq(s, prefix, context, actual, expected);
chk_TestState_report_eq(s, prefix, context, actual, expected);
}
void
@ -331,6 +375,70 @@ chk_TestState_ptr_eq(
chk_ptr_eq(s, full_context, actual->ptr, expected->ptr);
}
static void
chk_TestState_context_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("context\n");
snprintf(full_context, STR_BUF_SIZE, "%s context:", context);
chk_str_eq(s, full_context, actual->context, expected->context);
}
static void
chk_TestState_full_context_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("full_context\n");
snprintf(full_context, STR_BUF_SIZE, "%s full_context:", context);
chk_str_eq(s, full_context, actual->full_context, expected->full_context);
}
static void
chk_TestState_depth_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("depth\n");
snprintf(full_context, STR_BUF_SIZE, "%s depth:", context);
chk_int_eq(s, full_context, actual->depth, expected->depth);
}
static void
chk_TestState_report_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("report()\n");
snprintf(full_context, STR_BUF_SIZE, "%s report():", context);
chk_ptr_eq(s, full_context, actual->report, expected->report);
}
def_test(chk_int_eq_test, s)
{
const CompareInts *ci = s->ptr;
@ -371,6 +479,7 @@ def_test(chk_ptr_ne_test, s)
def_test(chk_str_eq_test, s)
{
const ComparePtrs *cp = s->ptr;
if (!cp->chk_val && !cp->ref_val) return test_success;
if (!strcmp(cp->chk_val, cp->ref_val)) return test_success;
char str[STR_BUF_SIZE];
append_test_log(s, cp->context);
@ -381,4 +490,10 @@ def_test(chk_str_eq_test, s)
return test_failure;
}
static void
report(const char *)
{
return;
}
//jl