summaryrefslogtreecommitdiff
path: root/_test/test_comments.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2016-05-02 08:50:15 +0200
committerAnthon van der Neut <anthon@mnt.org>2016-05-02 08:50:15 +0200
commit4fa9ad23e69dcea31fe1b3dc15dc3cc1510b797c (patch)
treecef93d3c5c2c9e40f58360df87958e3d8b8175fa /_test/test_comments.py
parentad69246c90b178e99a752769ee4ac70dd4dedb26 (diff)
downloadruamel.yaml-4fa9ad23e69dcea31fe1b3dc15dc3cc1510b797c.tar.gz
allow insert in CommentedMap (Py3) and add comment0.11.11
The insert() would only be available for CommentedMap on Py2 (thanks to ruamel.ordereddict). The standard library lacks this functionality. The method was added in the compatibility layer, and on CommentedMap the optional comment parameter allows directly setting an end-of-line comment on the inserted key.
Diffstat (limited to '_test/test_comments.py')
-rw-r--r--_test/test_comments.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/_test/test_comments.py b/_test/test_comments.py
index ffe0b04..b6b12e2 100644
--- a/_test/test_comments.py
+++ b/_test/test_comments.py
@@ -423,3 +423,52 @@ class TestInsertPopList:
- 1
- 2
""")
+
+
+# inspired by demux' question on stackoverflow
+# http://stackoverflow.com/a/36970608/1307905
+class TestInsertInMapping:
+ @property
+ def ins(self):
+ return """\
+ first_name: Art
+ occupation: Architect # This is an occupation comment
+ about: Art Vandelay is a fictional character that George invents...
+ """
+
+ def test_insert_at_pos_1(self):
+ d = round_trip_load(self.ins)
+ d.insert(1, 'last name', 'Vandelay', comment="new key")
+ y = round_trip_dump(d)
+ print(y)
+ assert y == dedent("""\
+ first_name: Art
+ last name: Vandelay # new key
+ occupation: Architect # This is an occupation comment
+ about: Art Vandelay is a fictional character that George invents...
+ """)
+
+ def test_insert_at_pos_0(self):
+ d = round_trip_load(self.ins)
+ d.insert(0, 'last name', 'Vandelay', comment="new key")
+ y = round_trip_dump(d)
+ print(y)
+ assert y == dedent("""\
+ last name: Vandelay # new key
+ first_name: Art
+ occupation: Architect # This is an occupation comment
+ about: Art Vandelay is a fictional character that George invents...
+ """)
+
+ def test_insert_at_pos_3(self):
+ # much more simple if done with appending.
+ d = round_trip_load(self.ins)
+ d.insert(3, 'last name', 'Vandelay', comment="new key")
+ y = round_trip_dump(d)
+ print(y)
+ assert y == dedent("""\
+ first_name: Art
+ occupation: Architect # This is an occupation comment
+ about: Art Vandelay is a fictional character that George invents...
+ last name: Vandelay # new key
+ """)