7 from test
import test_support
9 import sys
, os
, uu
, cStringIO
12 plaintext
= "The smooth-scaled python crept over the sleeping dog\n"
15 M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
18 encodedtextwrapped
= "begin %03o %s\n" + encodedtext
.replace("%", "%%") + "\n \nend\n"
20 class UUTest(unittest
.TestCase
):
22 def test_encode(self
):
23 inp
= cStringIO
.StringIO(plaintext
)
24 out
= cStringIO
.StringIO()
25 uu
.encode(inp
, out
, "t1")
26 self
.assertEqual(out
.getvalue(), encodedtextwrapped
% (0666, "t1"))
27 inp
= cStringIO
.StringIO(plaintext
)
28 out
= cStringIO
.StringIO()
29 uu
.encode(inp
, out
, "t1", 0644)
30 self
.assertEqual(out
.getvalue(), encodedtextwrapped
% (0644, "t1"))
32 def test_decode(self
):
33 inp
= cStringIO
.StringIO(encodedtextwrapped
% (0666, "t1"))
34 out
= cStringIO
.StringIO()
36 self
.assertEqual(out
.getvalue(), plaintext
)
37 inp
= cStringIO
.StringIO(
38 "UUencoded files may contain many lines,\n" +
39 "even some that have 'begin' in them.\n" +
40 encodedtextwrapped
% (0666, "t1")
42 out
= cStringIO
.StringIO()
44 self
.assertEqual(out
.getvalue(), plaintext
)
46 def test_truncatedinput(self
):
47 inp
= cStringIO
.StringIO("begin 644 t1\n" + encodedtext
)
48 out
= cStringIO
.StringIO()
51 self
.fail("No exception thrown")
53 self
.assertEqual(str(e
), "Truncated input file")
55 def test_missingbegin(self
):
56 inp
= cStringIO
.StringIO("")
57 out
= cStringIO
.StringIO()
60 self
.fail("No exception thrown")
62 self
.assertEqual(str(e
), "No valid begin line found in input file")
64 class UUStdIOTest(unittest
.TestCase
):
67 self
.stdin
= sys
.stdin
68 self
.stdout
= sys
.stdout
71 sys
.stdin
= self
.stdin
72 sys
.stdout
= self
.stdout
74 def test_encode(self
):
75 sys
.stdin
= cStringIO
.StringIO(plaintext
)
76 sys
.stdout
= cStringIO
.StringIO()
77 uu
.encode("-", "-", "t1", 0666)
79 sys
.stdout
.getvalue(),
80 encodedtextwrapped
% (0666, "t1")
83 def test_decode(self
):
84 sys
.stdin
= cStringIO
.StringIO(encodedtextwrapped
% (0666, "t1"))
85 sys
.stdout
= cStringIO
.StringIO()
87 self
.assertEqual(sys
.stdout
.getvalue(), plaintext
)
89 class UUFileTest(unittest
.TestCase
):
92 # close and remove file
95 except (SystemExit, KeyboardInterrupt):
101 except (SystemExit, KeyboardInterrupt):
107 self
.tmpin
= test_support
.TESTFN
+ "i"
108 self
.tmpout
= test_support
.TESTFN
+ "o"
114 def test_encode(self
):
117 test_support
.unlink(self
.tmpin
)
118 fin
= open(self
.tmpin
, 'wb')
122 fin
= open(self
.tmpin
, 'rb')
123 fout
= open(self
.tmpout
, 'w')
124 uu
.encode(fin
, fout
, self
.tmpin
, mode
=0644)
128 fout
= open(self
.tmpout
, 'r')
131 self
.assertEqual(s
, encodedtextwrapped
% (0644, self
.tmpin
))
133 # in_file and out_file as filenames
134 uu
.encode(self
.tmpin
, self
.tmpout
, self
.tmpin
, mode
=0644)
135 fout
= open(self
.tmpout
, 'r')
138 self
.assertEqual(s
, encodedtextwrapped
% (0644, self
.tmpin
))
144 def test_decode(self
):
147 test_support
.unlink(self
.tmpin
)
148 f
= open(self
.tmpin
, 'w')
149 f
.write(encodedtextwrapped
% (0644, self
.tmpout
))
152 f
= open(self
.tmpin
, 'r')
156 f
= open(self
.tmpout
, 'r')
159 self
.assertEqual(s
, plaintext
)
160 # XXX is there an xp way to verify the mode?
164 def test_decodetwice(self
):
165 # Verify that decode() will refuse to overwrite an existing file
168 f
= cStringIO
.StringIO(encodedtextwrapped
% (0644, self
.tmpout
))
170 f
= open(self
.tmpin
, 'r')
174 f
= open(self
.tmpin
, 'r')
175 self
.assertRaises(uu
.Error
, uu
.decode
, f
)
181 test_support
.run_unittest(UUTest
, UUStdIOTest
, UUFileTest
)
183 if __name__
=="__main__":