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: 2007-10-20 20:07:41 +0200 (Sa, 20 Okt 2007) $'
      6  __version__ = '$LastChangedRevision: 546 $'
      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 -    def test_InvalidModificationErr(self):
     29          "CSSCharsetRule InvalidModificationErr"
     30          self._test_InvalidModificationErr(u'@charset')
     31
     32 -    def test_init_encoding(self):
     33          "CSSCharsetRule.__init__(encoding)"
     34          for enc in (None, u'UTF-8', u'utf-8', u'iso-8859-1', u'ascii'):
     35              r = cssutils.css.CSSCharsetRule(enc)
     36              if enc is None:
     37                  self.assertEqual(None, r.encoding)
     38                  self.assertEqual(u'', r.cssText)
     39              else:
     40                  self.assertEqual(enc.lower(), r.encoding)
     41                  self.assertEqual(
     42                      u'@charset "%s";' % enc.lower(), r.cssText)
     43
     44          for enc in (' ascii ', ' ascii', 'ascii '):
     45              self.assertRaisesEx(xml.dom.SyntaxErr,
     46                      cssutils.css.CSSCharsetRule, enc,
     47                      exc_pattern=re.compile("Syntax Error"))
     48
     49          for enc in (u'unknown', ):
     50              self.assertRaisesEx(xml.dom.SyntaxErr,
     51                      cssutils.css.CSSCharsetRule, enc,
     52                      exc_pattern=re.compile("Unknown \(Python\) encoding"))
     53
     54 -    def test_encoding(self):
     55          "CSSCharsetRule.encoding"
     56          for enc in (u'UTF-8', u'utf-8', u'iso-8859-1', u'ascii'):
     57              self.r.encoding = enc
     58              self.assertEqual(enc.lower(), self.r.encoding)
     59              self.assertEqual(
     60                  u'@charset "%s";' % enc.lower(), self.r.cssText)
     61
     62          for enc in (None,' ascii ', ' ascii', 'ascii '):
     63              self.assertRaisesEx(xml.dom.SyntaxErr,
     64                      self.r.__setattr__, 'encoding', enc,
     65                      exc_pattern=re.compile("Syntax Error"))
     66
     67          for enc in (u'unknown', ):
     68              self.assertRaisesEx(xml.dom.SyntaxErr,
     69                      self.r.__setattr__, 'encoding', enc,
     70                      exc_pattern=re.compile("Unknown \(Python\) encoding"))
     71
     72 -    def test_cssText(self):
     73          """CSSCharsetRule.cssText
     74
     75          setting cssText is ok to use @CHARSET or other but a file
     76          using parse MUST use ``@charset "ENCODING";``
     77          """
     78          tests = {
     79              u'@charset "utf-8";': None,
     80              u"@charset 'utf-8';": u'@charset "utf-8";',
     81              }
     82          self.do_equal_r(tests)
     83          self.do_equal_p(tests) # also parse
     84
     85          tests = {
     86              u'@charset;"': xml.dom.InvalidModificationErr,
     87              u'@CHARSET "UTF-8";': xml.dom.InvalidModificationErr,
     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              }
     96          self.do_raise_r(tests)
     97
     98 -    def test_repr(self):
     99          "CSSCharsetRule.__repr__()"
    100          self.r.encoding = 'utf-8'
    101          self.assert_('utf-8' in repr(self.r))
    102
    103 -    def test_reprANDstr(self):
    104          "CSSCharsetRule.__repr__(), .__str__()"
    105          encoding='utf-8'
    106
    107          s = cssutils.css.CSSCharsetRule(encoding=encoding)
    108
    109          self.assert_(encoding in str(s))
    110
    111          s2 = eval(repr(s))
    112          self.assert_(isinstance(s2, s.__class__))
    113          self.assert_(encoding == s2.encoding)
    114
    115  if __name__ == '__main__':
    116      import unittest
    117      unittest.main()
    118