示例#1
0
def test_separate_comma_imports_separates_some():
    assert separate_comma_imports([
        CodePartition(CodeType.IMPORT, 'import os, sys\n'),
    ]) == [
        CodePartition(CodeType.IMPORT, 'import os\n'),
        CodePartition(CodeType.IMPORT, 'import sys\n'),
    ]
示例#2
0
def test_remove_duplicated_imports_no_dupes_no_removals():
    input_partitions = [
        CodePartition(CodeType.IMPORT, 'import sys\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(CodeType.IMPORT, 'from six import text_type\n'),
    ]
    assert remove_duplicated_imports(input_partitions) == input_partitions
示例#3
0
def test_partition_source_comment_lines():
    assert partition_source('# hello world\n'
                            'import os\n', ) == [
                                CodePartition(CodeType.PRE_IMPORT_CODE,
                                              '# hello world\n'),
                                CodePartition(CodeType.IMPORT, 'import os\n'),
                            ]
示例#4
0
def test_separate_comma_imports_none_to_separate():
    input_partitions = [
        CodePartition(CodeType.IMPORT, 'import os\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(CodeType.IMPORT, 'import six\n'),
    ]
    assert separate_comma_imports(input_partitions) == input_partitions
示例#5
0
def test_apply_import_sorting_removes_padding_if_only_imports():
    assert apply_import_sorting([
        CodePartition(CodeType.IMPORT, 'import foo\n'),
        CodePartition(CodeType.NON_CODE, '\n\n'),
    ]) == [
        CodePartition(CodeType.IMPORT, 'import foo\n'),
    ]
示例#6
0
def test_remove_duplicated_imports_removes_duplicated():
    assert remove_duplicated_imports([
        CodePartition(CodeType.IMPORT, 'import sys\n'),
        CodePartition(CodeType.IMPORT, 'import sys\n'),
    ]) == [
        CodePartition(CodeType.IMPORT, 'import sys\n'),
    ]
示例#7
0
def test_partition_source_encoding_and_shebang():
    assert partition_source(
        '#!/usr/bin/env python\n'
        '# -*- coding: UTF-8 -*-\n', ) == [
            CodePartition(CodeType.PRE_IMPORT_CODE, '#!/usr/bin/env python\n'),
            CodePartition(CodeType.PRE_IMPORT_CODE,
                          '# -*- coding: UTF-8 -*-\n'),
        ]
示例#8
0
def test_partition_source_unicode_docstring():
    assert partition_source('# -*- coding: UTF-8 -*-\n'
                            'u"""☃☃☃"""\n', ) == [
                                CodePartition(CodeType.PRE_IMPORT_CODE,
                                              '# -*- coding: UTF-8 -*-\n'),
                                CodePartition(CodeType.PRE_IMPORT_CODE,
                                              'u"""☃☃☃"""\n'),
                            ]
示例#9
0
def test_partition_source_multiple_docstrings():
    assert partition_source(
        '"""foo"""\n'
        '"""bar"""\n', ) == [
            # only the first docstring should count as a docstring
            CodePartition(CodeType.PRE_IMPORT_CODE, '"""foo"""\n'),
            CodePartition(CodeType.PRE_IMPORT_CODE, '"""bar"""\n'),
        ]
示例#10
0
def test_apply_import_sorting_all_types():
    input_partitions = [
        CodePartition(CodeType.PRE_IMPORT_CODE, '#!/usr/bin/env python\n'),
        CodePartition(CodeType.PRE_IMPORT_CODE, '# -*- coding: UTF-8 -*-\n'),
        CodePartition(CodeType.PRE_IMPORT_CODE, '"""foo"""\n'),
        CodePartition(CodeType.IMPORT, 'import os\n'),
        CodePartition(CodeType.CODE, '\n\nx = 5\n'),
    ]
    assert apply_import_sorting(input_partitions) == input_partitions
示例#11
0
def test_separate_comma_imports_removes_comments():
    # Since it's not really possible to know what the comma points to, we just
    # remove it
    assert separate_comma_imports([
        CodePartition(CodeType.IMPORT, 'import os, sys  # derp\n'),
    ]) == [
        CodePartition(CodeType.IMPORT, 'import os\n'),
        CodePartition(CodeType.IMPORT, 'import sys\n'),
    ]
示例#12
0
def test_partition_source_blank_lines_with_whitespace():
    assert partition_source('import os\n'
                            '\n'
                            '    \n'
                            'import sys\n', ) == [
                                CodePartition(CodeType.IMPORT, 'import os\n'),
                                CodePartition(CodeType.NON_CODE, '\n'),
                                CodePartition(CodeType.NON_CODE, '    \n'),
                                CodePartition(CodeType.IMPORT, 'import sys\n'),
                            ]
示例#13
0
def test_partition_source_indented_encoding():
    assert partition_source('   # -*- coding: UTF-8 -*-\n') == [
        CodePartition(
            CodeType.PRE_IMPORT_CODE,
            '   # -*- coding: UTF-8 -*-\n',
        ),
    ]
示例#14
0
def test_apply_import_sorting_sorts_imports_with_application_module():
    assert apply_import_sorting(
        [
            CodePartition(CodeType.IMPORT, 'import _c_module\n'),
            CodePartition(CodeType.IMPORT, 'import reorder_python_imports\n'),
            CodePartition(CodeType.IMPORT, 'import third_party\n'),
        ],
        unclassifiable_application_modules=['_c_module'],
    ) == [
        CodePartition(CodeType.IMPORT, 'import third_party\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(CodeType.IMPORT, 'import _c_module\n'),
        CodePartition(CodeType.IMPORT, 'import reorder_python_imports\n'),
    ]
示例#15
0
def test_remove_duplicate_redundant_import_imports():
    assert remove_duplicated_imports([
        CodePartition(CodeType.IMPORT, 'import os\n'),
        CodePartition(CodeType.IMPORT, 'import os.path\n'),
    ]) == [
        CodePartition(CodeType.IMPORT, 'import os.path\n'),
    ]
    assert remove_duplicated_imports([
        CodePartition(CodeType.IMPORT, 'import os.path\n'),
        CodePartition(CodeType.IMPORT, 'import os\n'),
    ]) == [
        CodePartition(CodeType.IMPORT, 'import os.path\n'),
    ]
示例#16
0
def test_partition_source_encoding_no_nl():
    assert partition_source('# -*- coding: UTF-8 -*-') == [
        CodePartition(CodeType.PRE_IMPORT_CODE, '# -*- coding: UTF-8 -*-'),
    ]
示例#17
0
def test_partition_source_code_no_nl():
    assert partition_source('x = 1') == [
        CodePartition(CodeType.CODE, 'x = 1'),
    ]
示例#18
0
def test_apply_import_sorting_maintains_comments():
    input_partitions = [
        CodePartition(CodeType.IMPORT, 'import foo  # noqa\n'),
    ]
    assert apply_import_sorting(input_partitions) == input_partitions
示例#19
0
def test_partition_source_shebang_no_nl():
    assert partition_source('#!/usr/bin/env python') == [
        CodePartition(CodeType.PRE_IMPORT_CODE, '#!/usr/bin/env python'),
    ]
示例#20
0
def test_apply_import_sorting_sorts_imports_with_separate_from_import():
    assert apply_import_sorting(
        [
            # local imports
            CodePartition(
                CodeType.IMPORT,
                'from reorder_python_imports import main\n',
            ),
            CodePartition(CodeType.IMPORT, 'import reorder_python_imports\n'),
            # site-package imports
            CodePartition(CodeType.IMPORT, 'from six import text_type\n'),
            CodePartition(CodeType.IMPORT, 'import aspy\n'),
            # System imports (out of order)
            CodePartition(CodeType.IMPORT, 'from os import path\n'),
            CodePartition(CodeType.IMPORT, 'import os\n'),
        ],
        separate_from_import=True,
    ) == [
        CodePartition(CodeType.IMPORT, 'import os\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(CodeType.IMPORT, 'from os import path\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(CodeType.IMPORT, 'import aspy\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(CodeType.IMPORT, 'from six import text_type\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(CodeType.IMPORT, 'import reorder_python_imports\n'),
        CodePartition(CodeType.NON_CODE, '\n'),
        CodePartition(
            CodeType.IMPORT,
            'from reorder_python_imports import main\n',
        ),
    ]
示例#21
0
def test_separate_comma_imports_does_not_remove_comments_when_not_splitting():
    input_partitions = [CodePartition(CodeType.IMPORT, 'import sys  # noqa\n')]
    assert separate_comma_imports(input_partitions) == input_partitions
示例#22
0
def test_partition_source_import_contains_comment():
    # We want to maintain comments with imports
    assert partition_source('from foo import *  # noqa\n') == [
        CodePartition(CodeType.IMPORT, 'from foo import *  # noqa\n'),
    ]
示例#23
0
def test_partion_source_import_no_nl():
    assert partition_source('import os') == [
        CodePartition(CodeType.IMPORT, 'import os'),
    ]
示例#24
0
def test_aliased_imports_not_considered_redundant_v2():
    partitions = [
        CodePartition(CodeType.IMPORT, 'import os as osmod\n'),
        CodePartition(CodeType.IMPORT, 'import os.path\n'),
    ]
    assert remove_duplicated_imports(partitions) == partitions
示例#25
0
def test_partition_source_import_inside_code_not_an_import():
    assert partition_source('x = 1\nimport os\n') == [
        CodePartition(CodeType.CODE, 'x = 1\nimport os\n'),
    ]
示例#26
0
def test_partition_source_docstring_no_nl():
    assert partition_source('"""foo"""') == [
        CodePartition(CodeType.PRE_IMPORT_CODE, '"""foo"""'),
    ]