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

Source Code for Module lxml.html.tests.test_css

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