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

# Source Code for Module cssutils.tests.test_csscharsetrule

      1  """Testcases for cssutils.css.CSSCharsetRule"""
      2  __version__ = '$Id: test_csscharsetrule.py 1116 2008-03-05 13:52:23Z cthedot $'
      3
      4  import re
      5  import xml.dom
      6  import test_cssrule
      7  import cssutils.css
      8
      9 -class CSSCharsetRuleTestCase(test_cssrule.CSSRuleTestCase):
     10
     11 -    def setUp(self):
     12          super(CSSCharsetRuleTestCase, self).setUp()
     13          self.r = cssutils.css.CSSCharsetRule()
     14          self.rRO = cssutils.css.CSSCharsetRule(readonly=True)
     15          self.r_type = cssutils.css.CSSCharsetRule.CHARSET_RULE
     16          self.r_typeString = 'CHARSET_RULE'
     17
     18 -    def test_init(self):
     19          "CSSCharsetRule.__init__()"
     20          super(CSSCharsetRuleTestCase, self).test_init()
     21          self.assertEqual(None, self.r.encoding)
     22          self.assertEqual(u'', self.r.cssText)
     23
     24          self.assertRaises(xml.dom.InvalidModificationErr, self.r._setCssText, u'xxx')
     25
     26 -    def test_InvalidModificationErr(self):
     27          "CSSCharsetRule InvalidModificationErr"
     28          self._test_InvalidModificationErr(u'@charset')
     29
     30 -    def test_init_encoding(self):
     31          "CSSCharsetRule.__init__(encoding)"
     32          for enc in (None, u'UTF-8', u'utf-8', u'iso-8859-1', u'ascii'):
     33              r = cssutils.css.CSSCharsetRule(enc)
     34              if enc is None:
     35                  self.assertEqual(None, r.encoding)
     36                  self.assertEqual(u'', r.cssText)
     37              else:
     38                  self.assertEqual(enc.lower(), r.encoding)
     39                  self.assertEqual(
     40                      u'@charset "%s";' % enc.lower(), r.cssText)
     41
     42          for enc in (' ascii ', ' ascii', 'ascii '):
     43              self.assertRaisesEx(xml.dom.SyntaxErr,
     44                      cssutils.css.CSSCharsetRule, enc,
     45                      exc_pattern=re.compile("Syntax Error"))
     46
     47          for enc in (u'unknown', ):
     48              self.assertRaisesEx(xml.dom.SyntaxErr,
     49                      cssutils.css.CSSCharsetRule, enc,
     50                      exc_pattern=re.compile("Unknown \(Python\) encoding"))
     51
     52 -    def test_encoding(self):
     53          "CSSCharsetRule.encoding"
     54          for enc in (u'UTF-8', u'utf-8', u'iso-8859-1', u'ascii'):
     55              self.r.encoding = enc
     56              self.assertEqual(enc.lower(), self.r.encoding)
     57              self.assertEqual(
     58                  u'@charset "%s";' % enc.lower(), self.r.cssText)
     59
     60          for enc in (None,' ascii ', ' ascii', 'ascii '):
     61              self.assertRaisesEx(xml.dom.SyntaxErr,
     62                      self.r.__setattr__, 'encoding', enc,
     63                      exc_pattern=re.compile("Syntax Error"))
     64
     65          for enc in (u'unknown', ):
     66              self.assertRaisesEx(xml.dom.SyntaxErr,
     67                      self.r.__setattr__, 'encoding', enc,
     68                      exc_pattern=re.compile("Unknown \(Python\) encoding"))
     69
     70 -    def test_cssText(self):
     71          """CSSCharsetRule.cssText
     72
     73          setting cssText is ok to use @CHARSET or other but a file
     74          using parse MUST use ``@charset "ENCODING";``
     75          """
     76          tests = {
     77              u'@charset "utf-8";': None,
     78              u"@charset 'utf-8';": u'@charset "utf-8";',
     79              }
     80          self.do_equal_r(tests)
     81          self.do_equal_p(tests) # also parse
     82
     83          tests = {
     84              # token is "@charset " with space!
     85              u'@charset;"': xml.dom.InvalidModificationErr,
     86              u'@CHARSET "UTF-8";': xml.dom.InvalidModificationErr,
     87              u'@charset "";': xml.dom.SyntaxErr,
     88              u'''@charset /*1*/"utf-8"/*2*/;''': xml.dom.SyntaxErr,
     89              u'''@charset /*1*/"utf-8";''': xml.dom.SyntaxErr,
     90              u'''@charset "utf-8"/*2*/;''': xml.dom.SyntaxErr,
     91              u'@charset { utf-8 }': xml.dom.SyntaxErr,
     92              u'@charset "utf-8"': xml.dom.SyntaxErr,
     93              u'@charset a;': xml.dom.SyntaxErr,
     94              u'@charset /**/;': xml.dom.SyntaxErr,
     95              # trailing content
     96              u'@charset "utf-8";s': xml.dom.SyntaxErr,
     97              u'@charset "utf-8";/**/': xml.dom.SyntaxErr,
     98              u'@charset "utf-8"; ': xml.dom.SyntaxErr,
     99
    100              # comments do not work in this rule!
    101              u'@charset "utf-8"/*1*//*2*/;': xml.dom.SyntaxErr
    102              }
    103          self.do_raise_r(tests)
    104
    105 -    def test_repr(self):
    106          "CSSCharsetRule.__repr__()"
    107          self.r.encoding = 'utf-8'
    108          self.assert_('utf-8' in repr(self.r))
    109
    110 -    def test_reprANDstr(self):
    111          "CSSCharsetRule.__repr__(), .__str__()"
    112          encoding='utf-8'
    113
    114          s = cssutils.css.CSSCharsetRule(encoding=encoding)
    115
    116          self.assert_(encoding in str(s))
    117
    118          s2 = eval(repr(s))
    119          self.assert_(isinstance(s2, s.__class__))
    120          self.assert_(encoding == s2.encoding)
    121
    122  if __name__ == '__main__':
    123      import unittest
    124      unittest.main()
    125