diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-03-08 17:41:00 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-03-08 17:41:00 +0200 |
commit | 2ac86dfdafb9bf4d4e9252260a431755b56d8ebe (patch) | |
tree | 30a19391906b913b8a0773c7fe5a4b046c0703c4 /tests/xml/parser | |
parent | df1e7318da3320cfcfaee7f3c1f00a9eee7c9d25 (diff) |
Add support for XML parsing and serialization
Diffstat (limited to 'tests/xml/parser')
-rw-r--r-- | tests/xml/parser/driver.cxx | 164 | ||||
-rw-r--r-- | tests/xml/parser/makefile | 69 |
2 files changed, 233 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; + } +} diff --git a/tests/xml/parser/makefile b/tests/xml/parser/makefile new file mode 100644 index 0000000..f654976 --- /dev/null +++ b/tests/xml/parser/makefile @@ -0,0 +1,69 @@ +# file : tests/xml/parser/makefile +# copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +# license : MIT; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make + +cxx_tun := driver.cxx + +# +# +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +cutl.l := $(out_root)/cutl/cutl.l +cutl.l.cpp-options := $(out_root)/cutl/cutl.l.cpp-options + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(cxx_obj) $(cutl.l) +$(cxx_obj) $(cxx_od): $(cutl.l.cpp-options) + + +$(call include-dep,$(cxx_od)) + + +# Alias for default target. +# +$(out_base)/: $(driver) + + +# Test. +# +$(test): $(driver) + $(call message,test $<,$<) + + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) + + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + + +# How to. +# +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) + +# Dependencies. +# +$(call import,$(src_root)/cutl/makefile) |