Skip to content

Commit

Permalink
Add check of field type in refinement
Browse files Browse the repository at this point in the history
Ref. #310
  • Loading branch information
treiher committed Jul 6, 2020
1 parent acc22aa commit 9dc1e44
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
14 changes: 14 additions & 0 deletions rflx/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,20 @@ def __init__(
package.location,
)

if not isinstance(pdu.types[field], Opaque):
self.error.append(
f'invalid type of field "{field.name}" in refinement of "{pdu.identifier}"',
Subsystem.MODEL,
Severity.ERROR,
field.identifier.location,
)
self.error.append(
"expected field of type Opaque",
Subsystem.MODEL,
Severity.INFO,
next(f for f in pdu.fields if f == field).identifier.location,
)

self.pdu = pdu
self.field = field
self.sdu = sdu
Expand Down
12 changes: 12 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,18 @@ def test_refinement_invalid_package() -> None:
)


def test_refinement_invalid_field_type() -> None:
x = Field(ID("X", Location((20, 10))))

message = Message("P.M", [Link(INITIAL, x), Link(x, FINAL)], {x: MODULAR_INTEGER})

assert_type_error(
Refinement("P", message, Field(ID("X", Location((33, 22)))), message),
r'^<stdin>:33:22: model: error: invalid type of field "X" in refinement of "P.M"\n'
r"<stdin>:20:10: model: info: expected field of type Opaque",
)


def test_field_locations() -> None:
f2 = Field(ID("F2", Location((2, 2))))
f3 = Field(ID("F3", Location((3, 2))))
Expand Down
20 changes: 12 additions & 8 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,19 @@ def test_duplicate_refinement() -> None:
assert_error_string(
"""
package Test is
type T is mod 256;
type PDU is
message
Foo : T;
null
then Foo
with Length => 8;
Foo : Opaque;
end message;
for Test.PDU use (Foo => Test.PDU);
for PDU use (Foo => PDU);
end Test;
""",
r'^<stdin>:9:16: parser: error: duplicate refinement with "Test.PDU"\n'
r"<stdin>:8:16: parser: info: previous occurrence",
r'^<stdin>:11:16: parser: error: duplicate refinement with "Test.PDU"\n'
r"<stdin>:10:16: parser: info: previous occurrence",
)


Expand Down Expand Up @@ -588,19 +590,21 @@ def test_refinement_invalid_condition() -> None:
assert_error_string(
"""
package Test is
type T is mod 256;
type PDU is
message
Foo : T;
null
then Foo
with Length => 8;
Foo : Opaque;
end message;
for PDU use (Foo => PDU)
if X < Y + 1;
end Test;
""",
r"^"
r'<stdin>:9:22: parser: error: unknown field or literal "X"'
r'<stdin>:11:22: parser: error: unknown field or literal "X"'
r' in refinement condition of "Test.PDU"\n'
r'<stdin>:9:26: parser: error: unknown field or literal "Y"'
r'<stdin>:11:26: parser: error: unknown field or literal "Y"'
r' in refinement condition of "Test.PDU"'
r"$",
)
Expand Down

0 comments on commit 9dc1e44

Please sign in to comment.