def test_create_source_map_multiple_nodes(self): source = """ from __future__ import print_function def test_fn(x): return x + 1 """ source = textwrap.dedent(source) nodes = parser.parse_str(source, single_node=False) fake_import_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) anno.setanno(nodes[0], anno.Basic.ORIGIN, fake_import_origin) fake_function_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) anno.setanno(nodes[1], anno.Basic.ORIGIN, fake_function_origin) source_map = origin_info.create_source_map(nodes, source, 'test_filename') loc = origin_info.LineLocation('test_filename', 2) self.assertIn(loc, source_map) self.assertIs(source_map[loc], fake_import_origin) loc = origin_info.LineLocation('test_filename', 3) self.assertIn(loc, source_map) self.assertIs(source_map[loc], fake_function_origin)
def test_get_message_converted_code(self): callsite_tb = [ ('/path/one.py', 11, 'test_fn_1', 'test code 1'), ('/path/two.py', 171, 'test_fn_2', 'test code 2'), ('/path/three.py', 171, 'test_fn_3', 'test code 3'), ] cause_message = 'Test message' em = error_utils.ErrorMetadataBase( callsite_tb=callsite_tb, cause_metadata=None, cause_message=cause_message, source_map={ origin_info.LineLocation(filename='/path/two.py', lineno=171): origin_info.OriginInfo( loc=origin_info.LineLocation( filename='/path/other_two.py', lineno=13), function_name='converted_fn', source_code_line='converted test code', comment=None) }, converter_filename=None) result = em.get_message() self.assertRegex( result, re.compile((r'converted_fn \*.*' r'"/path/three.py", line 171, in test_fn_3.*' r'Test message'), re.DOTALL)) self.assertNotRegex(result, re.compile('test_fn_1'))
def fake_origin(self, function, line_offset): _, lineno = tf_inspect.getsourcelines(function) filename = tf_inspect.getsourcefile(function) lineno += line_offset loc = origin_info.LineLocation(filename, lineno) origin = origin_info.OriginInfo(loc, 'test_function_name', 'test_code', 'test_comment') return loc, origin
def test_basic(self): def test_fn(): raise ValueError() node, ctx = self.prepare(test_fn, {}) anno.setanno( node, anno.Basic.ORIGIN, origin_info.OriginInfo(None, 'test_function_name', 'test_code', 'test_comment')) node = error_handlers.transform(node, ctx) with self.compiled(node, {}) as result: with self.assertRaises(errors.GraphConstructionError): # Here we just assert that the handler works. Its correctness is # verified by errors_test.py. result.test_fn()
def test_create_source_map(self): def test_fn(x): return x + 1 node, _, _ = parser.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) anno.setanno(node.body[0], anno.Basic.ORIGIN, fake_origin) converted_code = compiler.ast_to_source(node) source_map = origin_info.create_source_map(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 test_create_source_map(self): source = """ def test_fn(x): return x + 1 """ source = textwrap.dedent(source) node = parser.parse(source) 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) anno.setanno(node, anno.Basic.ORIGIN, fake_origin) source_map = origin_info.create_source_map(node, source, 'test_filename') loc = origin_info.LineLocation('test_filename', 2) self.assertIn(loc, source_map) self.assertIs(source_map[loc], fake_origin)