Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | from test 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 = "Here's a bunch of special \n" + \ | |
29 | """\ | |
30 | ||
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 | |
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 | # Some long lines. First, a single line of 108 characters | |
71 | ('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\xd8\xd9\xda\xdb\xdc\xdd\xde\xdfxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
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 ;) | |
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 | ) | |
106 | ||
107 | # These are used in the "header=1" tests. | |
108 | HSTRINGS = ( | |
109 | ('hello world', 'hello_world'), | |
110 | ('hello_world', 'hello=5Fworld'), | |
111 | ) | |
112 | ||
113 | def test_encodestring(self): | |
114 | for p, e in self.STRINGS: | |
115 | self.assert_(encodestring(p) == e) | |
116 | ||
117 | def test_decodestring(self): | |
118 | for p, e in self.STRINGS: | |
119 | self.assert_(decodestring(e) == p) | |
120 | ||
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 | ||
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) | |
151 | ||
152 | def test_main(): | |
153 | test_support.run_unittest(QuopriTestCase) | |
154 | ||
155 | ||
156 | if __name__ == "__main__": | |
157 | test_main() |