Home | Trees | Indices | Help |
|
---|
|
1 # -*- coding: utf-8 -*- 2 3 """ 4 Test cases related to DTD parsing and validation 5 """ 6 7 import unittest 8 9 from common_imports import etree, StringIO, doctest 10 from common_imports import HelperTestCase, fileInTestDir 1115123 12417 parse = etree.parse 18 tree = parse(fileInTestDir("test.xml")) 19 root = tree.getroot() 20 21 dtd = etree.DTD(fileInTestDir("test.dtd")) 22 self.assert_(dtd.validate(root))2325 root = etree.XML("<b/>") 26 dtd = etree.DTD(StringIO("<!ELEMENT b EMPTY>")) 27 self.assert_(dtd.validate(root))2830 fromstring = etree.fromstring 31 parser = etree.XMLParser(dtd_validation=True) 32 xml = '<!DOCTYPE b SYSTEM "%s"><b><a/></b>' % fileInTestDir("test.dtd") 33 self.assertRaises(etree.XMLSyntaxError, 34 fromstring, xml, parser=parser)3537 fromstring = etree.fromstring 38 dtd_filename = fileInTestDir("__nosuch.dtd") 39 parser = etree.XMLParser(dtd_validation=True) 40 xml = '<!DOCTYPE b SYSTEM "%s"><b><a/></b>' % dtd_filename 41 self.assertRaises(etree.XMLSyntaxError, 42 fromstring, xml, parser=parser) 43 errors = None 44 try: 45 fromstring(xml, parser=parser) 46 except etree.XMLSyntaxError, e: 47 errors = [ entry.message for entry in e.error_log 48 if dtd_filename in entry.message ] 49 self.assert_(errors)5052 parser = etree.XMLParser(dtd_validation=True) 53 xml = '<!DOCTYPE a SYSTEM "%s"><a><b/></a>' % fileInTestDir("test.dtd") 54 root = etree.fromstring(xml, parser=parser)5557 parser = etree.XMLParser(dtd_validation=True) 58 xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>' 59 root = etree.fromstring(xml, parser=parser, 60 base_url=fileInTestDir("test.xml"))6163 root = etree.XML("<b><a/></b>") 64 dtd = etree.DTD(StringIO("<!ELEMENT b EMPTY>")) 65 self.assertRaises(etree.DocumentInvalid, dtd.assertValid, root)6668 root = etree.XML("<b><a/></b>") 69 dtd = etree.DTD(StringIO("<!ELEMENT b (a)><!ELEMENT a EMPTY>")) 70 dtd.assertValid(root)7173 root = etree.XML(''' 74 <!DOCTYPE b SYSTEM "none" [ 75 <!ELEMENT b (a)> 76 <!ELEMENT a EMPTY> 77 ]> 78 <b><a/></b> 79 ''') 80 dtd = etree.ElementTree(root).docinfo.internalDTD 81 self.assert_(dtd) 82 dtd.assertValid(root)8385 root = etree.XML(''' 86 <!DOCTYPE b SYSTEM "none" [ 87 <!ELEMENT b (a)> 88 <!ELEMENT a (c)> 89 <!ELEMENT c EMPTY> 90 ]> 91 <b><a/></b> 92 ''') 93 dtd = etree.ElementTree(root).docinfo.internalDTD 94 self.assert_(dtd) 95 self.assertFalse(dtd.validate(root))96 100102 parser = etree.XMLParser(attribute_defaults=True) 103 104 tree = etree.parse(fileInTestDir('test.xml'), parser) 105 root = tree.getroot() 106 107 self.assertEquals( 108 "valueA", 109 root.get("default")) 110 self.assertEquals( 111 "valueB", 112 root[0].get("default"))113115 iterparse = etree.iterparse 116 iterator = iterparse(fileInTestDir("test.xml"), events=("start",), 117 attribute_defaults=True) 118 attributes = [ element.get("default") 119 for event, element in iterator ] 120 self.assertEquals( 121 ["valueA", "valueB"], 122 attributes)126 suite = unittest.TestSuite() 127 suite.addTests([unittest.makeSuite(ETreeDtdTestCase)]) 128 suite.addTests( 129 [doctest.DocFileSuite('../../../doc/validation.txt')]) 130 return suite131 132 if __name__ == '__main__': 133 print 'to test use test.py %s' % __file__ 134
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0 on Fri Dec 12 22:47:09 2008 | http://epydoc.sourceforge.net |