1 /* Copyright (C) 2015 The libxml++ development team
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <libxml++/libxml++.h>
24 class OnCdataBlockTestParser : public xmlpp::SaxParser
27 void on_cdata_block(const Glib::ustring& text) override
29 g_assert_cmpstr(text.c_str(), ==, "some CDATA");
30 throw std::runtime_error("on_cdata_block runtime exception");
34 void test_on_cdata_block()
36 OnCdataBlockTestParser parser;
37 bool exceptionThrown = false;
40 parser.parse_memory("<root><![CDATA[some CDATA]]></root>");
42 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
43 catch(const std::runtime_error& e)
45 catch(const xmlpp::exception& e)
48 exceptionThrown = true;
49 g_assert_cmpstr(e.what(), ==, "on_cdata_block runtime exception");
51 g_assert_true(exceptionThrown);
55 class OnCharactersTestParser : public xmlpp::SaxParser
58 void on_characters(const Glib::ustring& characters) override
60 g_assert_cmpstr(characters.c_str(), ==, "abc");
61 throw std::runtime_error("on_characters runtime exception");
65 void test_on_characters()
67 OnCharactersTestParser parser;
68 bool exceptionThrown = false;
71 parser.parse_memory("<root>abc</root>");
73 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
74 catch(const std::runtime_error& e)
76 catch(const xmlpp::exception& e)
79 exceptionThrown = true;
80 g_assert_cmpstr(e.what(), ==, "on_characters runtime exception");
82 g_assert_true(exceptionThrown);
86 class OnCommentTestParser : public xmlpp::SaxParser
89 void on_comment(const Glib::ustring& text) override
91 g_assert_cmpstr(text.c_str(), ==, "a comment");
92 throw std::runtime_error("on_comment runtime exception");
96 void test_on_comment()
98 OnCommentTestParser parser;
99 bool exceptionThrown = false;
102 parser.parse_memory("<root><!--a comment--></root>");
104 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
105 catch(const std::runtime_error& e)
107 catch(const xmlpp::exception& e)
110 exceptionThrown = true;
111 g_assert_cmpstr(e.what(), ==, "on_comment runtime exception");
113 g_assert_true(exceptionThrown);
117 class OnEndDocumentTestParser : public xmlpp::SaxParser
120 void on_end_document() override
122 throw std::runtime_error("on_end_document runtime exception");
126 void test_on_end_document()
128 OnEndDocumentTestParser parser;
129 bool exceptionThrown = false;
132 parser.parse_memory("<root></root>");
134 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
135 catch(const std::runtime_error& e)
137 catch(const xmlpp::exception& e)
140 exceptionThrown = true;
141 g_assert_cmpstr(e.what(), ==, "on_end_document runtime exception");
143 g_assert_true(exceptionThrown);
147 class OnEndElementTestParser : public xmlpp::SaxParser
150 void on_end_element(const Glib::ustring& name) override
152 g_assert_cmpstr(name.c_str(), ==, "a:root");
153 throw std::runtime_error("on_end_element runtime exception");
157 void test_on_end_element()
159 OnEndElementTestParser parser;
160 bool exceptionThrown = false;
163 parser.parse_memory("<a:root xmlns:a=\"urn:test\"></a:root>");
165 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
166 catch(const std::runtime_error& e)
168 catch(const xmlpp::exception& e)
171 exceptionThrown = true;
172 g_assert_cmpstr(e.what(), ==, "on_end_element runtime exception");
174 g_assert_true(exceptionThrown);
178 class OnEntityDeclarationTestParser : public xmlpp::SaxParser
181 void on_entity_declaration(const Glib::ustring& name, xmlpp::XmlEntityType /* type */,
182 const Glib::ustring& /* publicId */, const Glib::ustring& /* systemId */,
183 const Glib::ustring& content) override
185 g_assert_cmpstr(name.c_str(), ==, "number");
186 g_assert_cmpstr(content.c_str(), ==, "42");
187 throw std::runtime_error("on_entity_declaration runtime exception");
191 void test_on_entity_declaration()
193 OnEntityDeclarationTestParser parser;
194 bool exceptionThrown = false;
197 parser.parse_memory("<!DOCTYPE MyDocument [<!ENTITY number \"42\">]><root></root>");
199 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
200 catch(const std::runtime_error& e)
202 catch(const xmlpp::exception& e)
205 exceptionThrown = true;
206 g_assert_cmpstr(e.what(), ==, "on_entity_declaration runtime exception");
208 g_assert_true(exceptionThrown);
212 class OnErrorTestParser : public xmlpp::SaxParser
215 void on_error(const Glib::ustring& /* text */) override
217 throw std::runtime_error("on_error runtime exception");
223 OnErrorTestParser parser;
224 bool exceptionThrown = false;
227 parser.parse_memory("<root>&unknown;</root>");
229 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
230 catch(const std::runtime_error& e)
232 catch(const xmlpp::exception& e)
235 exceptionThrown = true;
236 g_assert_cmpstr(e.what(), ==, "on_error runtime exception");
238 g_assert_true(exceptionThrown);
242 class OnGetEntityTestParser : public xmlpp::SaxParser
245 OnGetEntityTestParser()
246 : xmlpp::SaxParser(true)
250 _xmlEntity* on_get_entity(const Glib::ustring& name) override
252 g_assert_cmpstr(name.c_str(), ==, "number");
253 throw std::runtime_error("on_get_entity runtime exception");
257 void test_on_get_entity()
259 OnGetEntityTestParser parser;
260 bool exceptionThrown = false;
263 parser.parse_memory("<!DOCTYPE MyDocument [<!ENTITY number \"42\">]><root>&number;</root>");
265 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
266 catch(const std::runtime_error& e)
268 catch(const xmlpp::exception& e)
271 exceptionThrown = true;
272 g_assert_cmpstr(e.what(), ==, "on_get_entity runtime exception");
274 g_assert_true(exceptionThrown);
278 class OnStartDocumentTestParser : public xmlpp::SaxParser
281 void on_start_document() override
283 throw std::runtime_error("on_start_document runtime exception");
287 void test_on_start_document()
289 OnStartDocumentTestParser parser;
290 bool exceptionThrown = false;
293 parser.parse_memory("<root></root>");
295 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
296 catch(const std::runtime_error& e)
298 catch(const xmlpp::exception& e)
301 exceptionThrown = true;
302 g_assert_cmpstr(e.what(), ==, "on_start_document runtime exception");
304 g_assert_true(exceptionThrown);
308 class OnStartElementTestParser : public xmlpp::SaxParser
311 void on_start_element(const Glib::ustring& name, const xmlpp::SaxParser::AttributeList& attributes) override
313 g_assert_cmpstr(name.c_str(), ==, "b:root");
314 g_assert_cmpint(attributes.size(), ==, 2);
315 throw std::runtime_error("on_start_element runtime exception");
319 void test_on_start_element()
321 OnStartElementTestParser parser;
322 bool exceptionThrown = false;
325 parser.parse_memory("<b:root xmlns:b=\"urn:test\" someattr=\"test\"></b:root>");
327 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
328 catch(const std::runtime_error& e)
330 catch(const xmlpp::exception& e)
333 exceptionThrown = true;
334 g_assert_cmpstr(e.what(), ==, "on_start_element runtime exception");
336 g_assert_true(exceptionThrown);
344 test_on_cdata_block();
345 test_on_characters();
347 test_on_end_document();
348 test_on_end_element();
349 test_on_entity_declaration();
351 // TODO test on_fatal_error()
352 // This is not currently possible because the fatalError handler is never called;
353 // error is called for all errors.
354 // http://xmlsoft.org/html/libxml-parser.html#fatalErrorSAXFunc
355 test_on_get_entity();
356 // TODO test on_internal_subset()
357 test_on_start_document();
358 test_on_start_element();
359 // TODO test on_warning(), on_validity_error(), on_validity_warning()