diff options
author | Anthon van der Neut <anthon@mnt.org> | 2016-02-27 14:53:44 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2016-02-27 14:53:44 +0100 |
commit | 638b3d07b210f8636507c1530e925445f9c3aba9 (patch) | |
tree | d0c35dd79eae8ed610a9e33df8c3ea8b4e76a767 | |
parent | 0fbfd2d3f7895ebae49dc85abbe8844abb0e19bf (diff) | |
download | ruamel.yaml-638b3d07b210f8636507c1530e925445f9c3aba9.tar.gz |
implement indent for scalar list elements
-rw-r--r-- | __init__.py | 2 | ||||
-rw-r--r-- | _test/test_fail.py | 40 | ||||
-rw-r--r-- | _test/test_indentation.py | 8 | ||||
-rw-r--r-- | emitter.py | 5 | ||||
-rw-r--r-- | util.py | 4 |
5 files changed, 42 insertions, 17 deletions
diff --git a/__init__.py b/__init__.py index fa23f4d..d6bf32d 100644 --- a/__init__.py +++ b/__init__.py @@ -9,7 +9,7 @@ from __future__ import absolute_import _package_data = dict( full_package_name="ruamel.yaml", - version_info=(0, 11, 2), + version_info=(0, 11, 3, "dev"), author="Anthon van der Neut", author_email="a.van.der.neut@ruamel.eu", description="ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order", # NOQA diff --git a/_test/test_fail.py b/_test/test_fail.py index de5bdb8..852fde7 100644 --- a/_test/test_fail.py +++ b/_test/test_fail.py @@ -87,13 +87,34 @@ class TestIndentFailures: a: - foo - bar - """), indent=4) == dedent(""" + """)) == dedent(""" a: - foo - bar """) @pytest.mark.xfail + def test_roundtrip_four_space_indents_expl_indent(self): + s = ( + 'a:\n' + '- foo\n' + '- bar\n' + ) + output = round_trip_dump(round_trip_load(s), indent=4) + assert s == output + + def test_roundtrip_four_space_indents_expl_indent_no_fail(self): + assert round_trip_dump(round_trip_load(""" + a: + - foo + - bar + """), indent=4) == dedent(""" + a: + - foo + - bar + """) + + @pytest.mark.xfail def test_indent_not_retained(self): round_trip(""" verbosity: 1 # 0 is minimal output, -1 none @@ -145,9 +166,9 @@ class TestIndentFailures: base_url: http://gopher.net special_indices: [1, 5, 8] also_special: - - a - - 19 - - 32 + - a + - 19 + - 32 asia and europe: &asia_europe Turkey: Ankara Russia: Moscow @@ -160,17 +181,10 @@ class TestIndentFailures: Spain: Madrid Italy: Rome Antarctica: - - too cold + - too cold """) - @pytest.mark.xfail - def test_indent_top_level(self): - round_trip(""" - - a: - - b - """, indent=4) - - def test_indent_top_level_no_fail(self): + def Xtest_indent_top_level_no_fail(self): round_trip(""" - a: - b diff --git a/_test/test_indentation.py b/_test/test_indentation.py index c6131f1..4d2ca4f 100644 --- a/_test/test_indentation.py +++ b/_test/test_indentation.py @@ -10,6 +10,7 @@ from textwrap import dedent import pytest # NOQA import ruamel.yaml +from roundtrip import round_trip def rt(s): @@ -88,4 +89,11 @@ class TestIndent: output = rt(s) assert s == output + def test_indent_top_level(self): + round_trip(""" + - a: + - b + """, indent=4) + + # ############ indentation @@ -257,7 +257,7 @@ class Emitter(object): def expect_node(self, root=False, sequence=False, mapping=False, simple_key=False): self.root_context = root - self.sequence_context = sequence + self.sequence_context = sequence # not used in PyYAML self.mapping_context = mapping self.simple_key_context = simple_key if isinstance(self.event, AliasEvent): @@ -583,6 +583,8 @@ class Emitter(object): # if self.analysis.multiline and split \ # and (not self.style or self.style in '\'\"'): # self.write_indent() + if self.sequence_context and not self.flow_level: + self.write_indent() if self.style == '"': self.write_double_quoted(self.analysis.scalar, split) elif self.style == '\'': @@ -1204,7 +1206,6 @@ class Emitter(object): def write_comment(self, comment): value = comment.value - print('################## comment', repr(value)) # print('{:02d} {:02d} {}'.format(self.column, comment.start_mark.column, value)) if value[-1] == '\n': value = value[:-1] @@ -44,11 +44,13 @@ def load_yaml_guess_indent(stream): continue if prev_line_key_only is not None and rline: idx = 0 - while line[idx] in ' -': # this will end on ':' + while line[idx] in ' -': idx += 1 if idx > prev_line_key_only: indent = idx - prev_line_key_only break + else: + indent = 2 prev_line_key_only = None return round_trip_load(yaml_str), indent |