GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
unittest-support.h
1 /********************************************************************
2  * unittest-support.h: Support structures for GLib Unit Testing *
3  * Copyright 2011-12 John Ralls <[email protected]> *
4  * Copyright 2011 Muslim Chochlov <[email protected]> *
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 [email protected] *
22 ********************************************************************/
23 #ifndef UNITTEST_SUPPORT_H
24 #define UNITTEST_SUPPORT_H
25 
26 #include <glib.h>
27 #include <qof.h>
28 
34 #define GNC_TEST_ADD( suite, path, fixture, data, setup, test, teardown )\
35 {\
36  gchar *testpath = g_strdup_printf( "%s/%s", suite, path );\
37  g_test_add( testpath, fixture, data, setup, test, teardown );\
38  g_free( testpath );\
39 }
40 
46 #define GNC_TEST_ADD_FUNC( suite, path, test )\
47 {\
48  gchar *testpath = g_strdup_printf( "%s/%s", suite, path );\
49  g_test_add_func( testpath, test );\
50  g_free( testpath );\
51 }
52 
87 typedef struct
88 {
89  GLogLevelFlags log_level;
90  gchar *log_domain;
91  gchar *msg;
92  guint hits;
94 
108 TestErrorStruct* test_error_struct_new (gchar *log_domain,
109  GLogLevelFlags log_level,
110  gchar *msg);
111 
116 void test_error_struct_free (TestErrorStruct *);
117 
118 typedef struct
119 {
120  TestErrorStruct *error;
121  gint handler;
122  gboolean list_handler;
124 
125 
139 GSList *test_log_set_handler (GSList *list, TestErrorStruct *error,
140  GLogFunc handler);
141 
158 GSList *test_log_set_fatal_handler (GSList *list, TestErrorStruct *error,
159  GLogFunc handler);
160 
161 /* Clears all the log handlers. Pass this to g_slist_free() in teardown */
162 void test_free_log_handler (gpointer item);
163 
170 gboolean test_checked_handler (const char *log_domain, GLogLevelFlags log_level,
171  const gchar *msg, gpointer user_data);
172 
179 gboolean test_log_handler (const char *log_domain, GLogLevelFlags log_level,
180  const gchar *msg, gpointer user_data);
185 gboolean test_null_handler (const char *log_domain, GLogLevelFlags log_level,
186  const gchar *msg, gpointer user_data );
196 void test_add_error (TestErrorStruct *error);
197 void test_clear_error_list (void);
198 
206 gboolean test_list_handler (const char *log_domain,
207  GLogLevelFlags log_level,
208  const gchar *msg, gpointer user_data );
213 void test_set_called( const gboolean val );
214 
219 gboolean test_reset_called( void );
220 
225 void test_set_data( gpointer data );
226 
231 gpointer test_reset_data( void );
232 
237 void test_free( gpointer data );
238 
239 /* TestSignal is an opaque struct used to mock handling signals
240  * emitted by functions-under-test. It registers a handler and counts
241  * how many times it is called with the right instance and type. The
242  * struct is allocated using g_slice_new, and it registers a
243  * qof_event_handler; test_signal_free cleans up at the end of the
244  * test function (or sooner, if you want to reuse a TestSignal). If
245  * event_data isn't NULL, the mock signal handler will test that it
246  * matches the event_data passed with the signal and assert if it
247  * isn't the same object (pointer comparison). If the actual event
248  * data is a local variable, it won't be accessible, so the event_data
249  * passed to test_signal_new should be NULL to avoid the test.
250  */
251 typedef gpointer TestSignal;
252 TestSignal test_signal_new (QofInstance *entity, QofEventId eventType,
253  gpointer event_data);
254 /* test_signal_return_hits gets the number of times the TestSignal has
255  * been called.
256  */
257 guint test_signal_return_hits (TestSignal sig);
258 
259 /* test_signal_assert_hits is a convenience macro which wraps
260  * test_signal_return_hits with and equality assertion.
261  */
262 
263 #define test_signal_assert_hits(sig, hits) \
264  g_assert_cmpint (test_signal_return_hits (sig), ==, hits)
265 
266 void test_signal_free (TestSignal sig);
267 
268 /* test_object_checked_destroy unrefs obj and returns true if its finalize
269  * method was called.
270  */
271 
272 gboolean test_object_checked_destroy (GObject *obj);
273 
283 #define test_destroy(obj) \
284  g_assert (obj != NULL && G_IS_OBJECT (obj)); \
285  g_assert (test_object_checked_destroy (G_OBJECT (obj)))
286 
287 /* For Scheme testing access:
288 void gnc_log_init_filename_special (gchar *filename);
289 void gnc_log_shutdown (void);
290 void gnc_log_set_handler (guint logdomain, gchar *logdomain, GLogFunc * func, gpointer data);
291 */
292 
293 #endif /*UNITTEST_SUPPORT_H*/
gint QofEventId
Definition: qofevent.h:45