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)
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"))
def test_filter(self): n = [ast.Name(), ast.Assign(), ast.Return] self.assertEqual(n[1], tools.npath(n, "{Assign}"))
def test_nested_attribute(self): n = ast.Return(value=ast.Name(id="hi")) self.assertEqual("hi", tools.npath(n, ".value.id"))
def test_attribute(self): n = ast.FunctionDef(name="hi") self.assertEqual("hi", tools.npath(n, ".name"))
def test_empty(self): n = ast.Module() self.assertEqual(n, tools.npath(n, ""))
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)
def get(self, src, path): node = self.parse(src) return tools.npath(node, path)