From c6262360d067fb988e06266865701bee9ddf4231 Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 16 Jun 2022 21:14:36 -0500 Subject: add helper macros for mocking --- src/logging/logging.test.c | 25 +++++++++---------------- src/test/honey-test.h | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/logging/logging.test.c b/src/logging/logging.test.c index dfcbe4e..a796903 100644 --- a/src/logging/logging.test.c +++ b/src/logging/logging.test.c @@ -2,7 +2,6 @@ #include #include -#include "test/lily-test.h" #include "test/honey-test.h" /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -24,8 +23,7 @@ void mock_vfprintf(FILE*, const char*, va_list vl); * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -lily_mock_t *vfprintf_mock_data; -void mock_vfprintf(FILE *file, const char *fmt, va_list vl) +MOCK(mock_vfprintf)(FILE *file, const char *fmt, va_list vl) { /* to avoid basically just re-implementing printf parsing here, i am limiting this function to be able to receive strings only */ @@ -44,10 +42,10 @@ void mock_vfprintf(FILE *file, const char *fmt, va_list vl) { sizeof(const char*), &fmt }, { sizeof(int), &n_args }, }; - lily_mock_call(vfprintf_mock_data, args); + lily_mock_call(mock_vfprintf_data, args); /* store format arguments */ - lily_queue_t *queue = vfprintf_mock_data->values; + lily_queue_t *queue = mock_vfprintf_data->values; for (int i=0; in_calls, 1); + lily_assert_int_equal(mock_vfprintf_data->n_calls, 1); FILE *file; const char *fmt; int n_strings; struct lily_mock_arg_t args[] = { @@ -107,7 +101,7 @@ void level_fatal_log_fatal_succeeds() { sizeof(const char*), &fmt }, { sizeof(int), &n_strings }, }; - lily_get_call(vfprintf_mock_data, args, 0); + lily_get_call(mock_vfprintf_data, args, 0); lily_assert_ptr_equal(file, stderr); lily_assert_string_equal((char*) fmt, "[FATAL] some message"); @@ -117,10 +111,9 @@ void level_fatal_log_fatal_succeeds() void level_neg_log_fatal_fails() { - clean_mock(&vfprintf_mock_data); - vfprintf_mock_data = lily_mock_create(); + USE_MOCK(mock_vfprintf); honey_set_log_level(-1); honey_fatal("some message"); - lily_assert_int_equal(vfprintf_mock_data->n_calls, 0); + lily_assert_int_equal(mock_vfprintf_data->n_calls, 0); } diff --git a/src/test/honey-test.h b/src/test/honey-test.h index 65bd959..ac79d8a 100644 --- a/src/test/honey-test.h +++ b/src/test/honey-test.h @@ -3,6 +3,25 @@ #include "lily-test.h" +#define MOCK(name) \ + lily_mock_t * name ## _data = NULL; \ + void name + +#define CLEAN_MOCK(name) \ + do { \ + if (name ## _data != NULL) { \ + lily_mock_destroy(name ## _data); \ + name ## _data = NULL; \ + } \ + } while(0) + +#define USE_MOCK(name) \ + do { \ + CLEAN_MOCK(name); \ + name ## _data = lily_mock_create(); \ + } while(0) + + void suite_logging(); #define RUN_TESTS() \ -- cgit v1.2.1