0001 #!/usr/bin/env python 0002 # 0003 # test_multibytecodec_support.py 0004 # Common Unittest Routines for CJK codecs 0005 # 0006 # $CJKCodecs: test_multibytecodec_support.py,v 1.6 2004/06/19 06:09:55 perky Exp $ 0007 0008 import sys, codecs, os.path 0009 import unittest 0010 from test import test_support 0011 from StringIO import StringIO 0012 0013 __cjkcodecs__ = 0 # define this as 0 for python 0014 0015 class TestBase: 0016 encoding = '' # codec name 0017 codec = None # codec tuple (with 4 elements) 0018 tstring = '' # string to test StreamReader 0019 0020 codectests = None # must set. codec test tuple 0021 roundtriptest = 1 # set if roundtrip is possible with unicode 0022 has_iso10646 = 0 # set if this encoding contains whole iso10646 map 0023 xmlcharnametest = None # string to test xmlcharrefreplace 0024 0025 def setUp(self): 0026 if self.codec is None: 0027 self.codec = codecs.lookup(self.encoding) 0028 self.encode, self.decode, self.reader, self.writer = self.codec 0029 0030 def test_chunkcoding(self): 0031 for native, utf8 in zip(*[StringIO(f).readlines() 0032 for f in self.tstring]): 0033 u = self.decode(native)[0] 0034 self.assertEqual(u, utf8.decode('utf-8')) 0035 if self.roundtriptest: 0036 self.assertEqual(native, self.encode(u)[0]) 0037 0038 def test_errorhandle(self): 0039 for source, scheme, expected in self.codectests: 0040 if type(source) == type(''): 0041 func = self.decode 0042 else: 0043 func = self.encode 0044 if expected: 0045 result = func(source, scheme)[0] 0046 self.assertEqual(result, expected) 0047 else: 0048 self.assertRaises(UnicodeError, func, source, scheme) 0049 0050 if sys.hexversion >= 0x02030000: 0051 def test_xmlcharrefreplace(self): 0052 if self.has_iso10646: 0053 return 0054 0055 s = u"\u0b13\u0b23\u0b60 nd eggs" 0056 self.assertEqual( 0057 self.encode(s, "xmlcharrefreplace")[0], 0058 "ଓଣୠ nd eggs" 0059 ) 0060 0061 def test_customreplace(self): 0062 if self.has_iso10646: 0063 return 0064 0065 import htmlentitydefs 0066 0067 names = {} 0068 for (key, value) in htmlentitydefs.entitydefs.items(): 0069 if len(value)==1: 0070 names[value.decode('latin-1')] = self.decode(key)[0] 0071 else: 0072 names[unichr(int(value[2:-1]))] = self.decode(key)[0] 0073 0074 def xmlcharnamereplace(exc): 0075 if not isinstance(exc, UnicodeEncodeError): 0076 raise TypeError("don't know how to handle %r" % exc) 0077 l = [] 0078 for c in exc.object[exc.start:exc.end]: 0079 try: 0080 l.append(u"&%s;" % names[c]) 0081 except KeyError: 0082 l.append(u"&#%d;" % ord(c)) 0083 return (u"".join(l), exc.end) 0084 0085 codecs.register_error( 0086 "test.xmlcharnamereplace", xmlcharnamereplace) 0087 0088 if self.xmlcharnametest: 0089 sin, sout = self.xmlcharnametest 0090 else: 0091 sin = u"\xab\u211c\xbb = \u2329\u1234\u232a" 0092 sout = "«ℜ» = ⟨ሴ⟩" 0093 self.assertEqual(self.encode(sin, 0094 "test.xmlcharnamereplace")[0], sout) 0095 0096 def test_streamreader(self): 0097 UTF8Writer = codecs.getwriter('utf-8') 0098 for name in ["read", "readline", "readlines"]: 0099 for sizehint in [None, -1] + range(1, 33) + \ 0100 [64, 128, 256, 512, 1024]: 0101 istream = self.reader(StringIO(self.tstring[0])) 0102 ostream = UTF8Writer(StringIO()) 0103 func = getattr(istream, name) 0104 while 1: 0105 data = func(sizehint) 0106 if not data: 0107 break 0108 if name == "readlines": 0109 ostream.writelines(data) 0110 else: 0111 ostream.write(data) 0112 0113 self.assertEqual(ostream.getvalue(), self.tstring[1]) 0114 0115 def test_streamwriter(self): 0116 if __cjkcodecs__: 0117 readfuncs = ('read', 'readline', 'readlines') 0118 else: 0119 # standard utf8 codec has broken readline and readlines. 0120 readfuncs = ('read',) 0121 UTF8Reader = codecs.getreader('utf-8') 0122 for name in readfuncs: 0123 for sizehint in [None] + range(1, 33) + \ 0124 [64, 128, 256, 512, 1024]: 0125 istream = UTF8Reader(StringIO(self.tstring[1])) 0126 ostream = self.writer(StringIO()) 0127 func = getattr(istream, name) 0128 while 1: 0129 if sizehint is not None: 0130 data = func(sizehint) 0131 else: 0132 data = func() 0133 0134 if not data: 0135 break 0136 if name == "readlines": 0137 ostream.writelines(data) 0138 else: 0139 ostream.write(data) 0140 0141 self.assertEqual(ostream.getvalue(), self.tstring[0]) 0142 0143 if len(u'\U00012345') == 2: # ucs2 build 0144 _unichr = unichr 0145 def unichr(v): 0146 if v >= 0x10000: 0147 return _unichr(0xd800 + ((v - 0x10000) >> 10)) + \ 0148 _unichr(0xdc00 + ((v - 0x10000) & 0x3ff)) 0149 else: 0150 return _unichr(v) 0151 _ord = ord 0152 def ord(c): 0153 if len(c) == 2: 0154 return 0x10000 + ((_ord(c[0]) - 0xd800) << 10) + \ 0155 (ord(c[1]) - 0xdc00) 0156 else: 0157 return _ord(c) 0158 0159 class TestBase_Mapping(unittest.TestCase): 0160 pass_enctest = [] 0161 pass_dectest = [] 0162 supmaps = [] 0163 0164 def __init__(self, *args, **kw): 0165 unittest.TestCase.__init__(self, *args, **kw) 0166 if not os.path.exists(self.mapfilename): 0167 raise test_support.TestSkipped('%s not found, download from %s' % 0168 (self.mapfilename, self.mapfileurl)) 0169 0170 def test_mapping_file(self): 0171 unichrs = lambda s: u''.join(map(unichr, map(eval, s.split('+')))) 0172 urt_wa = {} 0173 0174 for line in open(self.mapfilename): 0175 if not line: 0176 break 0177 data = line.split('#')[0].strip().split() 0178 if len(data) != 2: 0179 continue 0180 0181 csetval = eval(data[0]) 0182 if csetval <= 0x7F: 0183 csetch = chr(csetval & 0xff) 0184 elif csetval >= 0x1000000: 0185 csetch = chr(csetval >> 24) + chr((csetval >> 16) & 0xff) + \ 0186 chr((csetval >> 8) & 0xff) + chr(csetval & 0xff) 0187 elif csetval >= 0x10000: 0188 csetch = chr(csetval >> 16) + \ 0189 chr((csetval >> 8) & 0xff) + chr(csetval & 0xff) 0190 elif csetval >= 0x100: 0191 csetch = chr(csetval >> 8) + chr(csetval & 0xff) 0192 else: 0193 continue 0194 0195 unich = unichrs(data[1]) 0196 if ord(unich) == 0xfffd or urt_wa.has_key(unich): 0197 continue 0198 urt_wa[unich] = csetch 0199 0200 self._testpoint(csetch, unich) 0201 0202 def test_mapping_supplemental(self): 0203 for mapping in self.supmaps: 0204 self._testpoint(*mapping) 0205 0206 def _testpoint(self, csetch, unich): 0207 if (csetch, unich) not in self.pass_enctest: 0208 self.assertEqual(unich.encode(self.encoding), csetch) 0209 if (csetch, unich) not in self.pass_dectest: 0210 self.assertEqual(unicode(csetch, self.encoding), unich) 0211 0212 def load_teststring(encoding): 0213 if __cjkcodecs__: 0214 etxt = open(os.path.join('sampletexts', encoding) + '.txt').read() 0215 utxt = open(os.path.join('sampletexts', encoding) + '.utf8').read() 0216 return (etxt, utxt) 0217 else: 0218 from test import cjkencodings_test 0219 return cjkencodings_test.teststring[encoding] 0220 0221 def register_skip_expected(*cases): 0222 for case in cases: # len(cases) must be 1 at least. 0223 for path in [os.path.curdir, os.path.pardir]: 0224 fn = os.path.join(path, case.mapfilename) 0225 if os.path.exists(fn): 0226 case.mapfilename = fn 0227 break 0228 else: 0229 sys.modules[case.__module__].skip_expected = True 0230 break 0231 else: 0232 sys.modules[case.__module__].skip_expected = False 0233
Generated by PyXR 0.9.4