diff options
author | Anthon van der Neut <anthon@mnt.org> | 2015-03-27 06:53:20 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2015-03-27 06:53:20 +0100 |
commit | 7c213b38b3e20aa5f7d1c400f9b7b4d2f41e02d5 (patch) | |
tree | 0a41d4f3003da5648eaf102af7b16e44eb66cacc | |
parent | 93d0a63f886d78f179e388bc5668bdae00c61f7a (diff) | |
download | ruamel.yaml-7c213b38b3e20aa5f7d1c400f9b7b4d2f41e02d5.tar.gz |
flow mapping style preservation + tests
-rw-r--r-- | py/__init__.py | 2 | ||||
-rw-r--r-- | py/constructor.py | 8 | ||||
-rw-r--r-- | py/representer.py | 4 | ||||
-rw-r--r-- | test/test_indentation.py | 46 |
4 files changed, 58 insertions, 2 deletions
diff --git a/py/__init__.py b/py/__init__.py index 1c6a93a..8ee5446 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -21,7 +21,7 @@ def _convert_version(tup): return ret_val -version_info = (0, 7) +version_info = (0, 7, 1) __version__ = _convert_version(version_info) del _convert_version diff --git a/py/constructor.py b/py/constructor.py index ae1046b..fa44d28 100644 --- a/py/constructor.py +++ b/py/constructor.py @@ -920,12 +920,20 @@ class RoundTripConstructor(SafeConstructor): def construct_yaml_map(self, node): data = CommentedMap() + if node.flow_style is True: + data.fa.set_flow_style() + elif node.flow_style is False: + data.fa.set_block_style() yield data self.construct_mapping(node, data) def construct_yaml_omap(self, node): # Note: we do now check for duplicate keys omap = CommentedOrderedMap() + if node.flow_style is True: + data.fa.set_flow_style() + elif node.flow_style is False: + data.fa.set_block_style() yield omap if node.comment: omap._yaml_add_comment(node.comment[:2]) diff --git a/py/representer.py b/py/representer.py index f4ca75f..1a5f922 100644 --- a/py/representer.py +++ b/py/representer.py @@ -627,6 +627,10 @@ class RoundTripRepresenter(SafeRepresenter): def represent_mapping(self, tag, mapping, flow_style=None): value = [] + try: + flow_style = mapping.fa.flow_style(flow_style) + except AttributeError: + pass node = MappingNode(tag, value, flow_style=flow_style) if self.alias_key is not None: self.represented_objects[self.alias_key] = node diff --git a/test/test_indentation.py b/test/test_indentation.py index 3b4e939..67e2951 100644 --- a/test/test_indentation.py +++ b/test/test_indentation.py @@ -17,12 +17,43 @@ def rt(s): ).strip() + '\n' -# @pytest.mark.xfail def test_roundtrip_inline_list(): s = 'a: [a, b, c]\n' output = rt(s) assert s == output +def test_roundtrip_mapping_of_inline_lists(): + s = dedent("""\ + a: [a, b, c] + j: [k, l, m] + """) + output = rt(s) + assert s == output + +def test_roundtrip_mapping_of_inline_lists_comments(): + s = dedent("""\ + # comment A + a: [a, b, c] + # comment B + j: [k, l, m] + """) + output = rt(s) + assert s == output + +# the following doesn't work correctly. The comment for the sequence is +# emitted on the next line instead of after the flow sequence +@pytest.mark.xfail +def test_roundtrip_mapping_of_inline_lists_eol_comments(): + s = dedent("""\ + # comment A + a: [a, b, c] # comment B + j: [k, l, m] # comment C + """) + output = rt(s) + assert s == output + + +# first test by explicitly setting flow style def test_added_inline_list(): s1 = dedent(""" a: @@ -38,6 +69,19 @@ def test_added_inline_list(): output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper) assert s == output +############# flow mappings + +def test_roundtrip_flow_mapping(): + s = dedent("""\ + - {a: 1, b: hallo} + - {j: fka, k: 42} + """) + data = ruamel.yaml.load(s, Loader=ruamel.yaml.RoundTripLoader) + output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper) + assert s == output + + +############# indentation @pytest.mark.xfail def test_roundtrip_four_space_indents(): |