def test_not_separate_not_import_before_from(): ret = sort(IMPORTS, separate=False, import_before_from=False) assert ret == (( FromImport.from_str('from aspy import refactor_imports'), FromImport.from_str('from os import path'), ImportImport.from_str('import pyramid'), ImportImport.from_str('import sys'), ), )
def test_not_separate_not_import_before_from(): ret = sort(IMPORTS, separate=False, import_before_from=False) assert ret == ( ( FromImport.from_str('from aspy import refactor_imports'), FromImport.from_str('from os import path'), ImportImport.from_str('import pyramid'), ImportImport.from_str('import sys'), ), )
def test_future_separate_block_non_separate(): ret = sort( ( FromImport.from_str('from __future__ import absolute_import'), ImportImport.from_str('import pyramid'), ), separate=False, import_before_from=True, ) assert ret == ( (FromImport.from_str('from __future__ import absolute_import'), ), (ImportImport.from_str('import pyramid'), ), )
def test_future_separate_block_non_separate(): ret = sort( ( FromImport.from_str('from __future__ import absolute_import'), ImportImport.from_str('import pyramid'), ), separate=False, import_before_from=True, ) assert ret == ( (FromImport.from_str('from __future__ import absolute_import'),), (ImportImport.from_str('import pyramid'),), )
def test_passes_through_kwargs_to_classify(in_tmpdir, no_empty_path): # Make a module in_tmpdir.join('my_module.py').ensure() imports = ( ImportImport.from_str('import my_module'), ImportImport.from_str('import pyramid'), ) # Without kwargs, my_module should get classified as application (in a # separate group). ret = sort(imports) assert ret == ( (ImportImport.from_str('import pyramid'), ), (ImportImport.from_str('import my_module'), ), ) # But when we put the application at a nonexistent directory # it'll be third party (and in the same group as pyramid) ret = sort(imports, application_directories=('dne', )) assert ret == (imports, )
def test_passes_through_kwargs_to_classify(in_tmpdir, no_empty_path): # Make a module in_tmpdir.join('my_module.py').ensure() imports = ( ImportImport.from_str('import my_module'), ImportImport.from_str('import pyramid'), ) # Without kwargs, my_module should get classified as application (in a # separate group). ret = sort(imports) assert ret == ( (ImportImport.from_str('import pyramid'),), (ImportImport.from_str('import my_module'),), ) # But when we put the application at a nonexistent directory # it'll be third party (and in the same group as pyramid) ret = sort(imports, application_directories=('dne',)) assert ret == (imports,)
def test_import_import_cmp(): not_sorted = [ ImportImport.from_str('import herp.derp'), ImportImport.from_str('import harp.darp'), ImportImport.from_str('import Foo as baz'), ImportImport.from_str('import Foo as bar'), ] assert sorted(not_sorted) == [ ImportImport.from_str('import Foo as bar'), ImportImport.from_str('import Foo as baz'), ImportImport.from_str('import harp.darp'), ImportImport.from_str('import herp.derp'), ]
def test_import_import_cmp(): not_sorted = [ ImportImport.from_str('import herp.derp'), ImportImport.from_str('import harp.darp'), ImportImport.from_str('import Foo as baz'), ImportImport.from_str('import Foo as bar'), ] assert sorted(not_sorted) == [ ImportImport.from_str('import Foo as bar'), ImportImport.from_str('import Foo as baz'), ImportImport.from_str('import harp.darp'), ImportImport.from_str('import herp.derp'), ]
def test_separate_relative(): ret = sort(RELATIVE_IMPORTS) assert ret == ( ( ImportImport.from_str('import sys'), FromImport.from_str('from os import path'), ), (ImportImport.from_str('import pyramid'), ), ( FromImport.from_str('from .sort import sort'), FromImport.from_str('from aspy import refactor_imports'), ), ) ret = sort(RELATIVE_IMPORTS, separate_relative=True) assert ret == (( ImportImport.from_str('import sys'), FromImport.from_str('from os import path'), ), (ImportImport.from_str('import pyramid'), ), ( FromImport.from_str('from aspy import refactor_imports'), ), (FromImport.from_str('from .sort import sort'), ))
def test_import_import_hashable(): my_set = set() my_set.add(ImportImport.from_str('import foo')) my_set.add(ImportImport.from_str('import foo')) assert len(my_set) == 1
def import_import(): yield ImportImport.from_str('import Foo as bar')
def test_import_import_to_text(import_str): assert ImportImport.from_str(import_str).to_text() == import_str
def test_import_import_to_text_normalizes_whitespace(import_str, expected): assert ImportImport.from_str(import_str).to_text() == expected
@pytest.mark.parametrize( ('input_str', 'expected'), ( ('import foo', False), ('import foo, bar', True), ), ) def test_import_import_has_multiple_imports(input_str, expected): assert ImportImport.from_str(input_str).has_multiple_imports is expected @pytest.mark.parametrize( ('input_str', 'expected'), ( ('import foo', [ImportImport.from_str('import foo')]), ( 'import foo, bar', [ ImportImport.from_str('import foo'), ImportImport.from_str('import bar'), ], ), ), ) def test_import_import_split_imports(input_str, expected): assert ImportImport.from_str(input_str).split_imports() == expected @pytest.mark.parametrize( 'import_str',
def test_import_import_split_imports(input_str, expected): assert ImportImport.from_str(input_str).split_imports() == expected
@pytest.mark.parametrize( ('input_str', 'expected'), ( ('import foo', False), ('import foo, bar', True), ), ) def test_import_import_has_multiple_imports(input_str, expected): assert ImportImport.from_str(input_str).has_multiple_imports is expected @pytest.mark.parametrize( ('input_str', 'expected'), ( ('import foo', [ImportImport.from_str('import foo')]), ( 'import foo, bar', [ ImportImport.from_str('import foo'), ImportImport.from_str('import bar'), ], ), ) ) def test_import_import_split_imports(input_str, expected): assert ImportImport.from_str(input_str).split_imports() == expected @pytest.mark.parametrize( 'import_str',
from __future__ import absolute_import from __future__ import unicode_literals from aspy.refactor_imports.import_obj import FromImport from aspy.refactor_imports.import_obj import ImportImport from aspy.refactor_imports.sort import sort IMPORTS = ( FromImport.from_str('from os import path'), FromImport.from_str('from aspy import refactor_imports'), ImportImport.from_str('import sys'), ImportImport.from_str('import pyramid'), ) def test_separate_import_before_from(): ret = sort(IMPORTS, separate=True, import_before_from=True) assert ret == ( ( ImportImport.from_str('import sys'), FromImport.from_str('from os import path'), ), (ImportImport.from_str('import pyramid'), ), (FromImport.from_str('from aspy import refactor_imports'), ), ) def test_separate_not_import_before_from(): ret = sort(IMPORTS, separate=True, import_before_from=False) assert ret == ( (
def test_import_import_has_multiple_imports(input_str, expected): assert ImportImport.from_str(input_str).has_multiple_imports is expected
from __future__ import absolute_import from __future__ import unicode_literals from aspy.refactor_imports.import_obj import FromImport from aspy.refactor_imports.import_obj import ImportImport from aspy.refactor_imports.sort import sort IMPORTS = ( FromImport.from_str('from os import path'), FromImport.from_str('from aspy import refactor_imports'), ImportImport.from_str('import sys'), ImportImport.from_str('import pyramid'), ) def test_separate_import_before_from(): ret = sort(IMPORTS, separate=True, import_before_from=True) assert ret == ( ( ImportImport.from_str('import sys'), FromImport.from_str('from os import path'), ), ( ImportImport.from_str('import pyramid'), ), ( FromImport.from_str('from aspy import refactor_imports'), ), )
def test_import_import_equality_casing(): assert ( ImportImport.from_str('import herp.DERP') != ImportImport.from_str('import herp.derp') )
def import_import(): yield ImportImport.from_str('import Foo as bar')
def test_import_import_to_text_normalizes_whitespace(import_str, expected): assert ImportImport.from_str(import_str).to_text() == expected
def test_import_import_to_text(import_str): assert ImportImport.from_str(import_str).to_text() == import_str
def test_import_import_split_imports(input_str, expected): assert ImportImport.from_str(input_str).split_imports() == expected
def test_import_import_has_multiple_imports(input_str, expected): assert ImportImport.from_str(input_str).has_multiple_imports is expected
def test_import_import_equality_casing(): assert ( ImportImport.from_str('import herp.DERP') != ImportImport.from_str('import herp.derp') )