Package lxml :: Package tests :: Module test_css
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_css

  1  import unittest, sys 
  2  from lxml.tests.common_imports import doctest, make_doctest, HelperTestCase 
  3  from lxml import html 
  4  from lxml import cssselect 
  5  import os 
  6   
  7  doc_fn = os.path.join(os.path.dirname(__file__), 
  8                        'css_shakespear.html') 
  9   
 10  try: 
 11      basestring = __builtins__['basestring'] 
 12  except (NameError, KeyError): 
 13      basestring = (str, bytes) 
 14   
 15  # Data borrowed from http://mootools.net/slickspeed/ 
 16   
17 -class CSSTestCase(HelperTestCase):
18 19 selectors = [ 20 ## Changed from original; probably because I'm only searching the body 21 #('*', 252), 22 ('*', 246), 23 ('div:only-child', 22), # ? 24 ## Changed from original, because the original doesn't make sense. 25 ## There really aren't that many occurrances of 'celia' 26 #('div:contains(CELIA)', 243), 27 ('div:contains(CELIA)', 30), 28 ('div:nth-child(even)', 106), 29 ('div:nth-child(2n)', 106), 30 ('div:nth-child(odd)', 137), 31 ('div:nth-child(2n+1)', 137), 32 ('div:nth-child(n)', 243), 33 ('div:last-child', 53), 34 ('div:first-child', 51), 35 ('div > div', 242), 36 ('div + div', 190), 37 ('div ~ div', 190), 38 ('body', 1), 39 ('body div', 243), 40 ('div', 243), 41 ('div div', 242), 42 ('div div div', 241), 43 ('div, div, div', 243), 44 ('div, a, span', 243), 45 ('.dialog', 51), 46 ('div.dialog', 51), 47 ('div .dialog', 51), 48 ('div.character, div.dialog', 99), 49 ('div.direction.dialog', 0), 50 ('div.dialog.direction', 0), 51 ('div.dialog.scene', 1), 52 ('div.scene.scene', 1), 53 ('div.scene .scene', 0), 54 ('div.direction .dialog ', 0), 55 ('div .dialog .direction', 4), 56 ('div.dialog .dialog .direction', 4), 57 ('#speech5', 1), 58 ('div#speech5', 1), 59 ('div #speech5', 1), 60 ('div.scene div.dialog', 49), 61 ('div#scene1 div.dialog div', 142), 62 ('#scene1 #speech1', 1), 63 ('div[class]', 103), 64 ('div[class=dialog]', 50), 65 ('div[class^=dia]', 51), 66 ('div[class$=log]', 50), 67 ('div[class*=sce]', 1), 68 ('div[class|=dialog]', 50), # ? Seems right 69 ('div[class!=madeup]', 243), # ? Seems right 70 ('div[class~=dialog]', 51), # ? Seems right 71 ] 72
73 - def __init__(self, index):
74 self.index = index 75 super(HelperTestCase, self).__init__()
76
77 - def all(cls):
78 for i in range(len(cls.selectors)): 79 yield cls(i)
80 all = classmethod(all) 81
82 - def runTest(self):
83 f = open(doc_fn, 'rb') 84 c = f.read() 85 f.close() 86 doc = html.document_fromstring(c) 87 body = doc.xpath('//body')[0] 88 bad = [] 89 selector, count = self.selectors[self.index] 90 xpath = cssselect.css_to_xpath(cssselect.parse(selector)) 91 try: 92 results = body.xpath(xpath) 93 except Exception: 94 e = sys.exc_info()[1] 95 e.args = ("%s for xpath %r" % (e, xpath)) 96 raise 97 found = {} 98 for item in results: 99 if item in found: 100 assert 0, ( 101 "Element shows up multiple times: %r" % item) 102 found[item] = None 103 if isinstance(results, basestring): 104 assert 0, ( 105 "Got string result (%r), not element, for xpath %r" 106 % (results[:20], str(xpath))) 107 if len(results) != count: 108 #if self.shortDescription() == 'div.character, div.dialog': 109 # import pdb; pdb.set_trace() 110 assert 0, ( 111 "Did not get expected results (%s) instead %s for xpath %r" 112 % (count, len(results), str(xpath)))
113
114 - def shortDescription(self):
115 return self.selectors[self.index][0]
116
117 -def unique(s):
118 found = {} 119 result = [] 120 for item in s: 121 if item in found: 122 continue 123 found[item] = None 124 result.append(s) 125 return result
126
127 -def test_suite():
128 suite = unittest.TestSuite() 129 if sys.version_info >= (2,4): 130 suite.addTests([make_doctest('test_css_select.txt')]) 131 suite.addTests([make_doctest('test_css.txt')]) 132 suite.addTests(list(CSSTestCase.all())) 133 return suite
134