00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <glib.h>
00027 #include "qof.h"
00028 #include "test-stuff.h"
00029
00030 #define TEST_MODULE_NAME "object-test"
00031 #define TEST_MODULE_DESC "Test Object"
00032 #define DYNAMIC_MOD_NAME "dynamic_test"
00033 #define DYNAMIC_MOD_DESC "Full test of adding arbitrary objects"
00034
00035 static void obj_foreach (QofCollection *, QofEntityForeachCB, gpointer);
00036 static const gchar *printable (gpointer obj);
00037 static void test_printable (const gchar *name, gpointer obj);
00038 static void test_foreach (QofBook *, const gchar *);
00039
00040 static QofObject bus_obj = {
00041 .interface_version = QOF_OBJECT_VERSION,
00042 .e_type = TEST_MODULE_NAME,
00043 .type_label = TEST_MODULE_DESC,
00044 .create = NULL,
00045 .book_begin = NULL,
00046 .book_end = NULL,
00047 .is_dirty = NULL,
00048 .mark_clean = NULL,
00049 .foreach = obj_foreach,
00050 .printable = printable,
00051 .version_cmp = NULL,
00052 };
00053
00054 static G_GNUC_UNUSED const gchar *
00055 test_dyn_printable (gpointer obj)
00056 {
00057
00058 return "test";
00059 }
00060
00061 static QofObject *
00062 dyn_create (QofBook * book)
00063 {
00064 QofInstance * inst;
00065 QofCollection *coll;
00066 GList *all;
00067
00068 g_return_val_if_fail (book, NULL);
00069 inst = g_new0 (QofInstance, 1);
00070 qof_instance_init (inst, "dynamic_test", book);
00071 coll = qof_book_get_collection (book, "dynamic_test");
00072 all = qof_collection_get_data (coll);
00073 all = g_list_prepend (all, inst);
00074 qof_collection_set_data (coll, all);
00075 return (QofObject*)inst;
00076 }
00077
00078
00079 static QofParam * p_list;
00080
00081 static gchar *
00082 dynamic_get_string (QofEntity * ent)
00083 {
00084
00085 do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for string");
00086 return "test_string";
00087 }
00088
00089 static gint
00090 dynamic_get_int (QofEntity * ent)
00091 {
00092
00093 do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for int");
00094 return 1;
00095 }
00096
00097 static gboolean
00098 dynamic_get_boolean (QofEntity * ent)
00099 {
00100
00101 do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for int");
00102 return TRUE;
00103 }
00104
00105 static QofParam *
00106 add_boolean_param (void)
00107 {
00108 QofParam * p;
00109
00110 p = g_new0 (QofParam, 1);
00111 p->param_name = "test_boolean";
00112 p->param_type = QOF_TYPE_BOOLEAN;
00113 p->param_getfcn = (QofAccessFunc)dynamic_get_boolean;
00114 return p;
00115 }
00116
00117 static gboolean
00118 test_boolean_param (QofEntity * ent, const QofParam * p)
00119 {
00120 gboolean b, (*boolean_getter) (QofEntity *, QofParam *);
00121
00122 do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for bool");
00123 boolean_getter = (gboolean (*)(QofEntity *, QofParam *)) p->param_getfcn;
00124 b = boolean_getter (ent, (QofParam*)p);
00125 return b;
00126 }
00127
00128 static const QofParam *
00129 test_class_register (void)
00130 {
00131 QofParam * p;
00132
00133
00134
00135 static QofParam list[30];
00136
00137 p = g_new0 (QofParam, 1);
00138 p->param_name = "test_string";
00139 p->param_type = QOF_TYPE_STRING;
00140 p->param_getfcn = (QofAccessFunc)dynamic_get_string;
00141 list[0] = *p;
00142 p = g_new0 (QofParam, 1);
00143 p->param_name = "test_int";
00144 p->param_type = QOF_TYPE_INT32;
00145 p->param_getfcn = (QofAccessFunc)dynamic_get_int;
00146 list[1] = *p;
00147 list[2] = *add_boolean_param ();
00148
00149 p = g_new0 (QofParam, 1);
00150 list[3] = *p;
00151 p_list = list;
00152 return p_list;
00153 }
00154
00155 static void
00156 test_dynamic_object (void)
00157 {
00158 QofObject * dynamic;
00159 QofInstance * d_ent;
00160 const QofObject * check;
00161 const gchar * message;
00162 gchar * s;
00163 gint t, (*int32_getter) (QofEntity *, QofParam *);
00164 const QofParam * p;
00165 QofBook *book = qof_book_new ();
00166
00167 do_test ((NULL != book), "book null");
00168 dynamic = g_new0(QofObject,1);
00169 dynamic->interface_version = QOF_OBJECT_VERSION,
00170 dynamic->e_type = DYNAMIC_MOD_NAME;
00171 dynamic->type_label = DYNAMIC_MOD_DESC;
00172 dynamic->foreach = obj_foreach;
00173 dynamic->create = (gpointer) dyn_create;
00174 dynamic->printable = test_dyn_printable;
00175 do_test (qof_object_register (dynamic), "dynamic object registration");
00176 check = qof_object_lookup (DYNAMIC_MOD_NAME);
00177 do_test (check != NULL, "dynamic object lookup");
00178 message = qof_object_get_type_label (DYNAMIC_MOD_NAME);
00179 do_test (!safe_strcmp(message, "Full test of adding arbitrary objects"),
00180 "dynamic object type_label");
00181 d_ent = qof_object_new_instance (DYNAMIC_MOD_NAME, book);
00182 do_test (check->printable != NULL, "dynamic printable support");
00183 message = qof_object_printable (DYNAMIC_MOD_NAME, dynamic);
00184 do_test (message != NULL, "dynamic object printable");
00185 message = dynamic->printable(d_ent);
00186 do_test (message != NULL, "dynamic direct printable");
00187 qof_class_register(DYNAMIC_MOD_NAME, NULL, test_class_register());
00188 do_test (qof_class_is_registered (DYNAMIC_MOD_NAME), "class register");
00189 s = NULL;
00190 p = qof_class_get_parameter (DYNAMIC_MOD_NAME, "test_string");
00191 s = p->param_getfcn (d_ent, p);
00192 do_test (!safe_strcmp(s, "test_string"), "get string from dynamic object");
00193 t = 0;
00194 p = qof_class_get_parameter (DYNAMIC_MOD_NAME, "test_int");
00195 int32_getter = (gint32 (*)(QofEntity *, QofParam *)) p->param_getfcn;
00196 t = int32_getter ((QofEntity*)d_ent, (QofParam*)p);
00197 do_test (t == 1, "get int from dynamic object");
00198 p = qof_class_get_parameter (DYNAMIC_MOD_NAME, "test_boolean");
00199 do_test (test_boolean_param((QofEntity*)d_ent, p),
00200 "get boolean from dynamic object");
00201 }
00202
00203 static void
00204 test_object (void)
00205 {
00206 QofBook *book = qof_book_new ();
00207
00208 do_test ((NULL != book), "book null");
00209
00210
00211 {
00212 do_test (!qof_object_register (NULL), "register NULL");
00213 do_test (qof_object_register (&bus_obj), "register test object");
00214 do_test (!qof_object_register (&bus_obj),
00215 "register test object again");
00216 do_test (qof_object_lookup (TEST_MODULE_NAME) == &bus_obj,
00217 "lookup our installed object");
00218 do_test (qof_object_lookup ("snm98sn snml say dyikh9y9ha") == NULL,
00219 "lookup non-existant object object");
00220
00221 do_test (!safe_strcmp (qof_object_get_type_label (TEST_MODULE_NAME),
00222 (TEST_MODULE_DESC)),
00223 "test description return");
00224 }
00225
00226 test_foreach (book, TEST_MODULE_NAME);
00227 test_printable (TEST_MODULE_NAME, (gpointer) 1);
00228 }
00229
00230 static void
00231 obj_foreach (QofCollection * col,
00232 QofEntityForeachCB cb __attribute__ ((unused)), gpointer u_d)
00233 {
00234 int *foo = u_d;
00235
00236 do_test (col != NULL, "foreach: NULL collection");
00237 success ("called foreach callback");
00238
00239 *foo = 1;
00240 }
00241
00242 static void
00243 foreachCB (QofEntity * ent __attribute__ ((unused)),
00244 gpointer u_d __attribute__ ((unused)))
00245 {
00246 do_test (FALSE, "FAIL");
00247 }
00248
00249 static const char *
00250 printable (gpointer obj)
00251 {
00252 do_test (obj != NULL, "printable: object is NULL");
00253 success ("called printable callback");
00254 return ((const char *) obj);
00255 }
00256
00257 static void
00258 test_foreach (QofBook * book, const char *name)
00259 {
00260 int res = 0;
00261
00262 qof_object_foreach (NULL, NULL, NULL, &res);
00263 do_test (res == 0, "object: Foreach: NULL, NULL, NULL");
00264 qof_object_foreach (NULL, NULL, foreachCB, &res);
00265 do_test (res == 0, "object: Foreach: NULL, NULL, foreachCB");
00266
00267 qof_object_foreach (NULL, book, NULL, &res);
00268 do_test (res == 0, "object: Foreach: NULL, book, NULL");
00269 qof_object_foreach (NULL, book, foreachCB, &res);
00270 do_test (res == 0, "object: Foreach: NULL, book, foreachCB");
00271
00272 qof_object_foreach (name, NULL, NULL, &res);
00273 do_test (res == 0, "object: Foreach: name, NULL, NULL");
00274 qof_object_foreach (name, NULL, foreachCB, &res);
00275 do_test (res == 0, "object: Foreach: name, NULL, foreachCB");
00276
00277 qof_object_foreach (name, book, NULL, &res);
00278 do_test (res != 0, "object: Foreach: name, book, NULL");
00279
00280 res = 0;
00281 qof_object_foreach (name, book, foreachCB, &res);
00282 do_test (res != 0, "object: Foreach: name, book, foreachCB");
00283 }
00284
00285 static void
00286 test_printable (const char *name, gpointer obj)
00287 {
00288 const char *res;
00289
00290 do_test (qof_object_printable (NULL, NULL) == NULL,
00291 "object: Printable: NULL, NULL");
00292 do_test (qof_object_printable (NULL, obj) == NULL,
00293 "object: Printable: NULL, object");
00294 do_test (qof_object_printable (name, NULL) == NULL,
00295 "object: Printable: mod_name, NULL");
00296 res = qof_object_printable (name, obj);
00297 do_test (res != NULL, "object: Printable: mod_name, object");
00298 }
00299
00300 int
00301 main (void)
00302 {
00303 qof_init ();
00304 test_object ();
00305 test_dynamic_object ();
00306 print_test_results ();
00307 qof_close ();
00308 return get_rv ();
00309 }