| Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 1 | from test import test_support | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 2 | import unittest | 
 | 3 |  | 
 | 4 | from cStringIO import StringIO | 
 | 5 | from quopri import * | 
 | 6 |  | 
 | 7 |  | 
| Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 8 |  | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 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 | 
| Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 28 | DECSAMPLE = "Here's a bunch of special \n" + \ | 
 | 29 | """\ | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 30 |  | 
| Tim Peters | e861365 | 2001-08-03 20:40:18 +0000 | [diff] [blame] | 31 | \xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9 | 
 | 32 | \xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3 | 
 | 33 | \xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe | 
 | 34 | \xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6 | 
 | 35 | \xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf | 
 | 36 | \xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7 | 
 | 37 | \xd8\xd9\xda\xdb\xdc\xdd\xde\xdf | 
 | 38 | \xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7 | 
 | 39 | \xe8\xe9\xea\xeb\xec\xed\xee\xef | 
 | 40 | \xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7 | 
 | 41 | \xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 42 |  | 
 | 43 | characters... have fun! | 
 | 44 | """ | 
 | 45 |  | 
 | 46 |  | 
| Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 47 |  | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 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'), | 
| Barry Warsaw | 7599a3f | 2001-06-19 22:48:42 +0000 | [diff] [blame] | 70 |         # Some long lines.  First, a single line of 108 characters | 
| Tim Peters | e861365 | 2001-08-03 20:40:18 +0000 | [diff] [blame] | 71 |         ('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\xd8\xd9\xda\xdb\xdc\xdd\xde\xdfxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | 
| Barry Warsaw | 7599a3f | 2001-06-19 22:48:42 +0000 | [diff] [blame] | 72 |          '''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=D8=D9=DA=DB=DC=DD=DE=DFx= | 
 | 73 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'''), | 
 | 74 |         # A line of exactly 76 characters, no soft line break should be needed | 
 | 75 |         ('yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy', | 
 | 76 |         'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'), | 
 | 77 |         # A line of 77 characters, forcing a soft line break at position 75, | 
 | 78 |         # and a second line of exactly 2 characters (because the soft line | 
 | 79 |         # break `=' sign counts against the line length limit). | 
 | 80 |         ('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz', | 
 | 81 |          '''zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz= | 
 | 82 | zz'''), | 
 | 83 |         # A line of 151 characters, forcing a soft line break at position 75, | 
 | 84 |         # with a second line of exactly 76 characters and no trailing = | 
 | 85 |         ('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz', | 
 | 86 |          '''zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz= | 
 | 87 | zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''), | 
 | 88 |         # A string containing a hard line break, but which the first line is | 
 | 89 |         # 151 characters and the second line is exactly 76 characters.  This | 
 | 90 |         # should leave us with three lines, the first which has a soft line | 
 | 91 |         # break, and which the second and third do not. | 
 | 92 |         ('''yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy | 
 | 93 | zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz''', | 
 | 94 |          '''yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy= | 
 | 95 | yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy | 
 | 96 | zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''), | 
 | 97 |         # Now some really complex stuff ;) | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 98 |         (DECSAMPLE, ENCSAMPLE), | 
 | 99 |         ) | 
 | 100 |  | 
 | 101 |     # These are used in the "quotetabs=1" tests. | 
 | 102 |     ESTRINGS = ( | 
 | 103 |         ('hello world', 'hello=20world'), | 
 | 104 |         ('hello\tworld', 'hello=09world'), | 
 | 105 |         ) | 
| Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 106 |  | 
| Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 107 |     # These are used in the "header=1" tests. | 
 | 108 |     HSTRINGS = ( | 
 | 109 |         ('hello world', 'hello_world'), | 
 | 110 |         ('hello_world', 'hello=5Fworld'), | 
 | 111 |         ) | 
 | 112 |  | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 113 |     def test_encodestring(self): | 
 | 114 |         for p, e in self.STRINGS: | 
 | 115 |             self.assert_(encodestring(p) == e) | 
| Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 116 |  | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 117 |     def test_decodestring(self): | 
 | 118 |         for p, e in self.STRINGS: | 
 | 119 |             self.assert_(decodestring(e) == p) | 
| Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 120 |  | 
| Barry Warsaw | 7069763 | 2001-06-19 19:08:13 +0000 | [diff] [blame] | 121 |     def test_idempotent_string(self): | 
 | 122 |         for p, e in self.STRINGS: | 
 | 123 |             self.assert_(decodestring(encodestring(e)) == e) | 
 | 124 |  | 
 | 125 |     def test_encode(self): | 
 | 126 |         for p, e in self.STRINGS: | 
 | 127 |             infp = StringIO(p) | 
 | 128 |             outfp = StringIO() | 
 | 129 |             encode(infp, outfp, quotetabs=0) | 
 | 130 |             self.assert_(outfp.getvalue() == e) | 
 | 131 |  | 
 | 132 |     def test_decode(self): | 
 | 133 |         for p, e in self.STRINGS: | 
 | 134 |             infp = StringIO(e) | 
 | 135 |             outfp = StringIO() | 
 | 136 |             decode(infp, outfp) | 
 | 137 |             self.assert_(outfp.getvalue() == p) | 
 | 138 |  | 
 | 139 |     def test_embedded_ws(self): | 
 | 140 |         for p, e in self.ESTRINGS: | 
 | 141 |             self.assert_(encodestring(p, quotetabs=1) == e) | 
 | 142 |             self.assert_(decodestring(e) == p) | 
 | 143 |  | 
| Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 144 |     def test_encode_header(self): | 
 | 145 |         for p, e in self.HSTRINGS: | 
 | 146 |             self.assert_(encodestring(p, header = 1) == e) | 
 | 147 |  | 
 | 148 |     def test_decode_header(self): | 
 | 149 |         for p, e in self.HSTRINGS: | 
 | 150 |             self.assert_(decodestring(e, header = 1) == p) | 
| Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 151 |  | 
| Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 152 | def test_main(): | 
 | 153 |     test_support.run_unittest(QuopriTestCase) | 
 | 154 |  | 
 | 155 |  | 
 | 156 | if __name__ == "__main__": | 
 | 157 |     test_main() |