diff options
Diffstat (limited to 'tests/cxx/parser/error-handling')
-rw-r--r-- | tests/cxx/parser/error-handling/codes/driver.cxx | 276 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/codes/makefile | 74 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/codes/output | 15 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/codes/test.map | 4 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/codes/test.xml | 9 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/codes/test.xsd | 19 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/exceptions/driver.cxx | 256 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/exceptions/makefile | 75 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/exceptions/output | 15 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/exceptions/test.map | 4 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/exceptions/test.xml | 9 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/exceptions/test.xsd | 19 | ||||
-rw-r--r-- | tests/cxx/parser/error-handling/makefile | 24 |
13 files changed, 799 insertions, 0 deletions
diff --git a/tests/cxx/parser/error-handling/codes/driver.cxx b/tests/cxx/parser/error-handling/codes/driver.cxx new file mode 100644 index 0000000..8b3c693 --- /dev/null +++ b/tests/cxx/parser/error-handling/codes/driver.cxx @@ -0,0 +1,276 @@ +// file : tests/cxx/parser/error-handling/codes/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +// Test error reporting with error codes. +// + +#include <string> +#include <cassert> +#include <sstream> +#include <fstream> +#include <iostream> + +#include "test-pskel.hxx" + +using namespace std; +using namespace test; + +struct person_pimpl: person_pskel +{ + person_pimpl (unsigned long i) + : i_ (i) + { + } + + virtual void + pre () + { + if (i_ == 4) + _app_error (4); + } + +#ifdef XSDE_STL + virtual void + id (string const&) + { + if (i_ == 5) + _app_error (5); + } +#else + virtual void + id (char* s) + { + delete[] s; + + if (i_ == 5) + _app_error (5); + } +#endif + + virtual void + age (int age) + { + if (i_ == 6) + _app_error (6); + + age_ = age; + } + + virtual int + post_person () + { + if (i_ == 7) + { + _app_error (7); + return 0; + } + + return age_; + } + +private: + int age_; + unsigned long i_; + +}; + + +struct type_pimpl: type_pskel +{ + type_pimpl (unsigned long i) + : i_ (i) + { + } + + virtual void + pre () + { + if (i_ == 1) + _app_error (1); + } + + virtual void + person (int) + { + if (i_ == 2) + _app_error (2); + } + + virtual void + post_type () + { + if (i_ == 3) + _app_error (3); + } + +private: + unsigned long i_; +}; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " test.xml" << endl; + return 1; + } + + // 0: xml error + // + // 1: app error from top-level pre + // 2: app error from top-level callback + // 3: app error from top-level post + // 4: app error from nested pre + // 5: app error from nested attribute callback + // 6: app error from nested element callback + // 7: app error from nested post + // + // 8: schema error (root element mismatch) + // 9: schema error (missing required element) + // 10: schema error (missing required attribute) + // 11: schema error (extraneous element) + // 12: schema error (extraneous attribute) + // 13: schema error (invalid datatype value) + // + // 14: io error (no such file) + // + // + for (unsigned long i (0); i < 15; ++i) + { + xml_schema::int_pimpl int_p; + xml_schema::string_pimpl string_p; + person_pimpl person_p (i); + type_pimpl type_p (i); + + person_p.parsers (string_p, int_p); + type_p.parsers (person_p); + + xml_schema::document_pimpl doc_p (type_p, "test", "root"); + + // + // + typedef xml_schema::parser_error error; + + type_p.pre (); + + if (error e = type_p._error ()) + { + assert (e.type () == error::app); + cout << "app: " << e.app_code () << endl; + continue; + } + + + if (i == 0) + { + istringstream is ("<t:root xmlns:t='test'>\n</t:rot>"); + doc_p.parse (is); + } + else if (i == 8) + { + istringstream is ("<t:rot xmlns:t='test'>\n</t:rot>"); + doc_p.parse (is); + } + else if (i == 9) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a'/>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 10) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person><age>64</age></person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 11) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a'>\n" + "<age>64</age>\n" + "<foo/>\n" + "</person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 12) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a' foo='f'>\n" + "<age>64</age>\n" + "</person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 13) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a'>\n" + "<age>a64</age>\n" + "</person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 14) + { + doc_p.parse ("no-such-file.xml"); + } + else + doc_p.parse (argv[1]); + + + if (error e = doc_p._error ()) + { + switch (e.type ()) + { + case error::sys: + { + cout << "sys: " << e.sys_code () << ": " << e.sys_text () + << endl; + break; + } + case error::xml: + { + cout << "xml:" << e.line () << ":" << e.column () << ": " + << e.xml_code () << ": " << e.xml_text () << endl; + break; + } +#ifdef XSDE_PARSER_VALIDATION + case error::schema: + { + cout << "schema:" << e.line () << ":" << e.column () << ": " + << e.schema_code () << ": " << e.schema_text () << endl; + break; + } +#endif + case error::app: + { + cout << "app:" << e.line () << ":" << e.column () << ": " + << e.app_code () << endl; + break; + } + } + + continue; + } + + type_p.post_type (); + + if (error e = type_p._error ()) + { + assert (e.type () == error::app); + cout << "app: " << e.app_code () << endl; + continue; + } + } +} diff --git a/tests/cxx/parser/error-handling/codes/makefile b/tests/cxx/parser/error-handling/codes/makefile new file mode 100644 index 0000000..ca9ba5c --- /dev/null +++ b/tests/cxx/parser/error-handling/codes/makefile @@ -0,0 +1,74 @@ +# file : tests/cxx/parser/error-handling/codes/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../../build/bootstrap.make + +xsd := test.xsd +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-pskel.o)) +dep := $(obj:.o=.o.d) + +xsde.l := $(out_root)/libxsde/xsde/xsde.l +xsde.l.cpp-options := $(out_root)/libxsde/xsde/xsde.l.cpp-options + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + + +# Build. +# +$(driver): $(obj) $(xsde.l) + +$(obj) $(dep): $(xsde.l.cpp-options) + +skel := $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.ixx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) + +$(skel): xsde := $(out_root)/xsde/xsde +$(skel): xsde_options += --no-exceptions --type-map $(src_base)/test.map + +$(skel): $(out_root)/xsde/xsde $(src_base)/test.map + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Test. +# +.PHONY: $(test) + +$(test): driver := $(driver) +$(test): $(driver) $(src_base)/test.xml $(src_base)/output + $(call message,test $$1,$$1 $(src_base)/test.xml | diff -u $(src_base)/output -,$(driver)) + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=-pskel.cxx.xsd.clean)) + + +# 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) +$(call include,$(scf_root)/xsde/parser/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsde/makefile) +$(call import,$(src_root)/libxsde/xsde/makefile) diff --git a/tests/cxx/parser/error-handling/codes/output b/tests/cxx/parser/error-handling/codes/output new file mode 100644 index 0000000..bdf6520 --- /dev/null +++ b/tests/cxx/parser/error-handling/codes/output @@ -0,0 +1,15 @@ +xml:2:2: 7: mismatched tag +app: 1 +app:7:11: 2 +app: 3 +app:5:19: 4 +app:5:19: 5 +app:6:17: 6 +app:7:11: 7 +schema:1:22: 4: unexpected element encountered +schema:2:16: 3: expected element not encountered +schema:2:30: 1: expected attribute not encountered +schema:4:6: 4: unexpected element encountered +schema:2:23: 2: unexpected attribute encountered +schema:3:14: 11: invalid int value +sys: 2: open failed diff --git a/tests/cxx/parser/error-handling/codes/test.map b/tests/cxx/parser/error-handling/codes/test.map new file mode 100644 index 0000000..5da7e3d --- /dev/null +++ b/tests/cxx/parser/error-handling/codes/test.map @@ -0,0 +1,4 @@ +namespace test +{ + person int int; +} diff --git a/tests/cxx/parser/error-handling/codes/test.xml b/tests/cxx/parser/error-handling/codes/test.xml new file mode 100644 index 0000000..07def4b --- /dev/null +++ b/tests/cxx/parser/error-handling/codes/test.xml @@ -0,0 +1,9 @@ +<t:root xmlns:t="test" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="test test.xsd"> + + <person id="old"> + <age>67</age> + </person> + +</t:root> diff --git a/tests/cxx/parser/error-handling/codes/test.xsd b/tests/cxx/parser/error-handling/codes/test.xsd new file mode 100644 index 0000000..6850342 --- /dev/null +++ b/tests/cxx/parser/error-handling/codes/test.xsd @@ -0,0 +1,19 @@ +<?xml version="1.0"?> +<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:t="test" targetNamespace="test"> + + <complexType name="person"> + <sequence> + <element name="age" type="int"/> + </sequence> + <attribute name="id" type="string" use="required"/> + </complexType> + + <complexType name="type"> + <sequence> + <element name="person" type="t:person" maxOccurs="unbounded"/> + </sequence> + </complexType> + + <element name="root" type="t:type"/> + +</schema> diff --git a/tests/cxx/parser/error-handling/exceptions/driver.cxx b/tests/cxx/parser/error-handling/exceptions/driver.cxx new file mode 100644 index 0000000..0ad6b80 --- /dev/null +++ b/tests/cxx/parser/error-handling/exceptions/driver.cxx @@ -0,0 +1,256 @@ +// file : tests/cxx/parser/error-handling/exceptions/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +// Test error reporting with exceptions. +// + +#include <string> +#include <sstream> +#include <iostream> + +#include "test-pskel.hxx" + +using namespace std; +using namespace test; + +struct app +{ + app (unsigned long code) + : code_ (code) + { + } + + unsigned long + code () const + { + return code_; + } + +private: + unsigned long code_; +}; + +struct person_pimpl: person_pskel +{ + person_pimpl (unsigned long i) + : i_ (i) + { + } + + virtual void + pre () + { + if (i_ == 4) + throw app (4); + } + +#ifdef XSDE_STL + virtual void + id (string const&) + { + if (i_ == 5) + throw app (5); + } +#else + virtual void + id (char* s) + { + delete[] s; + + if (i_ == 5) + throw app (5); + } +#endif + + virtual void + age (int age) + { + if (i_ == 6) + throw app (6); + + age_ = age; + } + + virtual int + post_person () + { + if (i_ == 7) + throw app (7); + + return age_; + } + +private: + int age_; + unsigned long i_; +}; + + +struct type_pimpl: type_pskel +{ + type_pimpl (unsigned long i) + : i_ (i) + { + } + + virtual void + pre () + { + if (i_ == 1) + throw app (1); + } + + virtual void + person (int) + { + if (i_ == 2) + throw app (2); + } + + virtual void + post_type () + { + if (i_ == 3) + throw app (3); + } + +private: + unsigned long i_; +}; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " test.xml" << endl; + return 1; + } + + // 0: xml error + // + // 1: app error from top-level pre + // 2: app error from top-level callback + // 3: app error from top-level post + // 4: app error from nested pre + // 5: app error from nested attribute callback + // 6: app error from nested element callback + // 7: app error from nested post + // + // 8: schema error (root element mismatch) + // 9: schema error (missing required element) + // 10: schema error (missing required attribute) + // 11: schema error (extraneous element) + // 12: schema error (extraneous attribute) + // 13: schema error (invalid datatype value) + // + // 14: io error (no such file) + // + // + for (unsigned long i (0); i < 15; ++i) + { + try + { + xml_schema::int_pimpl int_p; + xml_schema::string_pimpl string_p; + person_pimpl person_p (i); + type_pimpl type_p (i); + + person_p.parsers (string_p, int_p); + type_p.parsers (person_p); + xml_schema::document_pimpl doc_p (type_p, "test", "root"); + + // + // + type_p.pre (); + + if (i == 0) + { + istringstream is ("<t:root xmlns:t='test'>\n</t:rot>"); + doc_p.parse (is); + } + else if (i == 8) + { + istringstream is ("<t:rot xmlns:t='test'>\n</t:rot>"); + doc_p.parse (is); + } + else if (i == 9) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a'/>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 10) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person><age>64</age></person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 11) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a'>\n" + "<age>64</age>\n" + "<foo/>\n" + "</person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 12) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a' foo='f'>\n" + "<age>64</age>\n" + "</person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 13) + { + istringstream is ( + "<t:root xmlns:t='test'>\n" + "<person id='a'>\n" + "<age>a64</age>\n" + "</person>\n" + "</t:root>"); + doc_p.parse (is); + } + else if (i == 14) + { + doc_p.parse ("no-such-file.xml"); + } + else + doc_p.parse (argv[1]); + + type_p.post_type (); + } + catch (ios_base::failure const&) + { + cout << "io: io error" << endl; + } + catch (xml_schema::parser_xml const& e) + { + cout << "xml:" << e.line () << ":" << e.column () << ": " + << e.code () << ": " << e.text () << endl; + } +#ifdef XSDE_PARSER_VALIDATION + catch (xml_schema::parser_schema const& e) + { + cout << "schema:" << e.line () << ":" << e.column () << ": " + << e.code () << ": " << e.text () << endl; + } +#endif + catch (app const& e) + { + cout << "app: " << e.code () << endl; + } + } +} diff --git a/tests/cxx/parser/error-handling/exceptions/makefile b/tests/cxx/parser/error-handling/exceptions/makefile new file mode 100644 index 0000000..8acb98a --- /dev/null +++ b/tests/cxx/parser/error-handling/exceptions/makefile @@ -0,0 +1,75 @@ +# file : tests/cxx/parser/error-handling/exceptions/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../../build/bootstrap.make + +xsd := test.xsd +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-pskel.o)) +dep := $(obj:.o=.o.d) + +xsde.l := $(out_root)/libxsde/xsde/xsde.l +xsde.l.cpp-options := $(out_root)/libxsde/xsde/xsde.l.cpp-options + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + + +# Build. +# +$(driver): $(obj) $(xsde.l) + +$(obj) $(dep): $(xsde.l.cpp-options) + +skel := $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.ixx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) + +$(skel): xsde := $(out_root)/xsde/xsde +$(skel): xsde_options += --type-map $(src_base)/test.map + +$(skel): $(out_root)/xsde/xsde $(src_base)/test.map + + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Test. +# +.PHONY: $(test) + +$(test): driver := $(driver) +$(test): $(driver) $(src_base)/test.xml $(src_base)/output + $(call message,test $$1,$$1 $(src_base)/test.xml | diff -u $(src_base)/output -,$(driver)) + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=-pskel.cxx.xsd.clean)) + + +# 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) +$(call include,$(scf_root)/xsde/parser/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsde/makefile) +$(call import,$(src_root)/libxsde/xsde/makefile) diff --git a/tests/cxx/parser/error-handling/exceptions/output b/tests/cxx/parser/error-handling/exceptions/output new file mode 100644 index 0000000..b8046aa --- /dev/null +++ b/tests/cxx/parser/error-handling/exceptions/output @@ -0,0 +1,15 @@ +xml:2:2: 7: mismatched tag +app: 1 +app: 2 +app: 3 +app: 4 +app: 5 +app: 6 +app: 7 +schema:1:22: 4: unexpected element encountered +schema:2:16: 3: expected element not encountered +schema:2:30: 1: expected attribute not encountered +schema:4:6: 4: unexpected element encountered +schema:2:23: 2: unexpected attribute encountered +schema:3:14: 11: invalid int value +io: io error diff --git a/tests/cxx/parser/error-handling/exceptions/test.map b/tests/cxx/parser/error-handling/exceptions/test.map new file mode 100644 index 0000000..5da7e3d --- /dev/null +++ b/tests/cxx/parser/error-handling/exceptions/test.map @@ -0,0 +1,4 @@ +namespace test +{ + person int int; +} diff --git a/tests/cxx/parser/error-handling/exceptions/test.xml b/tests/cxx/parser/error-handling/exceptions/test.xml new file mode 100644 index 0000000..07def4b --- /dev/null +++ b/tests/cxx/parser/error-handling/exceptions/test.xml @@ -0,0 +1,9 @@ +<t:root xmlns:t="test" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="test test.xsd"> + + <person id="old"> + <age>67</age> + </person> + +</t:root> diff --git a/tests/cxx/parser/error-handling/exceptions/test.xsd b/tests/cxx/parser/error-handling/exceptions/test.xsd new file mode 100644 index 0000000..6850342 --- /dev/null +++ b/tests/cxx/parser/error-handling/exceptions/test.xsd @@ -0,0 +1,19 @@ +<?xml version="1.0"?> +<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:t="test" targetNamespace="test"> + + <complexType name="person"> + <sequence> + <element name="age" type="int"/> + </sequence> + <attribute name="id" type="string" use="required"/> + </complexType> + + <complexType name="type"> + <sequence> + <element name="person" type="t:person" maxOccurs="unbounded"/> + </sequence> + </complexType> + + <element name="root" type="t:type"/> + +</schema> diff --git a/tests/cxx/parser/error-handling/makefile b/tests/cxx/parser/error-handling/makefile new file mode 100644 index 0000000..e183785 --- /dev/null +++ b/tests/cxx/parser/error-handling/makefile @@ -0,0 +1,24 @@ +# file : tests/cxx/parser/error-handling/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make + +ifeq ($(xsde_exceptions),y) +tests := exceptions +else +tests := codes +endif + +default := $(out_base)/ +test := $(out_base)/.test +clean := $(out_base)/.clean + +.PHONY: $(default) $(test) $(clean) + +$(default): $(addprefix $(out_base)/,$(addsuffix /,$(tests))) +$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(tests))) +$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(tests))) + +$(foreach t,$(tests),$(call import,$(src_base)/$t/makefile)) |