def test_rename_symbols_attributes(self): node = parsing.parse_str('b.c = b.c.d') node = qual_names.resolve(node) node = ast_util.rename_symbols( node, {qual_names.from_str('b.c'): qual_names.QN('renamed_b_c')}) source = parsing.ast_to_source(node) self.assertEqual(source.strip(), 'renamed_b_c = renamed_b_c.d')
def test_rename_symbols_basic(self): node = parsing.parse_str('a + b') node = qual_names.resolve(node) node = ast_util.rename_symbols( node, {qual_names.QN('a'): qual_names.QN('renamed_a')}) self.assertIsInstance(node.body[0].value.left.id, str) source = parsing.ast_to_source(node) self.assertEqual(source.strip(), 'renamed_a + b')
def _get_source(self, node): try: source, _ = parsing.ast_to_source(node) return source # pylint: disable=broad-except # This function is used for error reporting. If an exception occurs here, # it should be suppressed, in favor of emitting as informative a message # about the original error as possible. except Exception: return '<could not convert AST to source>'
def test_source_map_no_origin(self): def test_fn(x): return x + 1 node, _ = parsing.parse_entity(test_fn) fn_node = node.body[0] converted_code = parsing.ast_to_source(fn_node) source_map = origin_info.create_source_map(fn_node, converted_code, 'test_filename', [0]) self.assertEmpty(source_map)
def test_ast_to_source(self): node = gast.If( test=gast.Num(1), body=[ gast.Assign(targets=[gast.Name('a', gast.Store(), None)], value=gast.Name('b', gast.Load(), None)) ], orelse=[ gast.Assign(targets=[gast.Name('a', gast.Store(), None)], value=gast.Str('c')) ]) source = parsing.ast_to_source(node, indentation=' ') self.assertEqual( textwrap.dedent(""" if 1: a = b else: a = 'c' """).strip(), source.strip())
def test_create_source_map(self): def test_fn(x): return x + 1 node, _ = parsing.parse_entity(test_fn) fake_origin = origin_info.OriginInfo( loc=origin_info.Location('fake_filename', 3, 7), function_name='fake_function_name', source_code_line='fake source line', comment=None) fn_node = node.body[0] anno.setanno(fn_node.body[0], anno.Basic.ORIGIN, fake_origin) converted_code = parsing.ast_to_source(fn_node) source_map = origin_info.create_source_map(fn_node, converted_code, 'test_filename', [0]) loc = origin_info.LineLocation('test_filename', 2) self.assertIn(loc, source_map) self.assertIs(source_map[loc], fake_origin)
def assertLambdaNodes(self, matching_nodes, expected_bodies): self.assertEqual(len(matching_nodes), len(expected_bodies)) for node in matching_nodes: self.assertIsInstance(node, gast.Lambda) self.assertIn(parsing.ast_to_source(node.body).strip(), expected_bodies)
def _mock_apply_fn(self, target, source): target = parsing.ast_to_source(target) source = parsing.ast_to_source(source) self._invocation_counts[(target.strip(), source.strip())] += 1
def __repr__(self): if isinstance(self.ast_node, gast.FunctionDef): return 'def %s' % self.ast_node.name elif isinstance(self.ast_node, gast.withitem): return parsing.ast_to_source(self.ast_node.context_expr).strip() return parsing.ast_to_source(self.ast_node).strip()