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