Skip to content

Commit

Permalink
Prevent empty identifier
Browse files Browse the repository at this point in the history
Ref. #441, #443
  • Loading branch information
treiher committed Sep 15, 2020
1 parent 85e62c5 commit 6e62811
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions rflx/identifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Optional, Sequence, Union

# from rflx.contract import invariant
from rflx.error import Location


Expand All @@ -21,6 +20,7 @@ def __init__(
else:
assert False, f'unexpected identifier type "{type(identifier).__name__}"'

assert self._parts, "empty identifier"
assert "" not in self._parts, "empty part in identifier"
for c in [" ", ".", ":"]:
assert all(c not in part for part in self._parts), f'"{c}" in identifier parts'
Expand Down Expand Up @@ -85,11 +85,11 @@ def parts(self) -> Sequence[str]:

@property
def name(self) -> "ID":
return ID(self.parts[-1])
return self.__class__(self._parts[-1])

@property
def parent(self) -> "ID":
return ID(self.parts[:-1])
return self.__class__(self._parts[:-1])

@property
def _separator(self) -> str:
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/identifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def test_id_invalid_type() -> None:

@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_id_invalid_empty() -> None:
with pytest.raises(AssertionError, match=r"^empty identifier$"):
ID([])


@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_id_invalid_empty_string() -> None:
with pytest.raises(AssertionError, match=r"^empty part in identifier$"):
ID("")

Expand Down Expand Up @@ -90,5 +96,11 @@ def test_id_parent() -> None:
assert ID("A::B::C").parent == ID("A::B")


@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_id_parent_error() -> None:
with pytest.raises(AssertionError, match=r"^empty identifier$"):
ID("A").parent # pylint: disable=expression-not-assigned


def test_id_sorted() -> None:
assert sorted([ID("B"), ID("A")]) == [ID("A"), ID("B")]

0 comments on commit 6e62811

Please sign in to comment.