Skip to content

Commit

Permalink
Raise when data is both specification and section
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlan committed Jun 24, 2024
1 parent 89f3ddb commit 0d756b7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/parse/test_parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ def test_parse_vrplib_do_not_compute_edge_weights():
assert_("edge_weight" not in actual)


def test_parse_vrplib_raises_when_both_specification_and_section():
"""
Tests if a ValueError is raised when a specification is also a section.
"""
instance = "\n".join(
[
"SERVICE_TIME: 10",
"SERVICE_TIME_SECTION",
"1 20",
"2 20",
]
)

# Service time is both a specification and a section which is not allowed.
with assert_raises(ValueError):
parse_vrplib(instance)


def test_empty_text():
"""
Tests if an empty text file is still read correctly.
Expand Down
6 changes: 6 additions & 0 deletions vrplib/parse/parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def parse_vrplib(text: str, compute_edge_weights: bool = True) -> Instance:

for section in sections:
section_name, data = parse_section(section, instance)

if section_name in instance:
name = section_name.upper()
msg = f"{name} is used both as a specification and a section."
raise ValueError(msg)

instance[section_name] = data # type: ignore

if instance and compute_edge_weights and "edge_weight" not in instance:
Expand Down

0 comments on commit 0d756b7

Please sign in to comment.