diff options
author | sanine <sanine.not@pm.me> | 2022-06-16 21:14:36 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-06-16 21:14:36 -0500 |
commit | c6262360d067fb988e06266865701bee9ddf4231 (patch) | |
tree | 209b5769861b1e8713c7ff00ea975a9594f6ee64 /src | |
parent | ca4f950ee2e8a1881bf74c4931343d696ca44654 (diff) |
add helper macros for mocking
Diffstat (limited to 'src')
-rw-r--r-- | src/logging/logging.test.c | 25 | ||||
-rw-r--r-- | 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 <stdarg.h> #include <stdio.h> -#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; i<n_args; i++) { char *str = va_arg(vl, char*); lily_enqueue(queue, char*, str); @@ -75,13 +73,10 @@ void level_neg_log_fatal_fails(); void suite_logging() { - vfprintf_mock_data = NULL; - lily_run_test(level_neg_log_fatal_fails); lily_run_test(level_fatal_log_fatal_succeeds); - if (vfprintf_mock_data != NULL) - lily_mock_destroy(vfprintf_mock_data); + CLEAN_MOCK(mock_vfprintf); } @@ -94,12 +89,11 @@ void suite_logging() void level_fatal_log_fatal_succeeds() { - clean_mock(&vfprintf_mock_data); - vfprintf_mock_data = lily_mock_create(); + USE_MOCK(mock_vfprintf); honey_set_log_level(HONEY_FATAL); honey_fatal("some message"); - lily_assert_int_equal(vfprintf_mock_data->n_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() \ |