TestTreePath.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import unittest
  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
  6. class TestTreePath(TransformTest):
  7. _tree = None
  8. def _build_tree(self):
  9. if self._tree is None:
  10. self._tree = self.run_pipeline([], u"""
  11. def decorator(fun): # DefNode
  12. return fun # ReturnStatNode, NameNode
  13. @decorator # NameNode
  14. def decorated(): # DefNode
  15. pass
  16. """)
  17. return self._tree
  18. def test_node_path(self):
  19. t = self._build_tree()
  20. self.assertEqual(2, len(find_all(t, "//DefNode")))
  21. self.assertEqual(2, len(find_all(t, "//NameNode")))
  22. self.assertEqual(1, len(find_all(t, "//ReturnStatNode")))
  23. self.assertEqual(1, len(find_all(t, "//DefNode//ReturnStatNode")))
  24. def test_node_path_star(self):
  25. t = self._build_tree()
  26. self.assertEqual(10, len(find_all(t, "//*")))
  27. self.assertEqual(8, len(find_all(t, "//DefNode//*")))
  28. self.assertEqual(0, len(find_all(t, "//NameNode//*")))
  29. def test_node_path_attribute(self):
  30. t = self._build_tree()
  31. self.assertEqual(2, len(find_all(t, "//NameNode/@name")))
  32. self.assertEqual(['fun', 'decorator'], find_all(t, "//NameNode/@name"))
  33. def test_node_path_attribute_dotted(self):
  34. t = self._build_tree()
  35. self.assertEqual(1, len(find_all(t, "//ReturnStatNode/@value.name")))
  36. self.assertEqual(['fun'], find_all(t, "//ReturnStatNode/@value.name"))
  37. def test_node_path_child(self):
  38. t = self._build_tree()
  39. self.assertEqual(1, len(find_all(t, "//DefNode/ReturnStatNode/NameNode")))
  40. self.assertEqual(1, len(find_all(t, "//ReturnStatNode/NameNode")))
  41. def test_node_path_node_predicate(self):
  42. t = self._build_tree()
  43. self.assertEqual(0, len(find_all(t, "//DefNode[.//ForInStatNode]")))
  44. self.assertEqual(2, len(find_all(t, "//DefNode[.//NameNode]")))
  45. self.assertEqual(1, len(find_all(t, "//ReturnStatNode[./NameNode]")))
  46. self.assertEqual(Nodes.ReturnStatNode,
  47. type(find_first(t, "//ReturnStatNode[./NameNode]")))
  48. def test_node_path_node_predicate_step(self):
  49. t = self._build_tree()
  50. self.assertEqual(2, len(find_all(t, "//DefNode[.//NameNode]")))
  51. self.assertEqual(8, len(find_all(t, "//DefNode[.//NameNode]//*")))
  52. self.assertEqual(1, len(find_all(t, "//DefNode[.//NameNode]//ReturnStatNode")))
  53. self.assertEqual(Nodes.ReturnStatNode,
  54. type(find_first(t, "//DefNode[.//NameNode]//ReturnStatNode")))
  55. def test_node_path_attribute_exists(self):
  56. t = self._build_tree()
  57. self.assertEqual(2, len(find_all(t, "//NameNode[@name]")))
  58. self.assertEqual(ExprNodes.NameNode,
  59. type(find_first(t, "//NameNode[@name]")))
  60. def test_node_path_attribute_exists_not(self):
  61. t = self._build_tree()
  62. self.assertEqual(0, len(find_all(t, "//NameNode[not(@name)]")))
  63. self.assertEqual(2, len(find_all(t, "//NameNode[not(@honking)]")))
  64. def test_node_path_and(self):
  65. t = self._build_tree()
  66. self.assertEqual(1, len(find_all(t, "//DefNode[.//ReturnStatNode and .//NameNode]")))
  67. self.assertEqual(0, len(find_all(t, "//NameNode[@honking and @name]")))
  68. self.assertEqual(0, len(find_all(t, "//NameNode[@name and @honking]")))
  69. self.assertEqual(2, len(find_all(t, "//DefNode[.//NameNode[@name] and @name]")))
  70. def test_node_path_attribute_string_predicate(self):
  71. t = self._build_tree()
  72. self.assertEqual(1, len(find_all(t, "//NameNode[@name = 'decorator']")))
  73. def test_node_path_recursive_predicate(self):
  74. t = self._build_tree()
  75. self.assertEqual(2, len(find_all(t, "//DefNode[.//NameNode[@name]]")))
  76. self.assertEqual(1, len(find_all(t, "//DefNode[.//NameNode[@name = 'decorator']]")))
  77. self.assertEqual(1, len(find_all(t, "//DefNode[.//ReturnStatNode[./NameNode[@name = 'fun']]/NameNode]")))
  78. if __name__ == '__main__':
  79. unittest.main()