GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-xml-commodity.c
1 #include "config.h"
2 
3 #include <glib.h>
4 #include <glib/gstdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 
8 #include "gnc-module.h"
9 #include "gnc-xml-helper.h"
10 #include "gnc-xml.h"
11 #include "qof.h"
12 
13 #include "sixtp-parsers.h"
14 #include "sixtp-utils.h"
15 
16 #include "sixtp-dom-parsers.h"
17 
18 #include "io-gncxml-gen.h"
19 
20 #include "test-stuff.h"
21 #include "test-engine-stuff.h"
22 #include "test-file-stuff.h"
23 
24 #include "Account.h"
25 
26 static QofBook *book;
27 
28 static gchar*
29 node_and_commodity_equal(xmlNodePtr node, const gnc_commodity *com)
30 {
31  xmlNodePtr mark;
32 
33  while (g_strcmp0 ((char*)node->name, "text") == 0)
34  node = node->next;
35 
36  if (!check_dom_tree_version(node, "2.0.0"))
37  {
38  return "version wrong. Not 2.0.0 or not there";
39  }
40 
41  if (!node->name || g_strcmp0((char*)node->name, "gnc:commodity"))
42  {
43  return "Name of toplevel node is bad";
44  }
45 
46  for (mark = node->xmlChildrenNode; mark; mark = mark->next)
47  {
48  if (g_strcmp0((char*)mark->name, "text") == 0)
49  {
50  }
51  else if (g_strcmp0((char*)mark->name, "cmdty:space") == 0)
52  {
53  if (!equals_node_val_vs_string(
55  {
56  return "namespaces differ";
57  }
58  }
59  else if (g_strcmp0((char*)mark->name, "cmdty:id") == 0)
60  {
61  if (!equals_node_val_vs_string(
62  mark, gnc_commodity_get_mnemonic(com)))
63  {
64  return "mnemonic differ";
65  }
66  }
67  else if (g_strcmp0((char*)mark->name, "cmdty:name") == 0)
68  {
69  if (!equals_node_val_vs_string(
70  mark, gnc_commodity_get_fullname(com)))
71  {
72  return "names differ";
73  }
74  }
75  else if (g_strcmp0((char*)mark->name, "cmdty:xcode") == 0)
76  {
77  if (!equals_node_val_vs_string(
78  mark, gnc_commodity_get_cusip(com)))
79  {
80  return "exchange codes differ";
81  }
82  }
83  else if (g_strcmp0((char*)mark->name, "cmdty:fraction") == 0)
84  {
85  gchar *txt;
86  gint64 type;
87 
88  txt = dom_tree_to_text(mark);
89 
90  if (!txt)
91  {
92  return "couldn't get fraction string";
93  }
94 
95  else if (!string_to_gint64(txt, &type))
96  {
97  g_free(txt);
98  return "couldn't convert fraction string to int";
99  }
100  else if (type != gnc_commodity_get_fraction(com))
101  {
102  g_free(txt);
103  return "fractions differ";
104  }
105  else
106  {
107  g_free(txt);
108  }
109  }
110  else if (g_strcmp0((char*)mark->name, "cmdty:slots") == 0)
111  {
112  if (!equals_node_val_vs_kvp_frame(mark,
114  return "slots differ";
115  }
116  /* Legitimate tags which we don't yet have tests */
117  else if (g_strcmp0((char*)mark->name, "cmdty:get_quotes") == 0 ||
118  g_strcmp0((char*)mark->name, "cmdty:quote_source") == 0 ||
119  g_strcmp0((char*)mark->name, "cmdty:quote_tz") == 0)
120  {
121  continue;
122  }
123  else
124  {
125  return "unknown node";
126  }
127  }
128 
129  return NULL;
130 }
131 
133 {
134  gnc_commodity *com;
135  int value;
136 };
137 typedef struct com_data_struct com_data;
138 
139 static gboolean
140 test_add_commodity(const char *tag, gpointer globaldata, gpointer data)
141 {
142  com_data *gdata = (com_data*)globaldata;
143 
144  do_test_args(gnc_commodity_equiv((gnc_commodity*)data, gdata->com),
145  "gnc_commodity_sixtp_parser_create",
146  __FILE__, __LINE__, "%d", gdata->value );
148 
149  return TRUE;
150 
151 }
152 
153 static void
154 test_generation(void)
155 {
156  int i;
157  for (i = 0; i < 20; i++)
158  {
159  gnc_commodity *ran_com;
160  xmlNodePtr test_node;
161  gchar *filename1;
162  int fd;
163  gchar *compare_msg;
164 
165  ran_com = get_random_commodity(book);
166 
167  test_node = gnc_commodity_dom_tree_create(ran_com);
168  if (!test_node)
169  {
170  failure_args("commodity_xml", __FILE__, __LINE__,
171  "gnc_commodity_dom_tree_create returned NULL");
172  gnc_commodity_destroy(ran_com);
173  continue;
174  }
175 
176  if ((compare_msg = node_and_commodity_equal(test_node, ran_com)) !=
177  NULL)
178  {
179  failure_args("commodity_xml", __FILE__, __LINE__,
180  "node and commodity were not equal: %s", compare_msg);
181  xmlElemDump(stdout, NULL, test_node);
182  xmlFreeNode(test_node);
183  gnc_commodity_destroy(ran_com);
184  continue;
185  }
186  else
187  {
188  success_args("commodity_xml", __FILE__, __LINE__, "%d", i);
189  }
190 
191  filename1 = g_strdup_printf("test_file_XXXXXX");
192 
193  fd = g_mkstemp(filename1);
194 
195  write_dom_node_to_file(test_node, fd);
196 
197  close(fd);
198 
199  {
200  sixtp *parser;
201  com_data data;
202 
203  data.com = ran_com;
204  data.value = i;
205 
206  parser = gnc_commodity_sixtp_parser_create();
207 
208  if (!gnc_xml_parse_file(parser, filename1, test_add_commodity,
209  (gpointer)&data, book))
210  {
211  failure_args("gnc_xml_parse_file returned FALSE",
212  __FILE__, __LINE__, "%d", i);
213  }
214 
215  /* no handling of circular data structures. We'll do that later */
216  /* sixtp_destroy(parser); */
217  }
218 
219  g_unlink(filename1);
220  g_free(filename1);
221  gnc_commodity_destroy(ran_com);
222  xmlFreeNode(test_node);
223  }
224 }
225 
226 static gboolean
227 test_real_commodity(const char *tag, gpointer globaldata, gpointer data)
228 {
229  const char *msg = node_and_commodity_equal((xmlNodePtr)globaldata,
230  (gnc_commodity*)data);
231  do_test_args(msg == NULL, "test_real_commodity",
232  __FILE__, __LINE__, msg);
234  return TRUE;
235 }
236 
237 int
238 main(int argc, char **argv)
239 {
240  g_setenv ("GNC_UNINSTALLED", "1", TRUE);
241  gnc_engine_init(argc, argv);
242 
243  book = qof_book_new ();
244 
245  if (argc > 1)
246  {
247  test_files_in_dir(argc, argv, test_real_commodity,
248  gnc_commodity_sixtp_parser_create(),
249  "gnc:commodity", book);
250  }
251  else
252  {
253  test_generation();
254  }
255 
256  print_test_results();
257  exit(get_rv());
258 }
const char * gnc_commodity_get_cusip(const gnc_commodity *cm)
Definition: sixtp.h:93
int gnc_commodity_get_fraction(const gnc_commodity *cm)
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
const char * gnc_commodity_get_namespace_compat(const gnc_commodity *cm)
QofBook * qof_book_new(void)
Account handling public routines.
void gnc_engine_init(int argc, char **argv)
Definition: gnc-engine.c:139
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
#define gnc_commodity_get_kvp_frame(cm)
void gnc_commodity_destroy(gnc_commodity *cm)
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)