1 import unittest
2
3 import lxml.html
4
5 from lxml.tests.common_imports import doctest, HelperTestCase
6
7
8 HTML = '''
9 <div>
10 <a href="foo">link</a>
11 <a>anchor</a>
12 </div>
13 '''
14
15
17
19 div, = lxml.html.fromstring(HTML).xpath('//div')
20
21 def count(selector, expected_count, **kwargs):
22 result = div.cssselect(selector, **kwargs)
23 self.assertEqual(len(result), expected_count)
24
25 count('div', 1)
26 count('a', 2)
27 count('em', 0)
28
29 count('DIV', 1)
30
31 count('DIV', 0, translator='xhtml')
32 count('DIV', 0, translator='xml')
33
34
35 count(':contains("link")', 2)
36 count(':contains("LInk")', 2)
37
38 count(':contains("LInk")', 2, translator='xhtml')
39 count(':contains("LInk")', 2, translator='xml')
40
41 import cssselect
42 count(':contains("link")', 2, translator=cssselect.HTMLTranslator())
43 count(':contains("LInk")', 0, translator=cssselect.HTMLTranslator())
44
45
47 suite = unittest.TestSuite()
48 try:
49 import cssselect
50 except ImportError:
51
52 print("Skipping tests in lxml.cssselect - external cssselect package is not installed")
53 return suite
54
55 import lxml.cssselect
56 suite.addTests(doctest.DocTestSuite(lxml.cssselect))
57 suite.addTests([unittest.makeSuite(CSSTestCase)])
58 return suite
59