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

# Source Code for Module cssutils.tests.test_codec

      1  """Testcases for cssutils.codec"""
      2  __version__ = '$Id: test_codec.py 1116 2008-03-05 13:52:23Z cthedot $'
      3
      4  import unittest
      5
      6  import codecs
      7
      8  from cssutils import codec
      9
     10
     11  try:
     12      codecs.lookup("utf-32")
     13  except LookupError:
     14      haveutf32 = False
     15  else:
     16      haveutf32 = True
     17
     18
     19 -class Queue(object):
     20      """
     21      queue: write bytes at one end, read bytes from the other end
     22      """
     23 -    def __init__(self):
     24          self._buffer = ""
     25
     26 -    def write(self, chars):
     27          self._buffer += chars
     28
     29 -    def read(self, size=-1):
     30          if size<0:
     31              s = self._buffer
     32              self._buffer = ""
     33              return s
     34          else:
     35              s = self._buffer[:size]
     36              self._buffer = self._buffer[size:]
     37              return s
     38
     39
     40 -class CodecTestCase(unittest.TestCase):
     41
     42 -    def test_detectencoding_str(self):
     43          "codec._detectencoding_str()"
     44          self.assert_(codec._detectencoding_str('') is None)
     45          self.assert_(codec._detectencoding_str('\xef') is None)
     46          self.assertEqual(codec._detectencoding_str('\xef\x33'), "utf-8")
     47          self.assert_(codec._detectencoding_str('\xef\xbb') is None)
     48          self.assertEqual(codec._detectencoding_str('\xef\xbb\x33'), "utf-8")
     49          self.assertEqual(codec._detectencoding_str('\xef\xbb\xbf'), "utf-8-sig")
     50          self.assert_(codec._detectencoding_str('\xff') is None)
     51          self.assertEqual(codec._detectencoding_str('\xff\x33'), "utf-8")
     52          self.assert_(codec._detectencoding_str('\xff\xfe') is None)
     53          self.assertEqual(codec._detectencoding_str('\xff\xfe\x33'), "utf-16")
     54          self.assert_(codec._detectencoding_str('\xff\xfe\x00') is None)
     55          self.assertEqual(codec._detectencoding_str('\xff\xfe\x00\x33'), "utf-16")
     56          self.assertEqual(codec._detectencoding_str('\xff\xfe\x00\x00'), "utf-32")
     57          self.assert_(codec._detectencoding_str('\x00') is None)
     58          self.assertEqual(codec._detectencoding_str('\x00\x33'), "utf-8")
     59          self.assert_(codec._detectencoding_str('\x00\x00') is None)
     60          self.assertEqual(codec._detectencoding_str('\x00\x00\x33'), "utf-8")
     61          self.assert_(codec._detectencoding_str('\x00\x00\xfe') is None)
     62          self.assertEqual(codec._detectencoding_str('\x00\x00\x00\x33'), "utf-8")
     63          self.assertEqual(codec._detectencoding_str('\x00\x00\x00@'), "utf-32-be")
     64          self.assertEqual(codec._detectencoding_str('\x00\x00\xfe\xff'), "utf-32")
     65          self.assert_(codec._detectencoding_str('@') is None)
     66          self.assertEqual(codec._detectencoding_str('@\x33'), "utf-8")
     67          self.assert_(codec._detectencoding_str('@\x00') is None)
     68          self.assertEqual(codec._detectencoding_str('@\x00\x33'), "utf-8")
     69          self.assert_(codec._detectencoding_str('@\x00\x00') is None)
     70          self.assertEqual(codec._detectencoding_str('@\x00\x00\x33'), "utf-8")
     71          self.assertEqual(codec._detectencoding_str('@\x00\x00\x00'), "utf-32-le")
     72          self.assert_(codec._detectencoding_str('@c') is None)
     73          self.assert_(codec._detectencoding_str('@ch') is None)
     74          self.assert_(codec._detectencoding_str('@cha') is None)
     75          self.assert_(codec._detectencoding_str('@char') is None)
     76          self.assert_(codec._detectencoding_str('@chars') is None)
     77          self.assert_(codec._detectencoding_str('@charse') is None)
     78          self.assert_(codec._detectencoding_str('@charset') is None)
     79          self.assert_(codec._detectencoding_str('@charset ') is None)
     80          self.assert_(codec._detectencoding_str('@charset "') is None)
     81          self.assert_(codec._detectencoding_str('@charset "x') is None)
     82          self.assertEqual(codec._detectencoding_str('@charset ""'), "")
     83          self.assertEqual(codec._detectencoding_str('@charset "x"'), "x")
     84          self.assert_(codec._detectencoding_str("@", False) is None)
     85          self.assertEqual(codec._detectencoding_str("@", True), "utf-8")
     86          self.assert_(codec._detectencoding_str("@c", False) is None)
     87          self.assertEqual(codec._detectencoding_str("@c", True), "utf-8")
     88
     89 -    def test_detectencoding_unicode(self):
     90          "codec._detectencoding_unicode()"
     91          # Unicode version (only parses the header)
     92          self.assert_(codec._detectencoding_unicode(u'@charset "x') is None)
     93          self.assertEqual(codec._detectencoding_unicode(u'@charset "x', True), None)
     94          self.assertEqual(codec._detectencoding_unicode(u'@charset "x"'), "x")
     95
     96 -    def test_fixencoding(self):
     97          "codec._fixencoding()"
     98          s = u'@charset "'
     99          self.assert_(codec._fixencoding(s, u"utf-8") is None)
    100
    101          s = u'@charset "x'
    102          self.assert_(codec._fixencoding(s, u"utf-8") is None)
    103
    104          s = u'@charset "x'
    105          self.assertEqual(codec._fixencoding(s, u"utf-8", True), s)
    106
    107          s = u'@charset x'
    108          self.assertEqual(codec._fixencoding(s, u"utf-8"), s)
    109
    110          s = u'@charset "x"'
    111          self.assertEqual(codec._fixencoding(s, u"utf-8"), s.replace('"x"', '"utf-8"'))
    112
    113 -    def test_decoder(self):
    114          "codecs.decoder"
    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          "codec.encoder"
    213          def check(encoding, input=u'@charset "x";g\xfcrk\u20ac{}'):
    214              outputencoding = encoding
    215              if outputencoding == "utf-8-sig":
    216                  outputencoding = "utf-8"
    217
    218              # Check stateless encoder with encoding autodetection
    219              e = codecs.getencoder("css")
    220              inputdecl = input.replace('"x"', '"%s"' % encoding)
    221              outputdecl = input.replace('"x"', '"%s"' % outputencoding)
    222              self.assertEqual(e(inputdecl)[0].decode(encoding), outputdecl)
    223
    224              # Check stateless encoder with specified encoding
    225              self.assertEqual(e(input, encoding=encoding)[0].decode(encoding), outputdecl)
    226
    227              if hasattr(codec, "getincrementalencoder"):
    228                  # Check incremental encoder with encoding autodetection
    229                  ie = codecs.getincrementalencoder("css")()
    230                  self.assertEqual("".join(ie.iterencode(inputdecl)).decode(encoding), outputdecl)
    231
    232                  # Check incremental encoder with specified encoding
    233                  ie = codecs.getincrementalencoder("css")(encoding=encoding)
    234                  self.assertEqual("".join(ie.iterencode(input)).decode(encoding), outputdecl)
    235
    236              # Check stream writer with encoding autodetection
    237              q = Queue()
    238              sw = codecs.getwriter("css")(q)
    239              for c in inputdecl:
    240                  sw.write(c)
    241              self.assertEqual(q.read().decode(encoding), input.replace('"x"', '"%s"' % outputencoding))
    242
    243              # Check stream writer with specified encoding
    244              q = Queue()
    245              sw = codecs.getwriter("css")(q, encoding=encoding)
    246              for c in input:
    247                  sw.write(c)
    248              self.assertEqual(q.read().decode(encoding), input.replace('"x"', '"%s"' % outputencoding))
    249
    250          # Autodetectable encodings
    251          check("utf-8-sig")
    252          check("utf-16")
    253          check("utf-16-le")
    254          check("utf-16-be")
    255          if haveutf32:
    256              check("utf-32")
    257              check("utf-32-le")
    258              check("utf-32-be")
    259          check("utf-8")
    260          check("iso-8859-1", u'@charset "x";g\xfcrk{}')
    261          check("iso-8859-15")
    262          check("cp1252")
    263
    264          # No recursion
    265          self.assertRaises(ValueError, u'@charset "css";div{}'.encode, "css")
    266
    267
    268  if __name__ == '__main__':
    269      import unittest
    270      unittest.main()
    271