summaryrefslogtreecommitdiff
path: root/python/tests
diff options
context:
space:
mode:
authorDaniel Veillard <veillard@src.gnome.org>2005-03-30 22:47:10 +0000
committerDaniel Veillard <veillard@src.gnome.org>2005-03-30 22:47:10 +0000
commit0e460da346bfb2cc9d5d6cb4be8f851c18bfb81e (patch)
tree3f0e29bb120f40ec8dd280274a2bf00c3f7a6273 /python/tests
parentbb8502c0ef2af706fbdff69119e994e40da64a88 (diff)
downloadlibxml2-0e460da346bfb2cc9d5d6cb4be8f851c18bfb81e.tar.gz
another patch from Brent Hendricks to add new handlers with the various
* python/generator.py python/libxml.py: another patch from Brent Hendricks to add new handlers with the various validity contexts * python/tests/Makefile.am python/tests/validDTD.py python/tests/validRNG.py python/tests/validSchemas.py: also added the regression tests he provided Daniel
Diffstat (limited to 'python/tests')
-rw-r--r--python/tests/Makefile.am5
-rwxr-xr-xpython/tests/validDTD.py59
-rwxr-xr-xpython/tests/validRNG.py76
-rwxr-xr-xpython/tests/validSchemas.py83
4 files changed, 222 insertions, 1 deletions
diff --git a/python/tests/Makefile.am b/python/tests/Makefile.am
index f41cd6d1..c6d03e99 100644
--- a/python/tests/Makefile.am
+++ b/python/tests/Makefile.am
@@ -39,7 +39,10 @@ PYTESTS= \
tstLastError.py \
indexes.py \
dtdvalid.py \
- tstmem.py
+ tstmem.py \
+ validDTD.py \
+ validSchemas.py \
+ validRNG.py
XMLS= \
tst.xml \
diff --git a/python/tests/validDTD.py b/python/tests/validDTD.py
new file mode 100755
index 00000000..1222f9fb
--- /dev/null
+++ b/python/tests/validDTD.py
@@ -0,0 +1,59 @@
+#!/usr/bin/python -u
+import libxml2
+import sys
+
+ARG = 'test string'
+
+class ErrorHandler:
+
+ def __init__(self):
+ self.errors = []
+
+ def handler(self, msg, data):
+ if data != ARG:
+ raise Exception, "Error handler did not receive correct argument"
+ self.errors.append(msg)
+
+
+# Memory debug specific
+libxml2.debugMemory(1)
+
+dtd="""<!ELEMENT foo EMPTY>"""
+valid="""<?xml version="1.0"?>
+<foo></foo>"""
+
+invalid="""<?xml version="1.0"?>
+<foo><bar/></foo>"""
+
+dtd = libxml2.parseDTD(None, 'test.dtd')
+ctxt = libxml2.newValidCtxt()
+e = ErrorHandler()
+ctxt.setValidityErrorHandler(e.handler, e.handler, ARG)
+
+# Test valid document
+doc = libxml2.parseDoc(valid)
+ret = doc.validateDtd(ctxt, dtd)
+if ret != 1 or e.errors:
+ print "error doing DTD validation"
+ sys.exit(1)
+doc.freeDoc()
+
+# Test invalid document
+doc = libxml2.parseDoc(invalid)
+ret = doc.validateDtd(ctxt, dtd)
+if ret != 0 or not e.errors:
+ print "Error: document supposed to be invalid"
+doc.freeDoc()
+
+dtd.freeDtd()
+del dtd
+del ctxt
+
+# Memory debug specific
+libxml2.cleanupParser()
+if libxml2.debugMemory(1) == 0:
+ print "OK"
+else:
+ print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+ libxml2.dumpMemory()
+
diff --git a/python/tests/validRNG.py b/python/tests/validRNG.py
new file mode 100755
index 00000000..7022efe9
--- /dev/null
+++ b/python/tests/validRNG.py
@@ -0,0 +1,76 @@
+#!/usr/bin/python -u
+import libxml2
+import sys
+
+ARG = 'test string'
+
+class ErrorHandler:
+
+ def __init__(self):
+ self.errors = []
+
+ def handler(self, msg, data):
+ if data != ARG:
+ raise Exception, "Error handler did not receive correct argument"
+ self.errors.append(msg)
+
+# Memory debug specific
+libxml2.debugMemory(1)
+
+schema="""<?xml version="1.0"?>
+<element name="foo"
+ xmlns="http://relaxng.org/ns/structure/1.0"
+ xmlns:a="http://relaxng.org/ns/annotation/1.0"
+ xmlns:ex1="http://www.example.com/n1"
+ xmlns:ex2="http://www.example.com/n2">
+ <a:documentation>A foo element.</a:documentation>
+ <element name="ex1:bar1">
+ <empty/>
+ </element>
+ <element name="ex2:bar2">
+ <empty/>
+ </element>
+</element>
+"""
+
+valid="""<?xml version="1.0"?>
+<foo><pre1:bar1 xmlns:pre1="http://www.example.com/n1"/><pre2:bar2 xmlns:pre2="http://www.example.com/n2"/></foo>"""
+
+invalid="""<?xml version="1.0"?>
+<foo><pre1:bar1 xmlns:pre1="http://www.example.com/n1">bad</pre1:bar1><pre2:bar2 xmlns:pre2="http://www.example.com/n2"/></foo>"""
+
+rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
+rngs = rngp.relaxNGParse()
+ctxt = rngs.relaxNGNewValidCtxt()
+e = ErrorHandler()
+ctxt.setValidityErrorHandler(e.handler, e.handler, ARG)
+
+# Test valid document
+doc = libxml2.parseDoc(valid)
+ret = doc.relaxNGValidateDoc(ctxt)
+if ret != 0 or e.errors:
+ print "error doing RelaxNG validation"
+ sys.exit(1)
+doc.freeDoc()
+
+# Test invalid document
+doc = libxml2.parseDoc(invalid)
+ret = doc.relaxNGValidateDoc(ctxt)
+if ret == 0 or not e.errors:
+ print "Error: document supposed to be RelaxNG invalid"
+ sys.exit(1)
+doc.freeDoc()
+
+del rngp
+del rngs
+del ctxt
+libxml2.relaxNGCleanupTypes()
+
+# Memory debug specific
+libxml2.cleanupParser()
+if libxml2.debugMemory(1) == 0:
+ print "OK"
+else:
+ print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+ libxml2.dumpMemory()
+
diff --git a/python/tests/validSchemas.py b/python/tests/validSchemas.py
new file mode 100755
index 00000000..d4d62b24
--- /dev/null
+++ b/python/tests/validSchemas.py
@@ -0,0 +1,83 @@
+#!/usr/bin/python -u
+import libxml2
+import sys
+
+ARG = 'test string'
+
+class ErrorHandler:
+
+ def __init__(self):
+ self.errors = []
+
+ def handler(self, msg, data):
+ if data != ARG:
+ raise Exception, "Error handler did not receive correct argument"
+ self.errors.append(msg)
+
+# Memory debug specific
+libxml2.debugMemory(1)
+
+schema="""<?xml version="1.0" encoding="iso-8859-1"?>
+<schema xmlns = "http://www.w3.org/2001/XMLSchema">
+ <element name = "Customer">
+ <complexType>
+ <sequence>
+ <element name = "FirstName" type = "string" />
+ <element name = "MiddleInitial" type = "string" />
+ <element name = "LastName" type = "string" />
+ </sequence>
+ <attribute name = "customerID" type = "integer" />
+ </complexType>
+ </element>
+</schema>"""
+
+valid="""<?xml version="1.0" encoding="iso-8859-1"?>
+<Customer customerID = "24332">
+ <FirstName>Raymond</FirstName>
+ <MiddleInitial>G</MiddleInitial>
+ <LastName>Bayliss</LastName>
+</Customer>
+"""
+
+invalid="""<?xml version="1.0" encoding="iso-8859-1"?>
+<Customer customerID = "24332">
+ <MiddleInitial>G</MiddleInitial>
+ <LastName>Bayliss</LastName>
+</Customer>
+"""
+
+e = ErrorHandler()
+ctxt_parser = libxml2.schemaNewMemParserCtxt(schema, len(schema))
+ctxt_schema = ctxt_parser.schemaParse()
+ctxt_valid = ctxt_schema.schemaNewValidCtxt()
+ctxt_valid.setValidityErrorHandler(e.handler, e.handler, ARG)
+
+# Test valid document
+doc = libxml2.parseDoc(valid)
+ret = doc.schemaValidateDoc(ctxt_valid)
+if ret != 0 or e.errors:
+ print "error doing schema validation"
+ sys.exit(1)
+doc.freeDoc()
+
+# Test invalid document
+doc = libxml2.parseDoc(invalid)
+ret = doc.schemaValidateDoc(ctxt_valid)
+if ret == 0 or not e.errors:
+ print "Error: document supposer to be schema invalid"
+ sys.exit(1)
+doc.freeDoc()
+
+del ctxt_parser
+del ctxt_schema
+del ctxt_valid
+libxml2.schemaCleanupTypes()
+
+# Memory debug specific
+libxml2.cleanupParser()
+if libxml2.debugMemory(1) == 0:
+ print "OK"
+else:
+ print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+ libxml2.dumpMemory()
+