def test_fixers(fixture): expected_source = utils.clean_whitespace(fixture.expected_source) expected_ast = utils.parsedump_ast(expected_source) expected_header = transpile.parse_module_header(expected_source, fixture.target_version) test_source = utils.clean_whitespace(fixture.test_source) _debug_ast("testcase", test_source) _debug_ast("expected", expected_source) ctx = common.init_build_context( target_version=fixture.target_version, fixers=fixture.names, filepath="<testfile>" ) results = utils.transpile_and_dump(ctx, test_source) result_header_coding, result_header_text, result_source = results _debug_ast("result", result_source) result_ast = utils.parsedump_ast(result_source) assert result_header_coding == expected_header.coding assert result_header_text == expected_header.text assert result_ast == expected_ast assert _normalized_source(result_source) == _normalized_source(expected_source)
def test_header_preserved(): return test_source = """ #!/usr/bin/env python # This file is part of the lib3to6 project # https://github.com/mbarkhau/lib3to6 # (C) 2018 Manuel Barkhau <*****@*****.**> # # SPDX-License-Identifier: MIT hello = 'world' """ expected_source = """ #!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of the lib3to6 project # https://github.com/mbarkhau/lib3to6 # (C) 2018 Manuel Barkhau <*****@*****.**> # # SPDX-License-Identifier: MIT from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals hello = 'world' """ test_source = utils.clean_whitespace(test_source) expected_source = utils.clean_whitespace(expected_source) result_coding, result_header, result_source = utils.transpile_and_dump(test_source) assert result_coding == "utf-8" assert expected_source == result_source expected_ast = utils.parsedump_ast(expected_source) result_ast = utils.parsedump_ast(result_source) assert expected_ast == result_ast
def test_numeric_literals_with_underscore(): # NOTE (mb 2018-06-14): We don't need to transpile here # this case is taken care off by the fact that there # is no representation of the underscores at the ast # level. a_ast = utils.parsedump_ast("x = 200_000_000") b_ast = utils.parsedump_ast("x = 200000000") assert a_ast == b_ast
def _debug_ast(casename, source): if DEBUG_VERBOSITY > 0: print(casename.upper() * 9) if DEBUG_VERBOSITY > 1: ast = utils.parsedump_ast(source) print(ast) print("-------- " * 9) if DEBUG_VERBOSITY > 2: print(repr(source)) print(source)
def test_fixers(fixture): if "--capture=no" in sys.argv: print() expected_source = utils.clean_whitespace(fixture.expected_source) expected_ast = utils.parsedump_ast(expected_source) expected_coding, expected_header = transpile.parse_module_header( expected_source) test_source = utils.clean_whitespace(fixture.test_source) test_ast = utils.parsedump_ast(test_source) print(">>>>>>>>" * 9) print(test_ast) print("--------" * 9) print(repr(test_source)) print(">>>>>>>>" * 9) print("????????" * 9) # print(expected_ast) # print("--------" * 9) print(repr(expected_source)) print("????????" * 9) cfg = {"fixers": fixture.names, "target_version": fixture.target_version} result_coding, result_header, result_source = utils.transpile_and_dump( test_source, cfg) result_ast = utils.parsedump_ast(result_source) print("<<<<<<<<" * 9) # print(result_ast) # print("--------" * 9) print(repr(result_source)) print("<<<<<<<<" * 9) assert result_coding == expected_coding assert result_header == expected_header assert result_ast == expected_ast assert _normalized_source(result_source) == _normalized_source( expected_source)
def _normalized_source(in_source): """This is mostly to get rid of comments""" in_source = utils.clean_whitespace(in_source) out_source = utils.parsedump_source(in_source) assert utils.parsedump_ast(out_source) == utils.parsedump_ast(in_source) return out_source