1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * test-xml.c: Test libxml2 wrappers
4 *
5 * Copyright (C) 2015 Morten Welinder <terra@gnome.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 #include <stdio.h>
23 #include <gsf/gsf.h>
24
25 static int
test_xml_indent(void)26 test_xml_indent (void)
27 {
28 GsfOutput *mem = gsf_output_memory_new ();
29 GsfXMLOut *xml = gsf_xml_out_new (mem);
30 const char *data;
31 gboolean pprint;
32 int err;
33 const char *expected =
34 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
35 "<outer>\n"
36 " <data/>\n"
37 " <data attr=\"val\"/>\n"
38 " <data>text</data>\n"
39 " <data>\n"
40 " <inner>text</inner>\n"
41 " </data>\n"
42 " <data>text</data>\n"
43 " <data><inner>text</inner></data>\n"
44 "</outer>\n";
45
46 gsf_xml_out_start_element (xml, "outer");
47
48 gsf_xml_out_start_element (xml, "data");
49 gsf_xml_out_end_element (xml);
50
51 gsf_xml_out_start_element (xml, "data");
52 gsf_xml_out_add_cstr_unchecked (xml, "attr", "val");
53 gsf_xml_out_end_element (xml);
54
55 gsf_xml_out_start_element (xml, "data");
56 gsf_xml_out_add_cstr_unchecked (xml, NULL, "text");
57 gsf_xml_out_end_element (xml);
58
59 gsf_xml_out_start_element (xml, "data");
60 gsf_xml_out_start_element (xml, "inner");
61 gsf_xml_out_add_cstr_unchecked (xml, NULL, "text");
62 gsf_xml_out_end_element (xml);
63 gsf_xml_out_end_element (xml);
64
65 gsf_xml_out_start_element (xml, "data");
66 pprint = gsf_xml_out_set_pretty_print (xml, FALSE);
67 gsf_xml_out_add_cstr_unchecked (xml, NULL, "text");
68 gsf_xml_out_set_pretty_print (xml, pprint);
69 gsf_xml_out_end_element (xml);
70
71 gsf_xml_out_start_element (xml, "data");
72 pprint = gsf_xml_out_set_pretty_print (xml, FALSE);
73 gsf_xml_out_start_element (xml, "inner");
74 gsf_xml_out_add_cstr_unchecked (xml, NULL, "text");
75 gsf_xml_out_end_element (xml);
76 gsf_xml_out_set_pretty_print (xml, pprint);
77 gsf_xml_out_end_element (xml);
78
79 gsf_xml_out_end_element (xml);
80
81 g_object_unref (xml);
82
83 data = (const char *)gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (mem));
84 g_printerr ("Got\n%s\n", data);
85
86 err = !g_str_equal (data, expected);
87 if (err)
88 g_printerr ("Expected\n%s\n", expected);
89
90 g_object_unref (mem);
91
92 return err;
93 }
94
95 int
main(int argc,char * argv[])96 main (int argc, char *argv[])
97 {
98 int res;
99
100 (void)argc;
101 (void)argv;
102
103 gsf_init ();
104 res = test_xml_indent ();
105 gsf_shutdown ();
106
107 return res;
108 }
109