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-08-20 22:09:16 +0200 (Mo, 20 Aug 2007) $'
      4  __version__ = '$LastChangedRevision: 258 $'
      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          tests = {
     37              u'a {': u'a {}', # no }
     38              u'a { font-family: "arial sans': # no "
     39                  u'a {\n    font-family: "arial sans"\n    }',
     40          }
     41          self.do_equal_p(tests) # parse
     42
     43 -    def test_cssText(self):
     44          "CSSStyleRule.cssText"
     45          tests = {
     46              u'''a\n{color: #000}''': 'a {\n    color: #000\n    }', # issue 4
     47              u'''a\n{color: #000000}''': 'a {\n    color: #000000\n    }', # issue 4
     48              u'''a\n{color: #abc}''': 'a {\n    color: #abc\n    }', # issue 4
     49              u'''a\n{color: #abcdef}''': 'a {\n    color: #abcdef\n    }', # issue 4
     50              u'''a\n{color: #00a}''': 'a {\n    color: #00a\n    }', # issue 4
     51              u'''a\n{color: #1a1a1a}''': 'a {\n    color: #1a1a1a\n    }', # issue 4
     52              u'''#id\n{}''': '#id {}', # issue 3
     53              u'''* {}''': None,
     54              u'a {}': None,
     55              u'b { a: 1; }': u'b {\n    a: 1\n    }',
     56              # mix of comments and properties
     57              u'c1 {/*1*/a:1;}': u'c1 {\n    /*1*/\n    a: 1\n    }',
     58              u'c2 {a:1;/*2*/}': u'c2 {\n    a: 1;\n    /*2*/\n    }',
     59              u'd1 {/*0*/}': u'd1 {\n    /*0*/\n    }',
     60              u'd2 {/*0*//*1*/}': u'd2 {\n    /*0*/\n    /*1*/\n    }'
     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'''a {}x''': xml.dom.SyntaxErr, # trailing
     73              u'''/*x*/''': xml.dom.SyntaxErr,
     74              u'''a {''': xml.dom.SyntaxErr,
     75              })
     76          self.do_raise_r(tests) # set cssText
     77
     78 -    def test_selectorList(self):
     79          "CSSStyleRule.selectorList"
     80          r = cssutils.css.CSSStyleRule()
     81
     82          r.selectorList.appendSelector(u'a')
     83          self.assertEqual(1, r.selectorList.length)
     84          self.assertEqual(u'a', r.selectorText)
     85
     86          r.selectorList.appendSelector(u' b  ')
     87          # only simple selector!
     88          self.assertRaises(xml.dom.InvalidModificationErr,
     89                            r.selectorList.appendSelector, u'  h1, x ')
     90
     91          self.assertEqual(2, r.selectorList.length)
     92          self.assertEqual(u'a, b', r.selectorText)
     93
     94 -    def test_selectorText(self):
     95          "CSSStyleRule.selectorText"
     96          r = cssutils.css.CSSStyleRule()
     97
     98          r.selectorText = u'a'
     99          self.assertEqual(1, r.selectorList.length)
    100          self.assertEqual(u'a', r.selectorText)
    101
    102          r.selectorText = u' b, h1  '
    103          self.assertEqual(2, r.selectorList.length)
    104          self.assertEqual(u'b, h1', r.selectorText)
    105
    106 -    def test_style(self):
    107          "CSSStyleRule.style"
    108          d = cssutils.css.CSSStyleDeclaration()
    109          self.r.style = d
    110          self.assertEqual(d, self.r.style)
    111
    112          # check if parentRule of sd set
    113          self.assertEqual(self.r, d.parentRule)
    114
    115 -    def test_reprANDstr(self):
    116          "CSSStyleRule.__repr__(), .__str__()"
    117          sel=u'a>b+c'
    118
    119          s = cssutils.css.CSSStyleRule(selectorText=sel)
    120
    121          self.assert_(sel in str(s))
    122
    123          s2 = eval(repr(s))
    124          self.assert_(isinstance(s2, s.__class__))
    125          self.assert_(sel == s2.selectorText)
    126
    127
    128  if __name__ == '__main__':
    129      import unittest
    130      unittest.main()
    131