Skip to content

Commit

Permalink
test: fix expected error messages when duplicated names/types occur
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofavorito committed Jun 7, 2023
1 parent cf25df5 commit ccb5097
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
9 changes: 7 additions & 2 deletions pddl/parser/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,13 @@ def atomic_formula_skeleton(self, args):

def typed_list_name(self, args) -> Dict[str, Optional[str]]:
"""Process the 'typed_list_name' rule."""
types_index = TypesIndex.parse_typed_list(args)
return types_index.get_typed_list_of_names()
try:
types_index = TypesIndex.parse_typed_list(args)
return types_index.get_typed_list_of_names()
except ValueError as e:
raise PDDLParsingError(
f"error while parsing tokens {list(map(str, args))}: {str(e)}"
) from None

def typed_list_variable(self, args) -> Dict[str, Set[str]]:
"""
Expand Down
6 changes: 4 additions & 2 deletions pddl/parser/types_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def __init__(self) -> None:
def add_item(self, item_name: str, type_tags: Set[str]) -> None:
"""Add an item."""
if item_name in self._item_to_types:
types_list = sorted(map(str, self._item_to_types[item_name]))
types_list_str = f" with types {types_list}" if len(types_list) > 0 else ""
raise ValueError(
f"duplicate name '{item_name}' in typed list: already present with "
f"types {sorted(map(str,self._item_to_types[item_name]))}"
f"duplicate name '{item_name}' in typed list already present"
+ types_list_str
)

exisiting_tags = self._item_to_types.get(item_name, set())
Expand Down
16 changes: 8 additions & 8 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def test_types_repetition_in_simple_typed_lists_not_allowed() -> None:

with pytest.raises(
lark.exceptions.VisitError,
match="duplicate items \\['a'\\] found in the typed list: "
"\\['a', 'b', 'c', 'a'\\]",
match=".*error while parsing tokens \\['a', 'b', 'c', 'a'\\]: "
"duplicate name 'a' in typed list already present",
):
DomainParser()(domain_str)

Expand All @@ -154,8 +154,8 @@ def test_types_repetition_in_typed_lists_not_allowed() -> None:

with pytest.raises(
lark.exceptions.VisitError,
match="detected conflicting items in a typed list: items occurred "
"twice: \\['a'\\]",
match=".*error while parsing tokens \\['a', '-', 't1', 'b', 'c', '-', 't2', 'a', '-', 't3'\\]: "
"duplicate name 'a' in typed list already present with types \\['t1'\\]",
):
DomainParser()(domain_str)

Expand All @@ -174,8 +174,8 @@ def test_constants_repetition_in_simple_typed_lists_not_allowed() -> None:

with pytest.raises(
lark.exceptions.VisitError,
match="duplicate items \\['c1'\\] found in the typed list: "
"\\['c1', 'c2', 'c3', 'c1'\\]",
match=".*error while parsing tokens \\['c1', 'c2', 'c3', 'c1'\\]: "
"duplicate name 'c1' in typed list already present",
):
DomainParser()(domain_str)

Expand All @@ -194,7 +194,7 @@ def test_constants_repetition_in_typed_lists_not_allowed() -> None:

with pytest.raises(
lark.exceptions.VisitError,
match="detected conflicting items in a typed list: items occurred "
"twice: \\['c1'\\]",
match=".*error while parsing tokens \\['c1', '-', 't1', 'c1', '-', 't2'\\]: "
"duplicate name 'c1' in typed list already present with types \\['t1'\\]",
):
DomainParser()(domain_str)

0 comments on commit ccb5097

Please sign in to comment.