2 lumiere_nfc = u"lumi\u00E8re"
3 Artonwall_nfc = u"\u00C4rtonwall.mp3"
4 Artonwall_nfd = u"A\u0308rtonwall.mp3"
12 # The following main helps to generate a test class for other operating
15 if __name__ == "__main__":
19 if len(sys.argv) != 2:
20 print "Usage: %s lumi<e-grave>re" % sys.argv[0]
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)
36 from twisted.trial import unittest
37 from mock import patch
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
44 from twisted.python import usage
46 class StringUtilsErrors(ReallyEqualMixin, unittest.TestCase):
51 def test_get_output_encoding(self, mock_stdout):
52 mock_stdout.encoding = 'UTF-8'
54 self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
56 mock_stdout.encoding = 'cp65001'
58 self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
60 mock_stdout.encoding = 'koi8-r'
62 self.failUnlessReallyEqual(get_output_encoding(), 'koi8-r')
64 mock_stdout.encoding = 'nonexistent_encoding'
65 self.failUnlessRaises(AssertionError, _reload)
67 # TODO: mock_stdout.encoding = None
70 def test_argv_to_unicode(self, mock):
71 mock.encoding = 'utf-8'
74 self.failUnlessRaises(usage.UsageError,
76 lumiere_nfc.encode('latin1'))
79 def test_unicode_to_output(self, mock):
80 # Encoding koi8-r cannot represent e-grave
81 mock.encoding = 'koi8-r'
83 self.failUnlessRaises(UnicodeEncodeError, unicode_to_output, lumiere_nfc)
86 class StringUtils(ReallyEqualMixin):
88 # Mock sys.platform because unicode_platform() uses it
89 self.original_platform = sys.platform
90 sys.platform = self.platform
93 sys.platform = self.original_platform
97 def test_argv_to_unicode(self, mock):
98 if 'argv' not in dir(self):
101 mock.encoding = self.output_encoding
105 self.failUnlessReallyEqual(argv_to_unicode(argv), argu)
107 def test_unicode_to_url(self):
108 self.failUnless(unicode_to_url(lumiere_nfc), "lumi\xc3\xa8re")
111 def test_unicode_to_output(self, mock):
112 if 'output' not in dir(self):
115 mock.encoding = self.output_encoding
117 self.failUnlessReallyEqual(unicode_to_output(lumiere_nfc), self.output)
119 def test_unicode_platform(self):
128 self.failUnlessReallyEqual(unicode_platform(), matrix[self.platform])
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'
136 filesystem_encoding = 'UTF-8'
137 output_encoding = 'UTF-8'
138 argv_encoding = 'UTF-8'
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'
145 filesystem_encoding = 'ISO-8859-1'
146 output_encoding = 'ISO-8859-1'
147 argv_encoding = 'ISO-8859-1'
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'
153 filesystem_encoding = 'mbcs'
154 output_encoding = 'cp850'
155 argv_encoding = 'ascii'
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'
161 filesystem_encoding = 'mbcs'
162 output_encoding = 'cp65001'
163 argv_encoding = 'ascii'
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'
169 filesystem_encoding = 'mbcs'
170 output_encoding = 'cp850'
171 argv_encoding = 'ascii'
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'
178 filesystem_encoding = 'utf-8'
179 output_encoding = 'UTF-8'
180 argv_encoding = 'UTF-8'
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'
185 filesystem_encoding = 'utf-8'
186 output_encoding = 'US-ASCII'
187 argv_encoding = 'US-ASCII'
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