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

# Source Code for Module cssutils.tests.test_cssstylerule

      1  """testcases for cssutils.css.CSSStyleRuleTestCase"""
      2  __author__ = '$LastChangedBy: cthedot $'
      3  __date__ = '$LastChangedDate: 2007-10-27 21:33:00 +0200 (Sa, 27 Okt 2007) $'
      4  __version__ = '$LastChangedRevision: 579 $'
      5
      6  import xml.dom
      7  import test_cssrule
      8  import cssutils
      9
     10 -class CSSStyleRuleTestCase(test_cssrule.CSSRuleTestCase):
     11
     12 -    def setUp(self):
     13          super(CSSStyleRuleTestCase, self).setUp()
     14          self.r = cssutils.css.CSSStyleRule()
     15          self.rRO = cssutils.css.CSSStyleRule(readonly=True)
     16          self.r_type = cssutils.css.CSSStyleRule.STYLE_RULE
     17          self.r_typeString = 'STYLE_RULE'
     18
     19 -    def test_init(self):
     20          "CSSStyleRule.type and init"
     21          super(CSSStyleRuleTestCase, self).test_init()
     22          self.assertEqual(u'', self.r.cssText)
     23          self.assertEqual(cssutils.css.selectorlist.SelectorList,
     24                           type(self.r.selectorList))
     25          self.assertEqual(u'', self.r.selectorText)
     26          self.assertEqual(cssutils.css.CSSStyleDeclaration,
     27                           type(self.r.style))
     28          self.assertEqual(self.r, self.r.style.parentRule)
     29
     30 -    def test_InvalidModificationErr(self):
     31          "CSSStyleRule.cssText InvalidModificationErr"
     32          self._test_InvalidModificationErr(u'a style rule')
     33
     34 -    def test_incomplete(self):
     35          "CSSStyleRule (incomplete)"
     36          cssutils.ser.prefs.keepEmptyRules = True
     37          tests = {
     38              u'a {': u'a {}', # no }
     39              u'a { font-family: "arial sans': # no "
     40                  u'a {\n    font-family: "arial sans"\n    }',
     41          }
     42          self.do_equal_p(tests) # parse
     43          cssutils.ser.prefs.useDefaults()
     44
     45 -    def test_cssText(self):
     46          "CSSStyleRule.cssText"
     47          tests = {
     48              u'* {}': u'',
     49              u'a {}': u'',
     50              }
     51          self.do_equal_p(tests) # parse
     52          #self.do_equal_r(tests) # set cssText # TODO: WHY?
     53
     54          cssutils.ser.prefs.keepEmptyRules = True
     55          tests = {
     56              u'''a\n{color: #000}''': 'a {\n    color: #000\n    }', # issue 4
     57              u'''a\n{color: #000000}''': 'a {\n    color: #000000\n    }', # issue 4
     58              u'''a\n{color: #abc}''': 'a {\n    color: #abc\n    }', # issue 4
     59              u'''a\n{color: #abcdef}''': 'a {\n    color: #abcdef\n    }', # issue 4
     60              u'''a\n{color: #00a}''': 'a {\n    color: #00a\n    }', # issue 4
     61              u'''a\n{color: #1a1a1a}''': 'a {\n    color: #1a1a1a\n    }', # issue 4
     62              u'''#id\n{ color: red }''': '#id {\n    color: red\n    }', # issue 3
     63              u'''* {}''': None,
     64              u'a {}': None,
     65              u'b { a: 1; }': u'b {\n    a: 1\n    }',
     66              # mix of comments and properties
     67              u'c1 {/*1*/a:1;}': u'c1 {\n    /*1*/\n    a: 1\n    }',
     68              u'c2 {a:1;/*2*/}': u'c2 {\n    a: 1;\n    /*2*/\n    }',
     69              u'd1 {/*0*/}': u'd1 {\n    /*0*/\n    }',
     70              u'd2 {/*0*//*1*/}': u'd2 {\n    /*0*/\n    /*1*/\n    }'
     71              }
     72          self.do_equal_p(tests) # parse
     73          self.do_equal_r(tests) # set cssText
     74
     75          tests = {
     76              u'''a;''': xml.dom.SyntaxErr,
     77              u'''a {{}''': xml.dom.SyntaxErr,
     78              u'''a }''': xml.dom.SyntaxErr,
     79              }
     80          self.do_raise_p(tests) # parse
     81          tests.update({
     82              u'''a {}x''': xml.dom.SyntaxErr, # trailing
     83              u'''/*x*/''': xml.dom.SyntaxErr,
     84              u'''a {''': xml.dom.SyntaxErr,
     85              })
     86          self.do_raise_r(tests) # set cssText
     87          cssutils.ser.prefs.useDefaults()
     88
     89 -    def test_selectorList(self):
     90          "CSSStyleRule.selectorList"
     91          r = cssutils.css.CSSStyleRule()
     92
     93          r.selectorList.appendSelector(u'a')
     94          self.assertEqual(1, r.selectorList.length)
     95          self.assertEqual(u'a', r.selectorText)
     96
     97          r.selectorList.appendSelector(u' b  ')
     98          # only simple selector!
     99          self.assertRaises(xml.dom.InvalidModificationErr,
    100                            r.selectorList.appendSelector, u'  h1, x ')
    101
    102          self.assertEqual(2, r.selectorList.length)
    103          self.assertEqual(u'a, b', r.selectorText)
    104
    105 -    def test_selectorText(self):
    106          "CSSStyleRule.selectorText"
    107          r = cssutils.css.CSSStyleRule()
    108
    109          r.selectorText = u'a'
    110          self.assertEqual(1, r.selectorList.length)
    111          self.assertEqual(u'a', r.selectorText)
    112
    113          r.selectorText = u' b, h1  '
    114          self.assertEqual(2, r.selectorList.length)
    115          self.assertEqual(u'b, h1', r.selectorText)
    116
    117 -    def test_style(self):
    118          "CSSStyleRule.style"
    119          d = cssutils.css.CSSStyleDeclaration()
    120          self.r.style = d
    121          self.assertEqual(d, self.r.style)
    122
    123          # check if parentRule of sd set
    124          self.assertEqual(self.r, d.parentRule)
    125
    126 -    def test_reprANDstr(self):
    127          "CSSStyleRule.__repr__(), .__str__()"
    128          sel=u'a>b+c'
    129
    130          s = cssutils.css.CSSStyleRule(selectorText=sel)
    131
    132          self.assert_(sel in str(s))
    133
    134          s2 = eval(repr(s))
    135          self.assert_(isinstance(s2, s.__class__))
    136          self.assert_(sel == s2.selectorText)
    137
    138
    139  if __name__ == '__main__':
    140      import unittest
    141      unittest.main()
    142