From f41f182526270a4efe91cc62d3c31c977a57460b Mon Sep 17 00:00:00 2001 From: Tom Paton Date: Wed, 30 Dec 2020 22:27:10 +1100 Subject: [PATCH 1/2] Add (failing) tests for issue #32 and #33 --- _test/test_comment_manipulation.py | 85 ++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/_test/test_comment_manipulation.py b/_test/test_comment_manipulation.py index fe91273b..af0b5e39 100644 --- a/_test/test_comment_manipulation.py +++ b/_test/test_comment_manipulation.py @@ -636,3 +636,88 @@ def test_map_set_comment_before_and_after_non_first_key_02(self): test3: 3 """ compare(data, exp) + + # issue 32 + @pytest.mark.xfail(reason="open issue", raises=TypeError) + def test_yaml_add_eol_comment_issue_32(self): + data = load( + """ + items: + - one: 1 + uno: '1' + - # item 2 + two: 2 + duo: '2' + - three: 3 + """ + ) + + data['items'].yaml_add_eol_comment('second pass', key=1) + + exp = """ + items: + - one: 1 + uno: '1' + - # second pass + two: 2 + duo: '2' + - three: 3 + """ + + compare(data, exp) + + def test_yaml_add_eol_comment_issue_32_ok(self): + data = load( + """ + items: + - one + - two # item 2 + - three + """ + ) + + data['items'].yaml_add_eol_comment('second pass', key=1) + + exp = """ + items: + - one + - two # second pass + - three + """ + + compare(data, exp) + + # issue 33 + @pytest.mark.xfail(reason="open issue", raises=AttributeError) + def test_yaml_set_start_comment_issue_33(self): + data = load( + """ + items: + # item 1 + - one: 1 + uno: '1' + # item 2 + - two: 2 + duo: '2' + # item 3 + - three: 3 + """ + ) + + data['items'][0].yaml_set_start_comment('uno') + data['items'][1].yaml_set_start_comment('duo') + data['items'][2].yaml_set_start_comment('tre') + + exp = """ + items: + # uno + - one: 1 + uno: '1' + # duo + - two: 2 + duo: '2' + # tre + - three: 3 + """ + + compare(data, exp) From 33e86bb2fcee3d80bc57d169130c75b050c2997f Mon Sep 17 00:00:00 2001 From: Tom Paton Date: Sat, 2 Jan 2021 20:56:04 +1100 Subject: [PATCH 2/2] fix #32 --- _test/test_comment_manipulation.py | 1 - lib/ruyaml/comments.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/_test/test_comment_manipulation.py b/_test/test_comment_manipulation.py index af0b5e39..2fa8cc10 100644 --- a/_test/test_comment_manipulation.py +++ b/_test/test_comment_manipulation.py @@ -638,7 +638,6 @@ def test_map_set_comment_before_and_after_non_first_key_02(self): compare(data, exp) # issue 32 - @pytest.mark.xfail(reason="open issue", raises=TypeError) def test_yaml_add_eol_comment_issue_32(self): data = load( """ diff --git a/lib/ruyaml/comments.py b/lib/ruyaml/comments.py index ab6a1b85..d3c3f37a 100644 --- a/lib/ruyaml/comments.py +++ b/lib/ruyaml/comments.py @@ -426,7 +426,7 @@ def __eq__(self, other): def _yaml_add_comment(self, comment, key=NoComment): # type: (Any, Optional[Any]) -> None if key is not NoComment: - self.yaml_key_comment_extend(key, comment) + self.yaml_key_comment_extend(key, comment, clear=True) else: self.ca.comment = comment