Skip to content

Commit

Permalink
Factor out assert_message_model_error
Browse files Browse the repository at this point in the history
Ref. #336, #337
  • Loading branch information
Alexander Senier committed Jul 13, 2020
1 parent 49218ed commit 01fc085
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
15 changes: 5 additions & 10 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=too-many-lines
from copy import deepcopy
from typing import Mapping, Sequence
from typing import Sequence

import pytest

Expand Down Expand Up @@ -48,7 +48,7 @@
UnprovenMessage,
)
from tests.models import ENUMERATION, ETHERNET_FRAME, MODULAR_INTEGER, RANGE_INTEGER
from tests.utils import assert_equal
from tests.utils import assert_equal, assert_message_model_error

M_NO_REF = UnprovenMessage(
"P.No_Ref",
Expand Down Expand Up @@ -148,13 +148,6 @@ def assert_model_error(types: Sequence[Type], regex: str) -> None:
Model([*BUILTIN_TYPES.values(), *types])


def assert_message_model_error(
structure: Sequence[Link], types: Mapping[Field, Type], regex: str
) -> None:
with pytest.raises(RecordFluxError, match=regex):
Message("P.M", structure, types, Location((10, 8)))


def assert_message(actual: Message, expected: Message, msg: str = None) -> None:
msg = f"{expected.full_name} - {msg}" if msg else expected.full_name
assert actual.full_name == expected.full_name, msg
Expand Down Expand Up @@ -414,6 +407,7 @@ def test_message_ambiguous_first_field() -> None:
'^<stdin>:10:8: model: error: ambiguous first field in "P.M"\n'
"<stdin>:2:6: model: info: duplicate\n"
"<stdin>:3:6: model: info: duplicate",
Location((10, 8)),
)


Expand Down Expand Up @@ -549,12 +543,13 @@ def test_message_cycle() -> None:
assert_message_model_error(
structure,
types,
'^<stdin>:10:8: model: error: structure of "P.M" contains cycle'
'^<stdin>:10:8: model: error: structure of "P.M" contains cycle',
# ISSUE: Componolit/RecordFlux#256
# '\n'
# '<stdin>:3:5: model: info: field "X" links to "Y"\n'
# '<stdin>:4:5: model: info: field "Y" links to "Z"\n'
# '<stdin>:5:5: model: info: field "Z" links to "X"\n',
Location((10, 8)),
)


Expand Down
38 changes: 14 additions & 24 deletions tests/test_verification.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pylint: disable=too-many-lines
from typing import Any, Mapping, Sequence
from typing import Any

import pytest

Expand Down Expand Up @@ -33,17 +33,9 @@
ModularInteger,
Opaque,
RangeInteger,
Type,
)
from tests.models import ARRAYS_MODULAR_VECTOR, ENUMERATION, MODULAR_INTEGER, RANGE_INTEGER


def assert_message_model_error(
structure: Sequence[Link], types: Mapping[Field, Type], regex: str
) -> None:
with pytest.raises(RecordFluxError, match=regex):
message = Message("P.M", structure, types)
message.error.propagate()
from tests.utils import assert_message_model_error


def test_exclusive_valid() -> None:
Expand Down Expand Up @@ -507,20 +499,18 @@ def test_invalid_negative_field_length_modular() -> None:


def test_invalid_negative_field_length_range_integer() -> None:
with pytest.raises(
RecordFluxError,
match=r'^<stdin>:44:3: model: error: negative length for field "O" [(]L -> O[)]$',
):
o = Field(ID("O", location=Location((44, 3))))
Message(
"P.M",
[
Link(INITIAL, Field("L")),
Link(Field("L"), o, length=Mul(Number(8), Sub(Variable("L"), Number(50))),),
Link(o, FINAL),
],
{Field("L"): RANGE_INTEGER, o: Opaque()},
)
o = Field(ID("O", location=Location((44, 3))))
structure = [
Link(INITIAL, Field("L")),
Link(Field("L"), o, length=Mul(Number(8), Sub(Variable("L"), Number(50))),),
Link(o, FINAL),
]
types = {Field("L"): RANGE_INTEGER, o: Opaque()}
assert_message_model_error(
structure,
types,
r'^<stdin>:44:3: model: error: negative length for field "O" [(]L -> O[)]$',
)


def test_payload_no_length() -> None:
Expand Down
14 changes: 13 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
from typing import Any
from typing import Any, Mapping, Sequence

import pytest

from rflx.error import Location, RecordFluxError
from rflx.model import Field, Link, Message, Type


def assert_equal(left: Any, right: Any) -> None:
assert left == right


def assert_message_model_error(
structure: Sequence[Link], types: Mapping[Field, Type], regex: str, location: Location = None
) -> None:
with pytest.raises(RecordFluxError, match=regex):
Message("P.M", structure, types, location)

0 comments on commit 01fc085

Please sign in to comment.