diff --git a/specfile/sections.py b/specfile/sections.py index 37d0ff8..141a039 100644 --- a/specfile/sections.py +++ b/specfile/sections.py @@ -173,7 +173,10 @@ def __contains__(self, id: object) -> bool: data = super().__getattribute__("data") except AttributeError: return False - return any(s.normalized_id == cast(str, id).lower() for s in data) + id_lower = cast(str, id).lower() + return any( + s.normalized_id == id_lower or s.normalized_name == id_lower for s in data + ) def __getattr__(self, id: str) -> Section: if id not in self: @@ -209,8 +212,9 @@ def get(self, id: str) -> Section: return self.data[self.find(id)] def find(self, id: str) -> int: + id_lower = id.lower() for i, section in enumerate(self.data): - if section.normalized_id == id.lower(): + if section.normalized_id == id_lower or section.normalized_name == id_lower: return i raise ValueError diff --git a/tests/unit/test_sections.py b/tests/unit/test_sections.py index 87bc855..8444093 100644 --- a/tests/unit/test_sections.py +++ b/tests/unit/test_sections.py @@ -39,6 +39,23 @@ def test_get(): sections.get("package foo") +def test_contains_and_getattr(): + sections = Sections( + [ + Section("package"), + Section("package", Options([Token(TokenType.DEFAULT, "baz")]), " "), + Section("install", delimiter=" "), + ] + ) + assert "package" in sections + assert "install" in sections + assert "install " in sections + assert sections.package == sections[0] + assert getattr(sections, "package baz") == sections[1] + assert sections.install == sections[-1] + assert getattr(sections, "install ") == sections[-1] + + @pytest.mark.parametrize( "id, existing, name, options, content", [