Package cssutils :: Package tests :: Module test_codec |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.tests.test_codec

      1  """
      2  testcases for cssutils.codec
      3  """
      4  __author__ = '$LastChangedBy: cthedot $'
      5  __date__ = '$LastChangedDate: 2008-01-12 21:28:54 +0100 (Sa, 12 Jan 2008) $'
      6  __version__ = '$LastChangedRevision: 831 $'
      7
      8  import unittest
      9
     10  import codecs
     11
     12  from cssutils import codec
     13
     14
     15  try:
     16      codecs.lookup("utf-32")
     17  except LookupError:
     18      haveutf32 = False
     19  else:
     20      haveutf32 = True
     21
     22
     23 -class Queue(object):
     24      """
     25      queue: write bytes at one end, read bytes from the other end
     26      """
     27 -    def __init__(self):
     28          self._buffer = ""
     29
     30 -    def write(self, chars):
     31          self._buffer += chars
     32
     33 -    def read(self, size=-1):
     34          if size<0:
     35              s = self._buffer
     36              self._buffer = ""
     37              return s
     38          else:
     39              s = self._buffer[:size]
     40              self._buffer = self._buffer[size:]
     41              return s
     42
     43
     44 -class CodecTestCase(unittest.TestCase):
     45
     46 -    def test_detectencoding_str(self):
     47          "codec._detectencoding_str()"
     48          self.assert_(codec._detectencoding_str('') is None)
     49          self.assert_(codec._detectencoding_str('\xef') is None)
     50          self.assertEqual(codec._detectencoding_str('\xef\x33'), "utf-8")
     51          self.assert_(codec._detectencoding_str('\xef\xbb') is None)
     52          self.assertEqual(codec._detectencoding_str('\xef\xbb\x33'), "utf-8")
     53          self.assertEqual(codec._detectencoding_str('\xef\xbb\xbf'), "utf-8-sig")
     54          self.assert_(codec._detectencoding_str('\xff') is None)
     55          self.assertEqual(codec._detectencoding_str('\xff\x33'), "utf-8")
     56          self.assert_(codec._detectencoding_str('\xff\xfe') is None)
     57          self.assertEqual(codec._detectencoding_str('\xff\xfe\x33'), "utf-16")
     58          self.assert_(codec._detectencoding_str('\xff\xfe\x00') is None)
     59          self.assertEqual(codec._detectencoding_str('\xff\xfe\x00\x33'), "utf-16")
     60          self.assertEqual(codec._detectencoding_str('\xff\xfe\x00\x00'), "utf-32")
     61          self.assert_(codec._detectencoding_str('\x00') is None)
     62          self.assertEqual(codec._detectencoding_str('\x00\x33'), "utf-8")
     63          self.assert_(codec._detectencoding_str('\x00\x00') is None)
     64          self.assertEqual(codec._detectencoding_str('\x00\x00\x33'), "utf-8")
     65          self.assert_(codec._detectencoding_str('\x00\x00\xfe') is None)
     66          self.assertEqual(codec._detectencoding_str('\x00\x00\x00\x33'), "utf-8")
     67          self.assertEqual(codec._detectencoding_str('\x00\x00\x00@'), "utf-32-be")
     68          self.assertEqual(codec._detectencoding_str('\x00\x00\xfe\xff'), "utf-32")
     69          self.assert_(codec._detectencoding_str('@') is None)
     70          self.assertEqual(codec._detectencoding_str('@\x33'), "utf-8")
     71          self.assert_(codec._detectencoding_str('@\x00') is None)
     72          self.assertEqual(codec._detectencoding_str('@\x00\x33'), "utf-8")
     73          self.assert_(codec._detectencoding_str('@\x00\x00') is None)
     74          self.assertEqual(codec._detectencoding_str('@\x00\x00\x33'), "utf-8")
     75          self.assertEqual(codec._detectencoding_str('@\x00\x00\x00'), "utf-32-le")
     76          self.assert_(codec._detectencoding_str('@c') is None)
     77          self.assert_(codec._detectencoding_str('@ch') is None)
     78          self.assert_(codec._detectencoding_str('@cha') is None)
     79          self.assert_(codec._detectencoding_str('@char') is None)
     80          self.assert_(codec._detectencoding_str('@chars') is None)
     81          self.assert_(codec._detectencoding_str('@charse') is None)
     82          self.assert_(codec._detectencoding_str('@charset') is None)
     83          self.assert_(codec._detectencoding_str('@charset ') is None)
     84          self.assert_(codec._detectencoding_str('@charset "') is None)
     85          self.assert_(codec._detectencoding_str('@charset "x') is None)
     86          self.assertEqual(codec._detectencoding_str('@charset ""'), "")
     87          self.assertEqual(codec._detectencoding_str('@charset "x"'), "x")
     88          self.assert_(codec._detectencoding_str("@", False) is None)
     89          self.assertEqual(codec._detectencoding_str("@", True), "utf-8")
     90          self.assert_(codec._detectencoding_str("@c", False) is None)
     91          self.assertEqual(codec._detectencoding_str("@c", True), "utf-8")
     92
     93 -    def test_detectencoding_unicode(self):
     94          "codec._detectencoding_unicode()"
     95          # Unicode version (only parses the header)
     96          self.assert_(codec._detectencoding_unicode(u'@charset "x') is None)
     97          self.assertEqual(codec._detectencoding_unicode(u'@charset "x', True), None)
     98          self.assertEqual(codec._detectencoding_unicode(u'@charset "x"'), "x")
     99
    100 -    def test_fixencoding(self):
    101          "codec._fixencoding()"
    102          s = u'@charset "'
    103          self.assert_(codec._fixencoding(s, u"utf-8") is None)
    104
    105          s = u'@charset "x'
    106          self.assert_(codec._fixencoding(s, u"utf-8") is None)
    107
    108          s = u'@charset "x'
    109          self.assertEqual(codec._fixencoding(s, u"utf-8", True), s)
    110
    111          s = u'@charset x'
    112          self.assertEqual(codec._fixencoding(s, u"utf-8"), s)
    113
    114          s = u'@charset "x"'
    115          self.assertEqual(codec._fixencoding(s, u"utf-8"), s.replace('"x"', '"utf-8"'))
    116
    117 -    def test_decoder(self):
    118          "codecs.decoder"
    119          def checkauto(encoding, input=u'@charset "x";g\xfcrk\u20ac{}'):
    120              outputencoding = encoding
    121              if outputencoding == "utf-8-sig":
    122                  outputencoding = "utf-8"
    123              # Check stateless decoder with encoding autodetection
    124              d = codecs.getdecoder("css")
    125              self.assertEqual(d(input.encode(encoding))[0], input.replace('"x"', '"%s"' % outputencoding))
    126
    127              # Check stateless decoder with specified encoding
    128              self.assertEqual(d(input.encode(encoding), encoding=encoding)[0], input.replace('"x"', '"%s"' % outputencoding))
    129
    130              if hasattr(codec, "getincrementaldecoder"):
    131                  # Check incremental decoder with encoding autodetection
    132                  id = codecs.getincrementaldecoder("css")()
    133                  self.assertEqual("".join(id.iterdecode(input.encode(encoding))), input.replace('"x"', '"%s"' % outputencoding))
    134
    135                  # Check incremental decoder with specified encoding
    136                  id = codecs.getincrementaldecoder("css")(encoding=encoding)
    137                  self.assertEqual("".join(id.iterdecode(input.encode(encoding))), input.replace('"x"', '"%s"' % outputencoding))
    138
    139              # Check stream reader with encoding autodetection
    140              q = Queue()
    141              sr = codecs.getreader("css")(q)
    142              result = []
    143              for c in input.encode(encoding):
    144                  q.write(c)
    145                  result.append(sr.read())
    146              self.assertEqual("".join(result), input.replace('"x"', '"%s"' % outputencoding))
    147
    148              # Check stream reader with specified encoding
    149              q = Queue()
    150              sr = codecs.getreader("css")(q, encoding=encoding)
    151              result = []
    152              for c in input.encode(encoding):
    153                  q.write(c)
    154                  result.append(sr.read())
    155              self.assertEqual("".join(result), input.replace('"x"', '"%s"' % outputencoding))
    156
    157          # Autodetectable encodings
    158          #checkauto("utf-8-sig")
    159          checkauto("utf-16")
    160          checkauto("utf-16-le")
    161          checkauto("utf-16-be")
    162          if haveutf32:
    163              checkauto("utf-32")
    164              checkauto("utf-32-le")
    165              checkauto("utf-32-be")
    166
    167          def checkdecl(encoding, input=u'@charset "%s";g\xfcrk{}'):
    168              # Check stateless decoder with encoding autodetection
    169              d = codecs.getdecoder("css")
    170              input = input % encoding
    171              outputencoding = encoding
    172              if outputencoding == "utf-8-sig":
    173                  outputencoding = "utf-8"
    174              self.assertEqual(d(input.encode(encoding))[0], input)
    175
    176              # Check stateless decoder with specified encoding
    177              self.assertEqual(d(input.encode(encoding), encoding=encoding)[0], input)
    178
    179              if hasattr(codec, "getincrementaldecoder"):
    180                  # Check incremental decoder with encoding autodetection
    181                  id = codecs.getincrementaldecoder("css")()
    182                  self.assertEqual("".join(id.iterdecode(input.encode(encoding))), input)
    183
    184                  # Check incremental decoder with specified encoding
    185                  id = codecs.getincrementaldecoder("css")(encoding)
    186                  self.assertEqual("".join(id.iterdecode(input.encode(encoding))), input)
    187
    188              # Check stream reader with encoding autodetection
    189              q = Queue()
    190              sr = codecs.getreader("css")(q)
    191              result = []
    192              for c in input.encode(encoding):
    193                  q.write(c)
    194                  result.append(sr.read())
    195              self.assertEqual("".join(result), input)
    196
    197              # Check stream reader with specified encoding
    198              q = Queue()
    199              sr = codecs.getreader("css")(q, encoding=encoding)
    200              result = []
    201              for c in input.encode(encoding):
    202                  q.write(c)
    203                  result.append(sr.read())
    204              self.assertEqual("".join(result), input)
    205
    206          # Use correct declaration
    207          checkdecl("utf-8")
    208          checkdecl("iso-8859-1", u'@charset "%s";g\xfcrk')
    209          checkdecl("iso-8859-15")
    210          checkdecl("cp1252")
    211
    212          # No recursion
    213          self.assertRaises(ValueError, '@charset "css";div{}'.decode, "css")
    214
    215 -    def test_encoder(self):
    216          "codec.encoder"
    217          def check(encoding, input=u'@charset "x";g\xfcrk\u20ac{}'):
    218              outputencoding = encoding
    219              if outputencoding == "utf-8-sig":
    220                  outputencoding = "utf-8"
    221
    222              # Check stateless encoder with encoding autodetection
    223              e = codecs.getencoder("css")
    224              inputdecl = input.replace('"x"', '"%s"' % encoding)
    225              outputdecl = input.replace('"x"', '"%s"' % outputencoding)
    226              self.assertEqual(e(inputdecl)[0].decode(encoding), outputdecl)
    227
    228              # Check stateless encoder with specified encoding
    229              self.assertEqual(e(input, encoding=encoding)[0].decode(encoding), outputdecl)
    230
    231              if hasattr(codec, "getincrementalencoder"):
    232                  # Check incremental encoder with encoding autodetection
    233                  ie = codecs.getincrementalencoder("css")()
    234                  self.assertEqual("".join(ie.iterencode(inputdecl)).decode(encoding), outputdecl)
    235
    236                  # Check incremental encoder with specified encoding
    237                  ie = codecs.getincrementalencoder("css")(encoding=encoding)
    238                  self.assertEqual("".join(ie.iterencode(input)).decode(encoding), outputdecl)
    239
    240              # Check stream writer with encoding autodetection
    241              q = Queue()
    242              sw = codecs.getwriter("css")(q)
    243              for c in inputdecl:
    244                  sw.write(c)
    245              self.assertEqual(q.read().decode(encoding), input.replace('"x"', '"%s"' % outputencoding))
    246
    247              # Check stream writer with specified encoding
    248              q = Queue()
    249              sw = codecs.getwriter("css")(q, encoding=encoding)
    250              for c in input:
    251                  sw.write(c)
    252              self.assertEqual(q.read().decode(encoding), input.replace('"x"', '"%s"' % outputencoding))
    253
    254          # Autodetectable encodings
    255          check("utf-8-sig")
    256          check("utf-16")
    257          check("utf-16-le")
    258          check("utf-16-be")
    259          if haveutf32:
    260              check("utf-32")
    261              check("utf-32-le")
    262              check("utf-32-be")
    263          check("utf-8")
    264          check("iso-8859-1", u'@charset "x";g\xfcrk{}')
    265          check("iso-8859-15")
    266          check("cp1252")
    267
    268          # No recursion
    269          self.assertRaises(ValueError, u'@charset "css";div{}'.encode, "css")
    270
    271
    272  if __name__ == '__main__':
    273      import unittest
    274      unittest.main()
    275