mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
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 git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@20928 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
parent
4dc91e4441
commit
e7534148cc
@ -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 @@ get_random_string_in_array(const char* str_list[])
|
||||
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);
|
||||
}
|
||||
|
@ -77,6 +77,69 @@ Otherwise, only failures are printed out.
|
||||
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.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user