-
Notifications
You must be signed in to change notification settings - Fork 761
/
test-xml-account.c
374 lines (312 loc) · 9.62 KB
/
test-xml-account.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/***************************************************************************
* test-xml-account.c
*
* Sun Oct 9 15:37:26 2005
* Copyright 2005 Neil Williams
* linux@codehelp.co.uk
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "config.h"
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "gnc-xml-helper.h"
#include "gnc-xml.h"
#include "gnc-engine.h"
#include "cashobjects.h"
#include "sixtp-parsers.h"
#include "sixtp-dom-parsers.h"
#include "test-stuff.h"
#include "test-engine-stuff.h"
#include "test-file-stuff.h"
#include "Account.h"
#include "Group.h"
#include "Scrub.h"
static QofBook *sixbook;
static gchar*
node_and_account_equal(xmlNodePtr node, Account *act)
{
xmlNodePtr mark;
while (safe_strcmp ((char*)node->name, "text") == 0)
{
node = node->next;
}
if(!check_dom_tree_version(node, "2.0.0"))
{
return g_strdup("version wrong. Not 2.0.0 or not there");
}
if(!node->name || safe_strcmp((char*)node->name, "gnc:account"))
{
return g_strdup("Name of toplevel node is bad");
}
for(mark = node->xmlChildrenNode; mark; mark = mark->next)
{
if(safe_strcmp((char*)mark->name, "text") == 0)
{
}
else if(safe_strcmp((char*)mark->name, "act:name") == 0)
{
if(!equals_node_val_vs_string(mark, xaccAccountGetName(act)))
{
return g_strdup("names differ");
}
}
else if(safe_strcmp((char*)mark->name, "act:id") == 0)
{
if(!equals_node_val_vs_guid(mark, xaccAccountGetGUID(act)))
{
return g_strdup("ids differ");
}
}
else if(safe_strcmp((char*)mark->name, "act:type") == 0)
{
gchar *txt;
int type;
txt = dom_tree_to_text(mark);
if(!txt)
{
return g_strdup("couldn't get type string");
}
else if(!xaccAccountStringToType(txt, &type))
{
g_free(txt);
return g_strdup("couldn't convert type string to int");
}
else if(type != xaccAccountGetType(act))
{
g_free(txt);
return g_strdup("types differ");
}
else
{
g_free(txt);
}
}
else if(safe_strcmp((char*)mark->name, "act:commodity") == 0)
{
if(!equals_node_val_vs_commodity(
mark, xaccAccountGetCommodity(act), xaccAccountGetBook(act)))
{
return g_strdup("commodities differ");
}
}
else if(safe_strcmp((char*)mark->name, "act:code") == 0)
{
if(!equals_node_val_vs_string(mark, xaccAccountGetCode(act)))
{
return g_strdup("codes differ");
}
}
else if(safe_strcmp((char*)mark->name, "act:description") == 0)
{
if(!equals_node_val_vs_string(
mark, xaccAccountGetDescription(act)))
{
return g_strdup("descriptions differ");
}
}
else if(safe_strcmp((char*)mark->name, "act:slots") == 0)
{
/* xaccAccountDeleteOldData (act); */
if(!equals_node_val_vs_kvp_frame(mark, xaccAccountGetSlots(act)))
{
return g_strdup("slots differ");
}
}
else if(safe_strcmp((char*)mark->name, "act:parent") == 0)
{
if(!equals_node_val_vs_guid(
mark, xaccAccountGetGUID(xaccGroupGetParentAccount(
xaccAccountGetParent(act)))))
{
return g_strdup("parent ids differ");
}
}
else if(safe_strcmp((char*)mark->name, "act:commodity-scu") == 0)
{
if(!equals_node_val_vs_int(mark, xaccAccountGetCommoditySCU(act)))
{
return g_strdup("commodity scus differ");
}
}
else if (safe_strcmp((char*)mark->name, "act:security") == 0)
{
return NULL; // This tag is ignored.
}
else
{
return g_strdup_printf("unknown node in dom tree: %s", mark->name);
}
}
return NULL;
}
static void
delete_random_account(Account *act)
{
xaccAccountBeginEdit(act);
xaccAccountDestroy(act);
}
struct act_data_struct
{
Account *act;
int value;
};
typedef struct act_data_struct act_data;
static gboolean
test_add_account(const char *tag, gpointer globaldata, gpointer data)
{
Account *account = data;
act_data *gdata = (act_data*)globaldata;
gnc_commodity * com;
gnc_commodity * new_com;
gnc_commodity_table *t;
com = xaccAccountGetCommodity (account);
t = gnc_book_get_commodity_table (sixbook);
new_com = gnc_commodity_table_lookup (t,
gnc_commodity_get_namespace (com),
gnc_commodity_get_mnemonic (com));
if (new_com) { xaccAccountSetCommodity (account, new_com); }
do_test_args(xaccAccountEqual((Account*)account, (Account*)(gdata->act),
TRUE),
"gnc_account_sixtp_parser_create",
__FILE__, __LINE__, "%d", gdata->value );
return TRUE;
}
static void
test_account(int i, Account *test_act)
{
xmlNodePtr test_node;
gchar *filename1;
gchar *compare_msg;
int fd;
test_node = gnc_account_dom_tree_create(test_act, FALSE);
if(!test_node)
{
failure_args("account_xml", __FILE__, __LINE__,
"gnc_account_dom_tree_create returned NULL");
return;
}
if((compare_msg = node_and_account_equal(test_node, test_act)) != NULL)
{
failure_args("account_xml", __FILE__, __LINE__,
"node and account were not equal: %s", compare_msg);
xmlElemDump(stdout, NULL, test_node);
fprintf(stdout, "\n");
xmlFreeNode(test_node);
g_free(compare_msg);
return;
}
else
{
success("account_xml");
}
filename1 = g_strdup_printf("test_file_XXXXXX");
fd = mkstemp(filename1);
write_dom_node_to_file(test_node, fd);
close(fd);
{
sixtp *parser;
act_data data;
data.act = test_act;
data.value = i;
parser = gnc_account_sixtp_parser_create();
if(!gnc_xml_parse_file(parser, filename1, test_add_account,
&data, sixbook))
{
failure_args("gnc_xml_parse_file returned FALSE",
__FILE__, __LINE__, "%d", i);
}
/* no handling of circular data structures. We'll do that later */
/* sixtp_destroy(parser); */
}
unlink(filename1);
g_free(filename1);
xmlFreeNode(test_node);
}
static void
test_generation()
{
int i;
for(i = 0; i < 20; i++)
{
Account *ran_act;
ran_act = get_random_account(sixbook);
test_account(i, ran_act);
delete_random_account(ran_act);
}
{
/* empty some things. */
Account *act;
act = get_random_account(sixbook);
xaccAccountSetCode(act, "");
xaccAccountSetDescription(act, "");
xaccAccountSetCommodity(act, NULL);
test_account(-1, act);
delete_random_account(act);
}
/* { */
/* Account *act1; */
/* Account *act2; */
/* act1 = get_random_account(); */
/* act2 = get_random_account(); */
/* xaccAccountInsertSubAccount(act1, act2); */
/* test_account(-1, act2); */
/* test_account(-1, act1); */
/* delete_random_account(act2); */
/* delete_random_account(act1); */
/* } */
}
static gboolean
test_real_account(const char *tag, gpointer global_data, gpointer data)
{
char *msg;
Account *act = (Account*)data;
if(!xaccAccountGetParent(act))
{
xaccGroupInsertAccount(xaccGetAccountGroup(sixbook), act);
}
msg = node_and_account_equal((xmlNodePtr)global_data, act);
do_test_args(msg == NULL, "test_real_account",
__FILE__, __LINE__, msg);
g_free(msg);
return TRUE;
}
int
main (int argc, char ** argv)
{
QofSession *session;
qof_init();
cashobjects_register();
session = qof_session_new();
sixbook = qof_session_get_book (session);
if(argc > 1)
{
test_files_in_dir(argc, argv, test_real_account,
gnc_account_sixtp_parser_create(),
"gnc:account", sixbook);
}
else
{
test_generation();
}
qof_session_destroy(session);
print_test_results();
qof_close();
return 0;
}