25 #include <glib/gprintf.h> 37 test_checked_nohit_handler (
const char *log_domain, GLogLevelFlags log_level,
38 const gchar *msg, gpointer user_data);
40 test_list_nohit_handler (
const char *log_domain, GLogLevelFlags log_level,
41 const gchar *msg, gpointer user_data);
48 err->log_domain = g_strdup (log_domain);
49 err->log_level = log_level;
50 err->msg = g_strdup (msg);
57 g_free (err->log_domain);
67 hdlr->handler = g_log_set_handler (error->log_domain, error->log_level,
69 return g_slist_prepend (list, hdlr);
78 (GTestLogFatalFunc)test_list_nohit_handler :
79 (GTestLogFatalFunc)test_checked_nohit_handler;
81 hdlr->handler = g_log_set_handler (error->log_domain, error->log_level,
83 g_test_log_set_fatal_handler (f_hdlr, error);
84 return g_slist_prepend (list, hdlr);
91 g_log_remove_handler (handler->error->log_domain, handler->handler);
98 const gchar *msg, gpointer user_data )
106 test_log_level (GLogLevelFlags flags)
108 const gchar *message[] = {
"RECURSIVE",
"FATAL",
"ERROR",
"CRITICAL",
109 "WARNING",
"MESSAGE",
"INFO",
"DEBUG" 111 guint i = 0, last = 0, max_bit = 7;
114 for (i = 0; i <= max_bit; i++)
117 gchar *tmp_msg = msg;
118 gchar *sep = (last < 2 ?
" " :
"|");
120 msg = (tmp_msg ? g_strjoin (sep, tmp_msg, message[i], NULL)
121 : g_strdup (message[i]));
131 static GList *message_queue = NULL;
136 message_queue = g_list_append (message_queue, error);
140 test_clear_error_list (
void)
142 g_list_free (message_queue);
143 message_queue = NULL;
149 const gchar *msg, gpointer user_data)
151 GList *list = g_list_first (message_queue);
152 const guint fatal = G_LOG_FLAG_FATAL;
156 if (!g_strcmp0 (log_domain, error->log_domain)
157 && ((log_level | fatal) == (error->log_level | fatal))
158 && g_strrstr (msg, error->msg))
163 list = g_list_next (list);
170 do_test_list_handler (
const char *log_domain, GLogLevelFlags log_level,
171 const gchar *msg, gpointer user_data, gboolean hits)
173 GList *list = g_list_first (message_queue);
174 const guint fatal = G_LOG_FLAG_FATAL;
179 if (!g_strcmp0 (log_domain, error->log_domain)
180 && ((log_level | fatal) == (error->log_level | fatal))
181 && !g_strcmp0 (msg, error->msg))
187 list = g_list_next (list);
195 const gchar *msg, gpointer user_data)
197 return do_test_list_handler (log_domain, log_level, msg, user_data, TRUE);
201 test_list_nohit_handler (
const char *log_domain, GLogLevelFlags log_level,
202 const gchar *msg, gpointer user_data)
204 return do_test_list_handler (log_domain, log_level, msg, user_data, FALSE);
208 do_test_checked_handler (
const char *log_domain, GLogLevelFlags log_level,
209 const gchar *msg, gpointer user_data, gboolean hits)
214 || (tdata->log_domain != NULL
215 && g_strcmp0 (tdata->log_domain, log_domain))
216 || (tdata->log_level && tdata->log_level != log_level)
217 || (tdata->msg && g_strcmp0 (tdata->msg, msg)))
219 gchar *level = test_log_level (log_level);
220 g_printf (
"<%s> (%s) %s\n", level, log_domain, msg);
222 g_assert_true (log_level ^ G_LOG_FLAG_FATAL);
233 const gchar *msg, gpointer user_data)
237 || (tdata->log_domain != NULL
238 && g_strcmp0 (log_domain, tdata->log_domain))
239 || (tdata->log_level && tdata->log_level != log_level)
240 || (tdata->msg && !g_strrstr (msg, tdata->msg)))
242 gchar *level = test_log_level (log_level);
243 g_printf (
"<%s> (%s) %s\n", level, log_domain, msg);
245 g_assert_true (log_level ^ G_LOG_FLAG_FATAL);
254 const gchar *msg, gpointer user_data )
256 return do_test_checked_handler (log_domain, log_level, msg,
261 test_checked_nohit_handler (
const char *log_domain, GLogLevelFlags log_level,
262 const gchar *msg, gpointer user_data )
264 return do_test_checked_handler (log_domain, log_level, msg,
270 const gchar *msg, gpointer user_data )
272 gchar *level = test_log_level (log_level);
273 g_printf (
"<%s> (%s) %s\n", level, log_domain, msg);
275 g_assert_true (log_level ^ G_LOG_FLAG_FATAL);
288 const gboolean called = tdata.called;
289 tdata.called = FALSE;
302 const gpointer data = tdata.data;
326 gpointer handler_data, gpointer event_data)
329 if ((signal->entity == entity || signal->entity == NULL)
330 && signal->event_type == event_type)
332 if (signal->event_data)
333 g_assert_true (signal->event_data == event_data);
343 sig->entity = entity;
344 sig->event_type = event_type;
345 sig->event_data = event_data;
367 notify_destroy (gpointer pdata, GObject *obj)
369 gboolean *data = (gboolean*)pdata;
370 if (! (*data)) *data = TRUE;
376 gboolean is_destroyed = FALSE;
377 if (!obj || ! G_IS_OBJECT (obj))
return FALSE;
378 g_object_weak_ref (obj, notify_destroy, &is_destroyed);
379 g_object_unref (obj);
gboolean test_list_handler(const char *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
Checks received errors against the list created by test_add_error.
Macros and logging-capture functions to ease writing GLib-testing based unit tests.
void test_free_log_handler(gpointer item)
Clears all the log handlers.
gboolean test_checked_handler(const char *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
Check the user_data against the actual error and assert on any differences.
Holds a handler instance with its TestErrorStruct, handler id, and whether it's a list handler...
gboolean test_checked_substring_handler(const char *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
Check that the user_data error message is a substring of the actual error otherwise assert...
guint test_signal_return_hits(TestSignal sig)
gets the number of times the TestSignal has been called.
void test_set_data(gpointer data)
Set the test data pointer with the what you expect your mock to be called with.
gboolean test_log_handler(const char *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
Just print the log message.
gint qof_event_register_handler(QofEventHandler handler, gpointer user_data)
Register a handler for events.
void test_signal_free(TestSignal sig)
Free a test signal.
gboolean test_object_checked_destroy(GObject *obj)
Unrefs obj and returns true if its finalize method was called.
TestErrorStruct * test_error_struct_new(const char *log_domain, const GLogLevelFlags log_level, const char *msg)
Convenience function to create an error struct.
gint QofEventId
Define the type of events allowed.
gboolean test_reset_called(void)
Destructively tests (meaning that it resets called to FALSE) and returns the value of called...
gboolean test_null_handler(const char *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
Just returns FALSE or suppresses the message regardless of what the error is.
gboolean test_list_substring_handler(const char *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
Checks received errors against the list created by test_add_error.
void test_free(gpointer data)
A handy function to use to free memory from lists of simple pointers.
gpointer test_reset_data(void)
Destructively retrieves the test data pointer.
Struct to pass as user_data for the handlers.
void qof_event_unregister_handler(gint handler_id)
Unregister an event handler.
GSList * test_log_set_fatal_handler(GSList *list, TestErrorStruct *error, GLogFunc handler)
Set a log handler and add it to a GList for removal at teardown; also set the fatal handler so that t...
GSList * test_log_set_handler(GSList *list, TestErrorStruct *error, GLogFunc handler)
Set a log handler and add it to a GList for removal at teardown.
void test_error_struct_free(TestErrorStruct *)
Free a TestErrorStruct created with test_error_struct_new.
TestSignal test_signal_new(QofInstance *entity, QofEventId eventType, gpointer event_data)
Create a test signal.
void test_set_called(const gboolean val)
Call this from a mock object to indicate that the mock has in fact been called.
gpointer TestSignal
TestSignal is an opaque struct used to mock handling signals emitted by functions-under-test.
void test_add_error(TestErrorStruct *error)
Maintains an internal list of TestErrorStructs which are each checked by the list handler...