From da73c8aa0d8db3d0f54d88740a0f7f621ac563df Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Fri, 22 Jan 2016 21:22:33 +0100 Subject: moved test to _test to prevent setuptools from including test/test_*.py although the package dirs were explicitly specified. This used to lead to half included, non-working test directory contents that have no place in a distribution. This fixes issue #17 --- _test/roundtrip.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 _test/roundtrip.py (limited to '_test/roundtrip.py') diff --git a/_test/roundtrip.py b/_test/roundtrip.py new file mode 100644 index 0000000..446e943 --- /dev/null +++ b/_test/roundtrip.py @@ -0,0 +1,54 @@ + +from __future__ import print_function + +""" +helper routines for testing round trip of commented YAML data +""" +import textwrap + +import ruamel.yaml + + +def dedent(data): + try: + position_of_first_newline = data.index('\n') + for idx in range(position_of_first_newline): + if not data[idx].isspace(): + raise ValueError + except ValueError: + pass + else: + data = data[position_of_first_newline+1:] + return textwrap.dedent(data) + + +def round_trip_load(dinp): + return ruamel.yaml.load(dinp, ruamel.yaml.RoundTripLoader) + + +def round_trip_dump(data): + dumper = ruamel.yaml.RoundTripDumper + return ruamel.yaml.dump(data, default_flow_style=False, Dumper=dumper) + + +def round_trip(inp, outp=None, extra=None, intermediate=None): + dinp = dedent(inp) + if outp is not None: + doutp = dedent(outp) + else: + doutp = dinp + if extra is not None: + doutp += extra + data = round_trip_load(dinp) + if intermediate is not None: + if isinstance(intermediate, dict): + for k, v in intermediate.items(): + if data[k] != v: + print('{0!r} <> {1!r}'.format(data[k], v)) + raise ValueError + res = round_trip_dump(data) + print('roundtrip data:\n', res, sep='') + assert res == doutp + res = round_trip_dump(data) + print('roundtrip second round data:\n', res, sep='') + assert res == doutp -- cgit v1.2.1