2001-08-07 23:29:04 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
2003-02-22 08:15:53 +00:00
|
|
|
#include <libguile.h>
|
2001-08-07 23:29:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2001-10-26 12:10:06 +00:00
|
|
|
#include "gnc-module.h"
|
2001-08-07 23:29:04 +00:00
|
|
|
#include "gnc-xml-helper.h"
|
|
|
|
|
#include "gnc-xml.h"
|
|
|
|
|
#include "gnc-engine-util.h"
|
|
|
|
|
#include "gnc-engine.h"
|
|
|
|
|
|
|
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
#include "test-stuff.h"
|
|
|
|
|
#include "test-engine-stuff.h"
|
|
|
|
|
#include "test-file-stuff.h"
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
#include "Account.h"
|
|
|
|
|
#include "Group.h"
|
|
|
|
|
#include "Scrub.h"
|
|
|
|
|
#include "gnc-book.h"
|
|
|
|
|
|
2001-11-24 12:10:42 +00:00
|
|
|
static GNCBook *book;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
static gchar*
|
|
|
|
|
node_and_account_equal(xmlNodePtr node, Account *act)
|
|
|
|
|
{
|
|
|
|
|
xmlNodePtr mark;
|
|
|
|
|
|
2001-10-11 11:35:50 +00:00
|
|
|
while (safe_strcmp (node->name, "text") == 0)
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
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(node->name, "gnc:account"))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("Name of toplevel node is bad");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(mark = node->xmlChildrenNode; mark; mark = mark->next)
|
|
|
|
|
{
|
2001-10-11 11:35:50 +00:00
|
|
|
if(safe_strcmp(mark->name, "text") == 0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(mark->name, "act:name") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_string(mark, xaccAccountGetName(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("names differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(mark->name, "act:id") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_guid(mark, xaccAccountGetGUID(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("ids differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(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(mark->name, "act:commodity") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_commodity(
|
2002-01-02 04:28:32 +00:00
|
|
|
mark, xaccAccountGetCommodity(act), xaccAccountGetBook(act)))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
return g_strdup("commodities differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(mark->name, "act:currency") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_commodity(
|
2003-03-31 04:12:31 +00:00
|
|
|
mark, DxaccAccountGetCurrency(act), xaccAccountGetBook(act)))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
return g_strdup("currencies differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(mark->name, "act:code") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_string(mark, xaccAccountGetCode(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("codes differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(mark->name, "act:description") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_string(
|
|
|
|
|
mark, xaccAccountGetDescription(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("descriptions differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(mark->name, "act:security") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_commodity(
|
2003-03-31 04:12:31 +00:00
|
|
|
mark, DxaccAccountGetSecurity(act), xaccAccountGetBook(act)))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
return g_strdup("securities differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(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(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(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(mark->name, "act:currency-scu") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_int(mark, DxaccAccountGetCurrencySCU(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("currency scus differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(safe_strcmp(mark->name, "act:security-scu") == 0)
|
|
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_int(mark, xaccAccountGetCommoditySCU(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("commodity scus differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return g_strdup_printf("unknown node in dom tree: %s", mark->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
delete_random_account(Account *act)
|
|
|
|
|
{
|
|
|
|
|
gnc_commodity_destroy(xaccAccountGetCommodity(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)
|
|
|
|
|
{
|
2001-10-12 10:50:35 +00:00
|
|
|
Account *account = data;
|
2001-08-07 23:29:04 +00:00
|
|
|
act_data *gdata = (act_data*)globaldata;
|
2001-10-12 10:50:35 +00:00
|
|
|
gnc_commodity * com;
|
|
|
|
|
gnc_commodity * new_com;
|
|
|
|
|
gnc_commodity_table *t;
|
|
|
|
|
|
|
|
|
|
com = xaccAccountGetCommodity (account);
|
|
|
|
|
|
2001-11-24 12:10:42 +00:00
|
|
|
t = gnc_book_get_commodity_table (book);
|
2001-10-12 10:50:35 +00:00
|
|
|
|
|
|
|
|
new_com = gnc_commodity_table_lookup (t,
|
|
|
|
|
gnc_commodity_get_namespace (com),
|
|
|
|
|
gnc_commodity_get_mnemonic (com));
|
|
|
|
|
|
|
|
|
|
if (new_com)
|
|
|
|
|
xaccAccountSetCommodity (account, new_com);
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
do_test_args(xaccAccountEqual((Account*)data, (Account*)(gdata->act),
|
|
|
|
|
TRUE),
|
|
|
|
|
"gnc_account_sixtp_parser_create",
|
2003-09-01 15:08:26 +00:00
|
|
|
__FILE__, __LINE__, "%d", gdata->value );
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_account(int i, Account *test_act)
|
|
|
|
|
{
|
|
|
|
|
xmlNodePtr test_node;
|
|
|
|
|
gchar *filename1;
|
|
|
|
|
gchar *compare_msg;
|
|
|
|
|
int fd;
|
|
|
|
|
|
2003-01-05 05:49:27 +00:00
|
|
|
test_node = gnc_account_dom_tree_create(test_act, FALSE);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
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;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
parser = gnc_account_sixtp_parser_create();
|
|
|
|
|
|
|
|
|
|
if(!gnc_xml_parse_file(parser, filename1, test_add_account,
|
2001-11-24 12:10:42 +00:00
|
|
|
&data, book))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
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(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
for(i = 0; i < 20; i++)
|
|
|
|
|
{
|
|
|
|
|
Account *ran_act;
|
|
|
|
|
|
2001-11-24 12:10:42 +00:00
|
|
|
ran_act = get_random_account(book);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
test_account(i, ran_act);
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
delete_random_account(ran_act);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
/* empty some things. */
|
|
|
|
|
Account *act;
|
|
|
|
|
|
2001-11-24 12:10:42 +00:00
|
|
|
act = get_random_account(book);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
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;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
if(!xaccAccountGetParent(act))
|
|
|
|
|
{
|
2003-06-12 02:53:21 +00:00
|
|
|
xaccGroupInsertAccount(xaccGetAccountGroup(book), act);
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
msg = node_and_account_equal((xmlNodePtr)global_data, act);
|
|
|
|
|
do_test_args(msg == NULL, "test_real_account",
|
|
|
|
|
__FILE__, __LINE__, msg);
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
g_free(msg);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2001-10-03 10:07:45 +00:00
|
|
|
static void
|
2003-02-22 08:15:53 +00:00
|
|
|
guile_main (void *closure, int argc, char **argv)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
2001-10-03 10:07:45 +00:00
|
|
|
gnc_module_system_init();
|
|
|
|
|
gnc_module_load("gnucash/engine", 0);
|
|
|
|
|
|
2001-11-24 12:10:42 +00:00
|
|
|
book = gnc_book_new ();
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
if(argc > 1)
|
|
|
|
|
{
|
|
|
|
|
test_files_in_dir(argc, argv, test_real_account,
|
|
|
|
|
gnc_account_sixtp_parser_create(),
|
2001-11-24 12:10:42 +00:00
|
|
|
"gnc:account", book);
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
test_generation();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_test_results();
|
|
|
|
|
exit(get_rv());
|
|
|
|
|
}
|
2001-10-03 10:07:45 +00:00
|
|
|
|
|
|
|
|
int
|
2003-02-22 08:15:53 +00:00
|
|
|
main (int argc, char ** argv)
|
2001-10-03 10:07:45 +00:00
|
|
|
{
|
2003-02-22 08:15:53 +00:00
|
|
|
scm_boot_guile (argc, argv, guile_main, NULL);
|
2001-10-03 10:07:45 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|