diff options
Diffstat (limited to 'tests/tests.h')
-rw-r--r-- | tests/tests.h | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/tests.h b/tests/tests.h index ce89ac98..62f01719 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -23,6 +23,8 @@ #ifndef __TESTS_H__ #define __TESTS_H__ +#include "../lasso/lasso_config.h" + #define check_not_null(what) \ fail_unless((what) != NULL, "%s:%i: " #what " returned NULL", __func__, __LINE__); @@ -72,4 +74,72 @@ fail_unless(g_strcmp0(__tmp, to) != 0, "%s:%i: " #what " is equal to %s", __func__, __LINE__, to); \ } +static inline void mute_logger(G_GNUC_UNUSED const gchar *domain, + G_GNUC_UNUSED GLogLevelFlags log_level, G_GNUC_UNUSED const gchar *message, + G_GNUC_UNUSED gpointer user_data) { +} +G_GNUC_UNUSED static guint mute_log_handler = 0; + +#define block_lasso_logs mute_log_handler = g_log_set_handler(LASSO_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \ + mute_logger, NULL) + +#define unblock_lasso_logs g_log_remove_handler(LASSO_LOG_DOMAIN, mute_log_handler) + +struct CheckingLogHandlerUserData { + GLogLevelFlags log_level; + const char *message; + gboolean endswith; + GLogLevelFlags log_level_found; + const char *message_found; +}; +G_GNUC_UNUSED static guint checking_log_handler = 0; +G_GNUC_UNUSED static guint checking_log_handler_flag = 0; +G_GNUC_UNUSED static struct CheckingLogHandlerUserData checking_logger_user_data; + +static inline gboolean check_message(const char *a, const char *b, gboolean endswith) { + if (endswith) { + return strlen(a) >= strlen(b) && + strcmp(a+(strlen(a)-strlen(b)), b) == 0; + } else { + return strcmp(a, b) == 0; + } +} + +static inline void checking_logger(G_GNUC_UNUSED const gchar *domain, + G_GNUC_UNUSED GLogLevelFlags log_level, G_GNUC_UNUSED const gchar *message, + G_GNUC_UNUSED gpointer user_data) { + struct CheckingLogHandlerUserData *ck_user_data = user_data; + if (log_level == ck_user_data->log_level && check_message(message, ck_user_data->message, + ck_user_data->endswith)) { + } else { + g_log_default_handler(domain, log_level, message, user_data); + checking_log_handler_flag = 0; + } + ck_user_data->log_level_found = log_level; + ck_user_data->message_found = g_strdup(message); +} +/* begin_check_do_log(level, message, endswith)/end_check_do_log() with check that the only + * message emitted between the two macros is one equals to message at the level level, + * or ending with message if endswith is True. + */ +static inline void begin_check_do_log(GLogLevelFlags level, const char *message, gboolean endswith) { + memset(&checking_logger_user_data, 0, sizeof(struct CheckingLogHandlerUserData)); + checking_logger_user_data.log_level = level; + checking_logger_user_data.message = message; + checking_logger_user_data.endswith = endswith; + checking_log_handler = g_log_set_handler(LASSO_LOG_DOMAIN, level, checking_logger, &checking_logger_user_data); + checking_log_handler_flag = 1; +} + +static inline void end_check_do_log() { + g_log_remove_handler(LASSO_LOG_DOMAIN, checking_log_handler); + checking_log_handler = 0; + fail_unless(checking_log_handler_flag, "Logging failure: expected log level %d and message «%s», got %d and «%s»", + checking_logger_user_data.log_level, + checking_logger_user_data.message, + checking_logger_user_data.log_level_found, + checking_logger_user_data.message_found); + checking_log_handler_flag = 0; +} + #endif /*__TESTS_H__ */ |