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

# Source Code for Module cssutils.tests.test_selectorlist

      1  """Testcases for cssutils.css.selectorlist.SelectorList."""
      2  __version__ = '$Id: test_selectorlist.py 1116 2008-03-05 13:52:23Z cthedot $'
      3
      4  import xml.dom
      5  import basetest
      6  import cssutils
      7  from cssutils.css.selectorlist import SelectorList
      8
      9 -class SelectorListTestCase(basetest.BaseTestCase):
     10
     11 -    def setUp(self):
     12          self.r = SelectorList()
     13
     14 -    def test_init(self):
     15          "SelectorList.__init__() and .length"
     16          s = SelectorList()
     17          self.assertEqual(0, s.length)
     18
     19          s = SelectorList('a, b')
     20          self.assertEqual(2, s.length)
     21          self.assertEqual(u'a, b', s.selectorText)
     22
     23          s = SelectorList(selectorText='a')
     24          self.assertEqual(1, s.length)
     25          self.assertEqual(u'a', s.selectorText)
     26
     27          s = SelectorList(selectorText=('p|a', {'p': 'uri'})) # n-dict
     28          self.assertEqual(1, s.length)
     29          self.assertEqual(u'p|a', s.selectorText)
     30
     31          s = SelectorList(selectorText=('p|a', (('p', 'uri'),))) # n-tuples
     32          self.assertEqual(1, s.length)
     33          self.assertEqual(u'p|a', s.selectorText)
     34
     35 -    def test_parentRule(self):
     36          "Selector.parentRule"
     37          def check(style):
     38              self.assertEqual(style, style.selectorList.parentRule)
     39              for sel in style.selectorList:
     40                  self.assertEqual(style.selectorList, sel.parentList)
     41
     42          style = cssutils.css.CSSStyleRule('a, b')
     43          check(style)
     44
     45          # add new selector
     46          style.selectorList.append(cssutils.css.Selector('x'))
     47          check(style)
     48
     49          # replace selectorList
     50          style.selectorList = cssutils.css.SelectorList('x')
     51          check(style)
     52
     53          # replace selectorText
     54          style.selectorText = ('x, y')
     55          check(style)
     56
     57 -    def test_appendSelector(self):
     58          "SelectorList.appendSelector() and .length"
     59          s = SelectorList()
     60          s.appendSelector('a')
     61          self.assertEqual(1, s.length)
     62
     63          self.assertRaises(xml.dom.InvalidModificationErr,
     64                            s.appendSelector, 'b,')
     65          self.assertEqual(1, s.length)
     66
     67          self.assertEqual(u'a', s.selectorText)
     68
     69          s.append('b')
     70          self.assertEqual(2, s.length)
     71          self.assertEqual(u'a, b', s.selectorText)
     72
     73          s.append('a')
     74          self.assertEqual(2, s.length)
     75          self.assertEqual(u'b, a', s.selectorText)
     76
     77          # __setitem__
     78          self.assertRaises(IndexError, s.__setitem__, 4, 'x')
     79          s[1] = 'c'
     80          self.assertEqual(2, s.length)
     81          self.assertEqual(u'b, c', s.selectorText)
     82          # TODO: remove duplicates?
     83  #        s[0] = 'c'
     84  #        self.assertEqual(1, s.length)
     85  #        self.assertEqual(u'c', s.selectorText)
     86
     87          s = SelectorList()
     88          s.appendSelector(('p|a', {'p': 'uri', 'x': 'xxx'}))
     89          self.assertEqual(u'p|a', s.selectorText)
     90          # x gets lost as not used
     91          self.assertRaises(xml.dom.NamespaceErr, s.append, 'x|a')
     92          # not set at all
     93          self.assertRaises(xml.dom.NamespaceErr, s.append, 'y|a')
     94          # but p is retained
     95          s.append('p|b')
     96          self.assertEqual(u'p|a, p|b', s.selectorText)
     97
     98 -    def test_selectorText(self):
     99          "SelectorList.selectorText"
    100          s = SelectorList()
    101          s.selectorText = u'a, b'
    102          self.assertEqual(u'a, b', s.selectorText)
    103          self.assertRaises(xml.dom.SyntaxErr, s._setSelectorText, u',')
    104          # not changed as invalid!
    105          self.assertEqual(u'a, b', s.selectorText)
    106
    107          tests = {
    108              u'*': None,
    109              u'/*1*/*': None,
    110              u'/*1*/*, a': None,
    111              u'a, b': None,
    112              u'a ,b': u'a, b',
    113              u'a , b': u'a, b',
    114              u'a, b, c': u'a, b, c',
    115              u'#a, x#a, .b, x.b': u'#a, x#a, .b, x.b',
    116              (u'[p|a], p|*', (('p', 'uri'),)): u'[p|a], p|*',
    117              }
    118          # do not parse as not complete
    119          self.do_equal_r(tests, att='selectorText')
    120
    121          tests = {
    122              u'x|*': xml.dom.NamespaceErr,
    123              u'': xml.dom.SyntaxErr,
    124              u' ': xml.dom.SyntaxErr,
    125              u',': xml.dom.SyntaxErr,
    126              u'a,': xml.dom.SyntaxErr,
    127              u',a': xml.dom.SyntaxErr,
    128              u'/* 1 */,a': xml.dom.SyntaxErr,
    129              }
    130          # only set as not complete
    131          self.do_raise_r(tests, att='_setSelectorText')
    132
    133 -    def test_reprANDstr(self):
    134          "SelectorList.__repr__(), .__str__()"
    135          sel=(u'a, p|b', { 'p': 'uri'})
    136
    137          s = cssutils.css.SelectorList(selectorText=sel)
    138
    139          self.assert_(sel[0] in str(s))
    140
    141          s2 = eval(repr(s))
    142          self.assert_(isinstance(s2, s.__class__))
    143          self.assertEqual(sel[0], s2.selectorText)
    144
    145
    146  if __name__ == '__main__':
    147      import unittest
    148      unittest.main()
    149