Commit b4415548 authored by Amaury Forgeot d'Arc's avatar Amaury Forgeot d'Arc

#2982 More test coverage for the pyexpat parser.

parent 90774dd4
...@@ -37,6 +37,7 @@ data = b'''\ ...@@ -37,6 +37,7 @@ data = b'''\
<!-- comment data --> <!-- comment data -->
<!DOCTYPE quotations SYSTEM "quotations.dtd" [ <!DOCTYPE quotations SYSTEM "quotations.dtd" [
<!ELEMENT root ANY> <!ELEMENT root ANY>
<!ATTLIST root attr1 CDATA #REQUIRED attr2 CDATA #IMPLIED>
<!NOTATION notation SYSTEM "notation.jpeg"> <!NOTATION notation SYSTEM "notation.jpeg">
<!ENTITY acirc "&#226;"> <!ENTITY acirc "&#226;">
<!ENTITY external_entity SYSTEM "entity.file"> <!ENTITY external_entity SYSTEM "entity.file">
...@@ -50,6 +51,7 @@ data = b'''\ ...@@ -50,6 +51,7 @@ data = b'''\
</myns:subelement> </myns:subelement>
<sub2><![CDATA[contents of CDATA section]]></sub2> <sub2><![CDATA[contents of CDATA section]]></sub2>
&external_entity; &external_entity;
&skipped_entity;
</root> </root>
''' '''
...@@ -98,7 +100,7 @@ class ParseTest(unittest.TestCase): ...@@ -98,7 +100,7 @@ class ParseTest(unittest.TestCase):
entityName, base, systemId, publicId, notationName = args entityName, base, systemId, publicId, notationName = args
self.out.append('Unparsed entity decl: %s' %(args,)) self.out.append('Unparsed entity decl: %s' %(args,))
def NotStandaloneHandler(self, userData): def NotStandaloneHandler(self):
self.out.append('Not standalone') self.out.append('Not standalone')
return 1 return 1
...@@ -107,6 +109,34 @@ class ParseTest(unittest.TestCase): ...@@ -107,6 +109,34 @@ class ParseTest(unittest.TestCase):
self.out.append('External entity ref: %s' %(args[1:],)) self.out.append('External entity ref: %s' %(args[1:],))
return 1 return 1
def StartDoctypeDeclHandler(self, *args):
self.out.append(('Start doctype', args))
return 1
def EndDoctypeDeclHandler(self):
self.out.append("End doctype")
return 1
def EntityDeclHandler(self, *args):
self.out.append(('Entity declaration', args))
return 1
def XmlDeclHandler(self, *args):
self.out.append(('XML declaration', args))
return 1
def ElementDeclHandler(self, *args):
self.out.append(('Element declaration', args))
return 1
def AttlistDeclHandler(self, *args):
self.out.append(('Attribute list declaration', args))
return 1
def SkippedEntityHandler(self, *args):
self.out.append(("Skipped entity", args))
return 1
def DefaultHandler(self, userData): def DefaultHandler(self, userData):
pass pass
...@@ -114,36 +144,53 @@ class ParseTest(unittest.TestCase): ...@@ -114,36 +144,53 @@ class ParseTest(unittest.TestCase):
pass pass
handler_names = [ handler_names = [
'StartElementHandler', 'EndElementHandler', 'StartElementHandler', 'EndElementHandler', 'CharacterDataHandler',
'CharacterDataHandler', 'ProcessingInstructionHandler', 'ProcessingInstructionHandler', 'UnparsedEntityDeclHandler',
'UnparsedEntityDeclHandler', 'NotationDeclHandler', 'NotationDeclHandler', 'StartNamespaceDeclHandler',
'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', 'EndNamespaceDeclHandler', 'CommentHandler',
'CommentHandler', 'StartCdataSectionHandler', 'StartCdataSectionHandler', 'EndCdataSectionHandler', 'DefaultHandler',
'EndCdataSectionHandler', 'DefaultHandlerExpand', 'NotStandaloneHandler',
'DefaultHandler', 'DefaultHandlerExpand', 'ExternalEntityRefHandler', 'StartDoctypeDeclHandler',
#'NotStandaloneHandler', 'EndDoctypeDeclHandler', 'EntityDeclHandler', 'XmlDeclHandler',
'ExternalEntityRefHandler' 'ElementDeclHandler', 'AttlistDeclHandler', 'SkippedEntityHandler',
] ]
def _verify_parse_output(self, op): def _verify_parse_output(self, operations):
self.assertEquals(op[0], 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'') expected_operations = [
self.assertEquals(op[1], "Comment: ' comment data '") ('XML declaration', ('1.0', 'iso-8859-1', 0)),
self.assertEquals(op[2], "Notation declared: ('notation', None, 'notation.jpeg', None)") 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'',
self.assertEquals(op[3], "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')") "Comment: ' comment data '",
self.assertEquals(op[4], "Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\u1f40'}") "Not standalone",
self.assertEquals(op[5], "NS decl: 'myns' 'http://www.python.org/namespace'") ("Start doctype", ('quotations', 'quotations.dtd', None, 1)),
self.assertEquals(op[6], "Start element: 'http://www.python.org/namespace!subelement' {}") ('Element declaration', ('root', (2, 0, None, ()))),
self.assertEquals(op[7], "Character data: 'Contents of subelements'") ('Attribute list declaration', ('root', 'attr1', 'CDATA', None,
self.assertEquals(op[8], "End element: 'http://www.python.org/namespace!subelement'") 1)),
self.assertEquals(op[9], "End of NS decl: 'myns'") ('Attribute list declaration', ('root', 'attr2', 'CDATA', None,
self.assertEquals(op[10], "Start element: 'sub2' {}") 0)),
self.assertEquals(op[11], 'Start of CDATA section') "Notation declared: ('notation', None, 'notation.jpeg', None)",
self.assertEquals(op[12], "Character data: 'contents of CDATA section'") ('Entity declaration', ('acirc', 0, '\xe2', None, None, None, None)),
self.assertEquals(op[13], 'End of CDATA section') ('Entity declaration', ('external_entity', 0, None, None,
self.assertEquals(op[14], "End element: 'sub2'") 'entity.file', None, None)),
self.assertEquals(op[15], "External entity ref: (None, 'entity.file', None)") "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')",
self.assertEquals(op[16], "End element: 'root'") "Not standalone",
"End doctype",
"Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\u1f40'}",
"NS decl: 'myns' 'http://www.python.org/namespace'",
"Start element: 'http://www.python.org/namespace!subelement' {}",
"Character data: 'Contents of subelements'",
"End element: 'http://www.python.org/namespace!subelement'",
"End of NS decl: 'myns'",
"Start element: 'sub2' {}",
'Start of CDATA section',
"Character data: 'contents of CDATA section'",
'End of CDATA section',
"End element: 'sub2'",
"External entity ref: (None, 'entity.file', None)",
('Skipped entity', ('skipped_entity', 0)),
"End element: 'root'",
]
for operation, expected_operation in zip(operations, expected_operations):
self.assertEquals(operation, expected_operation)
def test_unicode(self): def test_unicode(self):
# Try the parse again, this time producing Unicode output # Try the parse again, this time producing Unicode output
...@@ -154,8 +201,8 @@ class ParseTest(unittest.TestCase): ...@@ -154,8 +201,8 @@ class ParseTest(unittest.TestCase):
parser.Parse(data, 1) parser.Parse(data, 1)
op = out.out operations = out.out
self._verify_parse_output(op) self._verify_parse_output(operations)
def test_parse_file(self): def test_parse_file(self):
# Try parsing a file # Try parsing a file
...@@ -167,8 +214,8 @@ class ParseTest(unittest.TestCase): ...@@ -167,8 +214,8 @@ class ParseTest(unittest.TestCase):
parser.ParseFile(file) parser.ParseFile(file)
op = out.out operations = out.out
self._verify_parse_output(op) self._verify_parse_output(operations)
class NamespaceSeparatorTest(unittest.TestCase): class NamespaceSeparatorTest(unittest.TestCase):
def test_legal(self): def test_legal(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment