GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-object.c
1 /***************************************************************************
2  * test-object.c
3  *
4  * Tue Sep 27 19:37:28 2005
5  * Copyright 2005 GnuCash team
6  ****************************************************************************/
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301, USA.
22  */
23 /*
24 * Lightly test the QofObject infrastructure.
25 */
26 #include "config.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include "qof.h"
30 #include "cashobjects.h"
31 #include "test-stuff.h"
32 
33 #define TEST_MODULE_NAME "object-test"
34 #define TEST_MODULE_DESC "Test Object"
35 
36 static void obj_foreach (const QofCollection *, QofInstanceForeachCB, gpointer);
37 static const char * printable (gpointer obj);
38 static void test_printable (const char *name, gpointer obj);
39 static void test_foreach (QofBook *, const char *);
40 
41 static QofObject bus_obj =
42 {
44  TEST_MODULE_NAME,
45  TEST_MODULE_DESC,
46  NULL,
47  NULL,
48  NULL,
49  NULL,
50  NULL,
51  obj_foreach,
52  printable,
53  NULL,
54 };
55 
56 static void
57 test_object (void)
58 {
59  QofBook *book = qof_book_new();
60 
61  do_test ((NULL != book), "book null");
62 
63  /* Test the global registration and lookup functions */
64  {
65  do_test (!qof_object_register (NULL), "register NULL");
66  do_test (qof_object_register (&bus_obj), "register test object");
67  do_test (!qof_object_register (&bus_obj), "register test object again");
68  do_test (qof_object_lookup (TEST_MODULE_NAME) == &bus_obj,
69  "lookup our installed object");
70  do_test (qof_object_lookup ("snm98sn snml say dyikh9y9ha") == NULL,
71  "lookup non-existant object object");
72 
73  do_test (!g_strcmp0 (qof_object_get_type_label (TEST_MODULE_NAME),
74  _(TEST_MODULE_DESC)),
75  "test description return");
76  }
77 
78  test_foreach (book, TEST_MODULE_NAME);
79  test_printable (TEST_MODULE_NAME, (gpointer)1);
80 }
81 
82 static void
83 obj_foreach (const QofCollection *col, QofInstanceForeachCB cb, gpointer u_d)
84 {
85  int *foo = u_d;
86 
87  do_test (col != NULL, "foreach: NULL collection");
88  success ("called foreach callback");
89 
90  *foo = 1;
91 }
92 
93 static void foreachCB (QofInstance *ent, gpointer u_d)
94 {
95  do_test (FALSE, "FAIL");
96 }
97 
98 static const char *
99 printable (gpointer obj)
100 {
101  do_test (obj != NULL, "printable: object is NULL");
102  success ("called printable callback");
103  return ((const char *)obj);
104 }
105 
106 static void
107 test_foreach (QofBook *book, const char *name)
108 {
109  int res = 0;
110 
111  qof_object_foreach (NULL, NULL, NULL, &res);
112  do_test (res == 0, "object: Foreach: NULL, NULL, NULL");
113  qof_object_foreach (NULL, NULL, foreachCB, &res);
114  do_test (res == 0, "object: Foreach: NULL, NULL, foreachCB");
115 
116  qof_object_foreach (NULL, book, NULL, &res);
117  do_test (res == 0, "object: Foreach: NULL, book, NULL");
118  qof_object_foreach (NULL, book, foreachCB, &res);
119  do_test (res == 0, "object: Foreach: NULL, book, foreachCB");
120 
121  qof_object_foreach (name, NULL, NULL, &res);
122  do_test (res == 0, "object: Foreach: name, NULL, NULL");
123  qof_object_foreach (name, NULL, foreachCB, &res);
124  do_test (res == 0, "object: Foreach: name, NULL, foreachCB");
125 
126  qof_object_foreach (name, book, NULL, &res);
127  do_test (res != 0, "object: Foreach: name, book, NULL");
128 
129  res = 0;
130  qof_object_foreach (name, book, foreachCB, &res);
131  do_test (res != 0, "object: Foreach: name, book, foreachCB");
132 }
133 
134 static void
135 test_printable (const char *name, gpointer obj)
136 {
137  const char *res;
138 
139  do_test (qof_object_printable (NULL, NULL) == NULL,
140  "object: Printable: NULL, NULL");
141  do_test (qof_object_printable (NULL, obj) == NULL,
142  "object: Printable: NULL, object");
143  do_test (qof_object_printable (name, NULL) == NULL,
144  "object: Printable: mod_name, NULL");
145  res = qof_object_printable (name, obj);
146  do_test (res != NULL, "object: Printable: mod_name, object");
147 }
148 
149 int
150 main (int argc, char **argv)
151 {
152  qof_init();
153  if (cashobjects_register())
154  {
155  test_object();
156  print_test_results();
157  }
158  qof_close();
159  return get_rv();
160 }
const char * qof_object_get_type_label(QofIdTypeConst type_name)
QofBook * qof_book_new(void)
#define QOF_OBJECT_VERSION
Definition: qofobject.h:64
const QofObject * qof_object_lookup(QofIdTypeConst type_name)
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
void(* QofInstanceForeachCB)(QofInstance *, gpointer user_data)
Definition: qofid.h:186
void qof_close(void)
Safely close down the Query Object Framework.
const char * qof_object_printable(QofIdTypeConst type_name, gpointer instance)
gboolean qof_object_register(const QofObject *object)
void qof_init(void)
Initialise the Query Object Framework.