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

# Source Code for Module cssutils.tests.test_cssrule

      1  """Testcases for cssutils.css.CSSRule"""
      2  __version__ = '$Id: test_cssrule.py 1175 2008-03-20 17:43:26Z cthedot $'
      3
      4  import xml.dom
      5  import basetest
      6  import cssutils.css
      7
      8 -class CSSRuleTestCase(basetest.BaseTestCase):
      9      """
     10      base class for all CSSRule subclass tests
     11
     12      overwrite setUp with the appriopriate values, will be used in
     13      test_init and test_readonly
     14      overwrite all tests as you please, use::
     15
     16          super(CLASSNAME, self).test_TESTNAME(params)
     17
     18      to use the base class tests too
     19      """
     20 -    def setUp(self):
     21          """
     22          OVERWRITE!
     23          self.r is the rule
     24          self.rRO the readonly rule
     25          relf.r_type the type as defined in CSSRule
     26          """
     27          super(CSSRuleTestCase, self).setUp()
     28          self.sheet = cssutils.css.CSSStyleSheet()
     29          self.r = cssutils.css.CSSRule()
     30          self.rRO = cssutils.css.CSSRule()
     31          self.rRO._readonly = True # must be set here!
     32          self.r_type = cssutils.css.CSSRule.UNKNOWN_RULE
     33          self.r_typeString = 'UNKNOWN_RULE'
     34
     35 -    def test_init(self):
     36          "CSSRule.type and init"
     37          self.assertEqual(self.r_type, self.r.type)
     38          self.assertEqual(self.r_typeString, self.r.typeString)
     39          self.assertEqual(u'', self.r.cssText)
     40          self.assertEqual(None, self.r.parentRule)
     41          self.assertEqual(None, self.r.parentStyleSheet)
     42
     43 -    def test_parentRule_parentStyleSheet_type(self):
     44          "CSSRule.parentRule .parentStyleSheet .type"
     45          rules = [
     46               ('@charset "ascii";', cssutils.css.CSSRule.CHARSET_RULE),
     47               ('@import "x";', cssutils.css.CSSRule.IMPORT_RULE),
     48               ('@namespace "x";', cssutils.css.CSSRule.NAMESPACE_RULE),
     49               ('@font-face { src: url(x) }', cssutils.css.CSSRule.FONT_FACE_RULE),
     50               ('''@media all {
     51                      @x;
     52                      a { color: red }
     53                      /* c  */
     54                  }''', cssutils.css.CSSRule.MEDIA_RULE),
     55               ('@page :left { color: red }', cssutils.css.CSSRule.PAGE_RULE),
     56               ('@unknown;', cssutils.css.CSSRule.UNKNOWN_RULE),
     57               ('b { left: 0 }', cssutils.css.CSSRule.STYLE_RULE),
     58               ('/*1*/', cssutils.css.CSSRule.COMMENT) # must be last for add test
     59          ]
     60          mrt = [cssutils.css.CSSRule.UNKNOWN_RULE,
     61                 cssutils.css.CSSRule.STYLE_RULE,
     62                 cssutils.css.CSSRule.COMMENT]
     63          def test(s):
     64              for i, rule in enumerate(s):
     65                  self.assertEqual(rule.parentRule, None)
     66                  self.assertEqual(rule.parentStyleSheet, s)
     67                  self.assertEqual(rule.type, rules[i][1])
     68                  if rule.MEDIA_RULE == rule.type:
     69                      for j, r in enumerate(rule):
     70                          self.assertEqual(r.parentRule, rule)
     71                          self.assertEqual(r.parentStyleSheet, s)
     72                          self.assertEqual(r.type, mrt[j])
     73
     74                  if i == 0: # check encoding
     75                      self.assertEqual('ascii', s.encoding)
     76                  elif i == 2: # check namespaces
     77                      self.assertEqual('x', s.namespaces[''])
     78
     79          cssText = u''.join(r[0] for r in rules)
     80          # parsing
     81          s = cssutils.parseString(cssText)
     82          test(s)
     83          # sheet.cssText
     84          s = cssutils.css.CSSStyleSheet()
     85          s.cssText = cssText
     86          test(s)
     87          # sheet.add CSS
     88          s = cssutils.css.CSSStyleSheet()
     89          for css, type_ in rules:
     90              s.add(css)
     91          test(s)
     92          # sheet.insertRule CSS
     93          s = cssutils.css.CSSStyleSheet()
     94          for css, type_ in rules:
     95              s.insertRule(css)
     96          test(s)
     97
     98          types = [cssutils.css.CSSCharsetRule,
     99                   cssutils.css.CSSImportRule,
    100                   cssutils.css.CSSNamespaceRule,
    101                   cssutils.css.CSSFontFaceRule,
    102                   cssutils.css.CSSMediaRule,
    103                   cssutils.css.CSSPageRule,
    104                   cssutils.css.CSSUnknownRule,
    105                   cssutils.css.CSSStyleRule,
    106                   cssutils.css.CSSComment]
    107          # sheet.add CSSRule
    108          s = cssutils.css.CSSStyleSheet()
    109          for i, (css, type_) in enumerate(rules):
    110              rule = types[i]()
    111              rule.cssText = css
    112              s.add(rule)
    113          test(s)
    114          # sheet.insertRule CSSRule
    115          s = cssutils.css.CSSStyleSheet()
    116          for i, (css, type_) in enumerate(rules):
    117              rule = types[i]()
    118              rule.cssText = css
    119              s.insertRule(rule)
    120          test(s)
    121
    122 -    def test_CSSMediaRule_cssRules_parentRule_parentStyleSheet_type(self):
    123          "CSSMediaRule.cssRules.parentRule .parentStyleSheet .type"
    124          rules = [
    125               ('b { left: 0 }', cssutils.css.CSSRule.STYLE_RULE),
    126               ('/*1*/', cssutils.css.CSSRule.COMMENT),
    127               ('@x', cssutils.css.CSSRule.UNKNOWN_RULE)
    128          ]
    129          def test(s):
    130              mr = s.cssRules[0]
    131              for i, rule in enumerate(mr):
    132                  self.assertEqual(rule.parentRule, mr)
    133                  self.assertEqual(rule.parentStyleSheet, s)
    134                  self.assertEqual(rule.parentStyleSheet, mr.parentStyleSheet)
    135                  self.assertEqual(rule.type, rules[i][1])
    136
    137          cssText = '@media all { %s }' % u''.join(r[0] for r in rules)
    138          # parsing
    139          s = cssutils.parseString(cssText)
    140          test(s)
    141          # sheet.cssText
    142          s = cssutils.css.CSSStyleSheet()
    143          s.cssText = cssText
    144          test(s)
    145
    146          def getMediaSheet():
    147              s = cssutils.css.CSSStyleSheet()
    148              s.cssText = '@media all {}'
    149              return s, s.cssRules[0]
    150          # sheet.add CSS
    151          s, mr = getMediaSheet()
    152          for css, type_ in rules:
    153              mr.add(css)
    154          test(s)
    155          # sheet.insertRule CSS
    156          s, mr = getMediaSheet()
    157          for css, type_ in rules:
    158              mr.insertRule(css)
    159          test(s)
    160
    161          types = [cssutils.css.CSSStyleRule,
    162                   cssutils.css.CSSComment,
    163                   cssutils.css.CSSUnknownRule]
    164          # sheet.add CSSRule
    165          s, mr = getMediaSheet()
    166          for i, (css, type_) in enumerate(rules):
    167              rule = types[i]()
    168              rule.cssText = css
    169              mr.add(rule)
    170          test(s)
    171          # sheet.insertRule CSSRule
    172          s, mr = getMediaSheet()
    173          for i, (css, type_) in enumerate(rules):
    174              rule = types[i]()
    175              rule.cssText = css
    176              mr.insertRule(rule)
    177          test(s)
    178
    179 -    def test_readonly(self):
    180          "CSSRule readonly"
    181          self.rRO = cssutils.css.CSSRule()
    182          self.rRO._readonly = True
    183          self.assertEqual(True, self.rRO._readonly)
    184          self.assertEqual(u'', self.rRO.cssText)
    185          self.assertRaises(xml.dom.NoModificationAllowedErr,
    186                            self.rRO._setCssText, u'x')
    187          self.assertEqual(u'', self.rRO.cssText)
    188
    189 -    def _test_InvalidModificationErr(self, startwithspace):
    190          """
    191          CSSRule.cssText InvalidModificationErr
    192
    193          called by subclasses
    194
    195          startwithspace
    196
    197          for test starting with this not the test but " test" is tested
    198          e.g. " @page {}"
    199          exception is the style rule test
    200          """
    201          tests = (u'',
    202                   u'/* comment */',
    203                   u'@charset "utf-8";',
    204                   u'@font-face {}',
    205                   u'@import url(x);',
    206                   u'@media all {}',
    207                   u'@namespace "x";'
    208                   u'@page {}',
    209                   u'@unknown;',
    210                   u'a style rule {}'
    211                   )
    212          for test in tests:
    213              if startwithspace in (u'a style rule', ) and test in (
    214                  u'/* comment */', u'a style rule {}'):
    215                  continue
    216
    217              if test.startswith(startwithspace):
    218                  test = u' %s' % test
    219
    220              self.assertRaises(xml.dom.InvalidModificationErr,
    221                   self.r._setCssText, test)
    222
    223          # check that type is readonly
    224          self.assertRaises(AttributeError, self.r.__setattr__, 'parentRule', None)
    225          self.assertRaises(AttributeError, self.r.__setattr__, 'parentStyleSheet', None)
    226          self.assertRaises(AttributeError, self.r.__setattr__, 'type', 1)
    227          self.assertRaises(AttributeError, self.r.__setattr__, 'typeString', "")
    228
    229
    230  if __name__ == '__main__':
    231      import unittest
    232      unittest.main()
    233