summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <bitbucket@ruamel.eu>2015-08-25 21:29:40 +0200
committerAnthon van der Neut <bitbucket@ruamel.eu>2015-08-25 21:29:40 +0200
commitbac63bd181213089ddb8d044e05f242a1c496916 (patch)
treeced12f11006c2f5462a3d803104d05b519cc4af6
parent15f13321d03baa5cf5ed7f1eff44ee5bab67a7cd (diff)
parentc6b37718c8dcfa008c23bf2bda21b3cebe69af8d (diff)
downloadruamel.yaml-bac63bd181213089ddb8d044e05f242a1c496916.tar.gz
Merged in samthursfield/yaml (pull request #4)
Fix assertion failure for certain kinds of merges
-rw-r--r--py/scanner.py34
-rw-r--r--test/test_string.py3
-rw-r--r--test/test_yaml.py34
3 files changed, 64 insertions, 7 deletions
diff --git a/py/scanner.py b/py/scanner.py
index 69c750d..24a13a8 100644
--- a/py/scanner.py
+++ b/py/scanner.py
@@ -1065,15 +1065,27 @@ class Scanner(object):
else:
break
- # Chomp the tail.
- if chomping is not False:
+ # Process trailing line breaks. The 'chomping' setting determines
+ # whether they are included in the value.
+ comment = []
+ if chomping in [None, True]:
chunks.append(line_break)
if chomping is True:
chunks.extend(breaks)
+ elif chomping in [None, False]:
+ comment.extend(breaks)
# We are done.
- return ScalarToken(u''.join(chunks), False, start_mark, end_mark,
- style)
+ token = ScalarToken(u''.join(chunks), False, start_mark, end_mark,
+ style)
+ if len(comment) > 0:
+ # Keep track of the trailing whitespace as a comment token, if
+ # isn't all included in the actual value.
+ comment_end_mark = self.get_mark()
+ comment = CommentToken(''.join(comment), end_mark,
+ comment_end_mark)
+ token.add_post_comment(comment)
+ return token
def scan_block_scalar_indicators(self, start_mark):
# See the specification for details.
@@ -1355,7 +1367,14 @@ class Scanner(object):
if not spaces or self.peek() == u'#' \
or (not self.flow_level and self.column < indent):
break
- return ScalarToken(u''.join(chunks), True, start_mark, end_mark)
+
+ token = ScalarToken(u''.join(chunks), True, start_mark, end_mark)
+ if spaces and spaces[0] == '\n':
+ # Create a comment token to preserve the trailing line breaks.
+ comment = CommentToken(''.join(spaces) + '\n', start_mark, end_mark)
+ token.add_post_comment(comment)
+ return token
+
def scan_plain_spaces(self, indent, start_mark):
# See the specification for details.
@@ -1614,6 +1633,11 @@ class RoundTripScanner(Scanner):
break
comment += ch
self.forward()
+ # gather any blank lines following the comment too
+ ch = self.scan_line_break()
+ while len(ch) > 0:
+ comment += ch
+ ch = self.scan_line_break()
end_mark = self.get_mark()
if not self.flow_level:
self.allow_simple_key = True
diff --git a/test/test_string.py b/test/test_string.py
index eb2e95c..8c08e7d 100644
--- a/test/test_string.py
+++ b/test/test_string.py
@@ -46,8 +46,7 @@ class TestYAML:
def
"""
- o = dedent(s).rstrip() + '\n'
- round_trip(s, outp=o, intermediate=dict(a='abc\ndef'))
+ round_trip(s, intermediate=dict(a='abc\ndef'))
def test_preserve_string_keep(self):
# with pytest.raises(AssertionError) as excinfo:
diff --git a/test/test_yaml.py b/test/test_yaml.py
index 7376d92..503e153 100644
--- a/test/test_yaml.py
+++ b/test/test_yaml.py
@@ -71,3 +71,37 @@ class TestYAML:
? b
? c
""")
+
+ def test_blank_line_after_comment(self):
+ round_trip("""
+ # Comment with spaces after it.
+
+
+ a: 1
+ """)
+
+ def test_blank_line_between_seq_items(self):
+ round_trip("""
+ # Seq with spaces in between items.
+ b:
+ - bar
+
+
+ - baz
+ """)
+
+ def test_blank_line_after_literal(self):
+ round_trip("""
+ c:
+ - |
+ This item
+ has a blank line
+ following it.
+
+ - |
+ To visually separate it from this item.
+
+ This item contains a blank line.
+
+
+ """)