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 ('#speech5', 1), 50 ('div#speech5', 1), 51 ('div #speech5', 1), 52 ('div.scene div.dialog', 49), 53 ('div#scene1 div.dialog div', 142), 54 ('#scene1 #speech1', 1), 55 ('div[class]', 103), 56 ('div[class=dialog]', 50), 57 ('div[class^=dia]', 51), 58 ('div[class$=log]', 50), 59 ('div[class*=sce]', 1), 60 ('div[class|=dialog]', 50), # ? Seems right 61 ('div[class!=madeup]', 243), # ? Seems right 62 ('div[class~=dialog]', 51), # ? Seems right 63 ] 64
65 - def __init__(self, index):
66 self.index = index 67 super(HelperTestCase, self).__init__()
68
69 - def all(cls):
70 for i in range(len(cls.selectors)): 71 yield cls(i)
72 all = classmethod(all) 73
74 - def runTest(self):
75 f = open(doc_fn, 'rb') 76 c = f.read() 77 f.close() 78 doc = html.document_fromstring(c) 79 body = doc.xpath('//body')[0] 80 bad = [] 81 selector, count = self.selectors[self.index] 82 xpath = cssselect.css_to_xpath(cssselect.parse(selector)) 83 try: 84 results = body.xpath(xpath) 85 except Exception: 86 e = sys.exc_info()[1] 87 e.args = ("%s for xpath %r" % (e, xpath)) 88 raise 89 found = {} 90 for item in results: 91 if item in found: 92 assert 0, ( 93 "Element shows up multiple times: %r" % item) 94 found[item] = None 95 if isinstance(results, basestring): 96 assert 0, ( 97 "Got string result (%r), not element, for xpath %r" 98 % (results[:20], str(xpath))) 99 if len(results) != count: 100 #if self.shortDescription() == 'div.character, div.dialog': 101 # import pdb; pdb.set_trace() 102 assert 0, ( 103 "Did not get expected results (%s) instead %s for xpath %r" 104 % (count, len(results), str(xpath)))
105
106 - def shortDescription(self):
107 return self.selectors[self.index][0]
108
109 -def unique(s):
110 found = {} 111 result = [] 112 for item in s: 113 if item in found: 114 continue 115 found[item] = None 116 result.append(s) 117 return result
118
119 -def test_suite():
120 suite = unittest.TestSuite() 121 if sys.version_info >= (2,4): 122 suite.addTests([make_doctest('test_css_select.txt')]) 123 suite.addTests([make_doctest('test_css.txt')]) 124 suite.addTests(list(CSSTestCase.all())) 125 return suite
126