1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, see <https://www.gnu.org/licenses/>.
14  *
15  * Copyright (C) 2011 Jon Nordby <jononor@gmail.com>
16  */
17 
18 /* Test XML load->save roundtrips  */
19 
20 #include <glib.h>
21 #include <glib/gprintf.h>
22 
23 #include <gegl.h>
24 
25 #include "common.c"
26 
27 typedef struct {
28     gchar *expected_result;
29     gchar *xml_output;
30     GeglNode *graph;
31 } TestRoundtripFixture;
32 
33 
34 static void
test_xml_roundtrip_setup(TestRoundtripFixture * fixture,const void * data)35 test_xml_roundtrip_setup(TestRoundtripFixture *fixture, const void *data)
36 {
37     const gchar *file_path = (const gchar *)data;
38     gchar *file_contents = NULL;
39     gchar *xml_output = NULL;
40     gchar *cwd = g_get_current_dir();
41     gboolean success = g_file_get_contents(file_path, &file_contents, NULL, NULL);
42     GeglNode *graph = NULL;
43 
44     g_assert(success);
45     g_assert(file_contents);
46 
47     graph = gegl_node_new_from_xml(file_contents, "");
48     g_assert(graph);
49 
50     xml_output = gegl_node_to_xml(graph, "");
51     g_assert(xml_output);
52 
53     fixture->expected_result = file_contents;
54     fixture->xml_output = xml_output;
55     g_free(cwd);
56 }
57 
58 /*
59  *
60  * Create a graph from XML, save this graph to XML and compare the results */
61 static void
test_xml_roundtrip(TestRoundtripFixture * fixture,const void * data)62 test_xml_roundtrip(TestRoundtripFixture *fixture, const void *data)
63 {
64     assert_equivalent_xml(fixture->xml_output, fixture->expected_result);
65 }
66 
67 
68 static void
test_xml_roundtrip_teardown(TestRoundtripFixture * fixture,const void * data)69 test_xml_roundtrip_teardown(TestRoundtripFixture *fixture, const void *data)
70 {
71     g_free(fixture->xml_output);
72     g_free(fixture->expected_result);
73     g_free((gpointer)data);
74 }
75 
76 static gboolean
add_tests_for_xml_files_in_directory(const gchar * path)77 add_tests_for_xml_files_in_directory(const gchar *path)
78 {
79     GError *error = NULL;
80     GDir *dir = g_dir_open(path, 0, &error);
81     const gchar *filename = NULL;
82 
83     if (!dir) {
84         g_fprintf(stderr, "Unable to open directory: %s\n", error->message);
85         g_error_free(error);
86         return FALSE;
87     }
88 
89     while ( (filename = g_dir_read_name(dir)) ) {
90         gchar *test_path;
91         const gchar *file_path = g_build_filename(path, filename, NULL);
92         test_path = g_strdup_printf("/xml/roundtrip/%s/%s", path, filename);;
93 
94         if (!g_str_has_suffix(filename, ".xml")) {
95             continue;
96         }
97 
98         /* Need to pass in the path name */
99         g_test_add (test_path, TestRoundtripFixture, file_path,
100                     test_xml_roundtrip_setup,
101                     test_xml_roundtrip,
102                     test_xml_roundtrip_teardown);
103         g_free(test_path);
104         /* file_path freed by teardown function*/
105 
106     }
107     g_dir_close(dir);
108 
109     return TRUE;
110 }
111 
112 int
main(int argc,char * argv[])113 main (int argc, char *argv[])
114 {
115     int result = -1;
116     gchar *datadir;
117 
118     gegl_init(&argc, &argv);
119     g_test_init(&argc, &argv, NULL);
120 
121     datadir = g_build_filename (g_getenv ("ABS_TOP_SRCDIR"), "tests/xml/data", NULL);
122     if (!add_tests_for_xml_files_in_directory(datadir)) {
123         result = -1;
124     }
125     else {
126         result = g_test_run();
127     }
128 
129     gegl_exit();
130     return result;
131 }
132