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_checkers(fixture): if "--capture=no" in sys.argv: print() if isinstance(fixture.expected_error_msg, list): expected_error_messages = fixture.expected_error_msg else: expected_error_messages = [fixture.expected_error_msg] cfg = { "checkers": fixture.names, "target_version": "2.7", } test_source = utils.clean_whitespace(fixture.test_source) try: utils.transpile_and_dump(test_source, cfg) assert fixture.expected_error_msg is None except CheckError as result_error: result_error_msg = str(result_error) print("!!!", repr(result_error_msg)) assert fixture.expected_error_msg is not None for expected_error_msg in expected_error_messages: print("???", repr(expected_error_msg)) assert expected_error_msg in str(result_error)
def test_parse_header_simple(): coding, header = transpile.parse_module_header( clean_whitespace(""" # coding: ascii # Header line expr = 1 + 1 """)) assert coding == "ascii" assert header == "# coding: ascii\n# Header line\n"
def test_parse_header_simple(): source = clean_whitespace( """ # coding: ascii # Header line expr = 1 + 1 """ ) header = transpile.parse_module_header(source, "2.7") assert header.coding == "ascii" assert header.text == "# coding: ascii\n# Header line\n"
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 test_parse_header_coding(): source = clean_whitespace(""" # coding: shift_jis # 今日は expr = 1 + 1 """) coding, header = transpile.parse_module_header(source) assert coding == "shift_jis" assert header == "# coding: shift_jis\n# 今日は\n" source_data = source.encode("shift_jis") coding, header = transpile.parse_module_header(source_data) assert coding == "shift_jis" assert header == "# coding: shift_jis\n# 今日は\n"
def make_fixture(names, test_source, expected_error_msg): test_source = utils.clean_whitespace(test_source) return CheckFixture(names, test_source, expected_error_msg)
def make_fixture(names, target_version, test_source, expected_source): test_source = utils.clean_whitespace(test_source) expected_source = utils.clean_whitespace(expected_source) return FixerFixture(names, target_version, test_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