2015-09-29 14:08:48 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* 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, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
2015-11-29 19:11:29 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2001-08-07 18:29:04 -05:00
|
|
|
|
|
|
|
#include <glib.h>
|
2007-01-21 10:38:42 -06:00
|
|
|
#include <glib/gstdio.h>
|
2001-08-07 18:29:04 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-10-26 07:10:06 -05:00
|
|
|
#include "gnc-module.h"
|
2005-11-21 13:31:44 -06:00
|
|
|
#include "qof.h"
|
2015-11-29 19:11:29 -06:00
|
|
|
#include "test-stuff.h"
|
|
|
|
#include "test-engine-stuff.h"
|
2001-08-07 18:29:04 -05:00
|
|
|
|
2015-11-29 19:11:29 -06:00
|
|
|
#include "Account.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "gnc-xml-helper.h"
|
|
|
|
#include "gnc-xml.h"
|
2001-08-07 18:29:04 -05:00
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
#include "io-gncxml-gen.h"
|
2001-08-18 15:33:51 -05:00
|
|
|
#include "test-file-stuff.h"
|
2001-08-07 18:29:04 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
static QofBook* book;
|
2001-08-07 18:29:04 -05:00
|
|
|
|
2015-11-29 19:11:29 -06:00
|
|
|
static const char*
|
2016-03-12 16:04:40 -06:00
|
|
|
node_and_commodity_equal (xmlNodePtr node, const gnc_commodity* com)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
xmlNodePtr mark;
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2012-08-07 12:24:55 -05:00
|
|
|
while (g_strcmp0 ((char*)node->name, "text") == 0)
|
2009-12-29 14:12:48 -06:00
|
|
|
node = node->next;
|
2001-10-11 06:35:50 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!check_dom_tree_version (node, "2.0.0"))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
return "version wrong. Not 2.0.0 or not there";
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!node->name || g_strcmp0 ((char*)node->name, "gnc:commodity"))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
return "Name of toplevel node is bad";
|
|
|
|
}
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
for (mark = node->xmlChildrenNode; mark; mark = mark->next)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
if (g_strcmp0 ((char*)mark->name, "text") == 0)
|
2001-10-11 06:35:50 -05:00
|
|
|
{
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (g_strcmp0 ((char*)mark->name, "cmdty:space") == 0)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2017-06-21 03:11:54 -05:00
|
|
|
/* Currency namespace is now stored as GNC_COMMODITY_NS_CURRENCY
|
|
|
|
* but used to be stored as GNC_COMMODITY_NS_ISO,
|
|
|
|
* so check both to cater for (very) old testfiles. */
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!equals_node_val_vs_string (
|
2017-06-21 03:11:54 -05:00
|
|
|
mark, gnc_commodity_get_namespace (com)) &&
|
|
|
|
!((g_strcmp0 (gnc_commodity_get_namespace (com), GNC_COMMODITY_NS_CURRENCY) == 0) &&
|
|
|
|
equals_node_val_vs_string (
|
|
|
|
mark, GNC_COMMODITY_NS_ISO)))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
return "namespaces differ";
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (g_strcmp0 ((char*)mark->name, "cmdty:id") == 0)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!equals_node_val_vs_string (
|
|
|
|
mark, gnc_commodity_get_mnemonic (com)))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
return "mnemonic differ";
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (g_strcmp0 ((char*)mark->name, "cmdty:name") == 0)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!equals_node_val_vs_string (
|
|
|
|
mark, gnc_commodity_get_fullname (com)))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
return "names differ";
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (g_strcmp0 ((char*)mark->name, "cmdty:xcode") == 0)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!equals_node_val_vs_string (
|
|
|
|
mark, gnc_commodity_get_cusip (com)))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
return "exchange codes differ";
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (g_strcmp0 ((char*)mark->name, "cmdty:fraction") == 0)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
gchar* txt;
|
2001-08-07 18:29:04 -05:00
|
|
|
gint64 type;
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
txt = dom_tree_to_text (mark);
|
2001-08-07 18:29:04 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (!txt)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
return "couldn't get fraction string";
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (!string_to_gint64 (txt, &type))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
g_free (txt);
|
2001-08-07 18:29:04 -05:00
|
|
|
return "couldn't convert fraction string to int";
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (type != gnc_commodity_get_fraction (com))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
g_free (txt);
|
2001-08-07 18:29:04 -05:00
|
|
|
return "fractions differ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
g_free (txt);
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (g_strcmp0 ((char*)mark->name, "cmdty:slots") == 0)
|
2013-10-31 17:23:28 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!equals_node_val_vs_kvp_frame (mark,
|
|
|
|
gnc_commodity_get_kvp_frame (com)))
|
2013-10-24 07:28:30 -05:00
|
|
|
return "slots differ";
|
2013-10-31 17:23:28 -05:00
|
|
|
}
|
2010-12-04 15:09:57 -06:00
|
|
|
/* Legitimate tags which we don't yet have tests */
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (g_strcmp0 ((char*)mark->name, "cmdty:get_quotes") == 0 ||
|
|
|
|
g_strcmp0 ((char*)mark->name, "cmdty:quote_source") == 0 ||
|
|
|
|
g_strcmp0 ((char*)mark->name, "cmdty:quote_tz") == 0)
|
2010-12-04 15:09:57 -06:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2001-08-07 18:29:04 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return "unknown node";
|
|
|
|
}
|
|
|
|
}
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2001-08-07 18:29:04 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct com_data_struct
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_commodity* com;
|
2001-08-07 18:29:04 -05:00
|
|
|
int value;
|
|
|
|
};
|
|
|
|
typedef struct com_data_struct com_data;
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
test_add_commodity (const char* tag, gpointer globaldata, gpointer data)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
com_data* gdata = (com_data*)globaldata;
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
do_test_args (gnc_commodity_equiv ((gnc_commodity*)data, gdata->com),
|
|
|
|
"gnc_commodity_sixtp_parser_create",
|
|
|
|
__FILE__, __LINE__, "%d", gdata->value);
|
|
|
|
gnc_commodity_destroy ((gnc_commodity*)data);
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2001-08-07 18:29:04 -05:00
|
|
|
return TRUE;
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
test_generation (void)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
|
|
|
int i;
|
2009-12-29 14:12:48 -06:00
|
|
|
for (i = 0; i < 20; i++)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_commodity* ran_com;
|
2001-08-07 18:29:04 -05:00
|
|
|
xmlNodePtr test_node;
|
2016-03-12 16:04:40 -06:00
|
|
|
gchar* filename1;
|
2001-08-07 18:29:04 -05:00
|
|
|
int fd;
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
ran_com = get_random_commodity (book);
|
2001-08-07 18:29:04 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
test_node = gnc_commodity_dom_tree_create (ran_com);
|
2009-12-29 14:12:48 -06:00
|
|
|
if (!test_node)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
failure_args ("commodity_xml", __FILE__, __LINE__,
|
|
|
|
"gnc_commodity_dom_tree_create returned NULL");
|
|
|
|
gnc_commodity_destroy (ran_com);
|
2001-08-07 18:29:04 -05:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
auto compare_msg = node_and_commodity_equal (test_node, ran_com);
|
2015-11-29 19:11:29 -06:00
|
|
|
if (compare_msg != nullptr)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
failure_args ("commodity_xml", __FILE__, __LINE__,
|
|
|
|
"node and commodity were not equal: %s", compare_msg);
|
|
|
|
xmlElemDump (stdout, NULL, test_node);
|
|
|
|
xmlFreeNode (test_node);
|
|
|
|
gnc_commodity_destroy (ran_com);
|
2001-08-07 18:29:04 -05:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-29 14:12:48 -06:00
|
|
|
else
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
success_args ("commodity_xml", __FILE__, __LINE__, "%d", i);
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
filename1 = g_strdup_printf ("test_file_XXXXXX");
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
fd = g_mkstemp (filename1);
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
write_dom_node_to_file (test_node, fd);
|
2001-08-07 18:29:04 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
close (fd);
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
sixtp* parser;
|
2001-08-07 18:29:04 -05:00
|
|
|
com_data data;
|
|
|
|
|
|
|
|
data.com = ran_com;
|
|
|
|
data.value = i;
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
parser = gnc_commodity_sixtp_parser_create ();
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!gnc_xml_parse_file (parser, filename1, test_add_commodity,
|
|
|
|
(gpointer)&data, book))
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
failure_args ("gnc_xml_parse_file returned FALSE",
|
|
|
|
__FILE__, __LINE__, "%d", i);
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* no handling of circular data structures. We'll do that later */
|
|
|
|
/* sixtp_destroy(parser); */
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_unlink (filename1);
|
|
|
|
g_free (filename1);
|
|
|
|
gnc_commodity_destroy (ran_com);
|
|
|
|
xmlFreeNode (test_node);
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
test_real_commodity (const char* tag, gpointer globaldata, gpointer data)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
const char* msg = node_and_commodity_equal ((xmlNodePtr)globaldata,
|
|
|
|
(gnc_commodity*)data);
|
|
|
|
do_test_args (msg == NULL, "test_real_commodity",
|
|
|
|
__FILE__, __LINE__, msg);
|
|
|
|
gnc_commodity_destroy ((gnc_commodity*)data);
|
2001-08-07 18:29:04 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-08-08 20:09:47 -05:00
|
|
|
int
|
2016-03-12 16:04:40 -06:00
|
|
|
main (int argc, char** argv)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2011-12-02 16:00:18 -06:00
|
|
|
g_setenv ("GNC_UNINSTALLED", "1", TRUE);
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_engine_init (argc, argv);
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
book = qof_book_new ();
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (argc > 1)
|
2001-08-07 18:29:04 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
test_files_in_dir (argc, argv, test_real_commodity,
|
|
|
|
gnc_commodity_sixtp_parser_create (),
|
|
|
|
"gnc:commodity", book);
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
test_generation ();
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
print_test_results ();
|
|
|
|
exit (get_rv ());
|
2001-08-07 18:29:04 -05:00
|
|
|
}
|