GnuCash
5.6-150-g038405b370+
|
Macros and logging-capture functions to ease writing GLib-testing based unit tests. More...
#include <glib.h>
#include <qof.h>
Go to the source code of this file.
Data Structures | |
struct | TestErrorStruct |
Struct to pass as user_data for the handlers. More... | |
struct | TestLogHandler |
Holds a handler instance with its TestErrorStruct, handler id, and whether it's a list handler. More... | |
Macros | |
Unit Test Macros | |
These macros facilitate combining a suite name and a test name to make a path when registering a test function with the corresponding g_test_add and g_test_add_func. Create a const char* suitename somewhere in the file and pass it as the first parameter, and the test name as the second. The remaining parameters are the same as in the underlying function. | |
#define | GNC_TEST_ADD(suite, path, fixture, data, setup, test, teardown) |
Wraps gnc_test_add() for test functions needing a fixture. More... | |
#define | GNC_TEST_ADD_FUNC(suite, path, test) |
Wraps gnc_test_add_func() for test functions which don't require a fixture. More... | |
Functions | |
Suppressing Expected Errors | |
Functions for suppressing expected errors during tests. Pass Note that you need to call both g_log_set_handler and g_test_log_set_fatal_handler to both avoid the assertion and suppress the error message. The callbacks work in either role, just cast them appropriately for the use. To simplify the process a bit and make sure that everything gets cleaned up at the end of each test, add a GSList for handlers to your Fixture and set it to NULL in setup(), then call g_slist_free_full() on it with test_free_log_handler as the function. Create new TestErrorStruct instances with test_error_struct_new, and pass that along with your handler of choice to test_log_set_handler or test_log_set_fatal_handler. This is much simpler, as teardown will clean everything up for you and you need call only those functions. As an added bonus, the hit count won't be doubled as it is if you do everything by hand. NB: If you have more than one fatal error in a test function be sure to use the test_list_handler: You can have only one fatal handler. | |
TestErrorStruct * | test_error_struct_new (const char *log_domain, const GLogLevelFlags log_level, const char *msg) |
Convenience function to create an error struct. More... | |
void | test_error_struct_free (TestErrorStruct *) |
Free a TestErrorStruct created with test_error_struct_new. More... | |
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. More... | |
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 the test program doesn't abort for fatal log messages. More... | |
void | test_free_log_handler (gpointer item) |
Clears all the log handlers. More... | |
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. More... | |
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. More... | |
gboolean | test_log_handler (const char *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data) |
Just print the log message. More... | |
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. More... | |
void | test_add_error (TestErrorStruct *error) |
Maintains an internal list of TestErrorStructs which are each checked by the list handler. More... | |
void | test_clear_error_list (void) |
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. More... | |
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. More... | |
void | test_set_called (const gboolean val) |
Call this from a mock object to indicate that the mock has in fact been called. | |
gboolean | test_reset_called (void) |
Destructively tests (meaning that it resets called to FALSE) and returns the value of called. | |
void | test_set_data (gpointer data) |
Set the test data pointer with the what you expect your mock to be called with. | |
gpointer | test_reset_data (void) |
Destructively retrieves the test data pointer. More... | |
void | test_free (gpointer data) |
A handy function to use to free memory from lists of simple pointers. More... | |
Test Signals | |
Test the emission of signals from objects. Signals are used to coordinate the behavior of the GUI with events in other parts of the program. | |
#define | test_signal_assert_hits(sig, hits) g_assert_cmpint (test_signal_return_hits (sig), ==, hits) |
Convenience macro which wraps test_signal_return_hits with and equality assertion. | |
typedef gpointer | TestSignal |
TestSignal is an opaque struct used to mock handling signals emitted by functions-under-test. More... | |
TestSignal | test_signal_new (QofInstance *entity, QofEventId eventType, gpointer event_data) |
Create a test signal. More... | |
guint | test_signal_return_hits (TestSignal sig) |
gets the number of times the TestSignal has been called. | |
void | test_signal_free (TestSignal sig) |
Free a test signal. | |
Testing for object disposal | |
Sometimes we need to make sure that certain objects that we've created aren't leaking. These functions can help. | |
#define | test_destroy(obj) |
Ensures that a GObject is still alive at the time it's called and that it is finalized. More... | |
gboolean | test_object_checked_destroy (GObject *obj) |
Unrefs obj and returns true if its finalize method was called. | |
Macros and logging-capture functions to ease writing GLib-testing based unit tests.
Definition in file unittest-support.h.
#define GNC_TEST_ADD | ( | suite, | |
path, | |||
fixture, | |||
data, | |||
setup, | |||
test, | |||
teardown | |||
) |
Wraps gnc_test_add() for test functions needing a fixture.
Definition at line 50 of file unittest-support.h.
#define GNC_TEST_ADD_FUNC | ( | suite, | |
path, | |||
test | |||
) |
Wraps gnc_test_add_func() for test functions which don't require a fixture.
Definition at line 61 of file unittest-support.h.
#define test_destroy | ( | obj | ) |
Ensures that a GObject is still alive at the time it's called and that it is finalized.
The first assertion will trigger if you pass it a pointer which isn't a GObject – which could be the case if the object has already been finalized. Then it calls test_object_checked_destroy() on it, asserting if the finalize method wasn't called (which indicates a leak).
Definition at line 351 of file unittest-support.h.
typedef gpointer TestSignal |
TestSignal is an opaque struct used to mock handling signals emitted by functions-under-test.
It registers a handler and counts how many times it is called with the right instance and type. The struct is allocated using g_slice_new, and it registers a qof_event_handler; test_signal_free cleans up at the end of the test function (or sooner, if you want to reuse a TestSignal). If event_data isn't NULL, the mock signal handler will test that it matches the event_data passed with the signal and assert if it isn't the same object (pointer comparison). If the actual event data is a local variable, it won't be accessible, so the event_data passed to test_signal_new should be NULL to avoid the test.
Definition at line 299 of file unittest-support.h.
void test_add_error | ( | TestErrorStruct * | error | ) |
Maintains an internal list of TestErrorStructs which are each checked by the list handler.
If an error matches any entry on the list, test_list_handler will return FALSE, blocking the error from halting the program.
Call test_add_error for each TestErrorStruct to check against and test_clear_error_list when you no longer expect the errors.
Definition at line 134 of file unittest-support.c.
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.
Displays the error (and asserts if G_LOG_FLAG_FATAL is TRUE) if NULL is passed as user_data, but a NULL or 0 value member matches anything.
Definition at line 253 of file unittest-support.c.
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.
Displays the error (and asserts if G_LOG_FLAG_FATAL is TRUE) if NULL is passed as user_data, but a NULL or 0 value member matches anything.
Definition at line 232 of file unittest-support.c.
void test_error_struct_free | ( | TestErrorStruct * | ) |
Free a TestErrorStruct created with test_error_struct_new.
error | The TestErrorStruct to be freed |
Definition at line 55 of file unittest-support.c.
TestErrorStruct* test_error_struct_new | ( | const char * | log_domain, |
const GLogLevelFlags | log_level, | ||
const char * | msg | ||
) |
Convenience function to create an error struct.
If you use this with test_set_log_handler it will get cleaned up at tesrdown, otherwise call test_error_free() at the end of your test function.
NB: If you need to change the message, be sure to free the old one and to allocate the new one on the stack.
log_domain | The string representing the domain of the log message |
log_level | The GLogLevelFlags for the message |
msg | The exact error message that the logger will emit |
Definition at line 44 of file unittest-support.c.
void test_free | ( | gpointer | data | ) |
A handy function to use to free memory from lists of simple pointers.
Call g_list_free_full(list, (GDestroyNotify)*test_free).
Definition at line 308 of file unittest-support.c.
void test_free_log_handler | ( | gpointer | item | ) |
Clears all the log handlers.
Pass this to g_slist_free() in teardown.
Definition at line 88 of file unittest-support.c.
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.
If the list is empty or nothing matches, passes control on to test_checked_handler, giving the opportunity for an additional check that's not in the list (set user_data to NULL if you want test_checked_handler to immediately print the error).
Definition at line 194 of file unittest-support.c.
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.
Rather than checking for an exact match, this function checks using a substring match. If the list is empty or nothing matches, passes control on to test_checked_substring_handler, giving the opportunity for an additional check that's not in the list (set user_data to NULL if you want test_checked_handler to immediately print the error).
Definition at line 148 of file unittest-support.c.
gboolean test_log_handler | ( | const char * | log_domain, |
GLogLevelFlags | log_level, | ||
const gchar * | msg, | ||
gpointer | user_data | ||
) |
Just print the log message.
Since GLib has a habit of eating its log messages, it's sometimes useful to call g_test_log_set_fatal_handler() with this to make sure that g_return_if_fail() error messages make it to the surface.
Definition at line 269 of file unittest-support.c.
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 the test program doesn't abort for fatal log messages.
If a test function has more than one fatal message, be sure to use the test_list_handler!
Don't pass a NULL TestErrorStruct! It's needed to set the parameters for g_log_set_handler. Use a TestErrorStruct created with test_error_struct_new() or you'll have errors with freeing it in teardown.
handler_list | A GSList of LogHandlers |
error | A TestErrorStruct with the necessary data |
handler | The Handler to set the data with |
Definition at line 73 of file unittest-support.c.
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.
Don't pass a NULL TestErrorStruct! It's needed to set the parameters for g_log_set_handler. Use a TestErrorStruct created with test_error_struct_new() or you'll have errors with freeing it in teardown.
handler_list | A GSList of LogHandlers |
error | A TestErrorStruct with the necessary data |
handler | The Handler to set the data with |
Definition at line 63 of file unittest-support.c.
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.
Use this only as a last resort.
Definition at line 97 of file unittest-support.c.
gpointer test_reset_data | ( | void | ) |
Destructively retrieves the test data pointer.
Call from your mock to ensure that it received the expected data.
Definition at line 300 of file unittest-support.c.
TestSignal test_signal_new | ( | QofInstance * | entity, |
QofEventId | eventType, | ||
gpointer | event_data | ||
) |
Create a test signal.
entity | The QofInstance emitting the signal |
eventType | The type of the signal |
event_data | Any data required by the signal or NULL if none is. |
Definition at line 339 of file unittest-support.c.