Index: Lib/test/test_pyexpat.py =================================================================== --- Lib/test/test_pyexpat.py (révision 63828) +++ Lib/test/test_pyexpat.py (copie de travail) @@ -42,6 +42,7 @@ + @@ -55,6 +56,7 @@ &external_entity; +&skipped_entity; ''' @@ -103,7 +105,7 @@ entityName, base, systemId, publicId, notationName = args self.out.append('Unparsed entity decl: %s' %(args,)) - def NotStandaloneHandler(self, userData): + def NotStandaloneHandler(self): self.out.append('Not standalone') return 1 @@ -112,6 +114,34 @@ self.out.append('External entity ref: %s' %(args[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): pass @@ -119,15 +149,15 @@ pass handler_names = [ - 'StartElementHandler', 'EndElementHandler', - 'CharacterDataHandler', 'ProcessingInstructionHandler', - 'UnparsedEntityDeclHandler', 'NotationDeclHandler', - 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', - 'CommentHandler', 'StartCdataSectionHandler', - 'EndCdataSectionHandler', - 'DefaultHandler', 'DefaultHandlerExpand', - #'NotStandaloneHandler', - 'ExternalEntityRefHandler' + 'StartElementHandler', 'EndElementHandler', 'CharacterDataHandler', + 'ProcessingInstructionHandler', 'UnparsedEntityDeclHandler', + 'NotationDeclHandler', 'StartNamespaceDeclHandler', + 'EndNamespaceDeclHandler', 'CommentHandler', + 'StartCdataSectionHandler', 'EndCdataSectionHandler', 'DefaultHandler', + 'DefaultHandlerExpand', 'NotStandaloneHandler', + 'ExternalEntityRefHandler', 'StartDoctypeDeclHandler', + 'EndDoctypeDeclHandler', 'EntityDeclHandler', 'XmlDeclHandler', + 'ElementDeclHandler', 'AttlistDeclHandler', 'SkippedEntityHandler', ] def test_utf8(self): @@ -140,24 +170,42 @@ parser.Parse(data, 1) # Verify output - op = out.out - self.assertEquals(op[0], 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'') - self.assertEquals(op[1], "Comment: ' comment data '") - self.assertEquals(op[2], "Notation declared: ('notation', None, 'notation.jpeg', None)") - self.assertEquals(op[3], "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')") - self.assertEquals(op[4], "Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\\xe1\\xbd\\x80'}") - self.assertEquals(op[5], "NS decl: 'myns' 'http://www.python.org/namespace'") - self.assertEquals(op[6], "Start element: 'http://www.python.org/namespace!subelement' {}") - self.assertEquals(op[7], "Character data: 'Contents of subelements'") - self.assertEquals(op[8], "End element: 'http://www.python.org/namespace!subelement'") - self.assertEquals(op[9], "End of NS decl: 'myns'") - self.assertEquals(op[10], "Start element: 'sub2' {}") - self.assertEquals(op[11], 'Start of CDATA section') - self.assertEquals(op[12], "Character data: 'contents of CDATA section'") - self.assertEquals(op[13], 'End of CDATA section') - self.assertEquals(op[14], "End element: 'sub2'") - self.assertEquals(op[15], "External entity ref: (None, 'entity.file', None)") - self.assertEquals(op[16], "End element: 'root'") + operations = out.out + expected_operations = [ + ('XML declaration', (u'1.0', u'iso-8859-1', 0)), + 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'', + "Comment: ' comment data '", + "Not standalone", + ("Start doctype", ('quotations', 'quotations.dtd', None, 1)), + ('Element declaration', (u'root', (2, 0, None, ()))), + ('Attribute list declaration', ('root', 'attr1', 'CDATA', None, + 1)), + ('Attribute list declaration', ('root', 'attr2', 'CDATA', None, + 0)), + "Notation declared: ('notation', None, 'notation.jpeg', None)", + ('Entity declaration', ('acirc', 0, '\xc3\xa2', None, None, None, None)), + ('Entity declaration', ('external_entity', 0, None, None, + 'entity.file', None, None)), + "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')", + "Not standalone", + "End doctype", + "Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\\xe1\\xbd\\x80'}", + "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): # Try the parse again, this time producing Unicode output @@ -169,24 +217,43 @@ parser.Parse(data, 1) - op = out.out - self.assertEquals(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'') - self.assertEquals(op[1], "Comment: u' comment data '") - self.assertEquals(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)") - self.assertEquals(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')") - self.assertEquals(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}") - self.assertEquals(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'") - self.assertEquals(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}") - self.assertEquals(op[7], "Character data: u'Contents of subelements'") - self.assertEquals(op[8], "End element: u'http://www.python.org/namespace!subelement'") - self.assertEquals(op[9], "End of NS decl: u'myns'") - self.assertEquals(op[10], "Start element: u'sub2' {}") - self.assertEquals(op[11], 'Start of CDATA section') - self.assertEquals(op[12], "Character data: u'contents of CDATA section'") - self.assertEquals(op[13], 'End of CDATA section') - self.assertEquals(op[14], "End element: u'sub2'") - self.assertEquals(op[15], "External entity ref: (None, u'entity.file', None)") - self.assertEquals(op[16], "End element: u'root'") + operations = out.out + expected_operations = [ + ('XML declaration', (u'1.0', u'iso-8859-1', 0)), + 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'', + "Comment: u' comment data '", + "Not standalone", + ("Start doctype", ('quotations', 'quotations.dtd', None, 1)), + ('Element declaration', (u'root', (2, 0, None, ()))), + ('Attribute list declaration', ('root', 'attr1', 'CDATA', None, + 1)), + ('Attribute list declaration', ('root', 'attr2', 'CDATA', None, + 0)), + "Notation declared: (u'notation', None, u'notation.jpeg', None)", + ('Entity declaration', (u'acirc', 0, u'\xe2', None, None, None, + None)), + ('Entity declaration', (u'external_entity', 0, None, None, + u'entity.file', None, None)), + "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')", + "Not standalone", + "End doctype", + "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}", + "NS decl: u'myns' u'http://www.python.org/namespace'", + "Start element: u'http://www.python.org/namespace!subelement' {}", + "Character data: u'Contents of subelements'", + "End element: u'http://www.python.org/namespace!subelement'", + "End of NS decl: u'myns'", + "Start element: u'sub2' {}", + 'Start of CDATA section', + "Character data: u'contents of CDATA section'", + 'End of CDATA section', + "End element: u'sub2'", + "External entity ref: (None, u'entity.file', None)", + ('Skipped entity', ('skipped_entity', 0)), + "End element: u'root'", + ] + for operation, expected_operation in zip(operations, expected_operations): + self.assertEquals(operation, expected_operation) def test_parse_file(self): # Try parsing a file @@ -199,24 +266,41 @@ parser.ParseFile(file) - op = out.out - self.assertEquals(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'') - self.assertEquals(op[1], "Comment: u' comment data '") - self.assertEquals(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)") - self.assertEquals(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')") - self.assertEquals(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}") - self.assertEquals(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'") - self.assertEquals(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}") - self.assertEquals(op[7], "Character data: u'Contents of subelements'") - self.assertEquals(op[8], "End element: u'http://www.python.org/namespace!subelement'") - self.assertEquals(op[9], "End of NS decl: u'myns'") - self.assertEquals(op[10], "Start element: u'sub2' {}") - self.assertEquals(op[11], 'Start of CDATA section') - self.assertEquals(op[12], "Character data: u'contents of CDATA section'") - self.assertEquals(op[13], 'End of CDATA section') - self.assertEquals(op[14], "End element: u'sub2'") - self.assertEquals(op[15], "External entity ref: (None, u'entity.file', None)") - self.assertEquals(op[16], "End element: u'root'") + operations = out.out + expected_operations = [ + ('XML declaration', (u'1.0', u'iso-8859-1', 0)), + 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'', + "Comment: u' comment data '", + "Not standalone", + ("Start doctype", ('quotations', 'quotations.dtd', None, 1)), + ('Element declaration', (u'root', (2, 0, None, ()))), + ('Attribute list declaration', ('root', 'attr1', 'CDATA', None, + 1)), + ('Attribute list declaration', ('root', 'attr2', 'CDATA', None, + 0)), + "Notation declared: (u'notation', None, u'notation.jpeg', None)", + ('Entity declaration', ('acirc', 0, u'\xe2', None, None, None, None)), + ('Entity declaration', (u'external_entity', 0, None, None, u'entity.file', None, None)), + "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')", + "Not standalone", + "End doctype", + "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}", + "NS decl: u'myns' u'http://www.python.org/namespace'", + "Start element: u'http://www.python.org/namespace!subelement' {}", + "Character data: u'Contents of subelements'", + "End element: u'http://www.python.org/namespace!subelement'", + "End of NS decl: u'myns'", + "Start element: u'sub2' {}", + 'Start of CDATA section', + "Character data: u'contents of CDATA section'", + 'End of CDATA section', + "End element: u'sub2'", + "External entity ref: (None, u'entity.file', None)", + ('Skipped entity', ('skipped_entity', 0)), + "End element: u'root'", + ] + for operation, expected_operation in zip(operations, expected_operations): + self.assertEquals(operation, expected_operation) class NamespaceSeparatorTest(unittest.TestCase):