]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_encodingutil.py
Rename stringutils to encodingutil, and drop listdir_unicode and open_unicode (since...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_encodingutil.py
1
2 lumiere_nfc = u"lumi\u00E8re"
3 Artonwall_nfc = u"\u00C4rtonwall.mp3"
4 Artonwall_nfd = u"A\u0308rtonwall.mp3"
5
6 TEST_FILENAMES = (
7   Artonwall_nfc,
8   u'test_file',
9   u'Blah blah.txt',
10 )
11
12 # The following main helps to generate a test class for other operating
13 # systems.
14
15 if __name__ == "__main__":
16     import sys
17     import platform
18
19     if len(sys.argv) != 2:
20         print "Usage: %s lumi<e-grave>re" % sys.argv[0]
21         sys.exit(1)
22     
23     print
24     print "class MyWeirdOS(StringUtils, unittest.TestCase):"
25     print "    uname = '%s'" % ' '.join(platform.uname())
26     if sys.platform != "win32":
27         print "    argv = %s" % repr(sys.argv[1])
28     print "    platform = '%s'" % sys.platform
29     print "    filesystem_encoding = '%s'" % sys.getfilesystemencoding()
30     print "    output_encoding = '%s'" % sys.stdout.encoding
31     print "    argv_encoding = '%s'" % (sys.platform == "win32" and 'ascii' or sys.stdout.encoding)
32     print
33
34     sys.exit(0)
35
36 from twisted.trial import unittest
37 from mock import patch
38 import sys
39
40 from allmydata.test.common_util import ReallyEqualMixin
41 from allmydata.util.encodingutil import argv_to_unicode, unicode_to_url, \
42     unicode_to_output, unicode_platform, get_output_encoding, _reload
43
44 from twisted.python import usage
45
46 class StringUtilsErrors(ReallyEqualMixin, unittest.TestCase):
47     def tearDown(self):
48         _reload()
49
50     @patch('sys.stdout')
51     def test_get_output_encoding(self, mock_stdout):
52         mock_stdout.encoding = 'UTF-8'
53         _reload()
54         self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
55
56         mock_stdout.encoding = 'cp65001'
57         _reload()
58         self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
59
60         mock_stdout.encoding = 'koi8-r'
61         _reload()
62         self.failUnlessReallyEqual(get_output_encoding(), 'koi8-r')
63
64         mock_stdout.encoding = 'nonexistent_encoding'
65         self.failUnlessRaises(AssertionError, _reload)
66
67         # TODO: mock_stdout.encoding = None
68
69     @patch('sys.stdout')
70     def test_argv_to_unicode(self, mock):
71         mock.encoding = 'utf-8'
72         _reload()
73
74         self.failUnlessRaises(usage.UsageError,
75                               argv_to_unicode,
76                               lumiere_nfc.encode('latin1'))
77
78     @patch('sys.stdout')
79     def test_unicode_to_output(self, mock):
80         # Encoding koi8-r cannot represent e-grave
81         mock.encoding = 'koi8-r'
82         _reload()
83         self.failUnlessRaises(UnicodeEncodeError, unicode_to_output, lumiere_nfc)
84
85
86 class StringUtils(ReallyEqualMixin):
87     def setUp(self):
88         # Mock sys.platform because unicode_platform() uses it
89         self.original_platform = sys.platform
90         sys.platform = self.platform
91
92     def tearDown(self):
93         sys.platform = self.original_platform
94         _reload()
95
96     @patch('sys.stdout')
97     def test_argv_to_unicode(self, mock):
98         if 'argv' not in dir(self):
99             return
100
101         mock.encoding = self.output_encoding
102         argu = lumiere_nfc
103         argv = self.argv
104         _reload()
105         self.failUnlessReallyEqual(argv_to_unicode(argv), argu)
106
107     def test_unicode_to_url(self):
108         self.failUnless(unicode_to_url(lumiere_nfc), "lumi\xc3\xa8re")
109
110     @patch('sys.stdout')
111     def test_unicode_to_output(self, mock):
112         if 'output' not in dir(self):
113             return
114
115         mock.encoding = self.output_encoding
116         _reload()
117         self.failUnlessReallyEqual(unicode_to_output(lumiere_nfc), self.output)
118
119     def test_unicode_platform(self):
120         matrix = {
121           'linux2': False,
122           'openbsd4': False,
123           'win32':  True,
124           'darwin': True,
125         }
126
127         _reload()
128         self.failUnlessReallyEqual(unicode_platform(), matrix[self.platform])
129  
130
131 class UbuntuKarmicUTF8(StringUtils, unittest.TestCase):
132     uname = 'Linux korn 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64'
133     output = 'lumi\xc3\xa8re'
134     argv = 'lumi\xc3\xa8re'
135     platform = 'linux2'
136     filesystem_encoding = 'UTF-8'
137     output_encoding = 'UTF-8'
138     argv_encoding = 'UTF-8'
139
140 class UbuntuKarmicLatin1(StringUtils, unittest.TestCase):
141     uname = 'Linux korn 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64'
142     output = 'lumi\xe8re'
143     argv = 'lumi\xe8re'
144     platform = 'linux2'
145     filesystem_encoding = 'ISO-8859-1'
146     output_encoding = 'ISO-8859-1'
147     argv_encoding = 'ISO-8859-1'
148
149 class WindowsXP(StringUtils, unittest.TestCase):
150     uname = 'Windows XP 5.1.2600 x86 x86 Family 15 Model 75 Step ping 2, AuthenticAMD'
151     output = 'lumi\x8are'
152     platform = 'win32'
153     filesystem_encoding = 'mbcs'
154     output_encoding = 'cp850'
155     argv_encoding = 'ascii'
156
157 class WindowsXP_UTF8(StringUtils, unittest.TestCase):
158     uname = 'Windows XP 5.1.2600 x86 x86 Family 15 Model 75 Step ping 2, AuthenticAMD'
159     output = 'lumi\xc3\xa8re'
160     platform = 'win32'
161     filesystem_encoding = 'mbcs'
162     output_encoding = 'cp65001'
163     argv_encoding = 'ascii'
164
165 class WindowsVista(StringUtils, unittest.TestCase):
166     uname = 'Windows Vista 6.0.6000 x86 x86 Family 6 Model 15 Stepping 11, GenuineIntel'
167     output = 'lumi\x8are'
168     platform = 'win32'
169     filesystem_encoding = 'mbcs'
170     output_encoding = 'cp850'
171     argv_encoding = 'ascii'
172
173 class MacOSXLeopard(StringUtils, unittest.TestCase):
174     uname = 'Darwin g5.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh powerpc'
175     output = 'lumi\xc3\xa8re'
176     argv = 'lumi\xc3\xa8re'
177     platform = 'darwin'
178     filesystem_encoding = 'utf-8'
179     output_encoding = 'UTF-8'
180     argv_encoding = 'UTF-8'
181
182 class MacOSXLeopard7bit(StringUtils, unittest.TestCase):
183     uname = 'Darwin g5.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh powerpc'
184     platform = 'darwin'
185     filesystem_encoding = 'utf-8'
186     output_encoding = 'US-ASCII'
187     argv_encoding = 'US-ASCII'
188
189 class OpenBSD(StringUtils, unittest.TestCase):
190     uname = 'OpenBSD 4.1 GENERIC#187 i386 Intel(R) Celeron(R) CPU 2.80GHz ("GenuineIntel" 686-class)'
191     platform = 'openbsd4'
192     filesystem_encoding = '646'
193     output_encoding = '646'
194     argv_encoding = '646'
195     # Oops, I cannot write filenames containing non-ascii characters