Skip to content

Commit

Permalink
Use custom errors instead of assertions for id check
Browse files Browse the repository at this point in the history
Ref. #529
  • Loading branch information
rssen committed Jan 7, 2021
1 parent d863dab commit 0e3bad1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions rflx/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Subsystem(Enum):
MODEL = auto()
CLI = auto()
GRAPH = auto()
ID = auto()

def __str__(self) -> str:
return str.lower(self.name)
Expand Down
16 changes: 11 additions & 5 deletions rflx/identifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, Sequence, Union

from rflx.error import Location
from rflx.error import Location, RecordFluxError, Severity, Subsystem


class ID:
Expand All @@ -20,10 +20,16 @@ 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'
error = RecordFluxError()
if not self._parts:
error.append("empty identifier", Subsystem.ID, Severity.ERROR)
elif "" in self._parts:
error.append("empty part in identifier", Subsystem.ID, Severity.ERROR)
else:
for c in [" ", ".", ":"]:
if any(c in part for part in self._parts):
error.append(f'"{c}" in identifier parts', Subsystem.ID, Severity.ERROR)
error.propagate()

def __eq__(self, other: object) -> bool:
if isinstance(other, self.__class__):
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/expression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,8 @@ def test_term_simplified() -> None:
)


@pytest.mark.skipif(not __debug__, reason="depends on contract")
def test_variable_invalid_name() -> None:
with pytest.raises(AssertionError):
with pytest.raises(RecordFluxError):
Variable("Foo (Bar)")


Expand Down
4 changes: 2 additions & 2 deletions tests/unit/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import rflx.expression as expr
from rflx.ada import ID
from rflx.error import RecordFluxError
from rflx.generator import Generator, common, const
from rflx.model import BUILTIN_TYPES, Model, Type
from tests.const import GENERATED_DIR
Expand All @@ -30,9 +31,8 @@ def generate(model: Model) -> Generator:
return generator


@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_invalid_prefix() -> None:
with pytest.raises(AssertionError, match=r"empty part in identifier"):
with pytest.raises(RecordFluxError, match=r"^id: error: empty part in identifier$"):
Generator(Model(), "A..B")


Expand Down
25 changes: 9 additions & 16 deletions tests/unit/identifier_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from rflx.error import RecordFluxError
from rflx.identifier import ID


Expand All @@ -18,45 +19,38 @@ def test_id_invalid_type() -> None:
ID(0) # type: ignore


@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_id_invalid_empty() -> None:
with pytest.raises(AssertionError, match=r"^empty identifier$"):
with pytest.raises(RecordFluxError, match=r"^id: error: 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$"):
with pytest.raises(RecordFluxError, match=r"^id: error: empty part in identifier$"):
ID("")


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


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


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


@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_id_invalid_whitespace() -> None:
with pytest.raises(AssertionError, match=r'^" " in identifier parts$'):
with pytest.raises(RecordFluxError, match=r'^id: error: " " in identifier parts$'):
ID("A::B C::D")


@pytest.mark.skipif(not __debug__, reason="depends on assertion")
def test_id_invalid_colon() -> None:
with pytest.raises(AssertionError, match=r'^":" in identifier parts$'):
with pytest.raises(RecordFluxError, match=r'^id: error: ":" in identifier parts$'):
ID("A::B:C::D")


Expand Down Expand Up @@ -104,9 +98,8 @@ 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$"):
with pytest.raises(RecordFluxError, match=r"^id: error: empty identifier$"):
ID("A").parent # pylint: disable=expression-not-assigned


Expand Down

0 comments on commit 0e3bad1

Please sign in to comment.