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