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
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'),
    ]
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
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'),
                            ]
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'),
    ]
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'),
    ]
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'),
        ]
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'),
                            ]
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'),
                            ]
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
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'),
    ]
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'),
                            ]
def test_partition_source_indented_encoding():
    assert partition_source('   # -*- coding: UTF-8 -*-\n') == [
        CodePartition(
            CodeType.PRE_IMPORT_CODE,
            '   # -*- coding: UTF-8 -*-\n',
        ),
    ]
def test_partion_source_import_no_nl():
    assert partition_source('import os') == [
        CodePartition(CodeType.IMPORT, 'import os'),
    ]
def test_partition_source_encoding_no_nl():
    assert partition_source('# -*- coding: UTF-8 -*-') == [
        CodePartition(CodeType.PRE_IMPORT_CODE, '# -*- coding: UTF-8 -*-'),
    ]
def test_partition_source_code_no_nl():
    assert partition_source('x = 1') == [
        CodePartition(CodeType.CODE, 'x = 1'),
    ]
def test_apply_import_sorting_maintains_comments():
    input_partitions = [
        CodePartition(CodeType.IMPORT, 'import foo  # noqa\n'),
    ]
    assert apply_import_sorting(input_partitions) == input_partitions
def test_partition_source_shebang_no_nl():
    assert partition_source('#!/usr/bin/env python') == [
        CodePartition(CodeType.PRE_IMPORT_CODE, '#!/usr/bin/env python'),
    ]
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',
            ),
        ]
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'),
    ]
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
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'),
    ]
def test_partition_source_docstring_no_nl():
    assert partition_source('"""foo"""') == [
        CodePartition(CodeType.PRE_IMPORT_CODE, '"""foo"""'),
    ]