示例#1
0
文件: test_scan.py 项目: chvmq/pycln
 def _get_import(self, import_stmnt: str, expec_end_lineno: Optional[int]) -> tuple:
     ast_impt = ast.parse(import_stmnt).body[0]
     if expec_end_lineno:
         end_lineno = expec_end_lineno
     else:
         end_lineno = ast_impt.end_lineno
     start = (ast_impt.lineno, ast_impt.col_offset)
     location = _nodes.NodeLocation(start, end_lineno)
     return ast_impt, location
示例#2
0
class TestImportFrom:
    """`ImportFrom` dataclass test case."""
    @pytest.mark.parametrize(
        "location, names, module, level",
        [
            pytest.param(
                _nodes.NodeLocation(
                    (1, 0), 1), [], "xxx", 0, id="with module"),
            pytest.param(_nodes.NodeLocation((1, 0), 1), [],
                         None,
                         1,
                         id="without module"),
        ],
    )
    def test_init(self, location, names, module, level):
        node = _nodes.ImportFrom(location, names, module, level)
        assert node.location == location
        assert node.names == names
        assert node.module == module
        assert node.level == level

    @pytest.mark.parametrize(
        "module, level, expec_rel_name",
        [
            pytest.param("xxx", 0, "xxx", id="non-relative"),
            pytest.param("xxx", 1, ".xxx", id="relative"),
            pytest.param(None, 2, "..", id="no-module, relative"),
        ],
    )
    def test_relative_name_property(self, module, level, expec_rel_name):
        location = _nodes.NodeLocation((1, 1), 1)
        node = _nodes.ImportFrom(location, [], module, level)
        assert node.relative_name == expec_rel_name

    def test_hash_dunder(self):
        location = _nodes.NodeLocation((1, 4), 1)
        node = _nodes.ImportFrom(location, [], None, 1)
        assert hash(node) == hash(node.location)
示例#3
0
class TestImport:
    """`Import` dataclass test case."""
    @pytest.mark.parametrize(
        "location, names",
        [pytest.param(_nodes.NodeLocation((1, 0), 1), [], id="normal init")],
    )
    def test_init(self, location, names):
        node = _nodes.Import(location, names)
        assert node.location == location
        assert node.names == names

    def test_hash_dunder(self):
        location = _nodes.NodeLocation((1, 4), 1)
        node = _nodes.Import(location, [])
        assert hash(node) == hash(node.location)
示例#4
0
 def test_relative_name_property(self, module, level, expec_rel_name):
     location = _nodes.NodeLocation((1, 1), 1)
     node = _nodes.ImportFrom(location, [], module, level)
     assert node.relative_name == expec_rel_name
示例#5
0
 def test_hash_dunder(self):
     location = _nodes.NodeLocation((1, 4), 1)
     node = _nodes.Import(location, [])
     assert hash(node) == hash(node.location)
示例#6
0
 def test_len_dunder(self, start, end, expec_len):
     location = _nodes.NodeLocation(start, end)
     assert len(location) == expec_len
示例#7
0
 def test_hash_dunder(self, start, end, expec_hash):
     location = _nodes.NodeLocation(start, end)
     assert hash(location) == expec_hash
示例#8
0
 def test_init(self, start, end):
     location = _nodes.NodeLocation(start, end)
     assert location.start.line == start[0]
     assert location.start.col == start[1]
     assert location.end.line == end