GnuCash  5.6-150-g038405b370+
unittest-support.h
Go to the documentation of this file.
1 /********************************************************************
2  * unittest-support.h: Support structures for GLib Unit Testing *
3  * Copyright 2011-12 John Ralls <jralls@ceridwen.us> *
4  * Copyright 2011 Muslim Chochlov <muslim.chochlov@gmail.com> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22 ********************************************************************/
23 #ifndef UNITTEST_SUPPORT_H
24 #define UNITTEST_SUPPORT_H
25 
26 #include <glib.h>
27 #include <qof.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
50 #define GNC_TEST_ADD( suite, path, fixture, data, setup, test, teardown )\
51 {\
52  gchar *testpath = g_strdup_printf( "%s/%s", suite, path );\
53  g_test_add( testpath, fixture, data, setup, test, teardown );\
54  g_free( testpath );\
55 }
56 
61 #define GNC_TEST_ADD_FUNC( suite, path, test )\
62 {\
63  gchar *testpath = g_strdup_printf( "%s/%s", suite, path );\
64  g_test_add_func( testpath, test );\
65  g_free( testpath );\
66 }
67 
102 typedef struct
103 {
104  GLogLevelFlags log_level;
105  char *log_domain;
106  char *msg;
107  guint hits;
109 
123 TestErrorStruct* test_error_struct_new (const char *log_domain,
124  const GLogLevelFlags log_level,
125  const char *msg);
126 
132 
138 typedef struct
139 {
140  TestErrorStruct *error;
141  gint handler;
142  gboolean list_handler;
144 
145 
159 GSList *test_log_set_handler (GSList *list, TestErrorStruct *error,
160  GLogFunc handler);
161 
178 GSList *test_log_set_fatal_handler (GSList *list, TestErrorStruct *error,
179  GLogFunc handler);
180 
184 void test_free_log_handler (gpointer item);
185 
192 gboolean test_checked_substring_handler (const char *log_domain, GLogLevelFlags log_level,
193  const gchar *msg, gpointer user_data);
200 gboolean test_checked_handler (const char *log_domain, GLogLevelFlags log_level,
201  const gchar *msg, gpointer user_data);
202 
209 gboolean test_log_handler (const char *log_domain, GLogLevelFlags log_level,
210  const gchar *msg, gpointer user_data);
215 gboolean test_null_handler (const char *log_domain, GLogLevelFlags log_level,
216  const gchar *msg, gpointer user_data );
226 void test_add_error (TestErrorStruct *error);
227 void test_clear_error_list (void);
228 
238 gboolean test_list_substring_handler (const char *log_domain, GLogLevelFlags log_level,
239  const gchar *msg, gpointer user_data);
240 
248 gboolean test_list_handler (const char *log_domain,
249  GLogLevelFlags log_level,
250  const gchar *msg, gpointer user_data );
255 void test_set_called( const gboolean val );
256 
261 gboolean test_reset_called( void );
262 
267 void test_set_data( gpointer data );
268 
273 gpointer test_reset_data( void );
274 
279 void test_free( gpointer data );
280 
300 typedef gpointer TestSignal;
301 
311  gpointer event_data);
316 
322 #define test_signal_assert_hits(sig, hits) \
323  g_assert_cmpint (test_signal_return_hits (sig), ==, hits)
324 
328 void test_signal_free (TestSignal sig);
329 
342 gboolean test_object_checked_destroy (GObject *obj);
343 
353 #define test_destroy(obj) \
354  g_assert_true (obj != NULL && G_IS_OBJECT (obj)); \
355  g_assert_true (test_object_checked_destroy (G_OBJECT (obj)))
356 
358 /* For Scheme testing access:
359 void gnc_log_init_filename_special (gchar *filename);
360 void gnc_log_shutdown (void);
361 void gnc_log_set_handler (guint logdomain, gchar *logdomain, GLogFunc * func, gpointer data);
362 */
363 
364 #ifdef __cplusplus
365 }
366 #endif
367 
368 #endif /*UNITTEST_SUPPORT_H*/
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.
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&#39;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.
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.
Definition: qofevent.h:45
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.
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...