aboutsummaryrefslogtreecommitdiff
path: root/tests/cxx/serializer/error-handling
diff options
context:
space:
mode:
Diffstat (limited to 'tests/cxx/serializer/error-handling')
-rw-r--r--tests/cxx/serializer/error-handling/codes/driver.cxx327
-rw-r--r--tests/cxx/serializer/error-handling/codes/makefile72
-rw-r--r--tests/cxx/serializer/error-handling/codes/output20
-rw-r--r--tests/cxx/serializer/error-handling/codes/test.xsd22
-rw-r--r--tests/cxx/serializer/error-handling/exceptions/driver.cxx285
-rw-r--r--tests/cxx/serializer/error-handling/exceptions/makefile72
-rw-r--r--tests/cxx/serializer/error-handling/exceptions/output20
-rw-r--r--tests/cxx/serializer/error-handling/exceptions/test.xsd22
-rw-r--r--tests/cxx/serializer/error-handling/makefile24
9 files changed, 864 insertions, 0 deletions
diff --git a/tests/cxx/serializer/error-handling/codes/driver.cxx b/tests/cxx/serializer/error-handling/codes/driver.cxx
new file mode 100644
index 0000000..ba1f6cd
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/codes/driver.cxx
@@ -0,0 +1,327 @@
+// file : tests/cxx/serializer/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 <cassert>
+#include <sstream>
+#include <iostream>
+
+#include "test-sskel.hxx"
+
+using namespace std;
+using namespace test;
+
+// bool fail = true;
+
+struct test_simpl: virtual test_sskel
+{
+ test_simpl (int c)
+ : case_ (c)
+ {
+ }
+
+ virtual void
+ pre ()
+ {
+ if (case_ == 6)
+ _app_error (case_);
+
+ n_ = 0;
+ }
+
+ virtual void
+ _pre ()
+ {
+ if (case_ == 7)
+ _app_error (case_);
+
+ if (case_ == 19)
+ _end_element ();
+ }
+
+ // Attributes.
+ //
+ virtual bool
+ x_present ()
+ {
+ if (case_ == 8)
+ {
+ _app_error (case_);
+ return false;
+ }
+
+ return true;
+ }
+
+ virtual int
+ x ()
+ {
+ if (case_ == 9)
+ _app_error (case_);
+
+ return -1;
+ }
+
+ virtual int
+ y ()
+ {
+ if (case_ == 10)
+ _app_error (case_);
+
+ return -2;
+ }
+
+ // Elements.
+ //
+ virtual int
+ a ()
+ {
+ if (case_ == 11)
+ _app_error (case_);
+
+ return 1;
+ }
+
+ virtual bool
+ b_present ()
+ {
+ if (case_ == 12)
+ {
+ _app_error (case_);
+ return false;
+ }
+
+ return true;
+ }
+
+ virtual int
+ b ()
+ {
+ if (case_ == 13)
+ _app_error (case_);
+
+ return 2;
+ }
+
+ virtual bool
+ c_next ()
+ {
+ if (case_ == 14)
+ {
+ _app_error (case_);
+ return false;
+ }
+
+ return n_++ < 3;
+ }
+
+ virtual int
+ c ()
+ {
+ if (case_ == 15)
+ _app_error (case_);
+
+ return 2 + n_;
+ }
+
+ virtual void
+ _post ()
+ {
+ if (case_ == 16)
+ _app_error (case_);
+ }
+
+ virtual void
+ post ()
+ {
+ if (case_ == 17)
+ _app_error (case_);
+ }
+
+private:
+ int case_;
+ int n_;
+};
+
+struct root_simpl: root_sskel
+{
+ root_simpl (int c)
+ : case_ (c)
+ {
+ }
+
+ virtual void
+ pre ()
+ {
+ if (case_ == 1)
+ _app_error (case_);
+ }
+
+ virtual void
+ _pre ()
+ {
+ if (case_ == 2)
+ _app_error (case_);
+ }
+
+ virtual void
+ test ()
+ {
+ if (case_ == 3)
+ _app_error (case_);
+ }
+
+ virtual void
+ _post ()
+ {
+ if (case_ == 4)
+ _app_error (case_);
+ }
+
+ virtual void
+ post ()
+ {
+ if (case_ == 5)
+ _app_error (case_);
+ }
+
+private:
+ int case_;
+};
+
+struct writer: xml_schema::writer
+{
+ virtual bool
+ write (const char*, size_t)
+ {
+ return false;
+ }
+
+ virtual bool
+ flush ()
+ {
+ return false;
+ }
+};
+
+
+void
+print (xml_schema::serializer_error e)
+{
+ typedef xml_schema::serializer_error error;
+
+ switch (e.type ())
+ {
+ case error::sys:
+ {
+ cout << "sys: " << e.sys_code () << ": " << e.sys_text () << endl;
+ break;
+ }
+ case error::xml:
+ {
+ cout << "xml: " << e.xml_code () << ": " << e.xml_text () << endl;
+ break;
+ }
+#ifdef XSDE_SERIALIZER_VALIDATION
+ case error::schema:
+ {
+ cout << "schema: " << e.schema_code () << ": " << e.schema_text ()
+ << endl;
+ break;
+ }
+#endif
+ case error::app:
+ {
+ cout << "app: " << e.app_code () << endl;
+ break;
+ }
+ default:
+ {
+ cout << "no error" << endl;
+ }
+ }
+}
+
+int
+main ()
+{
+ typedef xml_schema::serializer_error error;
+
+ /*
+ string output ("<g1:root xmlns:g1=\"test\"><test x=\"-1\" y=\"-2\">"
+ "<a>1</a><b>2</b><c>3</c><c>4</c><c>5</c></test></g1:root>");
+ */
+
+ // 1-5 : app errors in the root element serializer
+ // 6-17: app errors in the sub element serializer
+ // 18 : xml error in document serializer
+ // 19 : xml error in sub element serializer
+ // 20 : sys error
+ //
+ for (int i (1); i <= 20; ++i)
+ {
+ xml_schema::int_simpl int_s;
+ test_simpl test_s (i);
+ root_simpl root_s (i);
+
+ test_s.serializers (int_s, int_s, int_s, int_s, int_s);
+ root_s.serializers (test_s);
+
+ xml_schema::document_simpl doc_s (
+ root_s,
+ "test",
+ (i == 18 ? "bad name" : "root"));
+
+ root_s.pre ();
+
+ if (error e = root_s._error ())
+ print (e);
+ else
+ {
+ ostringstream os;
+
+ if (i == 20)
+ {
+ writer w;
+ doc_s.serialize (w);
+ }
+ else
+ doc_s.serialize (os);
+
+ if (error e = doc_s._error ())
+ print (e);
+ else
+ {
+ root_s.post ();
+
+ if (error e = root_s._error ())
+ print (e);
+ }
+ }
+
+ /*
+ {
+ fail = false;
+
+ ostringstream os;
+
+ root_s.pre ();
+ assert (!root_s.error ());
+
+ doc_s.serialize (os);
+ assert (!doc_s._error ());
+
+ root_s.post ();
+ assert (!root_s.error ());
+
+ assert (os.str () == output);
+
+ fail = true;
+ }
+ */
+ }
+}
diff --git a/tests/cxx/serializer/error-handling/codes/makefile b/tests/cxx/serializer/error-handling/codes/makefile
new file mode 100644
index 0000000..bdaca6c
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/codes/makefile
@@ -0,0 +1,72 @@
+# file : tests/cxx/serializer/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=-sskel.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=-sskel.hxx) \
+ $(out_base)/$(xsd:.xsd=-sskel.ixx) \
+ $(out_base)/$(xsd:.xsd=-sskel.cxx)
+
+$(skel): xsde := $(out_root)/xsde/xsde
+$(skel): $(out_root)/xsde/xsde
+
+$(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)/output
+ $(call message,test $$1,$$1 | 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=-sskel.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/serializer/xsd-cxx.make)
+
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsde/makefile)
+$(call import,$(src_root)/libxsde/xsde/makefile)
diff --git a/tests/cxx/serializer/error-handling/codes/output b/tests/cxx/serializer/error-handling/codes/output
new file mode 100644
index 0000000..c01aab9
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/codes/output
@@ -0,0 +1,20 @@
+app: 1
+app: 2
+app: 3
+app: 4
+app: 5
+app: 6
+app: 7
+app: 8
+app: 9
+app: 10
+app: 11
+app: 12
+app: 13
+app: 14
+app: 15
+app: 16
+app: 17
+xml: 3: illegal name
+xml: 8: call out of sequence
+sys: 4: write failed
diff --git a/tests/cxx/serializer/error-handling/codes/test.xsd b/tests/cxx/serializer/error-handling/codes/test.xsd
new file mode 100644
index 0000000..530118c
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/codes/test.xsd
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:t="test" targetNamespace="test">
+
+ <complexType name="test">
+ <sequence>
+ <element name="a" type="int"/>
+ <element name="b" type="int" minOccurs="0"/>
+ <element name="c" type="int" maxOccurs="unbounded"/>
+ </sequence>
+ <attribute name="x" type="int"/>
+ <attribute name="y" type="int" use="required"/>
+ </complexType>
+
+ <complexType name="root">
+ <sequence>
+ <element name="test" type="t:test"/>
+ </sequence>
+ </complexType>
+
+ <element name="root" type="t:root"/>
+
+</schema>
diff --git a/tests/cxx/serializer/error-handling/exceptions/driver.cxx b/tests/cxx/serializer/error-handling/exceptions/driver.cxx
new file mode 100644
index 0000000..3a7437b
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/exceptions/driver.cxx
@@ -0,0 +1,285 @@
+// file : tests/cxx/serializer/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 <cassert>
+#include <sstream>
+#include <iostream>
+
+#include "test-sskel.hxx"
+
+using namespace std;
+using namespace test;
+
+struct app
+{
+ app (int code)
+ : code_ (code)
+ {
+ }
+
+ int
+ code () const
+ {
+ return code_;
+ }
+
+private:
+ int code_;
+};
+
+struct test_simpl: virtual test_sskel
+{
+ test_simpl (int c)
+ : case_ (c)
+ {
+ }
+
+ virtual void
+ pre ()
+ {
+ if (case_ == 6)
+ throw app (case_);
+
+ n_ = 0;
+ }
+
+ virtual void
+ _pre ()
+ {
+ if (case_ == 7)
+ throw app (case_);
+
+ if (case_ == 19)
+ _end_element ();
+ }
+
+ // Attributes.
+ //
+ virtual bool
+ x_present ()
+ {
+ if (case_ == 8)
+ {
+ throw app (case_);
+ return false;
+ }
+
+ return true;
+ }
+
+ virtual int
+ x ()
+ {
+ if (case_ == 9)
+ throw app (case_);
+
+ return -1;
+ }
+
+ virtual int
+ y ()
+ {
+ if (case_ == 10)
+ throw app (case_);
+
+ return -2;
+ }
+
+ // Elements.
+ //
+ virtual int
+ a ()
+ {
+ if (case_ == 11)
+ throw app (case_);
+
+ return 1;
+ }
+
+ virtual bool
+ b_present ()
+ {
+ if (case_ == 12)
+ {
+ throw app (case_);
+ return false;
+ }
+
+ return true;
+ }
+
+ virtual int
+ b ()
+ {
+ if (case_ == 13)
+ throw app (case_);
+
+ return 2;
+ }
+
+ virtual bool
+ c_next ()
+ {
+ if (case_ == 14)
+ {
+ throw app (case_);
+ return false;
+ }
+
+ return n_++ < 3;
+ }
+
+ virtual int
+ c ()
+ {
+ if (case_ == 15)
+ throw app (case_);
+
+ return 2 + n_;
+ }
+
+ virtual void
+ _post ()
+ {
+ if (case_ == 16)
+ throw app (case_);
+ }
+
+ virtual void
+ post ()
+ {
+ if (case_ == 17)
+ throw app (case_);
+ }
+
+private:
+ int case_;
+ int n_;
+};
+
+struct root_simpl: root_sskel
+{
+ root_simpl (int c)
+ : case_ (c)
+ {
+ }
+
+ virtual void
+ pre ()
+ {
+ if (case_ == 1)
+ throw app (case_);
+ }
+
+ virtual void
+ _pre ()
+ {
+ if (case_ == 2)
+ throw app (case_);
+ }
+
+ virtual void
+ test ()
+ {
+ if (case_ == 3)
+ throw app (case_);
+ }
+
+ virtual void
+ _post ()
+ {
+ if (case_ == 4)
+ throw app (case_);
+ }
+
+ virtual void
+ post ()
+ {
+ if (case_ == 5)
+ throw app (case_);
+ }
+
+private:
+ int case_;
+};
+
+struct writer: xml_schema::writer
+{
+ virtual void
+ write (const char*, size_t)
+ {
+ throw ios_base::failure ("write failed");
+ }
+
+ virtual void
+ flush ()
+ {
+ throw ios_base::failure ("write failed");
+ }
+};
+
+int
+main ()
+{
+
+ // 1-5 : app errors in the root element serializer
+ // 6-17: app errors in the sub element serializer
+ // 18 : xml error in document serializer
+ // 19 : xml error in sub element serializer
+ // 20 : sys error
+ //
+ for (int i (1); i <= 20; ++i)
+ {
+ try
+ {
+ xml_schema::int_simpl int_s;
+ test_simpl test_s (i);
+ root_simpl root_s (i);
+
+ test_s.serializers (int_s, int_s, int_s, int_s, int_s);
+ root_s.serializers (test_s);
+
+ xml_schema::document_simpl doc_s (
+ root_s,
+ "test",
+ (i == 18 ? "bad name" : "root"));
+
+ root_s.pre ();
+
+ ostringstream os;
+
+ if (i == 20)
+ {
+ writer w;
+ doc_s.serialize (w);
+ }
+ else
+ doc_s.serialize (os);
+
+ root_s.post ();
+ }
+ catch (ios_base::failure const&)
+ {
+ cout << "sys: write failed" << endl;
+ }
+ catch (xml_schema::serializer_xml const& e)
+ {
+ cout << "xml: " << e.text () << endl;
+ }
+#ifdef XSDE_SERIALIZER_VALIDATION
+ catch (xml_schema::serializer_schema const& e)
+ {
+ cout << "schema: " << e.text () << endl;
+ }
+#endif
+ catch (app const& e)
+ {
+ cout << "app: " << e.code () << endl;
+ }
+ }
+}
diff --git a/tests/cxx/serializer/error-handling/exceptions/makefile b/tests/cxx/serializer/error-handling/exceptions/makefile
new file mode 100644
index 0000000..19027cc
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/exceptions/makefile
@@ -0,0 +1,72 @@
+# file : tests/cxx/serializer/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=-sskel.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=-sskel.hxx) \
+ $(out_base)/$(xsd:.xsd=-sskel.ixx) \
+ $(out_base)/$(xsd:.xsd=-sskel.cxx)
+
+$(skel): xsde := $(out_root)/xsde/xsde
+$(skel): $(out_root)/xsde/xsde
+
+$(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)/output
+ $(call message,test $$1,$$1 | 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=-sskel.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/serializer/xsd-cxx.make)
+
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsde/makefile)
+$(call import,$(src_root)/libxsde/xsde/makefile)
diff --git a/tests/cxx/serializer/error-handling/exceptions/output b/tests/cxx/serializer/error-handling/exceptions/output
new file mode 100644
index 0000000..e640bd6
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/exceptions/output
@@ -0,0 +1,20 @@
+app: 1
+app: 2
+app: 3
+app: 4
+app: 5
+app: 6
+app: 7
+app: 8
+app: 9
+app: 10
+app: 11
+app: 12
+app: 13
+app: 14
+app: 15
+app: 16
+app: 17
+xml: illegal name
+xml: call out of sequence
+sys: write failed
diff --git a/tests/cxx/serializer/error-handling/exceptions/test.xsd b/tests/cxx/serializer/error-handling/exceptions/test.xsd
new file mode 100644
index 0000000..530118c
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/exceptions/test.xsd
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:t="test" targetNamespace="test">
+
+ <complexType name="test">
+ <sequence>
+ <element name="a" type="int"/>
+ <element name="b" type="int" minOccurs="0"/>
+ <element name="c" type="int" maxOccurs="unbounded"/>
+ </sequence>
+ <attribute name="x" type="int"/>
+ <attribute name="y" type="int" use="required"/>
+ </complexType>
+
+ <complexType name="root">
+ <sequence>
+ <element name="test" type="t:test"/>
+ </sequence>
+ </complexType>
+
+ <element name="root" type="t:root"/>
+
+</schema>
diff --git a/tests/cxx/serializer/error-handling/makefile b/tests/cxx/serializer/error-handling/makefile
new file mode 100644
index 0000000..03597f5
--- /dev/null
+++ b/tests/cxx/serializer/error-handling/makefile
@@ -0,0 +1,24 @@
+# file : tests/cxx/serializer/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))