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
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)
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)
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.Import(location, []) assert hash(node) == hash(node.location)
def test_len_dunder(self, start, end, expec_len): location = _nodes.NodeLocation(start, end) assert len(location) == expec_len
def test_hash_dunder(self, start, end, expec_hash): location = _nodes.NodeLocation(start, end) assert hash(location) == expec_hash
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