Skip to content

Commit

Permalink
Make invalid identifiers a fatal error
Browse files Browse the repository at this point in the history
Ref. #892
  • Loading branch information
Alexander Senier committed Jan 6, 2022
1 parent 8dc0e4f commit ac47760
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 34 deletions.
2 changes: 1 addition & 1 deletion rflx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def validate(args: argparse.Namespace) -> None:

try:
identifier = ID(args.message_identifier)
except RecordFluxError as e:
except FatalError as e:
fail(f"invalid identifier: {e}", Subsystem.CLI)

try:
Expand Down
22 changes: 8 additions & 14 deletions rflx/identifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from typing import Optional, Sequence, TypeVar, Union

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

Self = TypeVar("Self", bound="ID")

Expand All @@ -25,23 +25,17 @@ def __init__(

error = RecordFluxError()
if not self._parts:
error.extend([("empty identifier", Subsystem.ID, Severity.ERROR, location)])
fatal_fail("empty identifier", Subsystem.ID, Severity.ERROR, location)
elif "" in self._parts:
error.extend(
[(f'empty part in identifier "{self}"', Subsystem.ID, Severity.ERROR, location)]
)
fatal_fail(f'empty part in identifier "{self}"', Subsystem.ID, Severity.ERROR, location)
else:
for c in [" ", ".", ":"]:
if any(c in part for part in self._parts):
error.extend(
[
(
f'"{c}" in identifier parts of "{self}"',
Subsystem.ID,
Severity.ERROR,
location,
)
],
fatal_fail(
f'"{c}" in identifier parts of "{self}"',
Subsystem.ID,
Severity.ERROR,
location,
)
error.propagate()

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

from rflx import ada, typing_ as rty
from rflx.error import Location, RecordFluxError
from rflx.error import FatalError, Location, RecordFluxError
from rflx.expression import (
FALSE,
TRUE,
Expand Down Expand Up @@ -719,7 +719,7 @@ def test_term_simplified() -> None:


def test_variable_invalid_name() -> None:
with pytest.raises(RecordFluxError):
with pytest.raises(FatalError):
Variable("Foo (Bar)")


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def generate(model: Model) -> Generator:


def test_invalid_prefix() -> None:
with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "A..B"$'):
with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "A..B"$'):
Generator(Model(), Integration(), "A..B")


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

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


Expand All @@ -20,41 +20,37 @@ def test_id_invalid_type() -> None:


def test_id_invalid_empty() -> None:
with pytest.raises(RecordFluxError, match=r"^id: error: empty identifier$"):
with pytest.raises(FatalError, match=r"^id: error: empty identifier$"):
ID([])


def test_id_invalid_empty_string() -> None:
with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier ""$'):
with pytest.raises(FatalError, match=r'^id: error: empty part in identifier ""$'):
ID("")


def test_id_invalid_empty_part() -> None:
with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "A::::B"$'):
with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "A::::B"$'):
ID("A::::B")


def test_id_invalid_empty_first_part() -> None:
with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "::A::B"$'):
with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "::A::B"$'):
ID("::A::B")


def test_id_invalid_empty_last_part() -> None:
with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "A::B::"$'):
with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "A::B::"$'):
ID("A::B::")


def test_id_invalid_whitespace() -> None:
with pytest.raises(
RecordFluxError, match=r'^id: error: " " in identifier parts of "A::B C::D"$'
):
with pytest.raises(FatalError, match=r'^id: error: " " in identifier parts of "A::B C::D"$'):
ID("A::B C::D")


def test_id_invalid_colon() -> None:
with pytest.raises(
RecordFluxError, match=r'^id: error: ":" in identifier parts of "A::B:C::D"$'
):
with pytest.raises(FatalError, match=r'^id: error: ":" in identifier parts of "A::B:C::D"$'):
ID("A::B:C::D")


Expand Down Expand Up @@ -107,7 +103,7 @@ def test_id_parent() -> None:


def test_id_parent_error() -> None:
with pytest.raises(RecordFluxError, match=r"^id: error: empty identifier$"):
with pytest.raises(FatalError, match=r"^id: error: empty identifier$"):
ID("A").parent # pylint: disable=expression-not-assigned


Expand Down
6 changes: 3 additions & 3 deletions tests/unit/pyrflx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from rflx import expression as expr
from rflx.error import FatalError
from rflx.identifier import ID
from rflx.model import (
BOOLEAN,
Expand Down Expand Up @@ -1215,9 +1216,8 @@ def test_set_checksum_to_pyrflx_invalid_id(
pyrflx_checksum: PyRFLX,
) -> None:
with pytest.raises(
PyRFLXError,
match='^pyrflx: error: "Refinement_With_Checksum:Message" is not a valid identifier:'
' id: error: ":" in identifier parts of "Refinement_With_Checksum:Message"$',
FatalError,
match='^id: error: ":" in identifier parts of "Refinement_With_Checksum:Message"$',
):
pyrflx_checksum.set_checksum_functions(
{
Expand Down

0 comments on commit ac47760

Please sign in to comment.