diff -r 6f0e49ed0589 Lib/test/test_quopri.py --- a/Lib/test/test_quopri.py Wed Nov 14 11:19:42 2012 +0000 +++ b/Lib/test/test_quopri.py Wed Nov 14 18:04:26 2012 -0300 @@ -1,9 +1,13 @@ from test import support + import unittest +import sys +import io +import subprocess -import sys, os, io, subprocess -import quopri +py_quopri = support.import_fresh_module('quopri', blocked=['binascii']) +c_quopri = support.import_fresh_module('quopri', fresh=['binascii']) ENCSAMPLE = b"""\ @@ -44,27 +48,11 @@ """ -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): # Each entry is a tuple of (plaintext, encoded string). These strings are # used in the "quotetabs=0" tests. + module = None + STRINGS = ( # Some normal strings (b'hello', b'hello'), @@ -81,9 +69,6 @@ world '''), (b'\201\202\203', b'=81=82=83'), - # Add some trailing MUST QUOTE strings - (b'hello ', b'hello=20'), - (b'hello\t', b'hello=09'), # Some long lines. First, a single line of 108 characters (b'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\xd8\xd9\xda\xdb\xdc\xdd\xde\xdfxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', b'''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=D8=D9=DA=DB=DC=DD=DE=DFx= @@ -127,52 +112,50 @@ (b'hello_world', b'hello=5Fworld'), ) - @withpythonimplementation def test_encodestring(self): for p, e in self.STRINGS: - self.assertEqual(quopri.encodestring(p), e) + self.assertEqual(self.module.encodestring(p), e) - @withpythonimplementation def test_decodestring(self): for p, e in self.STRINGS: - self.assertEqual(quopri.decodestring(e), p) + self.assertEqual(self.module.decodestring(e), p) - @withpythonimplementation + def test_decodestring_badly_enconded(self): + e = b"hello \t\r\n" + p = b"hello\n" + s = self.module.decodestring(e) + self.assertEqual(s, p) + def test_idempotent_string(self): for p, e in self.STRINGS: - self.assertEqual(quopri.decodestring(quopri.encodestring(e)), e) + self.assertEqual(self.module.decodestring(self.module.encodestring(e)), e) - @withpythonimplementation def test_encode(self): for p, e in self.STRINGS: infp = io.BytesIO(p) outfp = io.BytesIO() - quopri.encode(infp, outfp, quotetabs=False) + self.module.encode(infp, outfp, quotetabs=False) self.assertEqual(outfp.getvalue(), e) - @withpythonimplementation def test_decode(self): for p, e in self.STRINGS: infp = io.BytesIO(e) outfp = io.BytesIO() - quopri.decode(infp, outfp) + self.module.decode(infp, outfp) self.assertEqual(outfp.getvalue(), p) - @withpythonimplementation def test_embedded_ws(self): for p, e in self.ESTRINGS: - self.assertEqual(quopri.encodestring(p, quotetabs=True), e) - self.assertEqual(quopri.decodestring(e), p) + self.assertEqual(self.module.encodestring(p, quotetabs=True), e) + self.assertEqual(self.module.decodestring(e), p) - @withpythonimplementation def test_encode_header(self): for p, e in self.HSTRINGS: - self.assertEqual(quopri.encodestring(p, header=True), e) + self.assertEqual(self.module.encodestring(p, header=True), e) - @withpythonimplementation def test_decode_header(self): for p, e in self.HSTRINGS: - self.assertEqual(quopri.decodestring(e, header=True), p) + self.assertEqual(self.module.decodestring(e, header=True), p) def test_scriptencode(self): (p, e) = self.STRINGS[-1] @@ -185,7 +168,7 @@ # with the expected result, we need to do a line-by-line comparison. cout = cout.decode('latin-1').splitlines() e = e.decode('latin-1').splitlines() - assert len(cout)==len(e) + assert len(cout) == len(e) for i in range(len(cout)): self.assertEqual(cout[i], e[i]) self.assertEqual(cout, e) @@ -200,8 +183,18 @@ p = p.decode('latin-1') self.assertEqual(cout.splitlines(), p.splitlines()) + +class QuopriPythonTestCase(QuopriTestCase): + module = py_quopri + + +class QuopriCTestCase(QuopriTestCase): + module = c_quopri + + def test_main(): - support.run_unittest(QuopriTestCase) + support.run_unittest(QuopriCTestCase, + QuopriPythonTestCase) if __name__ == "__main__":