2 from Cython
.Compiler
.Visitor
import PrintTree
3 from Cython
.TestUtils
import TransformTest
4 from Cython
.Compiler
.TreePath
import find_first
, find_all
5 from Cython
.Compiler
import Nodes
, ExprNodes
7 class TestTreePath(TransformTest
):
10 def _build_tree(self
):
11 if self
._tree
is None:
12 self
._tree
= self
.run_pipeline([], u
"""
13 def decorator(fun): # DefNode
14 return fun # ReturnStatNode, NameNode
16 def decorated(): # DefNode
21 def test_node_path(self
):
22 t
= self
._build
_tree
()
23 self
.assertEquals(2, len(find_all(t
, "//DefNode")))
24 self
.assertEquals(2, len(find_all(t
, "//NameNode")))
25 self
.assertEquals(1, len(find_all(t
, "//ReturnStatNode")))
26 self
.assertEquals(1, len(find_all(t
, "//DefNode//ReturnStatNode")))
28 def test_node_path_star(self
):
29 t
= self
._build
_tree
()
30 self
.assertEquals(10, len(find_all(t
, "//*")))
31 self
.assertEquals(8, len(find_all(t
, "//DefNode//*")))
32 self
.assertEquals(0, len(find_all(t
, "//NameNode//*")))
34 def test_node_path_attribute(self
):
35 t
= self
._build
_tree
()
36 self
.assertEquals(2, len(find_all(t
, "//NameNode/@name")))
37 self
.assertEquals(['fun', 'decorator'], find_all(t
, "//NameNode/@name"))
39 def test_node_path_attribute_dotted(self
):
40 t
= self
._build
_tree
()
41 self
.assertEquals(1, len(find_all(t
, "//ReturnStatNode/@value.name")))
42 self
.assertEquals(['fun'], find_all(t
, "//ReturnStatNode/@value.name"))
44 def test_node_path_child(self
):
45 t
= self
._build
_tree
()
46 self
.assertEquals(1, len(find_all(t
, "//DefNode/ReturnStatNode/NameNode")))
47 self
.assertEquals(1, len(find_all(t
, "//ReturnStatNode/NameNode")))
49 def test_node_path_node_predicate(self
):
50 t
= self
._build
_tree
()
51 self
.assertEquals(0, len(find_all(t
, "//DefNode[.//ForInStatNode]")))
52 self
.assertEquals(2, len(find_all(t
, "//DefNode[.//NameNode]")))
53 self
.assertEquals(1, len(find_all(t
, "//ReturnStatNode[./NameNode]")))
54 self
.assertEquals(Nodes
.ReturnStatNode
,
55 type(find_first(t
, "//ReturnStatNode[./NameNode]")))
57 def test_node_path_node_predicate_step(self
):
58 t
= self
._build
_tree
()
59 self
.assertEquals(2, len(find_all(t
, "//DefNode[.//NameNode]")))
60 self
.assertEquals(8, len(find_all(t
, "//DefNode[.//NameNode]//*")))
61 self
.assertEquals(1, len(find_all(t
, "//DefNode[.//NameNode]//ReturnStatNode")))
62 self
.assertEquals(Nodes
.ReturnStatNode
,
63 type(find_first(t
, "//DefNode[.//NameNode]//ReturnStatNode")))
65 def test_node_path_attribute_exists(self
):
66 t
= self
._build
_tree
()
67 self
.assertEquals(2, len(find_all(t
, "//NameNode[@name]")))
68 self
.assertEquals(ExprNodes
.NameNode
,
69 type(find_first(t
, "//NameNode[@name]")))
71 def test_node_path_attribute_exists_not(self
):
72 t
= self
._build
_tree
()
73 self
.assertEquals(0, len(find_all(t
, "//NameNode[not(@name)]")))
74 self
.assertEquals(2, len(find_all(t
, "//NameNode[not(@honking)]")))
76 def test_node_path_and(self
):
77 t
= self
._build
_tree
()
78 self
.assertEquals(1, len(find_all(t
, "//DefNode[.//ReturnStatNode and .//NameNode]")))
79 self
.assertEquals(0, len(find_all(t
, "//NameNode[@honking and @name]")))
80 self
.assertEquals(0, len(find_all(t
, "//NameNode[@name and @honking]")))
81 self
.assertEquals(2, len(find_all(t
, "//DefNode[.//NameNode[@name] and @name]")))
83 def test_node_path_attribute_string_predicate(self
):
84 t
= self
._build
_tree
()
85 self
.assertEquals(1, len(find_all(t
, "//NameNode[@name = 'decorator']")))
87 def test_node_path_recursive_predicate(self
):
88 t
= self
._build
_tree
()
89 self
.assertEquals(2, len(find_all(t
, "//DefNode[.//NameNode[@name]]")))
90 self
.assertEquals(1, len(find_all(t
, "//DefNode[.//NameNode[@name = 'decorator']]")))
91 self
.assertEquals(1, len(find_all(t
, "//DefNode[.//ReturnStatNode[./NameNode[@name = 'fun']]/NameNode]")))
93 if __name__
== '__main__':