GnuCash 2.4.99
test-xml-commodity.c
00001 #include "config.h"
00002 
00003 #include <glib.h>
00004 #include <glib/gstdio.h>
00005 #include <libguile.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 
00009 #include "gnc-module.h"
00010 #include "gnc-xml-helper.h"
00011 #include "gnc-xml.h"
00012 #include "qof.h"
00013 
00014 #include "sixtp-parsers.h"
00015 #include "sixtp-utils.h"
00016 
00017 #include "sixtp-dom-parsers.h"
00018 
00019 #include "io-gncxml-gen.h"
00020 
00021 #include "test-stuff.h"
00022 #include "test-engine-stuff.h"
00023 #include "test-file-stuff.h"
00024 
00025 #include "Account.h"
00026 
00027 #define GNC_V2_STRING "gnc-v2"
00028 const gchar *gnc_v2_xml_version_string = GNC_V2_STRING;
00029 
00030 static QofBook *book;
00031 
00032 static gchar*
00033 node_and_commodity_equal(xmlNodePtr node, const gnc_commodity *com)
00034 {
00035     xmlNodePtr mark;
00036 
00037     while (safe_strcmp ((char*)node->name, "text") == 0)
00038         node = node->next;
00039 
00040     if (!check_dom_tree_version(node, "2.0.0"))
00041     {
00042         return "version wrong.  Not 2.0.0 or not there";
00043     }
00044 
00045     if (!node->name || safe_strcmp((char*)node->name, "gnc:commodity"))
00046     {
00047         return "Name of toplevel node is bad";
00048     }
00049 
00050     for (mark = node->xmlChildrenNode; mark; mark = mark->next)
00051     {
00052         if (safe_strcmp((char*)mark->name, "text") == 0)
00053         {
00054         }
00055         else if (safe_strcmp((char*)mark->name, "cmdty:space") == 0)
00056         {
00057             if (!equals_node_val_vs_string(
00058                         mark, gnc_commodity_get_namespace_compat(com)))
00059             {
00060                 return "namespaces differ";
00061             }
00062         }
00063         else if (safe_strcmp((char*)mark->name, "cmdty:id") == 0)
00064         {
00065             if (!equals_node_val_vs_string(
00066                         mark, gnc_commodity_get_mnemonic(com)))
00067             {
00068                 return "mnemonic differ";
00069             }
00070         }
00071         else if (safe_strcmp((char*)mark->name, "cmdty:name") == 0)
00072         {
00073             if (!equals_node_val_vs_string(
00074                         mark, gnc_commodity_get_fullname(com)))
00075             {
00076                 return "names differ";
00077             }
00078         }
00079         else if (safe_strcmp((char*)mark->name, "cmdty:xcode") == 0)
00080         {
00081             if (!equals_node_val_vs_string(
00082                         mark, gnc_commodity_get_cusip(com)))
00083             {
00084                 return "exchange codes differ";
00085             }
00086         }
00087         else if (safe_strcmp((char*)mark->name, "cmdty:fraction") == 0)
00088         {
00089             gchar *txt;
00090             gint64 type;
00091 
00092             txt = dom_tree_to_text(mark);
00093 
00094             if (!txt)
00095             {
00096                 return "couldn't get fraction string";
00097             }
00098 
00099             else if (!string_to_gint64(txt, &type))
00100             {
00101                 g_free(txt);
00102                 return "couldn't convert fraction string to int";
00103             }
00104             else if (type != gnc_commodity_get_fraction(com))
00105             {
00106                 g_free(txt);
00107                 return "fractions differ";
00108             }
00109             else
00110             {
00111                 g_free(txt);
00112             }
00113         }
00114         /* Legitimate tags which we don't yet have tests */
00115         else if (safe_strcmp((char*)mark->name, "cmdty:get_quotes") == 0 ||
00116                  safe_strcmp((char*)mark->name, "cmdty:quote_source") == 0 ||
00117                  safe_strcmp((char*)mark->name, "cmdty:quote_tz") == 0)
00118         {
00119             continue;
00120         }
00121         else
00122         {
00123             return "unknown node";
00124         }
00125     }
00126 
00127     return NULL;
00128 }
00129 
00130 struct com_data_struct
00131 {
00132     gnc_commodity *com;
00133     int value;
00134 };
00135 typedef struct com_data_struct com_data;
00136 
00137 static gboolean
00138 test_add_commodity(const char *tag, gpointer globaldata, gpointer data)
00139 {
00140     com_data *gdata = (com_data*)globaldata;
00141 
00142     do_test_args(gnc_commodity_equiv((gnc_commodity*)data, gdata->com),
00143                  "gnc_commodity_sixtp_parser_create",
00144                  __FILE__, __LINE__, "%d", gdata->value );
00145     gnc_commodity_destroy((gnc_commodity*)data);
00146 
00147     return TRUE;
00148 
00149 }
00150 
00151 static void
00152 test_generation(void)
00153 {
00154     int i;
00155     for (i = 0; i < 20; i++)
00156     {
00157         gnc_commodity *ran_com;
00158         xmlNodePtr test_node;
00159         gchar *filename1;
00160         int fd;
00161         gchar *compare_msg;
00162 
00163         ran_com = get_random_commodity(book);
00164 
00165         test_node = gnc_commodity_dom_tree_create(ran_com);
00166         if (!test_node)
00167         {
00168             failure_args("commodity_xml", __FILE__, __LINE__,
00169                          "gnc_commodity_dom_tree_create returned NULL");
00170             gnc_commodity_destroy(ran_com);
00171             continue;
00172         }
00173 
00174         if ((compare_msg = node_and_commodity_equal(test_node, ran_com)) !=
00175                 NULL)
00176         {
00177             failure_args("commodity_xml", __FILE__, __LINE__,
00178                          "node and commodity were not equal: %s", compare_msg);
00179             xmlElemDump(stdout, NULL, test_node);
00180             xmlFreeNode(test_node);
00181             gnc_commodity_destroy(ran_com);
00182             continue;
00183         }
00184         else
00185         {
00186             success_args("commodity_xml", __FILE__, __LINE__, "%d", i);
00187         }
00188 
00189         filename1 = g_strdup_printf("test_file_XXXXXX");
00190 
00191         fd = g_mkstemp(filename1);
00192 
00193         write_dom_node_to_file(test_node, fd);
00194 
00195         close(fd);
00196 
00197         {
00198             sixtp *parser;
00199             com_data data;
00200 
00201             data.com = ran_com;
00202             data.value = i;
00203 
00204             parser = gnc_commodity_sixtp_parser_create();
00205 
00206             if (!gnc_xml_parse_file(parser, filename1, test_add_commodity,
00207                                     (gpointer)&data, book))
00208             {
00209                 failure_args("gnc_xml_parse_file returned FALSE",
00210                              __FILE__, __LINE__, "%d", i);
00211             }
00212 
00213             /* no handling of circular data structures.  We'll do that later */
00214             /* sixtp_destroy(parser); */
00215         }
00216 
00217         g_unlink(filename1);
00218         g_free(filename1);
00219         gnc_commodity_destroy(ran_com);
00220         xmlFreeNode(test_node);
00221     }
00222 }
00223 
00224 static gboolean
00225 test_real_commodity(const char *tag, gpointer globaldata, gpointer data)
00226 {
00227     const char *msg = node_and_commodity_equal((xmlNodePtr)globaldata,
00228                       (gnc_commodity*)data);
00229     do_test_args(msg == NULL, "test_real_commodity",
00230                  __FILE__, __LINE__, msg);
00231     gnc_commodity_destroy((gnc_commodity*)data);
00232     return TRUE;
00233 }
00234 
00235 int
00236 main(int argc, char **argv)
00237 {
00238     g_setenv ("GNC_UNINSTALLED", "1", TRUE);
00239     gnc_engine_init(argc, argv);
00240 
00241     book = qof_book_new ();
00242 
00243     if (argc > 1)
00244     {
00245         test_files_in_dir(argc, argv, test_real_commodity,
00246                           gnc_commodity_sixtp_parser_create(),
00247                           "gnc:commodity", book);
00248     }
00249     else
00250     {
00251         test_generation();
00252     }
00253 
00254     print_test_results();
00255     exit(get_rv());
00256 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines