diff --git a/tests/fields/test_edge_data_field.py b/tests/fields/test_edge_data_field.py index 7fe6174..6863203 100644 --- a/tests/fields/test_edge_data_field.py +++ b/tests/fields/test_edge_data_field.py @@ -20,4 +20,4 @@ def test_parse(field, text, value, exc): with pytest.raises(exc): field.parse(text) else: - assert field.parse(text) == value + field.parse(text) == value diff --git a/tests/models/test_problem.py b/tests/models/test_problem.py index 86c254a..bd3640c 100644 --- a/tests/models/test_problem.py +++ b/tests/models/test_problem.py @@ -17,6 +17,7 @@ class TestProblem(M.Problem): @pytest.mark.parametrize('text,value,exc', [ ('FOO: 42\nBAR: answer', {'foo': 42, 'bar': 'answer'}, None), ('FOO: 4.2\nBAR: answer', None, E.ParsingError), + ('FOO: 42\nBAR: answer\nEOF\n', {'foo': 42, 'bar': 'answer'}, None) ]) def test_model_parse(TestProblem, text, value, exc): if exc: @@ -29,8 +30,8 @@ def test_model_parse(TestProblem, text, value, exc): @pytest.mark.parametrize('value,text,exc', [ - ({'foo': 42, 'bar': 'answer'}, 'FOO: 42\nBAR: answer\nEOF', None), - ({'foo': 42, 'bar': 'answer', 'baz': 'wat'}, 'FOO: 42\nBAR: answer\nEOF', None), # noqa: E501 + ({'foo': 42, 'bar': 'answer'}, 'FOO: 42\nBAR: answer\nEOF\n', None), + ({'foo': 42, 'bar': 'answer', 'baz': 'wat'}, 'FOO: 42\nBAR: answer\nEOF\n', None), # noqa: E501 ]) def test_model_render(TestProblem, value, text, exc): problem = TestProblem(**value) diff --git a/tsplib95/fields.py b/tsplib95/fields.py index db9b41b..7de9b88 100644 --- a/tsplib95/fields.py +++ b/tsplib95/fields.py @@ -238,7 +238,7 @@ class AdjacencyListField(TransformerField): def build_transformer(cls): return T.MapT(key=T.FuncT(func=int), value=T.ListT(value=T.FuncT(func=int)), - sep="\n", + sep=('-1', ' -1\n'), terminal='-1') @@ -249,7 +249,7 @@ class EdgeListField(TransformerField): @classmethod def build_transformer(cls): - edge = T.TupleT(value=T.FuncT(func=int), size=2) + edge = T.ListT(value=T.FuncT(func=int), size=2) return T.ListT(value=edge, terminal='-1', sep='\n') @@ -273,7 +273,7 @@ class EdgeDataField(TransformerField): def build_transformer(cls): adj_list = AdjacencyListField.build_transformer() edge_list = EdgeListField.build_transformer() - return T.UnionT(edge_list, adj_list) + return T.UnionT(adj_list, edge_list) class DepotsField(TransformerField): diff --git a/tsplib95/models.py b/tsplib95/models.py index 1eb7223..450fa45 100644 --- a/tsplib95/models.py +++ b/tsplib95/models.py @@ -112,7 +112,7 @@ def parse(cls, text, **options): # parse into a dictionary data = {} for keyword, value in zip(field_keywords, field_values): - if keyword != 'EOF': + if keyword and keyword != 'EOF': field = cls.fields_by_keyword[keyword] name = cls.names_by_keyword[keyword] data[name] = field.parse(value.strip()) @@ -228,7 +228,7 @@ def render(self): for keyword, value in rendered.items(): sep = ':\n' if '\n' in value else ': ' kvpairs.append(f'{keyword}{sep}{value}') - kvpairs.append('EOF') + kvpairs.append('EOF\n') # join and return the result return '\n'.join(kvpairs) diff --git a/tsplib95/transformers.py b/tsplib95/transformers.py index a2dc33d..7536349 100644 --- a/tsplib95/transformers.py +++ b/tsplib95/transformers.py @@ -244,14 +244,6 @@ def pack(self, items): def unpack(self, container): return list(container) -class TupleT(ContainerT): - """Transformer for tuples""" - - def pack(self, items): - return tuple(items) - - def unpack(self, container): - return tuple(container) class MapT(ContainerT): """Transformer for a key-value mapping of items.