From 9c99a4be4c3368fddad9b29864d29827bdfdff80 Mon Sep 17 00:00:00 2001 From: Leon Lan <30997278+leonlan@users.noreply.github.com> Date: Mon, 24 Jun 2024 20:21:31 +0200 Subject: [PATCH] Raise when data is both specification and section (#120) --- tests/parse/test_parse_vrplib.py | 19 +++++++++++++++++++ vrplib/parse/parse_vrplib.py | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/tests/parse/test_parse_vrplib.py b/tests/parse/test_parse_vrplib.py index e30c24a1..01dae71a 100644 --- a/tests/parse/test_parse_vrplib.py +++ b/tests/parse/test_parse_vrplib.py @@ -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", + ] + ) + + # 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. diff --git a/vrplib/parse/parse_vrplib.py b/vrplib/parse/parse_vrplib.py index 6fd6de8b..546b2244 100644 --- a/vrplib/parse/parse_vrplib.py +++ b/vrplib/parse/parse_vrplib.py @@ -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: