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

Source Code for Module cssutils.tests.test_selector

  1  """Testcases for cssutils.css.selector.Selector.""" 
  2  __author__ = '$LastChangedBy: cthedot $' 
  3  __date__ = '$LastChangedDate: 2007-10-18 19:39:12 +0200 (Do, 18 Okt 2007) $' 
  4  __version__ = '$LastChangedRevision: 502 $' 
  5   
  6  import xml.dom 
  7  import basetest 
  8  import cssutils 
  9   
10 -class SelectorTestCase(basetest.BaseTestCase):
11
12 - def setUp(self):
13 self.r = cssutils.css.Selector('*')
14
15 - def test_init(self):
16 "Selector.__init__()" 17 s = cssutils.css.Selector('*')
18
19 - def test_selectorText(self):
20 "Selector.selectorText" 21 tests = { 22 u'''*''': None, 23 u'''*/*x*/''': None, 24 u'''* /*x*/''': None, 25 u'''*:hover''': None, 26 u'''* :hover''': None, 27 u'''*:lang(fr)''': None, 28 u'''* :lang(fr)''': None, 29 u'''*::first-line''': None, 30 u'''* ::first-line''': None, 31 u'''*[lang=fr]''': None, 32 u'''[lang=fr]''': None, 33 34 u'''a''': None, 35 u'''h1''': None, 36 u'''.a a''': None, 37 38 u'''a1''': None, 39 u'''a1-1''': None, 40 u'''.a1-1''': None, 41 u'''.a1._1''': None, 42 43 u'''[x]''': None, 44 u'''*[x]''': None, 45 u'''a[x]''': None, 46 u'''a[ x]''': 'a[x]', 47 u'''a[x ]''': 'a[x]', 48 u'''a [x]''': 'a [x]', 49 u'''* [x]''': None, # is really * *[x] 50 51 u'''a[x="1"]''': None, 52 u'''a[x ="1"]''': 'a[x="1"]', 53 u'''a[x= "1"]''': 'a[x="1"]', 54 u'''a[x = "1"]''': 'a[x="1"]', 55 u'''a[ x = "1"]''': 'a[x="1"]', 56 u'''a[x = "1" ]''': 'a[x="1"]', 57 u'''a[ x = "1" ]''': 'a[x="1"]', 58 u'''a [ x = "1" ]''': 'a [x="1"]', 59 60 u'''a[x~=a1]''': None, 61 u'''a[x ~=a1]''': 'a[x~=a1]', 62 u'''a[x~= a1]''': 'a[x~=a1]', 63 u'''a[x ~= a1]''': 'a[x~=a1]', 64 u'''a[ x ~= a1]''': 'a[x~=a1]', 65 u'''a[x ~= a1 ]''': 'a[x~=a1]', 66 u'''a[ x ~= a1 ]''': 'a[x~=a1]', 67 u'''a [ x ~= a1 ]''': 'a [x~=a1]', # same as next! 68 u'''a *[ x ~= a1 ]''': 'a *[x~=a1]', 69 70 u'''a[x|=en]''': None, 71 u'''a[x|= en]''': 'a[x|=en]', 72 u'''a[x |=en]''': 'a[x|=en]', 73 u'''a[x |= en]''': 'a[x|=en]', 74 u'''a[ x |= en]''': 'a[x|=en]', 75 u'''a[x |= en ]''': 'a[x|=en]', 76 u'''a[ x |= en]''': 'a[x|=en]', 77 u'''a [ x |= en]''': 'a [x|=en]', 78 # CSS3 79 u'''a[x^=en]''': None, 80 u'''a[x$=en]''': None, 81 u'''a[x*=en]''': None, 82 83 u'''a[/*1*/x/*2*/]''': None, 84 u'''a[/*1*/x/*2*/=/*3*/a/*4*/]''': None, 85 u'''a[/*1*/x/*2*/~=/*3*/a/*4*/]''': None, 86 u'''a[/*1*/x/*2*/|=/*3*/a/*4*/]''': None, 87 88 u'''a b''': None, 89 u'''a b''': 'a b', 90 u'''a #b''': 'a #b', 91 u'''a .b''': 'a .b', 92 u'''ab''': 'ab', 93 u'''a.b''': None, 94 u'''a.b.c''': None, 95 96 u'''#a''': None, 97 u'''#a1''': None, 98 u'''#1a''': None, # valid to grammar but not for HTML 99 u'''#1''': None, # valid to grammar but not for HTML 100 u'''a#b''': None, 101 u'''a #b''': None, 102 103 u'''a>b''': None, 104 u'''a> b''': 'a>b', 105 u'''a >b''': 'a>b', 106 u'''a > b''': 'a>b', 107 # CSS2 combinator + 108 u'''a+b''': None, 109 u'''a+ b''': 'a+b', 110 u'''a +b''': 'a+b', 111 u'''a + b''': 'a+b', 112 # CSS3 combinator ~ 113 u'''a~b''': None, 114 u'''a~ b''': 'a~b', 115 u'''a ~b''': 'a~b', 116 u'''a ~ b''': 'a~b', 117 118 u'''a+ b c''': 'a+b c', 119 # namespaceprefixes 120 u'''|e''': None, 121 u'''*|e''': None, 122 u'''n|e''': None, 123 u'''n|*''': None, 124 u'''*|b[x|a]''': None, 125 126 u'''x:lang(de) y''': None, 127 u'''x:nth-child(odd) y''': None, 128 } 129 # do not parse as not complete 130 self.do_equal_r(tests, att='selectorText') 131 132 tests = { 133 u'': xml.dom.SyntaxErr, 134 u'1': xml.dom.SyntaxErr, 135 136 u'#': xml.dom.SyntaxErr, 137 u'|': xml.dom.SyntaxErr, 138 139 u':': xml.dom.SyntaxErr, 140 u'::': xml.dom.SyntaxErr, 141 u': a': xml.dom.SyntaxErr, 142 u':: a': xml.dom.SyntaxErr, 143 u':a()': xml.dom.SyntaxErr, # no value 144 u'::a()': xml.dom.SyntaxErr, # no value 145 u':::a': xml.dom.SyntaxErr, 146 u':1': xml.dom.SyntaxErr, 147 148 u'#.x': xml.dom.SyntaxErr, 149 u'.': xml.dom.SyntaxErr, 150 u'.1': xml.dom.SyntaxErr, 151 u'.a.1': xml.dom.SyntaxErr, 152 153 u'[a': xml.dom.SyntaxErr, 154 u'a]': xml.dom.SyntaxErr, 155 u'[a b]': xml.dom.SyntaxErr, 156 u'[=b]': xml.dom.SyntaxErr, 157 u'[a=]': xml.dom.SyntaxErr, 158 u'[a|=]': xml.dom.SyntaxErr, 159 u'[a~=]': xml.dom.SyntaxErr, 160 u'[a=1]': xml.dom.SyntaxErr, 161 162 u'a +': xml.dom.SyntaxErr, 163 u'a >': xml.dom.SyntaxErr, 164 u'a ++ b': xml.dom.SyntaxErr, 165 u'a + > b': xml.dom.SyntaxErr, 166 167 168 u'*:lang(': xml.dom.SyntaxErr, 169 170 # only one selector! 171 u',': xml.dom.InvalidModificationErr, 172 u',a': xml.dom.InvalidModificationErr, 173 u'a,': xml.dom.InvalidModificationErr, 174 } 175 # only set as not complete 176 self.do_raise_r(tests, att='_setSelectorText')
177
178 - def test_reprANDstr(self):
179 "Selector.__repr__(), .__str__()" 180 sel=u'a+b' 181 182 s = cssutils.css.Selector(selectorText=sel) 183 184 self.assert_(sel in str(s)) 185 186 s2 = eval(repr(s)) 187 self.assert_(isinstance(s2, s.__class__)) 188 self.assert_(sel == s2.selectorText)
189
190 - def test_prefixes(self):
191 "Selector.prefixes" 192 sel=u'a|x1 a|x2 |y *|z [b|x] [a|x="1"]' 193 s = cssutils.css.Selector(selectorText=sel) 194 195 self.assertEqual(set('ab'), s.prefixes)
196 197 if __name__ == '__main__': 198 import unittest 199 unittest.main() 200