Consume leading whitespace in lists and dictionaries
authorDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 14 Apr 2017 16:16:51 +0000 (12:16 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 14 Apr 2017 16:16:51 +0000 (12:16 -0400)
don/string.py
test_don.py

index 46f5a83..0162d42 100644 (file)
@@ -168,6 +168,7 @@ def _make_utf_parser(encoding):
     return utf_parser
 
 def _make_consume_constant_parser(constant):
+    @_consume_leading_whitespace
     def consume_character_parser(s):
         if s.startswith(constant):
             return _shared.ParseResult(
index cc04df3..0e35e91 100644 (file)
@@ -308,6 +308,12 @@ class TestStringDeserialize(unittest.TestCase):
             string.deserialize("[1i8,2i8,3i8,4i8,5i8]"),
         )
 
+    def test_deserializes_list_with_leading_whitespace(self):
+        self.assertEqual(
+            [1,2,3,4,5],
+            string.deserialize(" \t\n[ \t\n1i8 \t\n, \t\n2i8 \t\n, \t\n3i8 \t\n, \t\n4i8 \t\n, \t\n5i8 \t\n]"),
+        )
+
     def test_deserializes_dictionary(self):
         self.assertEqual(
             collections.OrderedDict([
@@ -317,4 +323,13 @@ class TestStringDeserialize(unittest.TestCase):
             string.deserialize('{"foo"utf8:1i32,"bar"utf8:"baz"utf8}'),
         )
 
+    def test_deserializes_dictionary_with_leading_whitespace(self):
+        self.assertEqual(
+            collections.OrderedDict([
+                ('foo', 1),
+                ('bar', 'baz'),
+            ]),
+            string.deserialize(' \t\n{ \t\n"foo"utf8 \t\n: \t\n1i32 \t\n, \t\n"bar"utf8 \t\n: \t\n"baz"utf8 \t\n}'),
+        )
+
 unittest.main()