30 #include <unittest-support.h>
36 #include "../qofobject-p.h"
38 static const gchar *suitename =
"/qof/qofobject";
39 void test_suite_qofobject (
void );
48 MOCK_OBJECT_BOOK_BEGIN = 1,
51 MOCK_OBJECT_MARK_CLEAN,
55 static void mock_object_book_begin(
QofBook *book );
56 static gboolean mock_object_dirty(
const QofCollection *col );
57 static void mock_object_mark_clean( QofCollection *col );
60 new_object(
QofIdType e_type,
const char *type_label, MockFields field)
67 object->e_type = e_type;
68 object->type_label = type_label;
71 case MOCK_OBJECT_BOOK_BEGIN:
72 object->book_begin = mock_object_book_begin;
74 case MOCK_OBJECT_BOOK_END:
75 object->book_end = mock_object_book_begin;
77 case MOCK_OBJECT_DIRTY:
78 object->is_dirty = mock_object_dirty;
80 case MOCK_OBJECT_MARK_CLEAN:
81 object->mark_clean = mock_object_mark_clean;
93 extern gboolean get_object_is_initialized(
void );
94 extern GList* get_object_modules(
void );
95 extern GList* get_book_list(
void );
96 extern GHashTable* get_backend_data(
void );
103 setup(
Fixture *fixture, gconstpointer pData )
105 qof_object_initialize();
106 fixture->qofobject = new_object(
"my type object",
"object desc", EMPTY );
110 teardown(
Fixture *fixture, gconstpointer pData )
112 g_free( fixture->qofobject );
113 qof_object_shutdown();
124 generate_and_register_objects( guint min_objects, MockFields mock_field )
126 gint32 list_length = g_test_rand_int_range( min_objects, 5 );
127 const char *types[5] = {
"type1",
"type2",
"type3",
"type4",
"type5"};
130 g_assert_cmpint( min_objects, >= , 0 );
131 g_assert_cmpint( min_objects, < , 5 );
132 for (i = 0; i < list_length; i++ )
134 QofObject *
object = new_object( types[i],
"desc", mock_field );
137 g_assert_cmpint( g_list_length( get_object_modules() ), == , (i + 1) );
139 g_assert_cmpint( list_length, == , g_list_length( get_object_modules() ) );
154 mock_book_begin(
QofBook *book )
157 g_assert( book == book_begin_struct.books->data );
158 book_begin_struct.books = book_begin_struct.books->next;
159 book_begin_struct.call_count++;
163 test_qof_object_register(
Fixture *fixture, gconstpointer pData )
166 gint32 list_length = g_test_rand_int_range( 0, 5 );
170 for (i = 0; i < list_length; i++ )
174 books = g_list_prepend ( books, book );
175 g_assert_cmpint( g_list_length( books ), == , (i + 1) );
177 g_assert_cmpint( list_length, == , g_list_length( books ) );
179 g_test_message(
"Test null check" );
182 g_test_message(
"Test new object register with book_begin specified" );
183 fixture->qofobject->
book_begin = mock_book_begin;
184 book_begin_struct.books = books;
185 book_begin_struct.call_count = 0;
188 g_assert_cmpint( book_begin_struct.call_count, == , list_length );
190 g_test_message(
"Test registering the same object one more time" );
191 book_begin_struct.call_count = 0;
194 g_assert_cmpint( book_begin_struct.call_count, == , 0 );
196 g_test_message(
"Test new object register without book_begin specified" );
197 simple_object = new_object(
"my type simple",
"simple desc", EMPTY );
200 g_assert_cmpint( book_begin_struct.call_count, == , 0 );
202 g_test_message(
"Test register simple object one more time" );
206 g_test_message(
"Test book begin is called only one time when object is registered" );
208 book_begin_struct.books = books;
209 book_begin_struct.call_count = 0;
211 g_assert_cmpint( book_begin_struct.call_count, == , 0 );
214 g_list_free( books );
215 g_free( simple_object );
219 test_qof_object_lookup(
Fixture *fixture, gconstpointer pData )
221 g_test_message(
"Test null check" );
224 g_test_message(
"Test existing object lookup" );
228 g_test_message(
"Test non existing object lookup" );
239 test_qof_object_backend_register_lookup(
Fixture *fixture, gconstpointer pData )
241 g_test_message(
"Test register and lookup null checks" );
247 g_assert( qof_object_lookup_backend( NULL,
"test" ) == NULL );
248 g_assert( qof_object_lookup_backend(
"",
"test" ) == NULL );
249 g_assert( qof_object_lookup_backend(
"test", NULL ) == NULL );
250 g_assert( qof_object_lookup_backend(
"test",
"" ) == NULL );
252 g_test_message(
"Test new backend and type insert" );
253 g_assert( qof_object_lookup_backend(
"type",
"backend" ) == NULL );
255 g_assert( qof_object_lookup_backend(
"type",
"backend" ) == &be_data.data1 );
257 g_test_message(
"Test type insert into existing backend" );
259 g_assert( qof_object_lookup_backend(
"type",
"backend" ) == &be_data.data1 );
260 g_assert( qof_object_lookup_backend(
"type2",
"backend" ) == &be_data.data2 );
264 test_qof_object_get_type_label(
Fixture *fixture, gconstpointer pData )
268 g_test_message(
"Test with non existing object" );
271 g_test_message(
"Test with existing registered object" );
282 mock_printable( gpointer instance )
284 g_assert( instance );
285 g_assert( instance == printable_struct.param );
286 return "printable was called";
290 test_qof_object_printable(
Fixture *fixture, gconstpointer pData )
294 g_test_message(
"Test null checks" );
298 g_test_message(
"Test with non registered object" );
301 g_test_message(
"Test with registered object and printable not set" );
305 g_test_message(
"Test with registered object and printable set" );
306 fixture->qofobject->
printable = mock_printable;
307 printable_struct.param = (gpointer)¶m;
308 g_assert_cmpstr(
qof_object_printable(
"my type object", (gpointer)¶m ), == ,
"printable was called" );
315 } object_book_begin_struct;
318 mock_object_book_begin(
QofBook *book )
321 g_assert( book == object_book_begin_struct.book );
322 object_book_begin_struct.call_count++;
326 test_qof_object_book_begin(
Fixture *fixture, gconstpointer pData )
328 QofBook *book = NULL, *book2 = NULL;
331 g_test_message(
"Test book begin with no objects" );
332 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) );
333 object_book_begin_struct.call_count = 0;
334 book = g_object_new(QOF_TYPE_BOOK, NULL);
337 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) );
338 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book), != , -1 );
339 g_assert_cmpint( object_book_begin_struct.call_count, == , 0 );
343 list_length = generate_and_register_objects( 1, MOCK_OBJECT_BOOK_BEGIN );
345 g_test_message(
"Test book begin with random objects registered and book begin set up" );
346 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) );
347 book2 = g_object_new(QOF_TYPE_BOOK, NULL);
349 object_book_begin_struct.book = book2;
351 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) );
352 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book2 ), != , -1 );
353 g_assert_cmpint( object_book_begin_struct.call_count, == , list_length );
359 test_qof_object_book_end(
Fixture *fixture, gconstpointer pData )
361 QofBook *book = NULL, *book2 = NULL;
364 g_test_message(
"Test book with no objects" );
367 object_book_begin_struct.call_count = 0;
368 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) );
369 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book), != , -1 );
371 g_assert_cmpint( object_book_begin_struct.call_count, == , 0 );
372 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) );
374 list_length = generate_and_register_objects( 1, MOCK_OBJECT_BOOK_END );
376 g_test_message(
"Test book end with random objects registered and book end set up" );
379 object_book_begin_struct.book = book2;
380 g_assert_cmpint( 1, == , g_list_length( get_book_list() ) );
381 g_assert_cmpint( g_list_index( get_book_list(), (gconstpointer) book2 ), != , -1 );
383 g_assert_cmpint( object_book_begin_struct.call_count, == , list_length );
384 g_assert_cmpint( 0, == , g_list_length( get_book_list() ) );
392 } object_dirty_struct;
395 mock_object_dirty(
const QofCollection *col )
400 obj = object_dirty_struct.objects->data;
401 object_dirty_struct.objects = object_dirty_struct.objects->next;
404 object_dirty_struct.call_count++;
405 return object_dirty_struct.result;
409 test_qof_object_is_dirty(
Fixture *fixture, gconstpointer pData )
414 g_test_message(
"Test null check returns false" );
415 g_assert( qof_object_is_dirty( NULL ) == FALSE );
417 g_test_message(
"Test with no objects" );
420 object_dirty_struct.call_count = 0;
421 g_assert( qof_object_is_dirty( book ) == FALSE );
422 g_assert_cmpint( object_dirty_struct.call_count, == , 0 );
424 list_length = generate_and_register_objects( 1, MOCK_OBJECT_DIRTY );
426 g_test_message(
"Test with registered objects and suppose all collections are clean" );
427 object_dirty_struct.objects = get_object_modules();
428 object_dirty_struct.result = FALSE;
429 g_assert( qof_object_is_dirty( book ) == FALSE );
430 g_assert_cmpint( object_dirty_struct.call_count, == , list_length );
432 g_test_message(
"Test with registered objects and suppose first collection is dirty" );
433 object_dirty_struct.objects = get_object_modules();
434 object_dirty_struct.result = TRUE;
435 object_dirty_struct.call_count = 0;
436 g_assert( qof_object_is_dirty( book ) == TRUE );
437 g_assert_cmpint( object_dirty_struct.call_count, == , 1 );
446 } object_mark_clean_struct;
449 mock_object_mark_clean( QofCollection *col )
454 obj = object_mark_clean_struct.objects->data;
455 object_mark_clean_struct.objects = object_mark_clean_struct.objects->next;
458 object_mark_clean_struct.call_count++;
462 test_qof_object_mark_clean(
Fixture *fixture, gconstpointer pData )
467 g_test_message(
"Test with no objects" );
470 object_mark_clean_struct.call_count = 0;
471 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 );
472 qof_object_mark_clean( book );
473 g_assert_cmpint( object_mark_clean_struct.call_count, == , 0 );
475 list_length = generate_and_register_objects( 1, MOCK_OBJECT_MARK_CLEAN );
477 g_test_message(
"Test with registered objects and mark clean set up" );
478 object_mark_clean_struct.objects = get_object_modules();
479 qof_object_mark_clean( book );
480 g_assert_cmpint( object_mark_clean_struct.call_count, == , list_length );
490 } object_create_struct;
493 mock_object_create(
QofBook *book )
497 inst = g_object_new(QOF_TYPE_INSTANCE, NULL);
499 g_assert( QOF_IS_INSTANCE( inst ) );
501 g_assert( book == object_create_struct.book );
502 object_create_struct.is_called = TRUE;
503 object_create_struct.inst = inst;
508 test_qof_object_new_instance(
Fixture *fixture, gconstpointer pData )
516 g_test_message(
"Test null check" );
519 g_test_message(
"Test non existing object type" );
522 g_test_message(
"Test with registered object type and create not set" );
526 g_test_message(
"Test with registered object type and create set" );
527 object_create_struct.is_called = FALSE;
528 object_create_struct.book = book;
529 object_create_struct.inst = NULL;
530 fixture->qofobject->
create = mock_object_create;
533 g_assert( object_create_struct.is_called == TRUE );
534 g_assert( object_create_struct.inst == inst );
536 g_object_unref( inst );
546 test_qof_object_compliance(
Fixture *fixture, gconstpointer pData )
550 g_test_message(
"Test when neither create nor foreach set" );
554 g_test_message(
"Test when only create set" );
555 fixture->qofobject->
create = mock_object_create;
559 g_test_message(
"Test when only foreach set" );
560 fixture->qofobject->
create = NULL;
561 fixture->qofobject->
foreach = mock_object_foreach;
565 g_test_message(
"Test when both set" );
566 fixture->qofobject->
create = mock_object_create;
567 fixture->qofobject->
foreach = mock_object_foreach;
577 } foreach_type_cb_struct;
580 mock_foreach_type_cb(
QofObject *
object, gpointer user_data )
583 g_assert( user_data );
584 g_assert(
object == foreach_type_cb_struct.objects->data );
585 g_assert( user_data == foreach_type_cb_struct.user_data );
586 foreach_type_cb_struct.objects = foreach_type_cb_struct.objects->next;
587 foreach_type_cb_struct.call_count++;
591 test_qof_object_foreach_type(
Fixture *fixture, gconstpointer pData )
596 g_test_message(
"Test with no objects" );
597 foreach_type_cb_struct.call_count = 0;
598 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 );
600 g_assert_cmpint( foreach_type_cb_struct.call_count, == , 0 );
602 list_length = generate_and_register_objects( 1, EMPTY );
604 g_test_message(
"Test foreach cb" );
605 foreach_type_cb_struct.objects = get_object_modules();
606 foreach_type_cb_struct.user_data = ( gpointer ) &user_data;
607 foreach_type_cb_struct.call_count = 0;
609 g_assert_cmpint( foreach_type_cb_struct.call_count, == , list_length );
621 mock_instance_foreach_cb(
QofInstance *inst, gpointer user_data )
630 g_assert( user_data );
631 g_assert( col == foreach_cb_struct.col );
632 g_assert( user_data == foreach_cb_struct.user_data );
633 g_assert( cb == foreach_cb_struct.cb );
634 foreach_cb_struct.is_called = TRUE;
638 test_qof_object_foreach(
Fixture *fixture, gconstpointer pData )
642 QofCollection *col = NULL;
647 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 );
649 g_assert_cmpint( g_list_length( get_object_modules() ), == , 1 );
653 g_test_message(
"Test foreach and data" );
654 foreach_cb_struct.user_data = ( gpointer ) &user_data;
655 foreach_cb_struct.is_called = FALSE;
656 foreach_cb_struct.col = col;
657 foreach_cb_struct.cb = mock_instance_foreach_cb;
658 fixture->qofobject->
foreach = mock_foreach;
659 qof_object_foreach( fixture->qofobject->e_type, book, mock_instance_foreach_cb, ( gpointer ) &user_data );
660 g_assert( foreach_cb_struct.is_called == TRUE );
670 } foreach_for_sorted_struct;
673 mock_foreach_for_sorted(
const QofCollection *col,
QofInstanceForeachCB cb, gpointer user_data )
679 g_assert( user_data );
681 for (iter = foreach_for_sorted_struct.instances; iter; iter = iter->next)
683 cb( iter->data, user_data );
688 mock_instance_foreach_cb_for_sorted(
QofInstance *inst, gpointer user_data )
691 g_assert( user_data );
692 g_assert_cmpint( g_list_index( foreach_for_sorted_struct.instances, (gconstpointer) inst ), != , -1 );
693 g_assert( user_data == foreach_for_sorted_struct.user_data );
694 foreach_for_sorted_struct.call_count++;
698 test_qof_object_foreach_sorted(
Fixture *fixture, gconstpointer pData )
701 gint32 list_length = g_test_rand_int_range( 0, 5 );
704 QofCollection *col = NULL;
705 foreach_for_sorted_struct.instances = NULL;
710 g_assert_cmpint( g_list_length( get_object_modules() ), == , 0 );
712 g_assert_cmpint( g_list_length( get_object_modules() ), == , 1 );
714 fixture->qofobject->
foreach = mock_foreach_for_sorted;
717 for (i = 0; i < list_length; i++ )
719 QofInstance * inst = g_object_new( QOF_TYPE_INSTANCE, NULL );
720 g_assert( QOF_IS_INSTANCE( inst ) );
721 foreach_for_sorted_struct.instances = g_list_append( foreach_for_sorted_struct.instances, inst );
724 g_assert_cmpint( list_length, == , g_list_length( foreach_for_sorted_struct.instances ) );
726 foreach_for_sorted_struct.call_count = 0;
727 foreach_for_sorted_struct.user_data = &user_data;
728 qof_object_foreach_sorted( fixture->qofobject->e_type, book, mock_instance_foreach_cb_for_sorted, ( gpointer ) &user_data );
729 g_assert_cmpint( list_length, == , foreach_for_sorted_struct.call_count );
732 g_list_free( foreach_for_sorted_struct.instances );
738 gpointer backend_data;
741 } foreach_backend_struct;
744 mock_foreach_backend(
QofIdTypeConst type, gpointer backend_data, gpointer user_data)
747 g_assert( backend_data );
748 g_assert( user_data );
749 g_assert_cmpstr( type, == , foreach_backend_struct.type );
750 g_assert( backend_data == foreach_backend_struct.backend_data );
751 g_assert( user_data == foreach_backend_struct.user_data );
752 foreach_backend_struct.call_count++;
756 test_qof_object_foreach_backend(
Fixture *fixture, gconstpointer pData )
761 g_assert_cmpint( g_hash_table_size( get_backend_data() ), == , 0 );
763 g_assert_cmpint( g_hash_table_size( get_backend_data() ), == , 1 );
765 foreach_backend_struct.call_count = 0;
766 foreach_backend_struct.backend_data = (gpointer) &backend_data;
767 foreach_backend_struct.user_data = (gpointer) &user_data;
768 foreach_backend_struct.type =
"type1";
769 qof_object_foreach_backend (
"backend", mock_foreach_backend, (gpointer) &user_data);
770 g_assert_cmpint( foreach_backend_struct.call_count, == , 1 );
774 test_suite_qofobject (
void)
776 GNC_TEST_ADD( suitename,
"qof object register",
Fixture, NULL, setup, test_qof_object_register, teardown );
777 GNC_TEST_ADD( suitename,
"qof object lookup",
Fixture, NULL, setup, test_qof_object_lookup, teardown );
778 GNC_TEST_ADD( suitename,
"qof object register and lookup backend",
Fixture, NULL, setup, test_qof_object_backend_register_lookup, teardown );
779 GNC_TEST_ADD( suitename,
"qof object get type label",
Fixture, NULL, setup, test_qof_object_get_type_label, teardown );
780 GNC_TEST_ADD( suitename,
"qof object printable",
Fixture, NULL, setup, test_qof_object_printable, teardown );
781 GNC_TEST_ADD( suitename,
"qof object book begin",
Fixture, NULL, setup, test_qof_object_book_begin, teardown );
782 GNC_TEST_ADD( suitename,
"qof object book end",
Fixture, NULL, setup, test_qof_object_book_end, teardown );
783 GNC_TEST_ADD( suitename,
"qof object is dirty",
Fixture, NULL, setup, test_qof_object_is_dirty, teardown );
784 GNC_TEST_ADD( suitename,
"qof object mark clean",
Fixture, NULL, setup, test_qof_object_mark_clean, teardown );
785 GNC_TEST_ADD( suitename,
"qof object new instance",
Fixture, NULL, setup, test_qof_object_new_instance, teardown );
786 GNC_TEST_ADD( suitename,
"qof object compliance",
Fixture, NULL, setup, test_qof_object_compliance, teardown );
787 GNC_TEST_ADD( suitename,
"qof object foreach type",
Fixture, NULL, setup, test_qof_object_foreach_type, teardown );
788 GNC_TEST_ADD( suitename,
"qof object foreach",
Fixture, NULL, setup, test_qof_object_foreach, teardown );
789 GNC_TEST_ADD( suitename,
"qof object foreach sorted",
Fixture, NULL, setup, test_qof_object_foreach_sorted, teardown );
790 GNC_TEST_ADD( suitename,
"qof object foreach backend",
Fixture, NULL, setup, test_qof_object_foreach_backend, teardown );
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
const char * qof_object_get_type_label(QofIdTypeConst type_name)
void qof_object_book_begin(QofBook *book)
const gchar * QofIdTypeConst
gboolean qof_object_compliance(QofIdTypeConst type_name, gboolean warn)
check an object can be created and supports iteration
QofBook * qof_book_new(void)
gpointer(* create)(QofBook *)
#define QOF_OBJECT_VERSION
void qof_object_foreach_type(QofForeachTypeCB cb, gpointer user_data)
void qof_object_foreach_sorted(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
const QofObject * qof_object_lookup(QofIdTypeConst type_name)
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
void qof_collection_insert_entity(QofCollection *, QofInstance *)
void(* book_begin)(QofBook *)
gpointer qof_object_new_instance(QofIdTypeConst type_name, QofBook *book)
const char *(* printable)(gpointer instance)
void(* QofInstanceForeachCB)(QofInstance *, gpointer user_data)
QofIdType qof_collection_get_type(const QofCollection *)
void(* foreach)(const QofCollection *, QofInstanceForeachCB, gpointer)
const char * qof_object_printable(QofIdTypeConst type_name, gpointer instance)
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
gboolean qof_object_register(const QofObject *object)
void qof_book_destroy(QofBook *book)