Commit 265a804a authored by Fred Drake's avatar Fred Drake

Revise the test case for pyexpat to avoid using asserts. Conform better

to the Python style guide, and remove unneeded imports.
parent 9e79a25b
test_pyexpat test_pyexpat
OK.
OK.
OK.
OK.
PI: PI:
'xml-stylesheet' 'href="stylesheet.css"' 'xml-stylesheet' 'href="stylesheet.css"'
Comment: Comment:
......
...@@ -3,9 +3,6 @@ ...@@ -3,9 +3,6 @@
# XXX TypeErrors on calling handlers, or on bad return values from a # XXX TypeErrors on calling handlers, or on bad return values from a
# handler, are obscure and unhelpful. # handler, are obscure and unhelpful.
import sys, string
import os
import pyexpat import pyexpat
class Outputter: class Outputter:
...@@ -16,7 +13,7 @@ class Outputter: ...@@ -16,7 +13,7 @@ class Outputter:
print 'End element:\n\t', repr(name) print 'End element:\n\t', repr(name)
def CharacterDataHandler(self, data): def CharacterDataHandler(self, data):
data = string.strip(data) data = data.strip()
if data: if data:
print 'Character data:' print 'Character data:'
print '\t', repr(data) print '\t', repr(data)
...@@ -63,29 +60,37 @@ class Outputter: ...@@ -63,29 +60,37 @@ class Outputter:
pass pass
def confirm(ok):
if ok:
print "OK."
else:
print "Not OK."
out = Outputter() out = Outputter()
parser = pyexpat.ParserCreate(namespace_separator='!') parser = pyexpat.ParserCreate(namespace_separator='!')
# Test getting/setting returns_unicode # Test getting/setting returns_unicode
parser.returns_unicode = 0 ; assert parser.returns_unicode == 0 parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
parser.returns_unicode = 1 ; assert parser.returns_unicode == 1 parser.returns_unicode = 1; confirm(parser.returns_unicode == 1)
parser.returns_unicode = 2 ; assert parser.returns_unicode == 1 parser.returns_unicode = 2; confirm(parser.returns_unicode == 1)
parser.returns_unicode = 0 ; assert parser.returns_unicode == 0 parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
HANDLER_NAMES = ['StartElementHandler', 'EndElementHandler', HANDLER_NAMES = [
'CharacterDataHandler', 'ProcessingInstructionHandler', 'StartElementHandler', 'EndElementHandler',
'UnparsedEntityDeclHandler', 'NotationDeclHandler', 'CharacterDataHandler', 'ProcessingInstructionHandler',
'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', 'UnparsedEntityDeclHandler', 'NotationDeclHandler',
'CommentHandler', 'StartCdataSectionHandler', 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
'EndCdataSectionHandler', 'CommentHandler', 'StartCdataSectionHandler',
'DefaultHandler', 'DefaultHandlerExpand', 'EndCdataSectionHandler',
#'NotStandaloneHandler', 'DefaultHandler', 'DefaultHandlerExpand',
'ExternalEntityRefHandler' #'NotStandaloneHandler',
] 'ExternalEntityRefHandler'
]
for name in HANDLER_NAMES: for name in HANDLER_NAMES:
setattr(parser, name, getattr(out, name) ) setattr(parser, name, getattr(out, name))
data = """<?xml version="1.0" encoding="iso-8859-1" standalone="no"?> data = '''\
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<?xml-stylesheet href="stylesheet.css"?> <?xml-stylesheet href="stylesheet.css"?>
<!-- comment data --> <!-- comment data -->
<!DOCTYPE quotations SYSTEM "quotations.dtd" [ <!DOCTYPE quotations SYSTEM "quotations.dtd" [
...@@ -104,14 +109,14 @@ data = """<?xml version="1.0" encoding="iso-8859-1" standalone="no"?> ...@@ -104,14 +109,14 @@ data = """<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<sub2><![CDATA[contents of CDATA section]]></sub2> <sub2><![CDATA[contents of CDATA section]]></sub2>
&external_entity; &external_entity;
</root> </root>
""" '''
# Produce UTF-8 output # Produce UTF-8 output
parser.returns_unicode = 0 parser.returns_unicode = 0
try: try:
parser.Parse(data, 1) parser.Parse(data, 1)
except pyexpat.error: except pyexpat.error:
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode) print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
print '** Line', parser.ErrorLineNumber print '** Line', parser.ErrorLineNumber
print '** Column', parser.ErrorColumnNumber print '** Column', parser.ErrorColumnNumber
print '** Byte', parser.ErrorByteIndex print '** Byte', parser.ErrorByteIndex
...@@ -121,11 +126,11 @@ parser = pyexpat.ParserCreate(namespace_separator='!') ...@@ -121,11 +126,11 @@ parser = pyexpat.ParserCreate(namespace_separator='!')
parser.returns_unicode = 1 parser.returns_unicode = 1
for name in HANDLER_NAMES: for name in HANDLER_NAMES:
setattr(parser, name, getattr(out, name) ) setattr(parser, name, getattr(out, name))
try: try:
parser.Parse(data, 1) parser.Parse(data, 1)
except pyexpat.error: except pyexpat.error:
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode) print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
print '** Line', parser.ErrorLineNumber print '** Line', parser.ErrorLineNumber
print '** Column', parser.ErrorColumnNumber print '** Column', parser.ErrorColumnNumber
print '** Byte', parser.ErrorByteIndex print '** Byte', parser.ErrorByteIndex
...@@ -135,14 +140,13 @@ parser = pyexpat.ParserCreate(namespace_separator='!') ...@@ -135,14 +140,13 @@ parser = pyexpat.ParserCreate(namespace_separator='!')
parser.returns_unicode = 1 parser.returns_unicode = 1
for name in HANDLER_NAMES: for name in HANDLER_NAMES:
setattr(parser, name, getattr(out, name) ) setattr(parser, name, getattr(out, name))
import StringIO import StringIO
file = StringIO.StringIO(data) file = StringIO.StringIO(data)
try: try:
parser.ParseFile(file) parser.ParseFile(file)
except pyexpat.error: except pyexpat.error:
print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode) print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
print '** Line', parser.ErrorLineNumber print '** Line', parser.ErrorLineNumber
print '** Column', parser.ErrorColumnNumber print '** Column', parser.ErrorColumnNumber
print '** Byte', parser.ErrorByteIndex print '** Byte', parser.ErrorByteIndex
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