created overridable report() function to be used in place of print()

This commit is contained in:
jlamothe 2023-11-14 23:53:43 +00:00
parent 85a6d49b38
commit cead3f67d0
5 changed files with 60 additions and 12 deletions

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
@ -69,6 +70,7 @@ run_tests(void (*tests)(TestState *))
if (!tests) return;
TestState s;
memset(&s, 0, sizeof(TestState));
s.report = report;
(*tests)(&s);
print_log(&s);
printf("Tests run: %d\n", s.run);
@ -161,4 +163,10 @@ reindex(TestState *s)
}
}
static void
report(const char *str)
{
print(str);
}
//jl

View File

@ -40,6 +40,7 @@ struct TestState
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

@ -39,6 +39,7 @@ decl_test(initial_ptr);
decl_test(initial_context);
decl_test(initial_full_context);
decl_test(initial_depth);
decl_test(initial_report);
// Public Functions
@ -56,6 +57,7 @@ test_initial_state(TestState *s)
run_test(s, initial_context);
run_test(s, initial_full_context);
run_test(s, initial_depth);
run_test(s, initial_report);
}
// Internal Functions
@ -150,4 +152,13 @@ def_test(initial_depth, s)
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

@ -137,6 +137,18 @@ static void chk_TestState_depth_eq(
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
@ -152,6 +164,7 @@ mk_sample_state(TestState *s)
s->failed = 2;
s->pending = 3;
s->run = 6;
s->report = report;
}
void
@ -173,6 +186,7 @@ chk_TestState_eq(
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
@ -409,6 +423,22 @@ chk_TestState_depth_eq(
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;
@ -460,4 +490,10 @@ def_test(chk_str_eq_test, s)
return test_failure;
}
static void
report(const char *)
{
return;
}
//jl