diff options
author | Anthon van der Neut <anthon@mnt.org> | 2017-05-01 08:46:37 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2017-05-01 08:46:37 +0200 |
commit | 7b4fe4c97b9f51d2c85b54b9d2ef62363bf7b273 (patch) | |
tree | f1a3cfc3c94d72c9d74047baa8e49164043bd597 | |
parent | 5c33f8dbfed0e918ae503392723d449ac5cbe7e1 (diff) | |
download | ruamel.yaml-7b4fe4c97b9f51d2c85b54b9d2ef62363bf7b273.tar.gz |
fixes issue #103
-rw-r--r-- | README.rst | 4 | ||||
-rw-r--r-- | __init__.py | 4 | ||||
-rw-r--r-- | _test/roundtrip.py | 31 | ||||
-rw-r--r-- | _test/test_documents.py | 69 | ||||
-rw-r--r-- | _test/test_tag.py | 47 | ||||
-rw-r--r-- | parser.py | 10 |
6 files changed, 157 insertions, 8 deletions
@@ -18,6 +18,10 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key +NEXT: + - fix for issue 103 allowing implicit documents after document end marker line (``...``) + in YAML 1.2 + 0.14.10 (2017-04-26): - fix problem with emitting using cyaml diff --git a/__init__.py b/__init__.py index 9073226..b3af799 100644 --- a/__init__.py +++ b/__init__.py @@ -11,8 +11,8 @@ if False: # MYPY _package_data = dict( full_package_name='ruamel.yaml', - version_info=(0, 14, 10), - __version__='0.14.10', + version_info=(0, 14, 11, 'dev'), + __version__='0.14.11.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/roundtrip.py b/_test/roundtrip.py index cd79f2c..cea1d81 100644 --- a/_test/roundtrip.py +++ b/_test/roundtrip.py @@ -22,9 +22,24 @@ def dedent(data): return textwrap.dedent(data) -def round_trip_load(inp, preserve_quotes=None): +def round_trip_load(inp, preserve_quotes=None, version=None): dinp = dedent(inp) - return ruamel.yaml.load(dinp, ruamel.yaml.RoundTripLoader, preserve_quotes=preserve_quotes) + return ruamel.yaml.load( + dinp, + Loader=ruamel.yaml.RoundTripLoader, + preserve_quotes=preserve_quotes, + version=version, + ) + + +def round_trip_load_all(inp, preserve_quotes=None, version=None): + dinp = dedent(inp) + return ruamel.yaml.load_all( + dinp, + Loader=ruamel.yaml.RoundTripLoader, + preserve_quotes=preserve_quotes, + version=version, + ) def round_trip_dump(data, indent=None, block_seq_indent=None, top_level_colon_align=None, @@ -40,7 +55,9 @@ def round_trip_dump(data, indent=None, block_seq_indent=None, top_level_colon_al def round_trip(inp, outp=None, extra=None, intermediate=None, indent=None, block_seq_indent=None, top_level_colon_align=None, prefix_colon=None, - preserve_quotes=None, explicit_start=None, version=None): + preserve_quotes=None, + explicit_start=None, explicit_end=None, + version=None): """ inp: input string to parse outp: expected output (equals input if not specified) @@ -59,13 +76,17 @@ def round_trip(inp, outp=None, extra=None, intermediate=None, indent=None, raise ValueError res = round_trip_dump(data, indent=indent, block_seq_indent=block_seq_indent, top_level_colon_align=top_level_colon_align, - prefix_colon=prefix_colon, explicit_start=explicit_start, + prefix_colon=prefix_colon, + explicit_start=explicit_start, + explicit_end=explicit_end, version=version) print('roundtrip data:\n', res, sep='') assert res == doutp res = round_trip_dump(data, indent=indent, block_seq_indent=block_seq_indent, top_level_colon_align=top_level_colon_align, - prefix_colon=prefix_colon, explicit_start=explicit_start, + prefix_colon=prefix_colon, + explicit_start=explicit_start, + explicit_end=explicit_end, version=version) print('roundtrip second round data:\n', res, sep='') assert res == doutp diff --git a/_test/test_documents.py b/_test/test_documents.py new file mode 100644 index 0000000..33fe84f --- /dev/null +++ b/_test/test_documents.py @@ -0,0 +1,69 @@ +# coding: utf-8 + +import pytest # NOQA + +from ruamel import yaml +from roundtrip import round_trip, round_trip_load_all + + +class TestDocument: + def test_single_doc_begin_end(self): + round_trip("""\ + --- + - a + - b + ... + """, explicit_start=True, explicit_end=True) + + def test_multi_doc_begin_end(self): + docs = list(round_trip_load_all("""\ + --- + - a + ... + --- + - b + ... + """)) + assert docs == [['a'], ['b']] + out = yaml.dump_all(docs, Dumper=yaml.RoundTripDumper, explicit_start=True, + explicit_end=True) + assert out == "---\n- a\n...\n---\n- b\n...\n" + + def test_multi_doc_no_start(self): + docs = list(round_trip_load_all("""\ + - a + ... + --- + - b + ... + """)) + assert docs == [['a'], ['b']] + + def test_multi_doc_no_end(self): + docs = list(round_trip_load_all("""\ + - a + --- + - b + """)) + assert docs == [['a'], ['b']] + + def test_multi_doc_ends_only(self): + # this is ok in 1.2 + docs = list(round_trip_load_all("""\ + - a + ... + - b + ... + """, version=(1,2))) + assert docs == [['a'], ['b']] + + def test_multi_doc_ends_only_1_1(self): + # this is not ok in 1.1 + with pytest.raises(yaml.parser.ParserError): + docs = list(round_trip_load_all("""\ + - a + ... + - b + ... + """, version=(1,1))) + assert False diff --git a/_test/test_tag.py b/_test/test_tag.py index 872f96a..58249ae 100644 --- a/_test/test_tag.py +++ b/_test/test_tag.py @@ -2,9 +2,25 @@ import pytest # NOQA +from ruamel import yaml from roundtrip import round_trip +class XXX(yaml.comments.CommentedMap): + @staticmethod + def yaml_dump(dumper, data): + return dumper.represent_mapping(u'!xxx', data) + + @classmethod + def yaml_load(cls, constructor, node): + data = cls() + yield data + constructor.construct_mapping(node, data) + +yaml.add_constructor(u'!xxx', XXX.yaml_load, constructor=yaml.RoundTripConstructor) +yaml.add_representer(XXX, XXX.yaml_dump, representer=yaml.RoundTripRepresenter) + + class TestIndentFailures: def test_tag(self): @@ -30,3 +46,34 @@ class TestIndentFailures: location: Germany language: python """) + + +class TestRoundTripCustom: + def test_X(self): + round_trip("""\ + !xxx + name: Anthon + location: Germany + language: python + """) + + @pytest.mark.xfail(strict=True) + def test_X_pre_tag_comment(self): + round_trip("""\ + - + # hello + !xxx + name: Anthon + location: Germany + language: python + """) + + @pytest.mark.xfail(strict=True) + def test_X_post_tag_comment(self): + round_trip("""\ + - !xxx + # hello + name: Anthon + location: Germany + language: python + """) @@ -118,6 +118,11 @@ class Parser(object): # type: () -> Any return self.loader._scanner + @property + def resolver(self): + # type: () -> Any + return self.loader._resolver + def dispose(self): # type: () -> None # Reset the state attributes (to clear self-references) @@ -239,7 +244,10 @@ class Parser(object): event = DocumentEndEvent(start_mark, end_mark, explicit=explicit) # Prepare the next state. - self.state = self.parse_document_start + if self.resolver.processing_version == (1, 1): + self.state = self.parse_document_start + else: + self.state = self.parse_implicit_document_start return event |