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
19 #include <libxml++/libxml++.h>
25 class OnCdataBlockTestParser : public xmlpp::SaxParser
28 void on_cdata_block(const Glib::ustring& text) override
30 g_assert_cmpstr(text.c_str(), ==, "some CDATA");
31 throw std::runtime_error("on_cdata_block runtime exception");
35 void test_on_cdata_block()
37 OnCdataBlockTestParser parser;
38 bool exceptionThrown = false;
41 parser.parse_memory("<root><![CDATA[some CDATA]]></root>");
43 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
44 catch(const std::runtime_error& e)
46 catch(const xmlpp::exception& e)
49 exceptionThrown = true;
50 g_assert_cmpstr(e.what(), ==, "on_cdata_block runtime exception");
52 g_assert_true(exceptionThrown);
56 class OnCharactersTestParser : public xmlpp::SaxParser
59 void on_characters(const Glib::ustring& characters) override
61 g_assert_cmpstr(characters.c_str(), ==, "abc");
62 throw std::runtime_error("on_characters runtime exception");
66 void test_on_characters()
68 OnCharactersTestParser parser;
69 bool exceptionThrown = false;
72 parser.parse_memory("<root>abc</root>");
74 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
75 catch(const std::runtime_error& e)
77 catch(const xmlpp::exception& e)
80 exceptionThrown = true;
81 g_assert_cmpstr(e.what(), ==, "on_characters runtime exception");
83 g_assert_true(exceptionThrown);
87 class OnCommentTestParser : public xmlpp::SaxParser
90 void on_comment(const Glib::ustring& text) override
92 g_assert_cmpstr(text.c_str(), ==, "a comment");
93 throw std::runtime_error("on_comment runtime exception");
97 void test_on_comment()
99 OnCommentTestParser parser;
100 bool exceptionThrown = false;
103 parser.parse_memory("<root><!--a comment--></root>");
105 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
106 catch(const std::runtime_error& e)
108 catch(const xmlpp::exception& e)
111 exceptionThrown = true;
112 g_assert_cmpstr(e.what(), ==, "on_comment runtime exception");
114 g_assert_true(exceptionThrown);
118 class OnEndDocumentTestParser : public xmlpp::SaxParser
121 void on_end_document() override
123 throw std::runtime_error("on_end_document runtime exception");
127 void test_on_end_document()
129 OnEndDocumentTestParser parser;
130 bool exceptionThrown = false;
133 parser.parse_memory("<root></root>");
135 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
136 catch(const std::runtime_error& e)
138 catch(const xmlpp::exception& e)
141 exceptionThrown = true;
142 g_assert_cmpstr(e.what(), ==, "on_end_document runtime exception");
144 g_assert_true(exceptionThrown);
148 class OnEndElementTestParser : public xmlpp::SaxParser
151 void on_end_element(const Glib::ustring& name) override
153 g_assert_cmpstr(name.c_str(), ==, "a:root");
154 throw std::runtime_error("on_end_element runtime exception");
158 void test_on_end_element()
160 OnEndElementTestParser parser;
161 bool exceptionThrown = false;
164 parser.parse_memory("<a:root xmlns:a=\"urn:test\"></a:root>");
166 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
167 catch(const std::runtime_error& e)
169 catch(const xmlpp::exception& e)
172 exceptionThrown = true;
173 g_assert_cmpstr(e.what(), ==, "on_end_element runtime exception");
175 g_assert_true(exceptionThrown);
179 class OnEntityDeclarationTestParser : public xmlpp::SaxParser
182 void on_entity_declaration(const Glib::ustring& name, xmlpp::XmlEntityType /* type */,
183 const Glib::ustring& /* publicId */, const Glib::ustring& /* systemId */,
184 const Glib::ustring& content) override
186 g_assert_cmpstr(name.c_str(), ==, "number");
187 g_assert_cmpstr(content.c_str(), ==, "42");
188 throw std::runtime_error("on_entity_declaration runtime exception");
192 void test_on_entity_declaration()
194 OnEntityDeclarationTestParser parser;
195 bool exceptionThrown = false;
198 parser.parse_memory("<!DOCTYPE MyDocument [<!ENTITY number \"42\">]><root></root>");
200 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
201 catch(const std::runtime_error& e)
203 catch(const xmlpp::exception& e)
206 exceptionThrown = true;
207 g_assert_cmpstr(e.what(), ==, "on_entity_declaration runtime exception");
209 g_assert_true(exceptionThrown);
213 class OnErrorTestParser : public xmlpp::SaxParser
216 void on_error(const Glib::ustring& /* text */) override
218 throw std::runtime_error("on_error runtime exception");
224 OnErrorTestParser parser;
225 bool exceptionThrown = false;
228 parser.parse_memory("<root>&unknown;</root>");
230 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
231 catch(const std::runtime_error& e)
233 catch(const xmlpp::exception& e)
236 exceptionThrown = true;
237 g_assert_cmpstr(e.what(), ==, "on_error runtime exception");
239 g_assert_true(exceptionThrown);
243 class OnGetEntityTestParser : public xmlpp::SaxParser
246 OnGetEntityTestParser()
247 : xmlpp::SaxParser(true)
251 _xmlEntity* on_get_entity(const Glib::ustring& name) override
253 g_assert_cmpstr(name.c_str(), ==, "number");
254 throw std::runtime_error("on_get_entity runtime exception");
258 void test_on_get_entity()
260 OnGetEntityTestParser parser;
261 bool exceptionThrown = false;
264 parser.parse_memory("<!DOCTYPE MyDocument [<!ENTITY number \"42\">]><root>&number;</root>");
266 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
267 catch(const std::runtime_error& e)
269 catch(const xmlpp::exception& e)
272 exceptionThrown = true;
273 g_assert_cmpstr(e.what(), ==, "on_get_entity runtime exception");
275 g_assert_true(exceptionThrown);
279 class OnStartDocumentTestParser : public xmlpp::SaxParser
282 void on_start_document() override
284 throw std::runtime_error("on_start_document runtime exception");
288 void test_on_start_document()
290 OnStartDocumentTestParser parser;
291 bool exceptionThrown = false;
294 parser.parse_memory("<root></root>");
296 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
297 catch(const std::runtime_error& e)
299 catch(const xmlpp::exception& e)
302 exceptionThrown = true;
303 g_assert_cmpstr(e.what(), ==, "on_start_document runtime exception");
305 g_assert_true(exceptionThrown);
309 class OnStartElementTestParser : public xmlpp::SaxParser
312 void on_start_element(const Glib::ustring& name, const xmlpp::SaxParser::AttributeList& attributes) override
314 g_assert_cmpstr(name.c_str(), ==, "b:root");
315 g_assert_cmpint(attributes.size(), ==, 2);
316 throw std::runtime_error("on_start_element runtime exception");
320 void test_on_start_element()
322 OnStartElementTestParser parser;
323 bool exceptionThrown = false;
326 parser.parse_memory("<b:root xmlns:b=\"urn:test\" someattr=\"test\"></b:root>");
328 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
329 catch(const std::runtime_error& e)
331 catch(const xmlpp::exception& e)
334 exceptionThrown = true;
335 g_assert_cmpstr(e.what(), ==, "on_start_element runtime exception");
337 g_assert_true(exceptionThrown);
345 test_on_cdata_block();
346 test_on_characters();
348 test_on_end_document();
349 test_on_end_element();
350 test_on_entity_declaration();
352 // TODO test on_fatal_error()
353 // This is not currently possible because the fatalError handler is never called;
354 // error is called for all errors.
355 // http://xmlsoft.org/html/libxml-parser.html#fatalErrorSAXFunc
356 test_on_get_entity();
357 // TODO test on_internal_subset()
358 test_on_start_document();
359 test_on_start_element();
360 // TODO test on_warning(), on_validity_error(), on_validity_warning()