123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- // file : tests/parser/driver.cxx
- // copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
- // license : MIT; see accompanying LICENSE file
- #ifdef NDEBUG
- # error tests require enabled assert(); un-define the NDEBUG macro
- #endif
- #include <string>
- #include <vector>
- #include <cassert>
- #include <iostream>
- #include <sstream>
- #include <xml/parser>
- using namespace std;
- 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&)
- {
- // 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&)
- {
- }
- // Test the next_expect() functionality.
- //
- {
- istringstream is ("<root/>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root");
- p.next_expect (parser::end_element);
- }
- try
- {
- istringstream is ("<root/>");
- parser p (is, "test");
- p.next_expect (parser::end_element);
- assert (false);
- }
- catch (const xml::exception&)
- {
- // cerr << e.what () << endl;
- }
- try
- {
- istringstream is ("<root/>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root1");
- assert (false);
- }
- catch (const xml::exception&)
- {
- // cerr << e.what () << endl;
- }
- // Test next_expect() with content setting.
- //
- {
- istringstream is ("<root> </root>");
- parser p (is, "empty");
- p.next_expect (parser::start_element, "root", content::empty);
- p.next_expect (parser::end_element);
- p.next_expect (parser::eof);
- }
- // Test namespace declarations.
- //
- {
- // Followup end element event that should be precedeeded by end
- // namespace declaration.
- //
- istringstream is ("<root xmlns:a='a'/>");
- parser p (is,
- "test",
- parser::receive_default |
- parser::receive_namespace_decls);
- p.next_expect (parser::start_element, "root");
- p.next_expect (parser::start_namespace_decl);
- p.next_expect (parser::end_namespace_decl);
- p.next_expect (parser::end_element);
- }
- // Test value extraction.
- //
- {
- istringstream is ("<root>123</root>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root");
- p.next_expect (parser::characters);
- assert (p.value<int> () == 123);
- p.next_expect (parser::end_element);
- }
- // Test attribute maps.
- //
- {
- istringstream is ("<root a='a' b='b' d='123' t='true'/>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root");
- assert (p.attribute ("a") == "a");
- assert (p.attribute ("b", "B") == "b");
- assert (p.attribute ("c", "C") == "C");
- assert (p.attribute<int> ("d") == 123);
- assert (p.attribute<bool> ("t") == true);
- assert (p.attribute ("f", false) == false);
- p.next_expect (parser::end_element);
- }
- {
- istringstream is ("<root a='a'><nested a='A'><inner/></nested></root>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root");
- assert (p.attribute ("a") == "a");
- assert (p.peek () == parser::start_element && p.name () == "nested");
- assert (p.attribute ("a") == "a");
- p.next_expect (parser::start_element, "nested");
- assert (p.attribute ("a") == "A");
- p.next_expect (parser::start_element, "inner");
- assert (p.attribute ("a", "") == "");
- p.next_expect (parser::end_element);
- assert (p.attribute ("a") == "A");
- assert (p.peek () == parser::end_element);
- assert (p.attribute ("a") == "A"); // Still valid.
- p.next_expect (parser::end_element);
- assert (p.attribute ("a") == "a");
- p.next_expect (parser::end_element);
- assert (p.attribute ("a", "") == "");
- }
- try
- {
- istringstream is ("<root a='a' b='b'/>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root");
- assert (p.attribute ("a") == "a");
- p.next_expect (parser::end_element);
- assert (false);
- }
- catch (const xml::exception&)
- {
- // cerr << e.what () << endl;
- }
- try
- {
- istringstream is ("<root a='abc'/>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root");
- p.attribute<int> ("a");
- assert (false);
- }
- catch (const xml::exception&)
- {
- // cerr << e.what () << endl;
- }
- // Test peeking and getting the current event.
- //
- {
- istringstream is ("<root x='x'>x<nested/></root>");
- parser p (is, "peek",
- parser::receive_default | parser::receive_attributes_event);
- assert (p.event () == parser::eof);
- assert (p.peek () == parser::start_element);
- assert (p.next () == parser::start_element);
- assert (p.event () == parser::start_element);
- assert (p.peek () == parser::start_attribute);
- assert (p.event () == parser::start_attribute);
- assert (p.next () == parser::start_attribute);
- assert (p.peek () == parser::characters && p.value () == "x");
- assert (p.next () == parser::characters && p.value () == "x");
- assert (p.event () == parser::characters && p.value () == "x");
- assert (p.peek () == parser::end_attribute);
- assert (p.event () == parser::end_attribute);
- assert (p.next () == parser::end_attribute);
- assert (p.peek () == parser::characters && p.value () == "x");
- assert (p.next () == parser::characters && p.value () == "x");
- assert (p.event () == parser::characters && p.value () == "x");
- assert (p.peek () == parser::start_element);
- assert (p.next () == parser::start_element);
- assert (p.event () == parser::start_element);
- assert (p.peek () == parser::end_element);
- assert (p.next () == parser::end_element);
- assert (p.event () == parser::end_element);
- assert (p.peek () == parser::end_element);
- assert (p.next () == parser::end_element);
- assert (p.event () == parser::end_element);
- assert (p.peek () == parser::eof);
- assert (p.next () == parser::eof);
- assert (p.event () == parser::eof);
- }
- // Test content processing.
- //
- // empty
- //
- {
- istringstream is ("<root x=' x '> \n\t </root>");
- parser p (is, "empty",
- parser::receive_default | parser::receive_attributes_event);
- assert (p.next () == parser::start_element);
- p.content (content::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 (content::empty);
- p.next ();
- assert (false);
- }
- catch (const xml::exception&)
- {
- // cerr << e.what () << endl;
- }
- // simple
- //
- {
- istringstream is ("<root> X </root>");
- parser p (is, "simple");
- assert (p.next () == parser::start_element);
- p.content (content::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 (content::simple);
- assert (p.next () == parser::characters && p.value () == " ? ");
- p.next ();
- assert (false);
- }
- catch (const xml::exception&)
- {
- // cerr << e.what () << endl;
- }
- {
- // Test content accumulation in simple content.
- //
- istringstream is ("<root xmlns:a='a'>123</root>");
- parser p (is,
- "simple",
- parser::receive_default |
- parser::receive_namespace_decls);
- assert (p.next () == parser::start_element);
- p.next_expect (parser::start_namespace_decl);
- p.content (content::simple);
- assert (p.next () == parser::characters && p.value () == "123");
- p.next_expect (parser::end_namespace_decl);
- assert (p.next () == parser::end_element);
- assert (p.next () == parser::eof);
- }
- try
- {
- // Test error handling in accumulation in simple content.
- //
- istringstream is ("<root xmlns:a='a'>12<nested/>3</root>");
- parser p (is,
- "simple",
- parser::receive_default |
- parser::receive_namespace_decls);
- assert (p.next () == parser::start_element);
- p.next_expect (parser::start_namespace_decl);
- p.content (content::simple);
- p.next ();
- assert (false);
- }
- catch (const xml::exception&)
- {
- // 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",
- parser::receive_default | parser::receive_attributes_event);
- assert (p.next () == parser::start_element); // root
- p.content (content::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 (content::complex);
- assert (p.next () == parser::start_element); // inner
- p.content (content::empty);
- assert (p.next () == parser::end_element); // inner
- assert (p.next () == parser::start_element); // inner
- p.content (content::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 (content::complex);
- assert (p.next () == parser::start_element);
- assert (p.next () == parser::end_element);
- p.next ();
- assert (false);
- }
- catch (const xml::exception&)
- {
- // cerr << e.what () << endl;
- }
- // Test element with simple content helpers.
- //
- {
- istringstream is ("<root>"
- " <nested>X</nested>"
- " <nested/>"
- " <nested>123</nested>"
- " <nested>Y</nested>"
- " <t:nested xmlns:t='test'>Z</t:nested>"
- " <nested>234</nested>"
- " <t:nested xmlns:t='test'>345</t:nested>"
- " <nested>A</nested>"
- " <t:nested xmlns:t='test'>B</t:nested>"
- " <nested1>A</nested1>"
- " <t:nested1 xmlns:t='test'>B</t:nested1>"
- " <nested>1</nested>"
- " <t:nested xmlns:t='test'>2</t:nested>"
- " <nested1>1</nested1>"
- " <t:nested1 xmlns:t='test'>2</t:nested1>"
- "</root>");
- parser p (is, "element");
- p.next_expect (parser::start_element, "root", content::complex);
- p.next_expect (parser::start_element, "nested");
- assert (p.element () == "X");
- p.next_expect (parser::start_element, "nested");
- assert (p.element () == "");
- p.next_expect (parser::start_element, "nested");
- assert (p.element<unsigned int> () == 123);
- assert (p.element ("nested") == "Y");
- assert (p.element (qname ("test", "nested")) == "Z");
- assert (p.element<unsigned int> ("nested") == 234);
- assert (p.element<unsigned int> (qname ("test", "nested")) == 345);
- assert (p.element ("nested", "a") == "A");
- assert (p.element (qname ("test", "nested"), "b") == "B");
- assert (p.element ("nested", "a") == "a" &&
- p.element ("nested1") == "A");
- assert (p.element (qname ("test", "nested"), "b") == "b" &&
- p.element (qname ("test", "nested1")) == "B");
- assert (p.element<unsigned int> ("nested", 10) == 1);
- assert (p.element<unsigned int> (qname ("test", "nested"), 20) == 2);
- assert (p.element<unsigned int> ("nested", 10) == 10 &&
- p.element<unsigned int> ("nested1") == 1);
- assert (p.element<unsigned int> (qname ("test", "nested"), 20) == 20 &&
- p.element<unsigned int> (qname ("test", "nested1")) == 2);
- p.next_expect (parser::end_element);
- }
- // Test the iterator interface.
- //
- {
- istringstream is ("<root><nested>X</nested></root>");
- parser p (is, "iterator");
- vector<parser::event_type> v;
- for (parser::iterator i (p.begin ()); i != p.end (); ++i)
- v.push_back (*i);
- //for (parser::event_type e: p)
- // v.push_back (e);
- assert (v.size () == 5);
- assert (v[0] == parser::start_element);
- assert (v[1] == parser::start_element);
- assert (v[2] == parser::characters);
- assert (v[3] == parser::end_element);
- assert (v[4] == parser::end_element);
- }
- // Test space extraction into the std::string value.
- //
- {
- istringstream is ("<root a=' a '> b </root>");
- parser p (is, "test");
- p.next_expect (parser::start_element, "root");
- assert (p.attribute<std::string> ("a") == " a ");
- p.next_expect (parser::characters);
- assert (p.value<std::string> () == " b ");
- p.next_expect (parser::end_element);
- }
- }
|