Commit 7e32244e authored by Jon Nordby's avatar Jon Nordby
Browse files

tests: Add some simple XML serialization tests

parent 76de2828
Makefile
Makefile.in
*o
.deps
.libs
/test-roundtrip
/test-save
/test-save-properties
# Make the tests run against the build and not the installation
TESTS_ENVIRONMENT = \
GEGL_PATH=$(top_builddir)/operations/common:$(top_builddir)/operations/core:$(top_builddir)/operations/external:$(top_builddir)/operations/affine:$(top_builddir)/operations/generated \
ABS_TOP_BUILDDIR=$(top_builddir) \
ABS_TOP_SRCDIR=$(top_srcdir) gtester -k --verbose
# The tests
noinst_PROGRAMS = \
test-save \
test-roundtrip
TESTS = $(noinst_PROGRAMS)
# Common CPPFLAGS
AM_CPPFLAGS = \
-I$(top_srcdir) \
-I$(top_builddir)/gegl \
-I$(top_srcdir)/gegl \
-I$(top_builddir)/gegl/buffer \
-I$(top_srcdir)/gegl/buffer \
-I$(top_builddir)/gegl/property-types \
-I$(top_srcdir)/gegl/property-types \
-I$(top_builddir)/gegl/operation \
-I$(top_srcdir)/gegl/operation
AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS)
# Common libs
LIBS = $(top_builddir)/gegl/libgegl-$(GEGL_API_VERSION).la \
$(DEP_LIBS) $(BABL_LIBS)
/*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Jon Nordby <jononor@gmail.com>
*/
/* Common code used when testing XML serialization */
/* Fails assert if actual != expected and prints the two strings
*
* Note: XML strings tested as literal strings.
* Vunerable to changes in whitespace and other insigificant things.
*
* It would be better to use a proper xml parser and/or xpath,
* but we don't want to have more dependencies than
* GEGL itself, to ensure that the tests are always available. */
static void
assert_equivalent_xml(const gchar *actual, const gchar *expected)
{
gboolean equal;
g_assert(actual);
g_assert(expected);
equal = (g_strcmp0(actual, expected) == 0);
if (!equal) {
g_print("\n\"%s\"\n!= (not equal)\n\"%s\"\n", actual, expected);
}
g_assert(equal);
}
<?xml version='1.0' encoding='UTF-8'?>
<gegl>
<node operation='gegl:crop'>
<params>
<param name='x'>0</param>
<param name='y'>0</param>
<param name='width'>10</param>
<param name='height'>10</param>
</params>
</node>
<node operation='gegl:color'>
<params>
<param name='value'>rgb(100.0000, 100.0000, 100.0000)</param>
</params>
</node>
</gegl>
<?xml version='1.0' encoding='UTF-8'?>
<gegl>
<node operation='svg:src-over'>
<node operation='gegl:translate'>
<params>
<param name='origin-x'>0</param>
<param name='origin-y'>0</param>
<param name='filter'>linear</param>
<param name='hard-edges'>false</param>
<param name='lanczos-width'>3</param>
<param name='x'>5</param>
<param name='y'>0</param>
</params>
</node>
<node operation='gegl:crop'>
<params>
<param name='x'>0</param>
<param name='y'>0</param>
<param name='width'>5</param>
<param name='height'>10</param>
</params>
</node>
<node operation='gegl:color'>
<params>
<param name='value'>rgb(0.0000, 5.0000, 1.0000)</param>
</params>
</node>
</node>
<node operation='gegl:crop'>
<params>
<param name='x'>0</param>
<param name='y'>0</param>
<param name='width'>10</param>
<param name='height'>10</param>
</params>
</node>
<node operation='gegl:color'>
<params>
<param name='value'>rgb(0.0000, 5.0000, 1.0000)</param>
</params>
</node>
</gegl>
/*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Jon Nordby <jononor@gmail.com>
*/
/* Test XML load->save roundtrips */
#include <glib.h>
#include <glib/gprintf.h>
#include <gegl.h>
#include "common.c"
typedef struct {
gchar *expected_result;
gchar *xml_output;
GeglNode *graph;
} TestRoundtripFixture;
static void
test_xml_roundtrip_setup(TestRoundtripFixture *fixture, const void *data)
{
const gchar *file_path = (const gchar *)data;
gchar *file_contents = NULL;
gchar *xml_output = NULL;
gchar *cwd = g_get_current_dir();
gboolean success = g_file_get_contents(file_path, &file_contents, NULL, NULL);
GeglNode *graph = NULL;
g_assert(success);
g_assert(file_contents);
graph = gegl_node_new_from_xml(file_contents, "");
g_assert(graph);
xml_output = gegl_node_to_xml(graph, "");
g_assert(xml_output);
fixture->expected_result = file_contents;
fixture->xml_output = xml_output;
g_free(cwd);
}
/*
*
* Create a graph from XML, save this graph to XML and compare the results */
static void
test_xml_roundtrip(TestRoundtripFixture *fixture, const void *data)
{
assert_equivalent_xml(fixture->xml_output, fixture->expected_result);
}
static void
test_xml_roundtrip_teardown(TestRoundtripFixture *fixture, const void *data)
{
g_free(fixture->xml_output);
g_free(fixture->expected_result);
g_free((gpointer)data);
}
static gboolean
add_tests_for_xml_files_in_directory(const gchar *path)
{
GError *error = NULL;
GDir *dir = g_dir_open(path, 0, &error);
const gchar *filename = NULL;
if (!dir) {
g_fprintf(stderr, "Unable to open directory: %s\n", error->message);
g_error_free(error);
return FALSE;
}
while ( (filename = g_dir_read_name(dir)) ) {
gchar *test_path;
const gchar *file_path = g_build_filename(path, filename, NULL);
test_path = g_strdup_printf("/xml/roundtrip/%s/%s", path, filename);;
if (!g_str_has_suffix(filename, ".xml")) {
continue;
}
/* Need to pass in the path name */
g_test_add (test_path, TestRoundtripFixture, file_path,
test_xml_roundtrip_setup,
test_xml_roundtrip,
test_xml_roundtrip_teardown);
g_free(test_path);
/* file_path freed by teardown function*/
}
g_dir_close(dir);
return TRUE;
}
int
main (int argc, char *argv[])
{
int result = -1;
gegl_init(&argc, &argv);
g_test_init(&argc, &argv, NULL);
if (!add_tests_for_xml_files_in_directory("data")) {
result = -1;
}
else {
result = g_test_run();
}
gegl_exit();
return result;
}
/*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Jon Nordby <jononor@gmail.com>
*/
/* Test saving/serialization of graphs to XML
*/
#include <glib.h>
#include <gegl.h>
#include "common.c"
/* Saving an empty graph should result in a valid UTF-8 encoded XML document,
* with the same basic structure as if the graph was non-empty.
*
* Kind-of a sanity test, should run before other tests. */
static void
test_save_empty_graph (void)
{
const gchar * const expected_result = "<?xml version='1.0' encoding='UTF-8'?>\n<gegl>\n</gegl>\n";
GeglNode *graph;
gchar *xml;
graph = gegl_node_new();
xml = gegl_node_to_xml(graph, "");
assert_equivalent_xml(xml, expected_result);
g_object_unref(graph);
g_free(xml);
}
/* gegl:nop nodes should be discarded when saving the graph */
static void
test_save_nop_nodes (void)
{
const gchar * const expected_result = "<?xml version='1.0' encoding='UTF-8'?>\n<gegl>\n</gegl>\n";
GeglNode *graph, *op1, *op2;
gchar *xml;
graph = gegl_node_new();
op1 = gegl_node_new_child(graph, "operation", "gegl:nop", NULL);
op2 = gegl_node_new_child(graph, "operation", "gegl:nop", NULL);
gegl_node_link_many(op1, op2, NULL);
xml = gegl_node_to_xml(op2, "");
assert_equivalent_xml(xml, expected_result);
g_object_unref(graph);
g_free(xml);
}
/* Saving a graph with multiple nodes
*
* Note: Relies on the number and names of properties of the used operations to not change */
static void
test_save_multiple_nodes (void)
{
const gchar * const expected_result = \
"<?xml version='1.0' encoding='UTF-8'?>\n\
<gegl>\n\
<node operation='gegl:invert'>\n\
</node>\n\
<node operation='gegl:crop'>\n\
<params>\n\
<param name='x'>0</param>\n\
<param name='y'>0</param>\n\
<param name='width'>0</param>\n\
<param name='height'>0</param>\n\
</params>\n\
</node>\n\
</gegl>\n";
GeglNode *graph, *op1, *op2;
gchar *xml;
graph = gegl_node_new();
op1 = gegl_node_new_child(graph, "operation", "gegl:crop",
"x", 0.0, "y", 0.0,
"width", 0.0, "height", 0.0,
NULL);
op2 = gegl_node_new_child(graph, "operation", "gegl:invert", NULL);
gegl_node_link_many(op1, op2, NULL);
xml = gegl_node_to_xml(op2, "");
assert_equivalent_xml(xml, expected_result);
g_object_unref(graph);
g_free(xml);
}
/* Test that saving a subgraph works */
static void
test_save_toplevel_graph (void)
{
const gchar * const expected_result = \
"<?xml version='1.0' encoding='UTF-8'?>\n\
<gegl>\n\
<node operation='gegl:invert'>\n\
</node>\n\
<node operation='gegl:crop'>\n\
<params>\n\
<param name='x'>0</param>\n\
<param name='y'>0</param>\n\
<param name='width'>0</param>\n\
<param name='height'>0</param>\n\
</params>\n\
</node>\n\
</gegl>\n";
GeglNode *graph, *op1, *op2;
gchar *xml;
graph = gegl_node_new();
op1 = gegl_node_new_child(graph, "operation", "gegl:crop",
"x", 0.0, "y", 0.0,
"width", 0.0, "height", 0.0,
NULL);
op2 = gegl_node_new_child(graph, "operation", "gegl:invert", NULL);
gegl_node_link_many(op1, op2, NULL);
xml = gegl_node_to_xml(graph, "");
assert_equivalent_xml(xml, expected_result);
g_object_unref(graph);
g_free(xml);
}
int
main (int argc, char *argv[])
{
int result = -1;
gegl_init(&argc, &argv);
g_test_init(&argc, &argv, NULL);
g_test_add_func("/xml/save/empty_graph", test_save_empty_graph);
g_test_add_func("/xml/save/only_nop_nodes", test_save_nop_nodes);
g_test_add_func("/xml/save/multiple_nodes", test_save_multiple_nodes);
/* Expected failure
g_test_add_func("/xml/save/toplevel_graph", test_save_toplevel_graph);
*/
result = g_test_run();
gegl_exit();
return result;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment