r20928 - gnucash/trunk/src/test-core - Testing: Add some convenience functions to make it
John Ralls
jralls at code.gnucash.org
Thu Jul 14 13:06:14 EDT 2011
Author: jralls
Date: 2011-07-14 13:06:13 -0400 (Thu, 14 Jul 2011)
New Revision: 20928
Trac: http://svn.gnucash.org/trac/changeset/20928
Modified:
gnucash/trunk/src/test-core/test-stuff.c
gnucash/trunk/src/test-core/test-stuff.h
Log:
Testing: Add some convenience functions to make it
easier to test callbacks and to intercept and
ignore or test expected error log messages.
Much lifted from Muslim's test-qofbook.c
Modified: gnucash/trunk/src/test-core/test-stuff.c
===================================================================
--- gnucash/trunk/src/test-core/test-stuff.c 2011-07-14 17:06:04 UTC (rev 20927)
+++ gnucash/trunk/src/test-core/test-stuff.c 2011-07-14 17:06:13 UTC (rev 20928)
@@ -20,6 +20,15 @@
#include <glib.h>
#include "test-stuff.h"
+typedef struct
+{
+ gpointer data;
+ gboolean called;
+ gchar *msg;
+}TestStruct;
+
+static TestStruct tdata;
+
void vsuccess_args(
const char *test_title,
const char *file,
@@ -347,3 +356,62 @@
num = get_random_int_in_range(0, num - 1);
return str_list[num];
}
+
+void
+test_silent_logger( const char *log_domain, GLogLevelFlags log_level,
+ const gchar *msg, gpointer user_data )
+{
+ //Silent, remember?
+ return;
+}
+
+gboolean
+test_handle_faults( const char *log_domain, GLogLevelFlags log_level,
+ const gchar *msg, gpointer user_data )
+{
+ TestErrorStruct *tdata = (TestErrorStruct*)user_data;
+ if (tdata == NULL) {
+ g_printf("Recieved Error Message %s\n", msg);
+ return FALSE;
+ }
+ if (tdata->log_domain != NULL)
+ g_assert(g_strcmp0(tdata->log_domain, log_domain) == 0);
+ if (tdata->log_level)
+ g_assert(log_level == tdata->log_level);
+ tdata->msg = g_strdup(msg);
+ return FALSE;
+}
+
+void
+test_set_called( const gboolean val )
+{
+ tdata.called = val;
+}
+
+const gboolean
+test_reset_called( void )
+{
+ const gboolean called = tdata.called;
+ tdata.called = FALSE;
+ return called;
+}
+
+void
+test_set_data( const gpointer val )
+{
+ tdata.data = val;
+}
+
+const gpointer
+test_reset_data( void )
+{
+ const gpointer data = tdata.data;
+ tdata.data = NULL;
+ return data;
+}
+
+void
+test_free( gpointer data ) {
+ if (!data) return;
+ g_free(data);
+}
Modified: gnucash/trunk/src/test-core/test-stuff.h
===================================================================
--- gnucash/trunk/src/test-core/test-stuff.h 2011-07-14 17:06:04 UTC (rev 20927)
+++ gnucash/trunk/src/test-core/test-stuff.h 2011-07-14 17:06:13 UTC (rev 20928)
@@ -77,6 +77,69 @@
g_free( testpath );\
}
+/**
+ * Test Support
+ *
+ * Struct and functions for unit test support:
+ * Intercept and report GLib error messages to test functions.
+ * Ensure that mock functions are called, and with the right data pointer
+ *
+ */
+
+typedef struct
+{
+ GLogLevelFlags log_level;
+ gchar *log_domain;
+ gchar *msg;
+} TestErrorStruct;
+
+ /**
+ * Pass this to g_test_log_set_fatal_handler(), setting user_data to
+ * a pointer to TestErrorStruct to intercept and handle expected
+ * error and warning messages. It will g_assert if an error is
+ * received which doesn't match the log_level, log_domain, and
+ * message in the struct (if they're set), or return FALSE to prevent
+ * the message from aborting. Be sure to g_free() the
+ * TestErrorData:msg after you're done testing it.
+ */
+gboolean test_handle_faults( const char *log_domain, GLogLevelFlags log_level,
+ const gchar *msg, gpointer user_data);
+/**
+ * When you know you're going to get a useless log message, pass this
+ * to g_log_set_default_handler to shut it up.
+ */
+void test_silent_logger( const char *log_domain, GLogLevelFlags log_level,
+ const gchar *msg, gpointer user_data );
+/**
+ * Call this from a mock object to indicate that the mock has in fact
+ * been called
+ */
+void test_set_called( const gboolean val );
+
+/**
+ * Destructively tests (meaning that it resets called to FALSE) and
+ * returns the value of called.
+ */
+const test_reset_called( void );
+
+/**
+ * Set the test data pointer with the what you expect your mock to be
+ * called with.
+ */
+void test_set_data( gpointer data );
+
+/**
+ * Destructively retrieves the test data pointer. Call from your mock
+ * to ensure that it received the expected data.
+ */
+const gpointer test_reset_data( void );
+
+/**
+ * A handy function to use to free memory from lists of simple
+ * pointers. Call g_list_free_full(list, (GDestroyNotify)*test_free).
+ */
+void test_free( gpointer data );
+
/* Privately used to indicate a test result. You may use these if you
* wish, but it's easier to use the do_test macro above.
*/
More information about the gnucash-changes
mailing list