Skip to content

Commit

Permalink
Added a few tests for NameResolutionMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
andrecsilva committed Oct 27, 2023
1 parent f1413c5 commit 61b5429
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/test_nameresolution_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import libcst as cst
from libcst.codemod import Codemod, CodemodContext
from codemodder.codemods.utils_mixin import NameResolutionMixin
from textwrap import dedent


class TestNameResolutionMixin:
def test_imported_prefix(self):
class TestCodemod(Codemod, NameResolutionMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
expr = cst.ensure_type(stmt.body[0], cst.Expr)
node = expr.value

maybe_name = self.get_aliased_prefix_name(node, "a.b")
assert maybe_name == "alias"
return tree

input_code = dedent(
"""\
import a.b as alias
alias.c[0].d()
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)

def test_no_imported_prefix(self):
class TestCodemod(Codemod, NameResolutionMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
expr = cst.ensure_type(stmt.body[0], cst.Expr)
node = expr.value

maybe_name = self.get_aliased_prefix_name(node, "a.b")
assert maybe_name is None
return tree

input_code = dedent(
"""\
c[0].d()
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)

def test_get_base_name_from_import(self):
class TestCodemod(Codemod, NameResolutionMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
expr = cst.ensure_type(stmt.body[0], cst.Expr)
node = expr.value

maybe_name = self.find_base_name(node.func)
assert maybe_name == "sys.executable.capitalize"
return tree

input_code = dedent(
"""\
from sys import executable as exec
exec.capitalize()
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)

def test_get_base_name_import(self):
class TestCodemod(Codemod, NameResolutionMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
expr = cst.ensure_type(stmt.body[0], cst.Expr)
node = expr.value

maybe_name = self.find_base_name(node.func)
assert maybe_name == "sys.executable.capitalize"
return tree

input_code = dedent(
"""\
import sys.executable as exec
exec.capitalize()
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)

def test_get_base_name_no_import(self):
class TestCodemod(Codemod, NameResolutionMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
expr = cst.ensure_type(stmt.body[0], cst.Expr)
node = expr.value

maybe_name = self.find_base_name(node.func)
assert maybe_name == "exec.capitalize"
return tree

input_code = dedent(
"""\
exec.capitalize()
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)

0 comments on commit 61b5429

Please sign in to comment.