diff options
Diffstat (limited to 'tests/xml/parser/driver.cxx')
-rw-r--r-- | tests/xml/parser/driver.cxx | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/tests/xml/parser/driver.cxx b/tests/xml/parser/driver.cxx new file mode 100644 index 0000000..05c0f35 --- /dev/null +++ b/tests/xml/parser/driver.cxx @@ -0,0 +1,164 @@ +// file : tests/xml/parser/driver.cxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include <string> +#include <cassert> +#include <iostream> +#include <sstream> + +#include <cutl/xml/parser.hxx> + +using namespace std; +namespace xml = cutl::xml; +using namespace xml; + +int +main () +{ + // Test error handling. + // + try + { + istringstream is ("<root><nested>X</nasted></root>"); + parser p (is, "test"); + + assert (p.next () == parser::start_element); + assert (p.next () == parser::start_element); + assert (p.next () == parser::characters && p.value () == "X"); + p.next (); + assert (false); + } + catch (const xml::exception& e) + { + // cerr << e.what () << endl; + } + + try + { + istringstream is ("<root/>"); + is.exceptions (ios_base::badbit | ios_base::failbit); + parser p (is, "test"); + + is.setstate (ios_base::badbit); + p.next (); + assert (false); + } + catch (const ios_base::failure& e) + { + } + + // Test content processing. + // + + // empty + // + { + istringstream is ("<root x=' x '> \n\t </root>"); + parser p (is, "empty"); + + assert (p.next () == parser::start_element); + p.content (parser::empty); + assert (p.next () == parser::start_attribute); + assert (p.next () == parser::characters && p.value () == " x "); + assert (p.next () == parser::end_attribute); + assert (p.next () == parser::end_element); + assert (p.next () == parser::eof); + } + + try + { + istringstream is ("<root> \n & X \t </root>"); + parser p (is, "empty"); + + assert (p.next () == parser::start_element); + p.content (parser::empty); + p.next (); + assert (false); + } + catch (const xml::exception& e) + { + // cerr << e.what () << endl; + } + + // simple + // + { + istringstream is ("<root> X </root>"); + parser p (is, "simple"); + + assert (p.next () == parser::start_element); + p.content (parser::simple); + assert (p.next () == parser::characters && p.value () == " X "); + assert (p.next () == parser::end_element); + assert (p.next () == parser::eof); + } + + try + { + istringstream is ("<root> ? <nested/></root>"); + parser p (is, "simple"); + + assert (p.next () == parser::start_element); + p.content (parser::simple); + assert (p.next () == parser::characters && p.value () == " ? "); + p.next (); + assert (false); + } + catch (const xml::exception& e) + { + // cerr << e.what () << endl; + } + + // complex + // + { + istringstream is ("<root x=' x '>\n" + " <nested>\n" + " <inner/>\n" + " <inner> X </inner>\n" + " </nested>\n" + "</root>\n"); + parser p (is, "complex"); + + assert (p.next () == parser::start_element); // root + p.content (parser::complex); + + assert (p.next () == parser::start_attribute); + assert (p.next () == parser::characters && p.value () == " x "); + assert (p.next () == parser::end_attribute); + + assert (p.next () == parser::start_element); // nested + p.content (parser::complex); + + assert (p.next () == parser::start_element); // inner + p.content (parser::empty); + assert (p.next () == parser::end_element); // inner + + assert (p.next () == parser::start_element); // inner + p.content (parser::simple); + assert (p.next () == parser::characters && p.value () == " X "); + assert (p.next () == parser::end_element); // inner + + assert (p.next () == parser::end_element); // nested + assert (p.next () == parser::end_element); // root + assert (p.next () == parser::eof); + } + + try + { + istringstream is ("<root> \n<n/> X <n> X </n> </root>"); + parser p (is, "complex"); + + assert (p.next () == parser::start_element); + p.content (parser::complex); + assert (p.next () == parser::start_element); + assert (p.next () == parser::end_element); + p.next (); + assert (false); + } + catch (const xml::exception& e) + { + // cerr << e.what () << endl; + } +} |