diff --git a/tests/fields/test_edge_data_field.py b/tests/fields/test_edge_data_field.py index 6863203..090fe20 100644 --- a/tests/fields/test_edge_data_field.py +++ b/tests/fields/test_edge_data_field.py @@ -12,7 +12,7 @@ def field(): @pytest.mark.parametrize('text,value,exc', [ ('1 2\n2 3\n-1', [(1, 2), (2, 3)], None), ('1 2\n2 x\n-1', [(1, 2), (2, 3)], E.ParsingError), - ('1 2 3\n2 3\n-1', {1: [2, 3], 2: [3]}, None), + ('1 2 3 -1\n2 3 -1\n-1', {1: [2, 3], 2: [3]}, None), ('1 x 3\n2 3\n-1', {1: [2, 3], 2: [3]}, E.ParsingError), ]) def test_parse(field, text, value, exc): @@ -20,4 +20,4 @@ def test_parse(field, text, value, exc): with pytest.raises(exc): field.parse(text) else: - field.parse(text) == value + assert field.parse(text) == value diff --git a/tests/fields/test_edge_list_field.py b/tests/fields/test_edge_list_field.py index 215ce8a..cbd823a 100644 --- a/tests/fields/test_edge_list_field.py +++ b/tests/fields/test_edge_list_field.py @@ -9,7 +9,7 @@ def edge_list_field(): def test_parse(edge_list_field): - assert edge_list_field.parse('1 2\n3 4\n-1') == [[1, 2], [3, 4]] + assert edge_list_field.parse('1 2\n3 4\n-1') == [(1, 2), (3, 4)] def test_render(edge_list_field): diff --git a/tsplib95/fields.py b/tsplib95/fields.py index 7de9b88..6f3e084 100644 --- a/tsplib95/fields.py +++ b/tsplib95/fields.py @@ -237,8 +237,8 @@ class AdjacencyListField(TransformerField): @classmethod def build_transformer(cls): return T.MapT(key=T.FuncT(func=int), - value=T.ListT(value=T.FuncT(func=int)), - sep=('-1', ' -1\n'), + value=T.ListT(value=T.FuncT(func=int), terminal='-1'), + sep='\n', terminal='-1') @@ -249,7 +249,7 @@ class EdgeListField(TransformerField): @classmethod def build_transformer(cls): - edge = T.ListT(value=T.FuncT(func=int), size=2) + edge = T.TupleT(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(adj_list, edge_list) + return T.UnionT(edge_list, adj_list) class DepotsField(TransformerField): diff --git a/tsplib95/transformers.py b/tsplib95/transformers.py index 7536349..a2dc33d 100644 --- a/tsplib95/transformers.py +++ b/tsplib95/transformers.py @@ -244,6 +244,14 @@ 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.