If quopri uses the implementations from binascii do the tests a second time

using the Python implementations of the functions. This imcreases code
coverage and makes sure that both implementations do the same thing.
This commit is contained in:
Walter Dörwald 2005-12-14 23:32:22 +00:00
parent c97ba7064e
commit b197543680
1 changed files with 35 additions and 10 deletions

View File

@ -2,7 +2,7 @@ from test import test_support
import unittest import unittest
from cStringIO import StringIO from cStringIO import StringIO
from quopri import * import quopri
@ -44,6 +44,23 @@ characters... have fun!
""" """
def withpythonimplementation(testfunc):
def newtest(self):
# Test default implementation
testfunc(self)
# Test Python implementation
if quopri.b2a_qp is not None or quopri.a2b_qp is not None:
oldencode = quopri.b2a_qp
olddecode = quopri.a2b_qp
try:
quopri.b2a_qp = None
quopri.a2b_qp = None
testfunc(self)
finally:
quopri.b2a_qp = oldencode
quopri.a2b_qp = olddecode
newtest.__name__ = testfunc.__name__
return newtest
class QuopriTestCase(unittest.TestCase): class QuopriTestCase(unittest.TestCase):
# Each entry is a tuple of (plaintext, encoded string). These strings are # Each entry is a tuple of (plaintext, encoded string). These strings are
@ -110,44 +127,52 @@ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz''')
('hello_world', 'hello=5Fworld'), ('hello_world', 'hello=5Fworld'),
) )
@withpythonimplementation
def test_encodestring(self): def test_encodestring(self):
for p, e in self.STRINGS: for p, e in self.STRINGS:
self.assert_(encodestring(p) == e) self.assert_(quopri.encodestring(p) == e)
@withpythonimplementation
def test_decodestring(self): def test_decodestring(self):
for p, e in self.STRINGS: for p, e in self.STRINGS:
self.assert_(decodestring(e) == p) self.assert_(quopri.decodestring(e) == p)
@withpythonimplementation
def test_idempotent_string(self): def test_idempotent_string(self):
for p, e in self.STRINGS: for p, e in self.STRINGS:
self.assert_(decodestring(encodestring(e)) == e) self.assert_(quopri.decodestring(quopri.encodestring(e)) == e)
@withpythonimplementation
def test_encode(self): def test_encode(self):
for p, e in self.STRINGS: for p, e in self.STRINGS:
infp = StringIO(p) infp = StringIO(p)
outfp = StringIO() outfp = StringIO()
encode(infp, outfp, quotetabs=0) quopri.encode(infp, outfp, quotetabs=False)
self.assert_(outfp.getvalue() == e) self.assert_(outfp.getvalue() == e)
@withpythonimplementation
def test_decode(self): def test_decode(self):
for p, e in self.STRINGS: for p, e in self.STRINGS:
infp = StringIO(e) infp = StringIO(e)
outfp = StringIO() outfp = StringIO()
decode(infp, outfp) quopri.decode(infp, outfp)
self.assert_(outfp.getvalue() == p) self.assert_(outfp.getvalue() == p)
@withpythonimplementation
def test_embedded_ws(self): def test_embedded_ws(self):
for p, e in self.ESTRINGS: for p, e in self.ESTRINGS:
self.assert_(encodestring(p, quotetabs=1) == e) self.assert_(quopri.encodestring(p, quotetabs=True) == e)
self.assert_(decodestring(e) == p) self.assert_(quopri.decodestring(e) == p)
@withpythonimplementation
def test_encode_header(self): def test_encode_header(self):
for p, e in self.HSTRINGS: for p, e in self.HSTRINGS:
self.assert_(encodestring(p, header = 1) == e) self.assert_(quopri.encodestring(p, header=True) == e)
@withpythonimplementation
def test_decode_header(self): def test_decode_header(self):
for p, e in self.HSTRINGS: for p, e in self.HSTRINGS:
self.assert_(decodestring(e, header = 1) == p) self.assert_(quopri.decodestring(e, header=True) == p)
def test_main(): def test_main():
test_support.run_unittest(QuopriTestCase) test_support.run_unittest(QuopriTestCase)