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

# Source Code for Module cssutils.tests.test_csscharsetrule

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