示例#1
0
 def test_flatten(self):
     n = ast.Module()
     f = ast.FunctionDef()
     e1 = ast.Expr()
     a1 = ast.Name()
     e2 = ast.Expr()
     a2 = ast.Name()
     n.body = [f]
     f.body = [e1, e2]
     e1.value = a1
     e2.value = a2
     
     expected = [f, e1, a1, e2, a2]
     actual =  tools.npath(n, ".**")
     self.assertEqual(expected, actual)
示例#2
0
 def test_subscript(self):
     n = ast.FunctionDef(body=[ast.Name(id="a"), ast.Name(id="b")])
     self.assertEqual("a", tools.npath(n, ".body[0].id"))
     self.assertEqual("b", tools.npath(n, ".body[1].id"))
示例#3
0
 def test_filter(self):
     n = [ast.Name(), ast.Assign(), ast.Return]
     self.assertEqual(n[1], tools.npath(n, "{Assign}"))
示例#4
0
 def test_nested_attribute(self):
     n = ast.Return(value=ast.Name(id="hi"))
     self.assertEqual("hi", tools.npath(n, ".value.id"))
示例#5
0
 def test_attribute(self):
     n = ast.FunctionDef(name="hi")
     self.assertEqual("hi", tools.npath(n, ".name"))
示例#6
0
 def test_empty(self):
     n = ast.Module()
     self.assertEqual(n, tools.npath(n, ""))
示例#7
0
 def assertNotSame(self, src, path_a, path_b):
     node = self.parse(src)
     a = tools.npath(node, path_a)
     b = tools.npath(node, path_b)
     self.assertIsNot(a, b)
示例#8
0
 def get(self, src, path):
     node = self.parse(src)
     return tools.npath(node, path)