summaryrefslogtreecommitdiff
path: root/_test/roundtrip.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2016-01-22 21:22:33 +0100
committerAnthon van der Neut <anthon@mnt.org>2016-01-22 21:22:33 +0100
commitda73c8aa0d8db3d0f54d88740a0f7f621ac563df (patch)
tree7cbbfec9780ab9a33f09926e91cab343be049cdb /_test/roundtrip.py
parenta5f03a5e66daaea85af2730fa961d11c62d8cf17 (diff)
downloadruamel.yaml-da73c8aa0d8db3d0f54d88740a0f7f621ac563df.tar.gz
moved test to _test to prevent setuptools from including test/test_*.py0.10.17
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
Diffstat (limited to '_test/roundtrip.py')
-rw-r--r--_test/roundtrip.py54
1 files changed, 54 insertions, 0 deletions
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