Package cssutils :: Package tests :: Package encutils
[hide private]
[frames] | no frames]

Source Code for Package cssutils.tests.encutils

  1  """ 
  2  tests for encutils.py 
  3  """ 
  4  __version__ = '$Id: __init__.py 1143 2008-03-16 17:18:41Z cthedot $' 
  5   
  6  import httplib 
  7  from StringIO import StringIO 
  8  import sys 
  9  import unittest 
 10   
 11  try: 
 12      import encutils 
 13  except ImportError: 
 14      import cssutils.encutils as encutils 
 15   
 16  # helper log 
 17  log = encutils.buildlog(stream=StringIO())     
 18   
19 -class AutoEncodingTestCase(unittest.TestCase):
20
21 - def _fakeRes(self, content):
22 "build a fake HTTP response" 23 class FakeRes: 24 def __init__(self, content): 25 fp = StringIO(content) 26 self._info = httplib.HTTPMessage(fp)
27 28 def info(self): 29 return self._info
30 return FakeRes(content) 31
32 - def test_getTextTypeByMediaType(self):
33 "encutils._getTextTypeByMediaType" 34 tests = { 35 'application/xml': encutils._XML_APPLICATION_TYPE, 36 'application/xml-dtd': encutils._XML_APPLICATION_TYPE, 37 'application/xml-external-parsed-entity': encutils._XML_APPLICATION_TYPE, 38 'application/xhtml+xml': encutils._XML_APPLICATION_TYPE, 39 'text/xml': encutils._XML_TEXT_TYPE, 40 'text/xml-external-parsed-entity': encutils._XML_TEXT_TYPE, 41 'text/xhtml+xml': encutils._XML_TEXT_TYPE, 42 'text/html': encutils._HTML_TEXT_TYPE, 43 'text/css': encutils._TEXT_UTF8, 44 'text/plain': encutils._TEXT_TYPE, 45 'x/x': encutils._OTHER_TYPE, 46 'ANYTHING': encutils._OTHER_TYPE 47 } 48 for test, exp in tests.items(): 49 self.assertEqual( 50 exp, encutils._getTextTypeByMediaType(test, log=log))
51
52 - def test_getTextType(self):
53 "encutils._getTextType" 54 tests = { 55 u'\x00\x00\xFE\xFF<?xml version="1.0"': encutils._XML_APPLICATION_TYPE, 56 u'\xFF\xFE\x00\x00<?xml version="1.0"': encutils._XML_APPLICATION_TYPE, 57 u'\xFE\xFF<?xml version="1.0"': encutils._XML_APPLICATION_TYPE, 58 u'\xFF\xFE<?xml version="1.0"': encutils._XML_APPLICATION_TYPE, 59 u'\xef\xbb\xbf<?xml version="1.0"': encutils._XML_APPLICATION_TYPE, 60 u'<?xml version="1.0"': encutils._XML_APPLICATION_TYPE, 61 u'\x00\x00\xFE\xFFanything': encutils._OTHER_TYPE, 62 u'\xFF\xFE\x00\x00anything': encutils._OTHER_TYPE, 63 u'\xFE\xFFanything': encutils._OTHER_TYPE, 64 u'\xFF\xFEanything': encutils._OTHER_TYPE, 65 u'\xef\xbb\xbfanything': encutils._OTHER_TYPE, 66 u'x/x': encutils._OTHER_TYPE, 67 u'ANYTHING': encutils._OTHER_TYPE 68 } 69 for test, exp in tests.items(): 70 self.assertEqual( 71 exp, encutils._getTextType(test, log=log))
72
73 - def test_encodingByMediaType(self):
74 "encutils.encodingByMediaType" 75 tests = { 76 'application/xml': 'utf-8', 77 'application/xml-dtd': 'utf-8', 78 'application/xml-external-parsed-entity': 'utf-8', 79 'application/ANYTHING+xml': 'utf-8', 80 ' application/xml ': 'utf-8', 81 'text/xml': 'ascii', 82 'text/xml-external-parsed-entity': 'ascii', 83 'text/ANYTHING+xml': 'ascii', 84 'text/html': 'iso-8859-1', 85 'text/css': 'utf-8', 86 'text/plain': 'iso-8859-1', 87 'ANYTHING': None 88 } 89 for test, exp in tests.items(): 90 self.assertEqual(exp, 91 encutils.encodingByMediaType(test, log=log))
92
93 - def test_getMetaInfo(self):
94 "encutils.getMetaInfo" 95 tests = { 96 """<meta tp-equiv='Content-Type' content='text/html; charset=ascii'>""": 97 (None, None), 98 """<meta http-equiv='ontent-Type' content='text/html; charset=ascii'>""": 99 (None, None), 100 101 """<meta http-equiv='Content-Type' content='text/html'>""": 102 ('text/html', None), 103 104 """<meta content='text/html' http-equiv='Content-Type'>""": 105 ('text/html', None), 106 """<meta content='text/html;charset=ascii' http-equiv='Content-Type'>""": 107 ('text/html', 'ascii'), 108 109 """<meta http-equiv='Content-Type' content='text/html ;charset=ascii'>""": 110 ('text/html', 'ascii'), 111 """<meta content='text/html;charset=iso-8859-1' http-equiv='Content-Type'>""": 112 ('text/html', 'iso-8859-1'), 113 """<meta http-equiv="Content-Type" content="text/html;charset = ascii">""": 114 ('text/html', 'ascii'), 115 116 """<meta http-equiv="Content-Type" content="text/html;charset=ascii;x=2">""": 117 ('text/html', 'ascii'), 118 """<meta http-equiv="Content-Type" content="text/html;x=2;charset=ascii">""": 119 ('text/html', 'ascii'), 120 """<meta http-equiv="Content-Type" content="text/html;x=2;charset=ascii;y=2">""": 121 ('text/html', 'ascii'), 122 123 """<meta http-equiv='Content-Type' content="text/html;charset=ascii">""": 124 ('text/html', 'ascii'), 125 """<meta http-equiv='Content-Type' content='text/html;charset=ascii' />""": 126 ('text/html', 'ascii'), 127 """<meta http-equiv = " Content-Type" content = " text/html;charset=ascii " >""": 128 ('text/html', 'ascii'), 129 """<meta http-equiv = " \n Content-Type " content = " \t text/html ; charset=ascii " >""": 130 ('text/html', 'ascii'), 131 132 """<meta content="text/html;charset=ascii" http-equiv="Content-Type">""": 133 ('text/html', 'ascii'), 134 """<meta content="text/html;charset=ascii" http-equiv="cONTENT-type">""": 135 ('text/html', 'ascii') 136 } 137 for test, exp in tests.items(): 138 self.assertEqual(exp, encutils.getMetaInfo(test, log=log))
139
140 - def test_detectXMLEncoding(self):
141 "encutils.detectXMLEncoding" 142 tests = { 143 # BOM 144 ('utf_32_be'): u'\x00\x00\xFE\xFFanything', 145 ('utf_32_le'): u'\xFF\xFE\x00\x00anything', 146 ('utf_16_be'): u'\xFE\xFFanything', 147 ('utf_16_le'): u'\xFF\xFEanything', 148 ('utf-8'): u'\xef\xbb\xbfanything', 149 # encoding= 150 ('ascii'): '<?xml version="1.0" encoding="ascii" ?>', 151 ('ascii'): "<?xml version='1.0' encoding='ascii' ?>", 152 ('iso-8859-1'): "<?xml version='1.0' encoding='iso-8859-1' ?>", 153 # default 154 ('utf-8'): '<?xml version="1.0" ?>', 155 ('utf-8'): '<?xml version="1.0"?><x encoding="ascii"/>' 156 } 157 for exp, test in tests.items(): 158 self.assertEqual(exp, encutils.detectXMLEncoding(test, log=log))
159
160 - def test_tryEncodings(self):
161 "encutils.tryEncodings" 162 try: 163 import chardet 164 tests = [ 165 ('ascii', 'abc'), 166 ('windows-1252', u'\xf6'), 167 ('ascii', u'\u1111') 168 ] 169 except ImportError: 170 tests = [ 171 ('ascii', 'abc'), 172 ('iso-8859-1', u'\xf6'), 173 ('utf-8', u'\u1111') 174 ] 175 for exp, test in tests: 176 self.assertEqual(exp, encutils.tryEncodings(test))
177 178 # (expectedencoding, expectedmismatch): (httpheader, filecontent) 179 fulltests = { 180 ('utf-8', False): ( 181 '''NoContentType''', '''OnlyText'''), 182 183 # --- application/xhtml+xml --- 184 # default enc 185 ('utf-8', False): ( 186 '''Content-Type: application/xhtml+xml''', 187 '''<?xml version="1.0" ?> 188 <example> 189 <meta http-equiv="Content-Type" 190 content="application/xhtml+xml"/> 191 </example>'''), 192 # header enc 193 ('iso-h', True): ( 194 '''Content-Type: application/xhtml+xml;charset=iso-H''', 195 '''<?xml version="1.0" ?> 196 <example> 197 <meta http-equiv="Content-Type" 198 content="application/xhtml+xml"/> 199 </example>'''), 200 # mismatch header - meta, meta ignored 201 ('iso-h', True): ( 202 '''Content-Type: application/xhtml+xml;charset=iso-H''', 203 '''<?xml version="1.0" ?> 204 <example> 205 <meta http-equiv="Content-Type" 206 content="application/xhtml+xml;charset=iso_M"/> 207 </example>'''), 208 # mismatch XML - meta, meta ignored 209 ('iso-x', False): ( 210 '''Content-Type: application/xhtml+xml''', 211 '''<?xml version="1.0" encoding="iso-X" ?> 212 <example> 213 <meta http-equiv="Content-Type" 214 content="application/xhtml+xml;charset=iso_M"/> 215 </example>'''), 216 # mismatch header and XML, header wins 217 ('iso-h', True): ( 218 '''Content-Type: application/xhtml+xml;charset=iso-H''', 219 '''<?xml version="1.0" encoding="iso-X" ?> 220 <example/>'''), 221 222 # --- text/xml --- 223 # default enc 224 ('ascii', False): ( 225 '''Content-Type: text/xml''', 226 '''<?xml version="1.0" ?> 227 <example> 228 <meta http-equiv="Content-Type" 229 content="text/xml"/> 230 </example>'''), 231 # header enc 232 ('iso-h', True): ( 233 '''Content-Type: text/xml;charset=iso-H''', 234 '''<?xml version="1.0" ?> 235 <example> 236 <meta http-equiv="Content-Type" 237 content="text/xml"/> 238 </example>'''), 239 # mismatch header - meta, meta ignored 240 ('iso-h', True): ( 241 '''Content-Type: text/xml;charset=iso-H''', 242 '''<?xml version="1.0" ?> 243 <example> 244 <meta http-equiv="Content-Type" 245 content="text/xml;charset=iso_M"/> 246 </example>'''), 247 # XML - meta, both ignored, use HTTP, meta completely ignored 248 ('ascii', False): ( 249 '''Content-Type: text/xml''', 250 '''<?xml version="1.0" encoding="iso-X" ?> 251 <example> 252 <meta http-equiv="Content-Type" 253 content="text/xml;charset=iso_M"/> 254 </example>'''), 255 # mismatch header and XML, XML ignored 256 ('iso-h', True): ( 257 '''Content-Type: text/xml;charset=iso-H''', 258 '''<?xml version="1.0" encoding="iso-X" ?> 259 <example/>'''), 260 261 # --- text/html --- 262 # no default enc 263 (None, False): ('Content-Type: text/html;', 264 '''<meta http-equiv="Content-Type" 265 content="text/html">'''), 266 # header enc 267 ('iso-h', True): ('Content-Type: text/html;charset=iso-H', 268 '''<meta http-equiv="Content-Type" 269 content="text/html">'''), 270 # meta enc 271 ('iso-m', False): ('Content-Type: text/html', 272 '''<meta http-equiv="Content-Type" 273 content="text/html;charset=iso-m">'''), 274 # mismatch header - meta, header wins 275 ('iso-h', True): ('Content-Type: text/html;charset=iso-H', 276 '''<meta http-equiv="Content-Type" 277 content="text/html;charset=iso-m">'''), 278 279 # no header: 280 (None, False): (None, 281 '''<meta http-equiv="Content-Type" 282 content="text/html;charset=iso-m">'''), 283 (None, False): (None, '''text'''), 284 ('utf-8', False): (None, '''<?xml version='''), 285 ('utf-8', False): (None, '''<?xml version='''), 286 ('iso-x', False): (None, '''<?xml version="1.0" encoding="iso-X"?>''') 287 } 288
289 - def test_getEncodingInfo(self):
290 "encutils.getEncodingInfo" 291 for exp, test in self.fulltests.items(): 292 header, text = test 293 if header: 294 res = encutils.getEncodingInfo(self._fakeRes(header), text) 295 else: 296 res = encutils.getEncodingInfo(text=text) 297 res = (res.encoding, res.mismatch) 298 self.assertEqual(exp, res)
299 300 301 if __name__ == '__main__': 302 unittest.main() 303