changeset 103:adf3cd83bf7a

Support a common stylistic variation
author Stefano Rivera <stefano@rivera.za.net>
date Mon, 02 Sep 2013 13:05:25 +0200
parents fd9c4db5ddfe
children 1be3eebb87c4
files nagslang/tests/test_yamlish.py nagslang/yamlish.py
diffstat 2 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/nagslang/tests/test_yamlish.py	Mon Sep 02 11:56:08 2013 +0200
+++ b/nagslang/tests/test_yamlish.py	Mon Sep 02 13:05:25 2013 +0200
@@ -9,6 +9,26 @@
 from nagslang.yamlish import load, dump
 
 
+class TestParse(TestCase):
+    def assertParsesAs(self, yaml, expected):
+        f = StringIO(yaml.strip())
+        self.assertEqual(load(f), expected)
+
+    def test_dict_list_1(self):
+        self.assertParsesAs('''
+foo:
+- bar
+- baz
+        ''', {'foo': ['bar', 'baz']})
+
+    def test_dict_list_2(self):
+        self.assertParsesAs('''
+foo:
+  - bar
+  - baz
+        ''', {'foo': ['bar', 'baz']})
+
+
 class TestRoundTrip(TestCase):
     def roundtrip(self, data):
         f = StringIO()
--- a/nagslang/yamlish.py	Mon Sep 02 11:56:08 2013 +0200
+++ b/nagslang/yamlish.py	Mon Sep 02 13:05:25 2013 +0200
@@ -63,10 +63,13 @@
                 stack.pop()
 
         if len(spaces) > indent():
-            # Nested dict
-            assert dm
             assert parent_key
-            stack.append((len(spaces), {}))
+            if dm:
+                # Nested dict
+                stack.append((len(spaces), {}))
+            elif lm:
+                # Over-indented list in a dict
+                stack.append((len(spaces), []))
             stack[-2][1][parent_key] = obj()
             parent_key = None