def test_ast_template(self): a = AST.ast_template("$ID = 1", ASTLanguage.Python, id="x") self.assertEqual(a.source_text, "x = 1") self.assertIsInstance(a, PythonAssignment0) a = AST.ast_template("fn(@ARGS)", ASTLanguage.Python, args=[1, 2, 3]) self.assertEqual(a.source_text, "fn(1, 2, 3)") self.assertIsInstance(a, PythonCall) a = AST.ast_template("$1 = $2", ASTLanguage.Python, "x", 1) self.assertEqual(a.source_text, "x = 1") self.assertIsInstance(a, PythonAssignment0) a = AST.ast_template("fn(@1)", ASTLanguage.Python, [1, 2, 3]) self.assertEqual(a.source_text, "fn(1, 2, 3)") self.assertIsInstance(a, PythonCall) lhs = AST.from_string("x", ASTLanguage.Python, deepest=True) a = AST.ast_template("$1 = value", ASTLanguage.Python, lhs) self.assertEqual(a.source_text, "x = value") self.assertIsInstance(a, PythonAssignment0) template = "$LEFT_HAND_SIDE = $RIGHT_HAND_SIDE" a = AST.ast_template(template, ASTLanguage.Python, left_hand_side="x", right_hand_side=1) self.assertEqual(a.source_text, "x = 1") self.assertIsInstance(a, PythonAssignment0)
def test_provided_by(self): root = AST.from_string("import os\nos.path.join(a, b)", ASTLanguage.Python) self.assertEqual(1, len(root.call_asts())) call = root.call_asts()[0] self.assertEqual("os.path", call.provided_by(root))
def test_imports(self): code = "import os\nimport sys as s\nfrom json import dump\nprint('Hello')" root = AST.from_string(code, ASTLanguage.Python) ast = root.children[-1] imports = ast.imports(root) self.assertEqual([["os"], ["sys", "s"], ["json", None, "dump"]], imports)
def test_insert_ast(self): new_ast = AST.from_string("y = 2\n", language=ASTLanguage.Python, deepest=True) new_root = AST.insert(self.root, self.statement, new_ast) self.assertNotEqual(new_root.oid, self.root.oid) self.assertEqual(2, len(new_root.children)) self.assertEqual("y = 2\nx = 88\n", new_root.source_text)
def test_no_arguments(self): root = AST.from_string("foo()", ASTLanguage.Python) self.assertEqual(1, len(root.call_asts())) call = root.call_asts()[0] self.assertEqual(None, call.provided_by(root)) self.assertEqual("foo", call.call_function().source_text) self.assertEqual([], call.call_arguments())
def test_no_params(self): ast = AST.from_string("def foo(): return None", ASTLanguage.Python) self.assertEqual(1, len(ast.function_asts())) function = ast.function_asts()[0] self.assertEqual("foo", function.function_name()) self.assertEqual([], function.function_parameters()) self.assertEqual("return None", function.function_body().source_text)
def test_ast_constructor_deepest_parameter(self): new = AST.from_string( self.root.source_text, language=self.root.language, deepest=True, ) self.assertEqual(new.source_text, self.root.source_text) self.assertNotEqual(type(new), type(self.root))
def test_multiple_parameters(self): ast = AST.from_string("def bar(a, b): return a*b", ASTLanguage.Python) self.assertEqual(1, len(ast.function_asts())) function = ast.function_asts()[0] params = [p.source_text for p in function.function_parameters()] self.assertEqual("bar", function.function_name()) self.assertEqual(["a", "b"], params) self.assertEqual("return a*b", function.function_body().source_text)
def test_multiple_arguments(self): root = AST.from_string("bar(a, b)", ASTLanguage.Python) self.assertEqual(1, len(root.call_asts())) call = root.call_asts()[0] args = [a.source_text for a in call.call_arguments()] self.assertEqual(None, call.provided_by(root)) self.assertEqual("bar", call.call_function().source_text) self.assertEqual(["a", "b"], args)
def test_inner_parent_asts(self): text = """__all__ = [ "c", # first comment # second comment ]""" root = AST.from_string(text, ASTLanguage.Python) inner_parent = root.children[0].children[0].children[1].children[1] self.assertIsInstance(inner_parent, InnerParent) self.assertTrue( inner_parent.source_text.startswith(" # first comment"))
def test_vars_in_scope_no_globals(self): root = AST.from_string("def bar(a, b): return a*b", ASTLanguage.Python) ast = root.children[-1].children[-1].children[-1] vars_in_scope = ast.get_vars_in_scope(root, keep_globals=False) names = [var["name"] for var in vars_in_scope] self.assertEqual(names[0], "a") self.assertEqual(names[1], "b") scopes = [var["scope"] for var in vars_in_scope] self.assertIsInstance(scopes[0], PythonFunctionDefinition2) self.assertIsInstance(scopes[1], PythonFunctionDefinition2) decls = [var["decl"] for var in vars_in_scope] self.assertIsInstance(decls[0], PythonIdentifier) self.assertIsInstance(decls[1], PythonIdentifier)
def test_copy_with_kwargs(self): copy = AST.copy( self.root, left=AST.from_string("y", ASTLanguage.Python, deepest=True), ) self.assertEqual(copy.source_text, "y + 1") self.assertNotEqual(copy.oid, self.root.oid) copy = AST.copy(self.root, left=0.5) self.assertEqual(copy.source_text, "0.5 + 1") self.assertNotEqual(copy.oid, self.root.oid) copy = AST.copy(self.root, left=2) self.assertEqual(copy.source_text, "2 + 1") self.assertNotEqual(copy.oid, self.root.oid) copy = AST.copy(self.root, left='"hi"') self.assertEqual(copy.source_text, '"hi" + 1') self.assertNotEqual(copy.oid, self.root.oid) copy = AST.copy(self.root, left="y") self.assertEqual(copy.source_text, "y + 1") self.assertNotEqual(copy.oid, self.root.oid)
def test_utf8_multibyte_characters(self): root = AST.from_string('"反复请求多次"', ASTLanguage.Python) rnge = root.ast_source_ranges()[0][1] self.assertEqual('"反复请求多次"', root.source_text) self.assertEqual([[1, 1], [1, 9]], rnge)
def setUp(self): self.root = AST.from_string("x + 88", ASTLanguage.Python) self.binop = self.root.children[0].children[0] return
def test_no_imports(self): root = AST.from_string("", ASTLanguage.Python) self.assertEqual([], root.imports(root))
def test_no_vars_in_scope(self): root = AST.from_string("", ASTLanguage.Python) self.assertEqual([], root.get_vars_in_scope(root))
def test_error_handling(self): with self.assertRaises(ASTException): AST.from_string("foo()", language="foo")
def setUp(self): self.root = AST.from_string("x + 1", ASTLanguage.Python, deepest=True)
def setUp(self): self.root = AST.from_string("x = 88\n", ASTLanguage.Python) self.statement = self.root.children[0] return
def setUp(self): text = slurp(DATA_DIR / "transform" / "original.py") self.root = AST.from_string(text, ASTLanguage.Python)
def test_no_functions(self): ast = AST.from_string("", ASTLanguage.Python) self.assertEqual([], ast.function_asts())
def setUp(self): text = slurp(DATA_DIR / "transform" / "ccast-original.c") self.root = AST.from_string(text, ASTLanguage.C)
def simple_parse_driver(self, text, language): root = AST.from_string(text, language) self.assertEqual(root.language, language) self.assertEqual(root.source_text, text)
def test_no_calls(self): root = AST.from_string("", ASTLanguage.Python) self.assertEqual([], root.call_asts())