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