1 /*
2  * Tests for xmlwriter.
3  *
4  * Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
5  *
6  */
7 
8 #include "../ctest.h"
9 #include "../helper.h"
10 
11 #include "../../../include/xlsxwriter/xmlwriter.h"
12 
13 // Test _xml_declaration().
CTEST(xmlwriter,xml_declaration)14 CTEST(xmlwriter, xml_declaration) {
15 
16     char* got;
17     char exp[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
18     FILE* testfile = lxw_tmpfile(NULL);
19 
20     lxw_xml_declaration(testfile);
21 
22     RUN_XLSX_STREQ(exp, got);
23 }
24 
25 // Test _xml_start_tag() with no attributes.
CTEST(xmlwriter,xml_start_tag)26 CTEST(xmlwriter, xml_start_tag) {
27 
28     char* got;
29     char exp[] = "<foo>";
30     FILE* testfile = lxw_tmpfile(NULL);
31 
32     lxw_xml_start_tag(testfile, "foo", NULL);
33 
34     RUN_XLSX_STREQ(exp, got);
35 }
36 
37 // Test _xml_start_tag() with attributes.
CTEST(xmlwriter,xml_start_tag_with_attributes)38 CTEST(xmlwriter, xml_start_tag_with_attributes) {
39 
40     char* got;
41     char exp[] = "<foo span=\"8\" baz=\"7\">";
42     FILE* testfile = lxw_tmpfile(NULL);
43     struct xml_attribute_list attributes;
44     struct xml_attribute *attribute;
45 
46     LXW_INIT_ATTRIBUTES();
47     LXW_PUSH_ATTRIBUTES_STR("span", "8");
48     LXW_PUSH_ATTRIBUTES_STR("baz",  "7");
49 
50     lxw_xml_start_tag(testfile, "foo", &attributes);
51 
52     RUN_XLSX_STREQ(exp, got);
53 
54     LXW_FREE_ATTRIBUTES();
55 }
56 
57 // Test _xml_start_tag() with attributes requiring escaping.
CTEST(xmlwriter,xml_start_tag_with_attributes_to_escape)58 CTEST(xmlwriter, xml_start_tag_with_attributes_to_escape) {
59 
60     char* got;
61     char exp[] = "<foo span=\"&amp;&lt;&gt;&quot;\">";
62     FILE* testfile = lxw_tmpfile(NULL);
63     struct xml_attribute_list attributes;
64     struct xml_attribute *attribute;
65 
66     LXW_INIT_ATTRIBUTES();
67     LXW_PUSH_ATTRIBUTES_STR("span", "&<>\"");
68 
69     lxw_xml_start_tag(testfile, "foo", &attributes);
70 
71     RUN_XLSX_STREQ(exp, got);
72 
73     LXW_FREE_ATTRIBUTES();
74 }
75 
76 // Test _xml_start_tag_unencoded() with attributes.
CTEST(xmlwriter,xml_start_tag_unencoded)77 CTEST(xmlwriter, xml_start_tag_unencoded) {
78 
79     char* got;
80     char exp[] = "<foo span=\"&<>\"\">";
81     FILE* testfile = lxw_tmpfile(NULL);
82     struct xml_attribute_list attributes;
83     struct xml_attribute *attribute;
84 
85     LXW_INIT_ATTRIBUTES();
86     LXW_PUSH_ATTRIBUTES_STR("span", "&<>\"");
87 
88     lxw_xml_start_tag_unencoded(testfile, "foo", &attributes);
89 
90     RUN_XLSX_STREQ(exp, got);
91 
92     LXW_FREE_ATTRIBUTES();
93 }
94 
95 // Test _xml_end_tag().
CTEST(xmlwriter,xml_end_tag)96 CTEST(xmlwriter, xml_end_tag) {
97 
98     char* got;
99     char exp[] = "</foo>";
100     FILE* testfile = lxw_tmpfile(NULL);
101 
102     lxw_xml_end_tag(testfile, "foo");
103 
104     RUN_XLSX_STREQ(exp, got);
105 }
106 
107 // Test _xml_empty_tag() with no attributes.
CTEST(xmlwriter,xml_empty_tag)108 CTEST(xmlwriter, xml_empty_tag) {
109 
110     char* got;
111     char exp[] = "<foo/>";
112     FILE* testfile = lxw_tmpfile(NULL);
113 
114     lxw_xml_empty_tag(testfile, "foo", NULL);
115 
116     RUN_XLSX_STREQ(exp, got);
117 }
118 
119 // Test _xml_empty_tag() with attributes.
CTEST(xmlwriter,xml_empty_tag_with_attributes)120 CTEST(xmlwriter, xml_empty_tag_with_attributes) {
121 
122     char* got;
123     char exp[] = "<foo span=\"8\" baz=\"7\"/>";
124     FILE* testfile = lxw_tmpfile(NULL);
125     struct xml_attribute_list attributes;
126     struct xml_attribute *attribute;
127 
128     LXW_INIT_ATTRIBUTES();
129     LXW_PUSH_ATTRIBUTES_STR("span", "8");
130     LXW_PUSH_ATTRIBUTES_STR("baz",  "7");
131 
132     lxw_xml_empty_tag(testfile, "foo", &attributes);
133 
134     RUN_XLSX_STREQ(exp, got);
135 
136     LXW_FREE_ATTRIBUTES();
137 }
138 
139 // Test _xml_empty_tag() with attributes requiring escaping.
CTEST(xmlwriter,xml_empty_tag_with_attributes_to_escape)140 CTEST(xmlwriter, xml_empty_tag_with_attributes_to_escape) {
141 
142     char* got;
143     char exp[] = "<foo span=\"&amp;&lt;&gt;&quot;\"/>";
144     FILE* testfile = lxw_tmpfile(NULL);
145     struct xml_attribute_list attributes;
146     struct xml_attribute *attribute;
147 
148     LXW_INIT_ATTRIBUTES();
149     LXW_PUSH_ATTRIBUTES_STR("span", "&<>\"");
150 
151     lxw_xml_empty_tag(testfile, "foo", &attributes);
152 
153     RUN_XLSX_STREQ(exp, got);
154 
155     LXW_FREE_ATTRIBUTES();
156 }
157 
158 // Test _xml_empty_tag_unencoded() with attributes.
CTEST(xmlwriter,xml_empty_tag_unencoded)159 CTEST(xmlwriter, xml_empty_tag_unencoded) {
160 
161     char* got;
162     char exp[] = "<foo span=\"&<>\"\"/>";
163     FILE* testfile = lxw_tmpfile(NULL);
164     struct xml_attribute_list attributes;
165     struct xml_attribute *attribute;
166 
167     LXW_INIT_ATTRIBUTES();
168     LXW_PUSH_ATTRIBUTES_STR("span", "&<>\"");
169 
170     lxw_xml_empty_tag_unencoded(testfile, "foo", &attributes);
171 
172     RUN_XLSX_STREQ(exp, got);
173 
174     LXW_FREE_ATTRIBUTES();
175 }
176 
177 // Test _xml_empty_tag() with no attributes.
CTEST(xmlwriter,xml_data_element)178 CTEST(xmlwriter, xml_data_element) {
179 
180     char* got;
181     char exp[] = "<foo>bar</foo>";
182     FILE* testfile = lxw_tmpfile(NULL);
183 
184     lxw_xml_data_element(testfile, "foo", "bar", NULL);
185 
186     RUN_XLSX_STREQ(exp, got);
187 }
188 
189 // Test _xml_data_element() with attributes.
CTEST(xmlwriter,xml_data_element_with_attributes)190 CTEST(xmlwriter, xml_data_element_with_attributes) {
191 
192     char* got;
193     char exp[] = "<foo span=\"8\">bar</foo>";
194     FILE* testfile = lxw_tmpfile(NULL);
195     struct xml_attribute_list attributes;
196     struct xml_attribute *attribute;
197 
198     LXW_INIT_ATTRIBUTES();
199     LXW_PUSH_ATTRIBUTES_STR("span", "8");
200 
201     lxw_xml_data_element(testfile, "foo", "bar", &attributes);
202 
203     RUN_XLSX_STREQ(exp, got);
204 
205     LXW_FREE_ATTRIBUTES();
206 }
207 
208 // Test _xml_data_element() with data requiring escaping.
CTEST(xmlwriter,xml_data_element_with_escapes)209 CTEST(xmlwriter, xml_data_element_with_escapes) {
210 
211     char* got;
212     char exp[] = "<foo span=\"8\">&amp;&lt;&gt;\"</foo>";
213     FILE* testfile = lxw_tmpfile(NULL);
214     struct xml_attribute_list attributes;
215     struct xml_attribute *attribute;
216 
217     LXW_INIT_ATTRIBUTES();
218     LXW_PUSH_ATTRIBUTES_STR("span", "8");
219 
220     lxw_xml_data_element(testfile, "foo", "&<>\"", &attributes);
221 
222     RUN_XLSX_STREQ(exp, got);
223 
224     LXW_FREE_ATTRIBUTES();
225 }
226 
227