From 10e56d32931ad26d79ac726a36366664cf4d8b63 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 24 Apr 2022 12:49:07 -0500 Subject: add test- and suite-running functions --- lily-test.c | 37 +++++++++++++++++++++++++++++++++++++ lily-test.h | 8 ++++++++ 2 files changed, 45 insertions(+) diff --git a/lily-test.c b/lily-test.c index 1e7e5f4..9711399 100644 --- a/lily-test.c +++ b/lily-test.c @@ -7,6 +7,7 @@ struct lily_globals_t _lily_globals; +/* set the initial values of lily globals */ void lily_init() { _lily_globals.error_msg_len = 0; @@ -14,6 +15,40 @@ void lily_init() _lily_globals.error_location = ""; } +/* run an individual test */ +void _lily_run_test(const char *name, lily_test_t test) +{ + printf(" %s: ", name); + + _lily_globals.error_msg = NULL; + _lily_globals.error_location = ""; + int val = setjmp(_lily_globals.env); + + test(); + + if (val == 0) { + /* test succeeded */ + printf("OK\n"); + } + else { + /* test failed */ + printf("FAIL - %s (%s)\n", + _lily_globals.error_msg, + _lily_globals.error_location); + free(_lily_globals.error_msg); /* error message was allocated! */ + } +} + +/* run a suite */ +void _lily_run_suite(const char *name, lily_test_t suite) +{ + printf("=== %s ===\n", name); + suite(); + printf("\n"); +} + + +/* ======== ASSERTIONS ======== */ static void _lily_assert_msg(bool statement, const char *location, const char *format_string, va_list args) @@ -187,6 +222,8 @@ void _lily_assert_memory_not_equal(const char *name_a, const char *name_b, } +/* ======== MOCKS ======== */ + lily_queue_t* lily_queue_create() { const size_t size = 256 * sizeof(uint8_t); diff --git a/lily-test.h b/lily-test.h index 7a46cd9..7270272 100644 --- a/lily-test.h +++ b/lily-test.h @@ -26,6 +26,14 @@ struct lily_globals_t { extern struct lily_globals_t _lily_globals; void lily_init(); +typedef void (*lily_test_t)(void); + +#define lily_run_test(test) _lily_run_test(#test, test) +void _lily_run_test(const char *name, lily_test_t test); + +#define lily_run_suite(suite) _lily_run_suite(#suite, suite); +void _lily_run_suite(const char *name, lily_test_t suite); + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * -- cgit v1.2.1