GnuCash 2.4.99
test-object.c
00001 /***************************************************************************
00002  *            test-object.c
00003  *
00004  *  Tue Sep 27 19:37:28 2005
00005  *  Copyright  2005  GnuCash team
00006  ****************************************************************************/
00007 /*
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00021  *  02110-1301, USA.
00022  */
00023 /*
00024 * Lightly test the QofObject infrastructure.
00025 */
00026 #include "config.h"
00027 #include <glib.h>
00028 #include <glib/gi18n.h>
00029 #include "qof.h"
00030 #include "cashobjects.h"
00031 #include "test-stuff.h"
00032 
00033 #define TEST_MODULE_NAME "object-test"
00034 #define TEST_MODULE_DESC "Test Object"
00035 
00036 static void obj_foreach (const QofCollection *, QofInstanceForeachCB, gpointer);
00037 static const char * printable (gpointer obj);
00038 static void test_printable (const char *name, gpointer obj);
00039 static void test_foreach (QofBook *, const char *);
00040 
00041 static QofObject bus_obj =
00042 {
00043 interface_version:
00044     QOF_OBJECT_VERSION,
00045 e_type:
00046     TEST_MODULE_NAME,
00047 type_label:
00048     TEST_MODULE_DESC,
00049 create:
00050     NULL,
00051 book_begin:
00052     NULL,
00053 book_end:
00054     NULL,
00055 is_dirty:
00056     NULL,
00057 mark_clean:
00058     NULL,
00059 foreach:
00060     obj_foreach,
00061 printable:
00062     printable,
00063 version_cmp:
00064     NULL,
00065 };
00066 
00067 static void
00068 test_object (void)
00069 {
00070     QofBook *book = qof_book_new();
00071 
00072     do_test ((NULL != book), "book null");
00073 
00074     /* Test the global registration and lookup functions */
00075     {
00076         do_test (!qof_object_register (NULL), "register NULL");
00077         do_test (qof_object_register (&bus_obj), "register test object");
00078         do_test (!qof_object_register (&bus_obj), "register test object again");
00079         do_test (qof_object_lookup (TEST_MODULE_NAME) == &bus_obj,
00080                  "lookup our installed object");
00081         do_test (qof_object_lookup ("snm98sn snml say  dyikh9y9ha") == NULL,
00082                  "lookup non-existant object object");
00083 
00084         do_test (!safe_strcmp (qof_object_get_type_label (TEST_MODULE_NAME),
00085                                _(TEST_MODULE_DESC)),
00086                  "test description return");
00087     }
00088 
00089     test_foreach (book, TEST_MODULE_NAME);
00090     test_printable (TEST_MODULE_NAME, (gpointer)1);
00091 }
00092 
00093 static void
00094 obj_foreach (const QofCollection *col, QofInstanceForeachCB cb, gpointer u_d)
00095 {
00096     int *foo = u_d;
00097 
00098     do_test (col != NULL, "foreach: NULL collection");
00099     success ("called foreach callback");
00100 
00101     *foo = 1;
00102 }
00103 
00104 static void foreachCB (QofInstance *ent, gpointer u_d)
00105 {
00106     do_test (FALSE, "FAIL");
00107 }
00108 
00109 static const char *
00110 printable (gpointer obj)
00111 {
00112     do_test (obj != NULL, "printable: object is NULL");
00113     success ("called printable callback");
00114     return ((const char *)obj);
00115 }
00116 
00117 static void
00118 test_foreach (QofBook *book, const char *name)
00119 {
00120     int res = 0;
00121 
00122     qof_object_foreach (NULL, NULL, NULL, &res);
00123     do_test (res == 0, "object: Foreach: NULL, NULL, NULL");
00124     qof_object_foreach (NULL, NULL, foreachCB, &res);
00125     do_test (res == 0, "object: Foreach: NULL, NULL, foreachCB");
00126 
00127     qof_object_foreach (NULL, book, NULL, &res);
00128     do_test (res == 0, "object: Foreach: NULL, book, NULL");
00129     qof_object_foreach (NULL, book, foreachCB, &res);
00130     do_test (res == 0, "object: Foreach: NULL, book, foreachCB");
00131 
00132     qof_object_foreach (name, NULL, NULL, &res);
00133     do_test (res == 0, "object: Foreach: name, NULL, NULL");
00134     qof_object_foreach (name, NULL, foreachCB, &res);
00135     do_test (res == 0, "object: Foreach: name, NULL, foreachCB");
00136 
00137     qof_object_foreach (name, book, NULL, &res);
00138     do_test (res != 0, "object: Foreach: name, book, NULL");
00139 
00140     res = 0;
00141     qof_object_foreach (name, book, foreachCB, &res);
00142     do_test (res != 0, "object: Foreach: name, book, foreachCB");
00143 }
00144 
00145 static void
00146 test_printable (const char *name, gpointer obj)
00147 {
00148     const char *res;
00149 
00150     do_test (qof_object_printable (NULL, NULL) == NULL,
00151              "object: Printable: NULL, NULL");
00152     do_test (qof_object_printable (NULL, obj) == NULL,
00153              "object: Printable: NULL, object");
00154     do_test (qof_object_printable (name, NULL) == NULL,
00155              "object: Printable: mod_name, NULL");
00156     res = qof_object_printable (name, obj);
00157     do_test (res != NULL, "object: Printable: mod_name, object");
00158 }
00159 
00160 int
00161 main (int argc, char **argv)
00162 {
00163     qof_init();
00164     if (cashobjects_register())
00165     {
00166         test_object();
00167         print_test_results();
00168     }
00169     qof_close();
00170     return get_rv();
00171 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines