Skip to content

Commit

Permalink
Correctly handle enumeration literals in PyRFLX
Browse files Browse the repository at this point in the history
Closes #865
  • Loading branch information
Alexander Senier authored and senier committed Dec 7, 2021
1 parent f0b427f commit f94b853
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 23 deletions.
31 changes: 14 additions & 17 deletions rflx/pyrflx/typevalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
Sequence,
Type,
)
from rflx.model.type_ import enum_literals
from rflx.pyrflx.bitstring import Bitstring
from rflx.pyrflx.error import PyRFLXError

Expand Down Expand Up @@ -571,19 +572,10 @@ def __init__(
}
)

self.__type_literals: ty.Mapping[Name, Expr] = (
state.type_literals
if state and state.type_literals
else {
k: v
for t in (
f.typeval.literals
for f in self._fields.values()
if isinstance(f.typeval, EnumValue)
)
for k, v in t.items()
}
)
self.__type_literals: ty.Mapping[Name, Expr] = {
Variable(l): e.literals[l.name]
for l, e in enum_literals(self._type.types.values(), self._type.package).items()
}

self.__additional_enum_literals: ty.Dict[Name, Expr] = {}
self.__message_first_name = First("Message")
Expand Down Expand Up @@ -886,10 +878,15 @@ def set_refinement(fld: MessageValue.Field, fld_name: str) -> None:
fld.typeval.set_refinement(ref.sdu)

def check_outgoing_condition_satisfied() -> None:
if all(
self.__simplified(o.condition) == FALSE
for o in self._type.outgoing(Field(field_name))
):
simplified = [
self.__simplified(o.condition) for o in self._type.outgoing(Field(field_name))
]
unresolved = [o for o in simplified if o not in (FALSE, TRUE)]
error_msg = ", ".join([str(o) for o in unresolved])
assert (
not unresolved
), f"unresolved field conditions in {self.model.name}.{field_name}: {error_msg}"
if all(o == FALSE for o in simplified):
self._fields[field_name].typeval.clear()
raise PyRFLXError(
f"none of the field conditions "
Expand Down
4 changes: 2 additions & 2 deletions tests/data/specs/parameterized.rflx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package Parameterized is
type Tag is (Tag_A, Tag_B) with Size => 8;
type Tag_Mode is (With_Tag, Without_Tag) with Size => 8;

type Message (Length : Length; Tag_Mode : Tag_Mode; Tag_Value : Tag) is
type Message (Length : Length; Tag_Mode : Tag_Mode; Use_Tag : Boolean; Tag_Value : Tag) is
message
Payload : Opaque
with Size => Length * 8
Expand All @@ -14,7 +14,7 @@ package Parameterized is
if Tag_Mode = Without_Tag;
Tag : Tag
then null
if Tag = Tag_Value;
if Tag = Tag_Value and Use_Tag = True;
end message;

end Parameterized;
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Length: 2
Tag_Mode: With_Tag
Tag_Value: Tag_A
Excess: 42
Use_Tag: True
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Length: 2
Tag_Value: Tag_A
Tag_Mode: Without_Tag
Use_Tag: True
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Length: 2
Tag_Mode: With_Tag
Tag_Value: Tag_A
Use_Tag: True
4 changes: 2 additions & 2 deletions tests/unit/pyrflx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ def test_get_model(icmp_message_value: MessageValue) -> None:

def test_parameterized_message(parameterized_package: Package) -> None:
message = parameterized_package.new_message(
"Message", {"Length": 8, "Has_Tag": False, "Tag_Value": "Tag_A"}
"Message", {"Length": 8, "Tag_Mode": "Without_Tag", "Tag_Value": "Tag_A", "Use_Tag": True}
)
assert message.fields == ["Payload", "Tag"]
assert message.required_fields == ["Payload"]
Expand All @@ -1389,7 +1389,7 @@ def test_parameterized_message_no_verification() -> None:
skip_message_verification=True,
)
message_unv = pyrflx_.package("Parameterized").new_message(
"Message", {"Length": 8, "Has_Tag": False, "Tag_Value": "Tag_A"}
"Message", {"Length": 8, "Tag_Mode": "Without_Tag", "Tag_Value": "Tag_A", "Use_Tag": True}
)
assert message_unv.fields == ["Payload", "Tag"]
message_unv.set("Payload", bytes(8))
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/validator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def test_parameterized_message_missing_parameter() -> None:
match=(
r"^"
f"{TEST_DIR}/parameterized/message/invalid/parameterized_message_missing_parameter.raw:"
r" pyrflx: error: missing parameter values: Has_Tag"
r" pyrflx: error: missing parameter values: Tag_Mode"
r"$"
),
):
Expand Down

0 comments on commit f94b853

Please sign in to comment.