def test_as_string_with_artifacts(self) -> None: group = BaseImportGroup( statements=[ImportStatement("b"), ImportStatement("a")], artifacts=Artifacts(sep="\r\n"), ) assert str(group.as_string()) == "import a\r\nimport b"
def test_formatted_with_artifacts(self) -> None: stem = "b" * 80 group = BaseImportGroup( statements=[ ImportStatement(stem, leafs=[ImportLeaf("c"), ImportLeaf("d")]), ImportStatement("a"), ], artifacts=Artifacts(sep="\r\n"), ) assert group.formatted() == "\r\n".join( [f"import a", f"from {stem} import (", f" c,", f" d,", f")"])
def test_formatted_with_artifacts(self) -> None: artifacts = Artifacts(sep="\r\n") groups = ImportGroups( [ RemainderGroup(artifacts=artifacts), LocalGroup(artifacts=artifacts) ], artifacts=artifacts, ) groups.add_statement(ImportStatement(".a")) groups.add_statement(ImportStatement("foo")) assert groups.formatted() == "import foo\r\n\r\nimport .a"
def test_inject_tree_artifacts(self) -> None: text = "\n".join([ "import os", "from contextlib import suppress", "from itertools import chain, count", "chain", ]) artifacts = UnusedImportsPlugin().inject_tree_artifacts( Artifacts.default(), parse_to_tree(text), text) assert typing.cast(UnsusedImportsArtifacts, artifacts).unused_imports == [ "os", "contextlib.suppress", "itertools.count", ]
def _test( self, stem: str, leafs: typing.List[ImportLeaf], expected: typing.List[str], sep: str = "\n", inline_comments: typing.List[str] = None, standalone_comments: typing.List[str] = None, ) -> None: """Facilitate the output tests of formatters""" statement = ImportStatement( stem, leafs=leafs, inline_comments=inline_comments, standalone_comments=standalone_comments, ) actual = self.formatter(statement, config=Config.default(), artifacts=Artifacts(sep=sep)).format() assert actual == sep.join(expected)
def test_should_include_statement(self) -> None: plugin = UnusedImportsPlugin() artifacts = Artifacts.default() typing.cast(UnsusedImportsArtifacts, artifacts).unused_imports = [ "os", "sys as system", ] assert plugin.should_include_statement( RemainderGroup(artifacts=artifacts), ImportStatement("os", inline_comments=["noqa"]), ) assert not plugin.should_include_statement( RemainderGroup(artifacts=artifacts), ImportStatement("os")) assert plugin.should_include_statement( RemainderGroup(artifacts=artifacts), ImportStatement("sys")) assert not plugin.should_include_statement( RemainderGroup(artifacts=artifacts), ImportStatement("sys", as_name="system"), )
def test_should_include_leaf(self) -> None: plugin = UnusedImportsPlugin() artifacts = Artifacts.default() typing.cast(UnsusedImportsArtifacts, artifacts).unused_imports = [ "os", "itertools.chain as ichain", ] assert plugin.should_include_leaf( RemainderGroup(artifacts=artifacts), ImportStatement("os", leafs=[ImportLeaf("path")], inline_comments=["noqa"]), ImportLeaf("path"), ) assert plugin.should_include_leaf( RemainderGroup(artifacts=artifacts), ImportStatement( "os", leafs=[ImportLeaf("path", statement_comments=["noqa"])]), ImportLeaf("path", statement_comments=["noqa"]), ) assert not plugin.should_include_leaf( RemainderGroup(artifacts=artifacts), ImportStatement("os", leafs=[ImportLeaf("path")]), ImportLeaf("path"), ) assert plugin.should_include_leaf( RemainderGroup(artifacts=artifacts), ImportStatement("itertools", leafs=[ImportLeaf("chain")]), ImportLeaf("chain"), ) assert not plugin.should_include_leaf( RemainderGroup(artifacts=artifacts), ImportStatement("itertools", leafs=[ImportLeaf("chain", as_name="ichain")]), ImportLeaf("chain", as_name="ichain"), )
def test_artifacts() -> None: assert Artifacts.default().first_line == 0 assert Artifacts.default().sep == "\n"