Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise when data is both specification and section #120

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/parse/test_parse_vrplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,25 @@ def test_parse_vrplib_do_not_compute_edge_weights():
assert_("edge_weight" not in actual)


def test_parse_vrplib_raises_data_specification_and_section():
"""
Tests that a ValueError is raised when data is included both as
specification and section.
"""
instance = "\n".join(
[
"SERVICE_TIME: 10",
"SERVICE_TIME_SECTION",
"1 20",
"2 20",
Comment on lines +241 to +244
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the section comes before the specification? Does that also raise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That raises an error, but it's not related to sections coming before specifications. I think we should be strict and add that too. I'll do that in another PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that works

]
)

# 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
Loading