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