|
| 1 | +import test_support |
| 2 | +import unittest |
| 3 | + |
| 4 | +from cStringIO import StringIO |
| 5 | +from quopri import * |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +ENCSAMPLE = """\ |
| 10 | +Here's a bunch of special=20 |
| 11 | +
|
| 12 | +=A1=A2=A3=A4=A5=A6=A7=A8=A9 |
| 13 | +=AA=AB=AC=AD=AE=AF=B0=B1=B2=B3 |
| 14 | +=B4=B5=B6=B7=B8=B9=BA=BB=BC=BD=BE |
| 15 | +=BF=C0=C1=C2=C3=C4=C5=C6 |
| 16 | +=C7=C8=C9=CA=CB=CC=CD=CE=CF |
| 17 | +=D0=D1=D2=D3=D4=D5=D6=D7 |
| 18 | +=D8=D9=DA=DB=DC=DD=DE=DF |
| 19 | +=E0=E1=E2=E3=E4=E5=E6=E7 |
| 20 | +=E8=E9=EA=EB=EC=ED=EE=EF |
| 21 | +=F0=F1=F2=F3=F4=F5=F6=F7 |
| 22 | +=F8=F9=FA=FB=FC=FD=FE=FF |
| 23 | +
|
| 24 | +characters... have fun! |
| 25 | +""" |
| 26 | + |
| 27 | +# First line ends with a space |
| 28 | +DECSAMPLE = """\ |
| 29 | +Here's a bunch of special |
| 30 | +
|
| 31 | +¡¢£¤¥¦§¨© |
| 32 | +ª«¬®¯°±²³ |
| 33 | +´µ¶·¸¹º»¼½¾ |
| 34 | +¿ÀÁÂÃÄÅÆ |
| 35 | +ÇÈÉÊËÌÍÎÏ |
| 36 | +ÐÑÒÓÔÕÖ× |
| 37 | +ØÙÚÛÜÝÞß |
| 38 | +àáâãäåæç |
| 39 | +èéêëìíîï |
| 40 | +ðñòóôõö÷ |
| 41 | +øùúûüýþÿ |
| 42 | +
|
| 43 | +characters... have fun! |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +class QuopriTestCase(unittest.TestCase): |
| 49 | + # Each entry is a tuple of (plaintext, encoded string). These strings are |
| 50 | + # used in the "quotetabs=0" tests. |
| 51 | + STRINGS = ( |
| 52 | + # Some normal strings |
| 53 | + ('hello', 'hello'), |
| 54 | + ('''hello |
| 55 | + there |
| 56 | + world''', '''hello |
| 57 | + there |
| 58 | + world'''), |
| 59 | + ('''hello |
| 60 | + there |
| 61 | + world |
| 62 | +''', '''hello |
| 63 | + there |
| 64 | + world |
| 65 | +'''), |
| 66 | + ('\201\202\203', '=81=82=83'), |
| 67 | + # Add some trailing MUST QUOTE strings |
| 68 | + ('hello ', 'hello=20'), |
| 69 | + ('hello\t', 'hello=09'), |
| 70 | + # Now some really complex stuff |
| 71 | + (DECSAMPLE, ENCSAMPLE), |
| 72 | + ) |
| 73 | + |
| 74 | + # These are used in the "quotetabs=1" tests. |
| 75 | + ESTRINGS = ( |
| 76 | + ('hello world', 'hello=20world'), |
| 77 | + ('hello\tworld', 'hello=09world'), |
| 78 | + ) |
| 79 | + |
| 80 | + def test_encodestring(self): |
| 81 | + for p, e in self.STRINGS: |
| 82 | + self.assert_(encodestring(p) == e) |
| 83 | + |
| 84 | + def test_decodestring(self): |
| 85 | + for p, e in self.STRINGS: |
| 86 | + self.assert_(decodestring(e) == p) |
| 87 | + |
| 88 | + def test_idempotent_string(self): |
| 89 | + for p, e in self.STRINGS: |
| 90 | + self.assert_(decodestring(encodestring(e)) == e) |
| 91 | + |
| 92 | + def test_encode(self): |
| 93 | + for p, e in self.STRINGS: |
| 94 | + infp = StringIO(p) |
| 95 | + outfp = StringIO() |
| 96 | + encode(infp, outfp, quotetabs=0) |
| 97 | + self.assert_(outfp.getvalue() == e) |
| 98 | + |
| 99 | + def test_decode(self): |
| 100 | + for p, e in self.STRINGS: |
| 101 | + infp = StringIO(e) |
| 102 | + outfp = StringIO() |
| 103 | + decode(infp, outfp) |
| 104 | + self.assert_(outfp.getvalue() == p) |
| 105 | + |
| 106 | + def test_embedded_ws(self): |
| 107 | + for p, e in self.ESTRINGS: |
| 108 | + self.assert_(encodestring(p, quotetabs=1) == e) |
| 109 | + self.assert_(decodestring(e) == p) |
| 110 | + |
| 111 | + |
| 112 | +test_support.run_unittest(QuopriTestCase) |
0 commit comments