-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·52 lines (41 loc) · 1.76 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import io
import unittest
import css2xpath
class Css2XpathTest(unittest.TestCase):
def runfunc(self, func, expwrite, expreturn, *args):
f = io.StringIO()
actreturn = func(*(args + (f.write,)))
actwrited = f.getvalue()
msg = lambda e, a: 'Tested: {}{},\n : Expected={}, Actual={}'.format(
func.__name__, args, e, a)
self.assertEqual(expwrite, actwrited, msg(expwrite, actwrited))
self.assertEqual(expreturn, actreturn, msg(expreturn, actreturn))
def batch(self, func, expwrite, expreturn, args):
for w, r, a in zip(expwrite, expreturn, args):
self.runfunc(func, w, r, a)
def test_node_star(self):
i = ['#egg', '.egg', '.spam:nth-child(1)',]
w = ['*', '*', '*']
r = i.copy()
self.batch(css2xpath.tok2nodetest, w, r, i)
def test_node_el(self):
i = ['h1#egg', 'h2.egg', 'h3.spam:nth-child(1)',]
w = ['h1', 'h2', 'h3']
r = ['#egg', '.egg', '.spam:nth-child(1)']
self.batch(css2xpath.tok2nodetest, w, r, i)
def test_predicate(self):
i = ['.eggs', '#egg', '.eggs:nth-child(1)', ':nth-child(1)']
w = ["[contains(@class, 'eggs')]", "[@id='egg']",
"[contains(@class, 'eggs')]", '[1]']
r = ['', '', ':nth-child(1)', '']
self.batch(css2xpath.tok2predicate, w, r, i)
def test_transform(self):
inp = '.spam > p.and-eggs:nth-child(3) a.something.otherthing'
exp = "//*[contains(@class, 'spam')]"\
"/p[contains(@class, 'and-eggs')][3]"\
"//a[contains(@class, 'something')]"\
"[contains(@class, 'otherthing')]"
self.assertEqual(exp, css2xpath.transform(inp))
if __name__ == '__main__':
unittest.main()