blob: f79eb4e0c7e93fdc06f3037586d4ec391beb5b49 [file] [log] [blame]
Barry Warsaw70697632001-06-19 19:08:13 +00001import test_support
2import unittest
3
4from cStringIO import StringIO
5from quopri import *
6
7
Tim Peters87cc0c32001-07-21 01:41:30 +00008
Barry Warsaw70697632001-06-19 19:08:13 +00009ENCSAMPLE = """\
10Here'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
24characters... have fun!
25"""
26
27# First line ends with a space
Tim Peters87cc0c32001-07-21 01:41:30 +000028DECSAMPLE = "Here's a bunch of special \n" + \
29"""\
Barry Warsaw70697632001-06-19 19:08:13 +000030
31¡¢£¤¥¦§¨©
32ª«¬­®¯°±²³
33´µ¶·¸¹º»¼½¾
34¿ÀÁÂÃÄÅÆ
35ÇÈÉÊËÌÍÎÏ
36ÐÑÒÓÔÕÖ×
37ØÙÚÛÜÝÞß
38àáâãäåæç
39èéêëìíîï
40ðñòóôõö÷
41øùúûüýþÿ
42
43characters... have fun!
44"""
45
46
Tim Peters87cc0c32001-07-21 01:41:30 +000047
Barry Warsaw70697632001-06-19 19:08:13 +000048class 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 Warsaw7599a3f2001-06-19 22:48:42 +000070 # Some long lines. First, a single line of 108 characters
71 ('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxØÙÚÛÜÝÞßxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
72 '''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=D8=D9=DA=DB=DC=DD=DE=DFx=
73xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'''),
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=
82zz'''),
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=
87zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''),
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
93zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz''',
94 '''yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy=
95yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
96zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''),
97 # Now some really complex stuff ;)
Barry Warsaw70697632001-06-19 19:08:13 +000098 (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 Peters87cc0c32001-07-21 01:41:30 +0000106
Barry Warsaw70697632001-06-19 19:08:13 +0000107 def test_encodestring(self):
108 for p, e in self.STRINGS:
109 self.assert_(encodestring(p) == e)
Tim Peters87cc0c32001-07-21 01:41:30 +0000110
Barry Warsaw70697632001-06-19 19:08:13 +0000111 def test_decodestring(self):
112 for p, e in self.STRINGS:
113 self.assert_(decodestring(e) == p)
Tim Peters87cc0c32001-07-21 01:41:30 +0000114
Barry Warsaw70697632001-06-19 19:08:13 +0000115 def test_idempotent_string(self):
116 for p, e in self.STRINGS:
117 self.assert_(decodestring(encodestring(e)) == e)
118
119 def test_encode(self):
120 for p, e in self.STRINGS:
121 infp = StringIO(p)
122 outfp = StringIO()
123 encode(infp, outfp, quotetabs=0)
124 self.assert_(outfp.getvalue() == e)
125
126 def test_decode(self):
127 for p, e in self.STRINGS:
128 infp = StringIO(e)
129 outfp = StringIO()
130 decode(infp, outfp)
131 self.assert_(outfp.getvalue() == p)
132
133 def test_embedded_ws(self):
134 for p, e in self.ESTRINGS:
135 self.assert_(encodestring(p, quotetabs=1) == e)
136 self.assert_(decodestring(e) == p)
137
Tim Peters87cc0c32001-07-21 01:41:30 +0000138
Barry Warsaw70697632001-06-19 19:08:13 +0000139test_support.run_unittest(QuopriTestCase)