From cead3f67d0975e1f05a8c62d9ea0e2d816740159 Mon Sep 17 00:00:00 2001 From: jlamothe Date: Tue, 14 Nov 2023 23:53:43 +0000 Subject: [PATCH] created overridable report() function to be used in place of print() --- 9unit.c | 8 ++++++++ 9unit.h | 1 + test/initial-state.c | 11 +++++++++++ test/run-test.c | 16 ++++------------ test/util.c | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/9unit.c b/9unit.c index c0573fd..e75d7ba 100644 --- a/9unit.c +++ b/9unit.c @@ -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 diff --git a/9unit.h b/9unit.h index 7b1b2d1..9641f1e 100644 --- a/9unit.h +++ b/9unit.h @@ -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 diff --git a/test/initial-state.c b/test/initial-state.c index b6c3602..cf00ee8 100644 --- a/test/initial-state.c +++ b/test/initial-state.c @@ -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 diff --git a/test/run-test.c b/test/run-test.c index 108d1f2..ce5008e 100644 --- a/test/run-test.c +++ b/test/run-test.c @@ -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; diff --git a/test/util.c b/test/util.c index aad296d..f45ee3a 100644 --- a/test/util.c +++ b/test/util.c @@ -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