Home | Trees | Indices | Help |
|
---|
|
1 # -*- coding: utf-8 -*- 2 3 """ 4 Test cases related to DTD parsing and validation 5 """ 6 7 import unittest, sys, os.path 8 9 this_dir = os.path.dirname(__file__) 10 if this_dir not in sys.path: 11 sys.path.insert(0, this_dir) # needed for Py3 12 13 from common_imports import etree, StringIO, BytesIO, _bytes, doctest 14 from common_imports import HelperTestCase, fileInTestDir, make_doctest 1519128 12921 parse = etree.parse 22 tree = parse(fileInTestDir("test.xml")) 23 root = tree.getroot() 24 25 dtd = etree.DTD(fileInTestDir("test.dtd")) 26 self.assert_(dtd.validate(root))2729 root = etree.XML(_bytes("<b/>")) 30 dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>")) 31 self.assert_(dtd.validate(root))3234 fromstring = etree.fromstring 35 parser = etree.XMLParser(dtd_validation=True) 36 xml = _bytes('<!DOCTYPE b SYSTEM "%s"><b><a/></b>' % fileInTestDir("test.dtd")) 37 self.assertRaises(etree.XMLSyntaxError, 38 fromstring, xml, parser=parser)3941 fromstring = etree.fromstring 42 dtd_filename = fileInTestDir("__nosuch.dtd") 43 parser = etree.XMLParser(dtd_validation=True) 44 xml = _bytes('<!DOCTYPE b SYSTEM "%s"><b><a/></b>' % dtd_filename) 45 self.assertRaises(etree.XMLSyntaxError, 46 fromstring, xml, parser=parser) 47 errors = None 48 try: 49 fromstring(xml, parser=parser) 50 except etree.XMLSyntaxError: 51 e = sys.exc_info()[1] 52 errors = [ entry.message for entry in e.error_log 53 if dtd_filename in entry.message ] 54 self.assert_(errors)5557 parser = etree.XMLParser(dtd_validation=True) 58 xml = '<!DOCTYPE a SYSTEM "%s"><a><b/></a>' % fileInTestDir("test.dtd") 59 root = etree.fromstring(xml, parser=parser)6062 parser = etree.XMLParser(dtd_validation=True) 63 xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>' 64 root = etree.fromstring(xml, parser=parser, 65 base_url=fileInTestDir("test.xml"))6668 root = etree.XML("<b><a/></b>") 69 dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>")) 70 self.assertRaises(etree.DocumentInvalid, dtd.assertValid, root)7173 root = etree.XML("<b><a/></b>") 74 dtd = etree.DTD(BytesIO("<!ELEMENT b (a)><!ELEMENT a EMPTY>")) 75 dtd.assertValid(root)7678 root = etree.XML(_bytes(''' 79 <!DOCTYPE b SYSTEM "none" [ 80 <!ELEMENT b (a)> 81 <!ELEMENT a EMPTY> 82 ]> 83 <b><a/></b> 84 ''')) 85 dtd = etree.ElementTree(root).docinfo.internalDTD 86 self.assert_(dtd) 87 dtd.assertValid(root)8890 root = etree.XML(_bytes(''' 91 <!DOCTYPE b SYSTEM "none" [ 92 <!ELEMENT b (a)> 93 <!ELEMENT a (c)> 94 <!ELEMENT c EMPTY> 95 ]> 96 <b><a/></b> 97 ''')) 98 dtd = etree.ElementTree(root).docinfo.internalDTD 99 self.assert_(dtd) 100 self.assertFalse(dtd.validate(root))101 105107 parser = etree.XMLParser(attribute_defaults=True) 108 109 tree = etree.parse(fileInTestDir('test.xml'), parser) 110 root = tree.getroot() 111 112 self.assertEquals( 113 "valueA", 114 root.get("default")) 115 self.assertEquals( 116 "valueB", 117 root[0].get("default"))118131 suite = unittest.TestSuite() 132 suite.addTests([unittest.makeSuite(ETreeDtdTestCase)]) 133 suite.addTests( 134 [make_doctest('../../../doc/validation.txt')]) 135 return suite136 137 if __name__ == '__main__': 138 print('to test use test.py %s' % __file__) 139
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0 on Wed Jan 7 20:35:59 2009 | http://epydoc.sourceforge.net |