|
GnuCash 2.4.99
|
00001 /******************************************************************** 00002 * test-qofobject.c: GLib g_test test suite for qofobject.c. * 00003 * Copyright 2011 Muslim Chochlov <muslim.chochlov@gmail.com> * 00004 * * 00005 * This program is free software; you can redistribute it and/or * 00006 * modify it under the terms of the GNU General Public License as * 00007 * published by the Free Software Foundation; either version 2 of * 00008 * the License, or (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License* 00016 * along with this program; if not, contact: * 00017 * * 00018 * Free Software Foundation Voice: +1-617-542-5942 * 00019 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 * 00020 * Boston, MA 02110-1301, USA gnu@gnu.org * 00021 ********************************************************************/ 00022 #include "config.h" 00023 #include <string.h> 00024 #include <glib.h> 00025 #include <unittest-support.h> 00026 #include "../qof.h" 00027 #include "../qofobject-p.h" 00028 00029 static const gchar *suitename = "/qof/qofobject"; 00030 void test_suite_qofobject ( void ); 00031 00032 typedef struct 00033 { 00034 QofObject *qofobject; 00035 } Fixture; 00036 00037 typedef enum 00038 { 00039 MOCK_OBJECT_BOOK_BEGIN = 1, 00040 MOCK_OBJECT_BOOK_END, 00041 MOCK_OBJECT_DIRTY, 00042 MOCK_OBJECT_MARK_CLEAN, 00043 EMPTY 00044 } MockFields; 00045 00046 static void mock_object_book_begin( QofBook *book ); 00047 static gboolean mock_object_dirty( const QofCollection *col ); 00048 static void mock_object_mark_clean( QofCollection *col ); 00049 00050 static QofObject* 00051 new_object( QofIdType e_type, const char *type_label, MockFields field) 00052 { 00053 QofObject *object = NULL; 00054 00055 object = g_new0( QofObject, 1 ); 00056 g_assert( object ); 00057 object->interface_version = QOF_OBJECT_VERSION; 00058 object->e_type = e_type; 00059 object->type_label = type_label; 00060 switch ( field ) 00061 { 00062 case MOCK_OBJECT_BOOK_BEGIN: 00063 object->book_begin = mock_object_book_begin; 00064 break; 00065 case MOCK_OBJECT_BOOK_END: 00066 object->book_end = mock_object_book_begin; 00067 break; 00068 case MOCK_OBJECT_DIRTY: 00069 object->is_dirty = mock_object_dirty; 00070 break; 00071 case MOCK_OBJECT_MARK_CLEAN: 00072 object->mark_clean = mock_object_mark_clean; 00073 case EMPTY: 00074 break; 00075 } 00076 return object; 00077 } 00078 00079 extern gboolean get_object_is_initialized( void ); 00080 extern GList* get_object_modules( void ); 00081 extern GList* get_book_list( void ); 00082 extern GHashTable* get_backend_data( void ); 00083 00084 static void 00085 setup( Fixture *fixture, gconstpointer pData ) 00086 { 00087 qof_object_initialize(); 00088 fixture->qofobject = new_object( "my type object", "object desc", EMPTY ); 00089 } 00090 00091 static void 00092 teardown( Fixture *fixture, gconstpointer pData ) 00093 { 00094 g_free( fixture->qofobject ); 00095 qof_object_shutdown(); 00096 } 00097 00098 /* 00099 * Safely generates objects and registers them 00100 * 00101 * Input: min_objects - minimum number of objects to be generated (should be between 0 and 5) 00102 * mock_filed - function in qofobject to be mocked 00103 * Output: number of generated objects 00104 */ 00105 static gint32 00106 generate_and_register_objects( guint min_objects, MockFields mock_field ) 00107 { 00108 gint32 list_length = g_test_rand_int_range( min_objects, 5 ); 00109 const char *types[5] = {"type1", "type2", "type3", "type4", "type5"}; 00110 int i; 00111 00112 g_assert_cmpint( min_objects, >= , 0 ); 00113 g_assert_cmpint( min_objects, < , 5 ); 00114 for (i = 0; i < list_length; i++ ) 00115 { 00116 QofObject *object = new_object( types[i], "desc", mock_field ); 00117 g_assert( object ); 00118 g_assert( qof_object_register( object ) ); 00119 g_assert_cmpint( g_list_length( get_object_modules() ), == , (i + 1) ); 00120 } 00121 g_assert_cmpint( list_length, == , g_list_length( get_object_modules() ) ); 00122 00123 return list_length; 00124 } 00125 00126 /* 00127 * TESTS 00128 */ 00129 static struct 00130 { 00131 GList *books; 00132 guint call_count; 00133 } book_begin_struct; 00134 00135 static void 00136 mock_book_begin( QofBook *book ) 00137 { 00138 g_assert( book ); 00139 g_assert( book == book_begin_struct.books->data ); 00140 book_begin_struct.books = book_begin_struct.books->next; 00141 book_begin_struct.call_count++; 00142 } 00143 00144 static void 00145 test_qof_object_register( Fixture *fixture, gconstpointer pData ) 00146 { 00147 GList *books = NULL; 00148 gint32 list_length = g_test_rand_int_range( 0, 5 ); 00149 int i; 00150 QofObject *simple_object = NULL; 00151 00152 for (i = 0; i < list_length; i++ ) 00153 { 00154 QofBook *book = qof_book_new(); 00155 g_assert( book ); 00156 books = g_list_prepend ( books, book ); 00157 g_assert_cmpint( g_list_length( books ), == , (i + 1) ); 00158 } 00159 g_assert_cmpint( list_length, == , g_list_length( books ) ); 00160 00161 g_test_message( "Test null check" ); 00162 g_assert( qof_object_register( NULL ) == FALSE ); 00163 00164 g_test_message( "Test new object register with book_begin specified" ); 00165 fixture->qofobject->book_begin = mock_book_begin; 00166 book_begin_struct.books = books; 00167 book_begin_struct.call_count = 0; 00168 g_assert( qof_object_register( fixture->qofobject ) == TRUE ); 00169 g_assert( qof_object_lookup( "my type object" ) == fixture->qofobject ); 00170 g_assert_cmpint( book_begin_struct.call_count, == , list_length ); 00171 00172 g_test_message( "Test registering the same object one more time" ); 00173 book_begin_struct.call_count = 0; 00174 g_assert( qof_object_register( fixture->qofobject ) == FALSE ); 00175 g_assert( qof_object_lookup( "my type object" ) == fixture->qofobject ); 00176 g_assert_cmpint( book_begin_struct.call_count, == , 0 ); 00177 00178 g_test_message( "Test new object register without book_begin specified" ); 00179 simple_object = new_object( "my type simple", "simple desc", EMPTY ); 00180 g_assert( qof_object_register( simple_object ) == TRUE ); 00181 g_assert( qof_object_lookup( "my type simple" ) == simple_object ); 00182 g_assert_cmpint( book_begin_struct.call_count, == , 0 ); 00183 00184 g_test_message( "Test register simple object one more time" ); 00185 g_assert( qof_object_register( simple_object ) == FALSE ); 00186 g_assert( qof_object_lookup( "my type simple" ) == simple_object ); 00187 00188 g_test_message( "Test book begin is called only one time when object is registered" ); 00189 simple_object->book_begin = mock_book_begin; 00190 book_begin_struct.books = books; 00191 book_begin_struct.call_count = 0; 00192 g_assert( qof_object_register( simple_object ) == FALSE ); 00193 g_assert_cmpint( book_begin_struct.call_count, == , 0 ); 00194 00195 g_list_foreach( books, (GFunc) qof_book_destroy, NULL ); 00196 g_list_free( books ); 00197 g_free( simple_object ); 00198 } 00199 00200 static void 00201 test_qof_object_lookup( Fixture *fixture, gconstpointer pData ) 00202 { 00203 g_test_message( "Test null check" ); 00204 g_assert( qof_object_lookup( NULL ) == NULL ); 00205 00206 g_test_message( "Test existing object lookup" ); 00207 g_assert( qof_object_register( fixture->qofobject ) == TRUE ); 00208 g_assert( qof_object_lookup( "my type object" ) == fixture->qofobject ); 00209 00210 g_test_message( "Test non existing object lookup" ); 00211 g_assert( qof_object_lookup( "anytype" ) == NULL ); 00212 } 00213 00214 static struct 00215 { 00216 gpointer data1; 00217 gpointer data2; 00218 } be_data; 00219 00220 static void 00221 test_qof_object_backend_register_lookup( Fixture *fixture, gconstpointer pData ) 00222 { 00223 g_test_message( "Test register and lookup null checks" ); 00224 g_assert( qof_object_register_backend( NULL, "test", &be_data ) == FALSE ); 00225 g_assert( qof_object_register_backend( "", "test", &be_data ) == FALSE ); 00226 g_assert( qof_object_register_backend( "test", NULL, &be_data ) == FALSE ); 00227 g_assert( qof_object_register_backend( "test", "", &be_data ) == FALSE ); 00228 g_assert( qof_object_register_backend( "test", "test", NULL ) == FALSE ); 00229 g_assert( qof_object_lookup_backend( NULL, "test" ) == NULL ); 00230 g_assert( qof_object_lookup_backend( "", "test" ) == NULL ); 00231 g_assert( qof_object_lookup_backend( "test", NULL ) == NULL ); 00232 g_assert( qof_object_lookup_backend( "test", "" ) == NULL ); 00233 00234 g_test_message( "Test new backend and type insert" ); 00235 g_assert( qof_object_lookup_backend( "type", "backend" ) == NULL ); 00236 g_assert( qof_object_register_backend( "type", "backend", &be_data.data1 ) == TRUE ); 00237 g_assert( qof_object_lookup_backend( "type", "backend" ) == &be_data.data1 ); 00238 00239 g_test_message( "Test type insert into existing backend" ); 00240 g_assert( qof_object_register_backend( "type2", "backend", &be_data.data2 ) == TRUE ); 00241 g_assert( qof_object_lookup_backend( "type", "backend" ) == &be_data.data1 ); 00242 g_assert( qof_object_lookup_backend( "type2", "backend" ) == &be_data.data2 ); 00243 } 00244 00245 static void 00246 test_qof_object_get_type_label( Fixture *fixture, gconstpointer pData ) 00247 { 00248 g_assert( qof_object_get_type_label( NULL ) == NULL ); 00249 00250 g_test_message( "Test with non existing object" ); 00251 g_assert( qof_object_get_type_label( "anytype" ) == NULL ); 00252 00253 g_test_message( "Test with existing registered object" ); 00254 g_assert( qof_object_register( fixture->qofobject ) == TRUE ); 00255 g_assert_cmpstr( qof_object_get_type_label( "my type object" ), == , "object desc" ); 00256 } 00257 00258 static struct 00259 { 00260 gpointer param; 00261 } printable_struct; 00262 00263 static const char * 00264 mock_printable( gpointer instance ) 00265 { 00266 g_assert( instance ); 00267 g_assert( instance == printable_struct.param ); 00268 return "printable was called"; 00269 } 00270 00271 static void 00272 test_qof_object_printable( Fixture *fixture, gconstpointer pData ) 00273 { 00274 gint param; 00275 00276 g_test_message( "Test null checks" ); 00277 g_assert( qof_object_printable( NULL, (gpointer)¶m ) == NULL ); 00278 g_assert( qof_object_printable( "test", NULL ) == NULL ); 00279 00280 g_test_message( "Test with non registered object" ); 00281 g_assert( qof_object_printable( "test", (gpointer)¶m ) == NULL ); 00282 00283 g_test_message( "Test with registered object and printable not set" ); 00284 g_assert( qof_object_register( fixture->qofobject ) == TRUE ); 00285 g_assert( qof_object_printable( "my type object", (gpointer)¶m ) == NULL ); 00286 00287 g_test_message( "Test with registered object and printable set" ); 00288 fixture->qofobject->printable = mock_printable; 00289 printable_struct.param = (gpointer)¶m; 00290 g_assert_cmpstr( qof_object_printable( "my type object", (gpointer)¶m ), == , "printable was called" ); 00291 } 00292 00293 static struct 00294 { 00295 QofBook *book; 00296 guint call_count; 00297 } object_book_begin_struct; 00298 00299 static void 00300 mock_object_book_begin( QofBook *book ) 00301 { 00302 g_assert( book ); 00303 g_assert( book == object_book_begin_struct.book ); 00304 object_book_begin_struct.call_count++; 00305 } 00306 00307 static void 00308 test_qof_object_book_begin( Fixture *fixture, gconstpointer pData ) 00309 { 00310 QofBook *book = NULL, *book2 = NULL; 00311 gint32 list_length; 00312 00313 g_test_message( "Test book begin with no objects" ); 00314 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) ); 00315 object_book_begin_struct.call_count = 0; 00316 book = g_object_new(QOF_TYPE_BOOK, NULL); 00317 g_assert( book ); 00318 qof_object_book_begin( book ); 00319 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) ); 00320 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book), != , -1 ); 00321 g_assert_cmpint( object_book_begin_struct.call_count, == , 0 ); 00322 00323 qof_book_destroy( book ); 00324 00325 list_length = generate_and_register_objects( 1, MOCK_OBJECT_BOOK_BEGIN ); 00326 00327 g_test_message( "Test book begin with random objects registered and book begin set up" ); 00328 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) ); 00329 book2 = g_object_new(QOF_TYPE_BOOK, NULL); 00330 g_assert( book2 ); 00331 object_book_begin_struct.book = book2; 00332 qof_object_book_begin( book2 ); 00333 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) ); 00334 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book2 ), != , -1 ); 00335 g_assert_cmpint( object_book_begin_struct.call_count, == , list_length ); 00336 00337 qof_book_destroy( book2 ); 00338 } 00339 00340 static void 00341 test_qof_object_book_end( Fixture *fixture, gconstpointer pData ) 00342 { 00343 QofBook *book = NULL, *book2 = NULL; 00344 gint32 list_length; 00345 00346 g_test_message( "Test book with no objects" ); 00347 book = qof_book_new(); 00348 g_assert( book ); 00349 object_book_begin_struct.call_count = 0; 00350 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) ); 00351 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book), != , -1 ); 00352 qof_book_destroy( book ); /* calls object_book_end */ 00353 g_assert_cmpint( object_book_begin_struct.call_count, == , 0 ); 00354 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) ); 00355 00356 list_length = generate_and_register_objects( 1, MOCK_OBJECT_BOOK_END ); 00357 00358 g_test_message( "Test book end with random objects registered and book end set up" ); 00359 book2 = qof_book_new(); 00360 g_assert( book2 ); 00361 object_book_begin_struct.book = book2; 00362 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) ); 00363 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book2 ), != , -1 ); 00364 qof_book_destroy( book2 ); /* calls object_book_end */ 00365 g_assert_cmpint( object_book_begin_struct.call_count, == , list_length ); 00366 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) ); 00367 } 00368 00369 static struct 00370 { 00371 GList *objects; 00372 guint call_count; 00373 gboolean result; 00374 } object_dirty_struct; 00375 00376 static gboolean 00377 mock_object_dirty( const QofCollection *col ) 00378 { 00379 QofObject *obj = NULL; 00380 00381 g_assert( col ); 00382 obj = object_dirty_struct.objects->data; 00383 object_dirty_struct.objects = object_dirty_struct.objects->next; 00384 g_assert( obj ); 00385 g_assert_cmpstr( qof_collection_get_type( col ), == , obj->e_type ); 00386 object_dirty_struct.call_count++; 00387 return object_dirty_struct.result; 00388 } 00389 00390 static void 00391 test_qof_object_is_dirty( Fixture *fixture, gconstpointer pData ) 00392 { 00393 QofBook *book = NULL; 00394 gint32 list_length; 00395 00396 g_test_message( "Test null check returns false" ); 00397 g_assert( qof_object_is_dirty( NULL ) == FALSE ); 00398 00399 g_test_message( "Test with no objects" ); 00400 book = qof_book_new(); 00401 g_assert( book ); 00402 object_dirty_struct.call_count = 0; 00403 g_assert( qof_object_is_dirty( book ) == FALSE ); 00404 g_assert_cmpint( object_dirty_struct.call_count, == , 0 ); 00405 00406 list_length = generate_and_register_objects( 1, MOCK_OBJECT_DIRTY ); 00407 00408 g_test_message( "Test with registered objects and suppose all collections are clean" ); 00409 object_dirty_struct.objects = get_object_modules(); 00410 object_dirty_struct.result = FALSE; 00411 g_assert( qof_object_is_dirty( book ) == FALSE ); 00412 g_assert_cmpint( object_dirty_struct.call_count, == , list_length ); 00413 00414 g_test_message( "Test with registered objects and suppose first collection is dirty" ); 00415 object_dirty_struct.objects = get_object_modules(); 00416 object_dirty_struct.result = TRUE; 00417 object_dirty_struct.call_count = 0; 00418 g_assert( qof_object_is_dirty( book ) == TRUE ); 00419 g_assert_cmpint( object_dirty_struct.call_count, == , 1 ); /* should break on first */ 00420 00421 qof_book_destroy( book ); 00422 } 00423 00424 static struct 00425 { 00426 GList *objects; 00427 guint call_count; 00428 } object_mark_clean_struct; 00429 00430 static void 00431 mock_object_mark_clean( QofCollection *col ) 00432 { 00433 QofObject *obj = NULL; 00434 00435 g_assert( col ); 00436 obj = object_mark_clean_struct.objects->data; 00437 object_mark_clean_struct.objects = object_mark_clean_struct.objects->next; 00438 g_assert( obj ); 00439 g_assert_cmpstr( qof_collection_get_type( col ), == , obj->e_type ); 00440 object_mark_clean_struct.call_count++; 00441 } 00442 00443 static void 00444 test_qof_object_mark_clean( Fixture *fixture, gconstpointer pData ) 00445 { 00446 QofBook *book = NULL; 00447 gint32 list_length; 00448 00449 g_test_message( "Test with no objects" ); 00450 book = qof_book_new(); 00451 g_assert( book ); 00452 object_mark_clean_struct.call_count = 0; 00453 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 ); 00454 qof_object_mark_clean( book ); 00455 g_assert_cmpint( object_mark_clean_struct.call_count, == , 0 ); 00456 00457 list_length = generate_and_register_objects( 1, MOCK_OBJECT_MARK_CLEAN ); 00458 00459 g_test_message( "Test with registered objects and mark clean set up" ); 00460 object_mark_clean_struct.objects = get_object_modules(); 00461 qof_object_mark_clean( book ); 00462 g_assert_cmpint( object_mark_clean_struct.call_count, == , list_length ); 00463 00464 qof_book_destroy( book ); 00465 } 00466 00467 static struct 00468 { 00469 QofBook *book; 00470 QofInstance *inst; 00471 gboolean is_called; 00472 } object_create_struct; 00473 00474 static gpointer 00475 mock_object_create( QofBook *book ) 00476 { 00477 QofInstance *inst = NULL; 00478 00479 inst = g_object_new(QOF_TYPE_INSTANCE, NULL); 00480 g_assert( inst ); 00481 g_assert( QOF_IS_INSTANCE( inst ) ); 00482 g_assert( book ); 00483 g_assert( book == object_create_struct.book ); 00484 object_create_struct.is_called = TRUE; 00485 object_create_struct.inst = inst; 00486 return inst; 00487 } 00488 00489 static void 00490 test_qof_object_new_instance( Fixture *fixture, gconstpointer pData ) 00491 { 00492 QofBook *book = NULL; 00493 QofInstance *inst = NULL; 00494 00495 book = qof_book_new(); 00496 g_assert( book ); 00497 00498 g_test_message( "Test null check" ); 00499 g_assert( qof_object_new_instance( NULL, book ) == NULL ); 00500 00501 g_test_message( "Test non existing object type" ); 00502 g_assert( qof_object_new_instance( "non existing type", book ) == NULL ); 00503 00504 g_test_message( "Test with registered object type and create not set" ); 00505 g_assert( qof_object_register( fixture->qofobject ) ); 00506 g_assert( qof_object_new_instance( fixture->qofobject->e_type, book ) == NULL ); 00507 00508 g_test_message( "Test with registered object type and create set" ); 00509 object_create_struct.is_called = FALSE; 00510 object_create_struct.book = book; 00511 object_create_struct.inst = NULL; 00512 fixture->qofobject->create = mock_object_create; 00513 inst = qof_object_new_instance( fixture->qofobject->e_type, book ); 00514 g_assert( inst ); 00515 g_assert( object_create_struct.is_called == TRUE ); 00516 g_assert( object_create_struct.inst == inst ); 00517 00518 g_object_unref( inst ); 00519 qof_book_destroy( book ); 00520 } 00521 00522 static void 00523 mock_object_foreach( const QofCollection *col, QofInstanceForeachCB cb, gpointer data) 00524 { 00525 } 00526 00527 static void 00528 test_qof_object_compliance( Fixture *fixture, gconstpointer pData ) 00529 { 00530 g_assert( qof_object_register( fixture->qofobject ) ); 00531 00532 g_test_message( "Test when neither create nor foreach set" ); 00533 g_assert( qof_object_compliance( fixture->qofobject->e_type, FALSE ) == FALSE ); 00534 g_assert( qof_object_compliance( fixture->qofobject->e_type, TRUE ) == FALSE ); 00535 00536 g_test_message( "Test when only create set" ); 00537 fixture->qofobject->create = mock_object_create; 00538 g_assert( qof_object_compliance( fixture->qofobject->e_type, FALSE ) == FALSE ); 00539 g_assert( qof_object_compliance( fixture->qofobject->e_type, TRUE ) == FALSE ); 00540 00541 g_test_message( "Test when only foreach set" ); 00542 fixture->qofobject->create = NULL; 00543 fixture->qofobject->foreach = mock_object_foreach; 00544 g_assert( qof_object_compliance( fixture->qofobject->e_type, FALSE ) == FALSE ); 00545 g_assert( qof_object_compliance( fixture->qofobject->e_type, TRUE ) == FALSE ); 00546 00547 g_test_message( "Test when both set" ); 00548 fixture->qofobject->create = mock_object_create; 00549 fixture->qofobject->foreach = mock_object_foreach; 00550 g_assert( qof_object_compliance( fixture->qofobject->e_type, FALSE ) == TRUE ); 00551 g_assert( qof_object_compliance( fixture->qofobject->e_type, TRUE ) == TRUE ); 00552 } 00553 00554 static struct 00555 { 00556 GList *objects; 00557 gpointer user_data; 00558 guint call_count; 00559 } foreach_type_cb_struct; 00560 00561 static void 00562 mock_foreach_type_cb( QofObject *object, gpointer user_data ) 00563 { 00564 g_assert( object ); 00565 g_assert( user_data ); 00566 g_assert( object == foreach_type_cb_struct.objects->data ); 00567 g_assert( user_data == foreach_type_cb_struct.user_data ); 00568 foreach_type_cb_struct.objects = foreach_type_cb_struct.objects->next; 00569 foreach_type_cb_struct.call_count++; 00570 } 00571 00572 static void 00573 test_qof_object_foreach_type( Fixture *fixture, gconstpointer pData ) 00574 { 00575 gint user_data; 00576 gint32 list_length; 00577 00578 g_test_message( "Test with no objects" ); 00579 foreach_type_cb_struct.call_count = 0; 00580 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 ); 00581 qof_object_foreach_type( mock_foreach_type_cb, ( gpointer ) &user_data ); 00582 g_assert_cmpint( foreach_type_cb_struct.call_count, == , 0 ); 00583 00584 list_length = generate_and_register_objects( 1, EMPTY ); 00585 00586 g_test_message( "Test foreach cb" ); 00587 foreach_type_cb_struct.objects = get_object_modules(); 00588 foreach_type_cb_struct.user_data = ( gpointer ) &user_data; 00589 foreach_type_cb_struct.call_count = 0; 00590 qof_object_foreach_type( mock_foreach_type_cb, ( gpointer ) &user_data ); 00591 g_assert_cmpint( foreach_type_cb_struct.call_count, == , list_length ); 00592 } 00593 00594 static struct 00595 { 00596 gpointer user_data; 00597 QofInstanceForeachCB cb; 00598 QofCollection *col; 00599 gboolean is_called; 00600 } foreach_cb_struct; 00601 00602 static void 00603 mock_instance_foreach_cb( QofInstance *inst, gpointer user_data ) 00604 { 00605 } 00606 00607 static void 00608 mock_foreach( const QofCollection *col, QofInstanceForeachCB cb, gpointer user_data ) 00609 { 00610 g_assert( col ); 00611 g_assert( cb ); 00612 g_assert( user_data ); 00613 g_assert( col == foreach_cb_struct.col ); 00614 g_assert( user_data == foreach_cb_struct.user_data ); 00615 g_assert( cb == foreach_cb_struct.cb ); 00616 foreach_cb_struct.is_called = TRUE; 00617 } 00618 00619 static void 00620 test_qof_object_foreach( Fixture *fixture, gconstpointer pData ) 00621 { 00622 gint user_data; 00623 QofBook *book = NULL; 00624 QofCollection *col = NULL; 00625 00626 /* setup */ 00627 book = qof_book_new(); 00628 g_assert( book ); 00629 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 ); 00630 qof_object_register( fixture->qofobject ); 00631 g_assert_cmpint( g_list_length( get_object_modules() ), == , 1 ); 00632 col = qof_book_get_collection( book, fixture->qofobject->e_type ); /* make col already exist */ 00633 g_assert( col ); 00634 00635 g_test_message( "Test foreach and data" ); 00636 foreach_cb_struct.user_data = ( gpointer ) &user_data; 00637 foreach_cb_struct.is_called = FALSE; 00638 foreach_cb_struct.col = col; 00639 foreach_cb_struct.cb = mock_instance_foreach_cb; 00640 fixture->qofobject->foreach = mock_foreach; 00641 qof_object_foreach( fixture->qofobject->e_type, book, mock_instance_foreach_cb, ( gpointer ) &user_data ); 00642 g_assert( foreach_cb_struct.is_called == TRUE ); 00643 00644 qof_book_destroy( book ); 00645 } 00646 00647 static struct 00648 { 00649 GList *instances; 00650 gpointer user_data; 00651 guint call_count; 00652 } foreach_for_sorted_struct; 00653 00654 static void 00655 mock_foreach_for_sorted( const QofCollection *col, QofInstanceForeachCB cb, gpointer user_data ) 00656 { 00657 GList *iter; 00658 00659 g_assert( col ); 00660 g_assert( cb ); 00661 g_assert( user_data ); 00662 00663 for (iter = foreach_for_sorted_struct.instances; iter; iter = iter->next) 00664 { 00665 cb( iter->data, user_data ); 00666 } 00667 } 00668 00669 static void 00670 mock_instance_foreach_cb_for_sorted( QofInstance *inst, gpointer user_data ) 00671 { 00672 g_assert( inst ); 00673 g_assert( user_data ); 00674 g_assert_cmpint( g_list_index( foreach_for_sorted_struct.instances, (gconstpointer) inst ), != , -1 ); 00675 g_assert( user_data == foreach_for_sorted_struct.user_data ); 00676 foreach_for_sorted_struct.call_count++; 00677 } 00678 00679 static void 00680 test_qof_object_foreach_sorted( Fixture *fixture, gconstpointer pData ) 00681 { 00682 int i; 00683 gint32 list_length = g_test_rand_int_range( 0, 5 ); 00684 gint user_data; 00685 QofBook *book = NULL; 00686 QofCollection *col = NULL; 00687 foreach_for_sorted_struct.instances = NULL; 00688 00689 /* setup */ 00690 book = qof_book_new(); 00691 g_assert( book ); 00692 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 ); 00693 qof_object_register( fixture->qofobject ); 00694 g_assert_cmpint( g_list_length( get_object_modules() ), == , 1 ); 00695 00696 fixture->qofobject->foreach = mock_foreach_for_sorted; 00697 /* init instances */ 00698 col = qof_book_get_collection( book, fixture->qofobject->e_type ); 00699 for (i = 0; i < list_length; i++ ) 00700 { 00701 QofInstance * inst = g_object_new( QOF_TYPE_INSTANCE, NULL ); 00702 g_assert( QOF_IS_INSTANCE( inst ) ); 00703 foreach_for_sorted_struct.instances = g_list_append( foreach_for_sorted_struct.instances, inst ); 00704 qof_collection_insert_entity( col, inst ); 00705 } 00706 g_assert_cmpint( list_length, == , g_list_length( foreach_for_sorted_struct.instances ) ); 00707 00708 foreach_for_sorted_struct.call_count = 0; 00709 foreach_for_sorted_struct.user_data = &user_data; 00710 qof_object_foreach_sorted( fixture->qofobject->e_type, book, mock_instance_foreach_cb_for_sorted, ( gpointer ) &user_data ); 00711 g_assert_cmpint( list_length, == , foreach_for_sorted_struct.call_count ); 00712 00713 qof_book_destroy( book ); 00714 g_list_free( foreach_for_sorted_struct.instances ); 00715 } 00716 00717 static struct 00718 { 00719 QofIdTypeConst type; 00720 gpointer backend_data; 00721 gpointer user_data; 00722 guint call_count; 00723 } foreach_backend_struct; 00724 00725 static void 00726 mock_foreach_backend( QofIdTypeConst type, gpointer backend_data, gpointer user_data) 00727 { 00728 g_assert( type ); 00729 g_assert( backend_data ); 00730 g_assert( user_data ); 00731 g_assert_cmpstr( type, == , foreach_backend_struct.type ); 00732 g_assert( backend_data == foreach_backend_struct.backend_data ); 00733 g_assert( user_data == foreach_backend_struct.user_data ); 00734 foreach_backend_struct.call_count++; 00735 } 00736 00737 static void 00738 test_qof_object_foreach_backend( Fixture *fixture, gconstpointer pData ) 00739 { 00740 gint backend_data; 00741 gint user_data; 00742 00743 g_assert_cmpint( g_hash_table_size( get_backend_data() ), == , 0 ); 00744 qof_object_register_backend( "type1", "backend", (gpointer) &backend_data ); /* register backend */ 00745 g_assert_cmpint( g_hash_table_size( get_backend_data() ), == , 1 ); 00746 00747 foreach_backend_struct.call_count = 0; 00748 foreach_backend_struct.backend_data = (gpointer) &backend_data; 00749 foreach_backend_struct.user_data = (gpointer) &user_data; 00750 foreach_backend_struct.type = "type1"; 00751 qof_object_foreach_backend ( "backend", mock_foreach_backend, (gpointer) &user_data); 00752 g_assert_cmpint( foreach_backend_struct.call_count, == , 1 ); 00753 } 00754 00755 void 00756 test_suite_qofobject (void) 00757 { 00758 GNC_TEST_ADD( suitename, "qof object register", Fixture, NULL, setup, test_qof_object_register, teardown ); 00759 GNC_TEST_ADD( suitename, "qof object lookup", Fixture, NULL, setup, test_qof_object_lookup, teardown ); 00760 GNC_TEST_ADD( suitename, "qof object register and lookup backend", Fixture, NULL, setup, test_qof_object_backend_register_lookup, teardown ); 00761 GNC_TEST_ADD( suitename, "qof object get type label", Fixture, NULL, setup, test_qof_object_get_type_label, teardown ); 00762 GNC_TEST_ADD( suitename, "qof object printable", Fixture, NULL, setup, test_qof_object_printable, teardown ); 00763 GNC_TEST_ADD( suitename, "qof object book begin", Fixture, NULL, setup, test_qof_object_book_begin, teardown ); 00764 GNC_TEST_ADD( suitename, "qof object book end", Fixture, NULL, setup, test_qof_object_book_end, teardown ); 00765 GNC_TEST_ADD( suitename, "qof object is dirty", Fixture, NULL, setup, test_qof_object_is_dirty, teardown ); 00766 GNC_TEST_ADD( suitename, "qof object mark clean", Fixture, NULL, setup, test_qof_object_mark_clean, teardown ); 00767 GNC_TEST_ADD( suitename, "qof object new instance", Fixture, NULL, setup, test_qof_object_new_instance, teardown ); 00768 GNC_TEST_ADD( suitename, "qof object compliance", Fixture, NULL, setup, test_qof_object_compliance, teardown ); 00769 GNC_TEST_ADD( suitename, "qof object foreach type", Fixture, NULL, setup, test_qof_object_foreach_type, teardown ); 00770 GNC_TEST_ADD( suitename, "qof object foreach", Fixture, NULL, setup, test_qof_object_foreach, teardown ); 00771 GNC_TEST_ADD( suitename, "qof object foreach sorted", Fixture, NULL, setup, test_qof_object_foreach_sorted, teardown ); 00772 GNC_TEST_ADD( suitename, "qof object foreach backend", Fixture, NULL, setup, test_qof_object_foreach_backend, teardown ); 00773 }
1.7.4