]> khamidou — GitHub mirrors - imapclient.git/commitdiff
More tests and better error reporting for juxtaposed tuple handling
authorMenno Smits <menno@freshfoo.com>
Sun, 31 Oct 2010 17:23:32 +0000 (17:23 +0000)
committerMenno Smits <menno@freshfoo.com>
Sun, 31 Oct 2010 17:23:32 +0000 (17:23 +0000)
imapclient/response_parser.py
imapclient/test/test_response_parser.py

index 00ed5fe0fe055a03553f5f20abf1f2e45d15928f..faef1c91850a6437d8104713505ecbd0759a3d6b 100644 (file)
@@ -149,9 +149,8 @@ def parse_tuple(src):
             return parse_juxtaposed_tuples(src, out)
         else:
             out.append(atom(src, token))
-    # oops - no terminator!
-    preceeding = ' '.join(str(val) for val in out)
-    raise ParseError('Tuple incomplete before "(%s"' % preceeding)
+    # no terminator
+    raise ParseError('Tuple incomplete before "(%s"' % _fmt_tuple(out))
 
 
 def parse_juxtaposed_tuples(src, init):
@@ -165,9 +164,14 @@ def parse_juxtaposed_tuples(src, init):
             current = []
         else:
             current.append(atom(src, token))
-    # oops - no terminator!
-    preceeding_items = out
+
+    # no terminator
+    preceeding = ''.join('(' + _fmt_tuple(t) + ')' for t in out)
     if current:
-        preceeding_items.append(tuple(current))
-    preceeding = ''.join(str(val) for val in preceeding_items)
-    raise ParseError('Juxtaposed tuples incomplete before "(%s"' % preceeding)
+        preceeding += '(' + _fmt_tuple(current)
+    raise ParseError('Juxtaposed tuples incomplete before "%s"' % preceeding)
+
+
+def _fmt_tuple(t):
+    return ' '.join(str(item) for item in t)
+
index 3565fa1fe0c3805518489b963db17f8577787cb6..951e22700a743a421f26f5dcab262b4173c93f1b 100644 (file)
@@ -67,12 +67,18 @@ class TestParseResponse(unittest.TestCase):
     def test_juxtatposed_tuples(self):
         # Tuples with no whitespace between them should be returned as
         # a list of tuples
+        self._test('(12 "foo")(34 NIL 0)',
+                   [(12, "foo"), (34, None, 0)])
+        self._test('(12 "foo")(34 NIL 0)(5 66)',
+                   [(12, "foo"), (34, None, 0), (5, 66)])
         self._test('((12 "foo")(34 NIL 0) "foo")',
                    ([(12, "foo"), (34, None, 0)], "foo"))
         self._test('((12 "foo")(34 NIL 0)(4 55 6) "foo")',
                    ([(12, "foo"), (34, None, 0), (4, 55, 6)], "foo"))
         self._test('((12 "foo")(34 (NIL 1 2) 0)(4 55 6) "foo")',
                    ([(12, "foo"), (34, (None, 1, 2), 0), (4, 55, 6)], "foo"))
+        self._test('(12 "foo")(34 NIL ("a" "b")("c" "d"))',
+                   [(12, "foo"), (34, None, [('a', 'b'), ('c', 'd')])])
 
     def test_complex_mixed(self):
         self._test('((FOO "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23) '
@@ -156,6 +162,9 @@ class TestParseResponse(unittest.TestCase):
     def test_incomplete_tuple(self):
         self._test_parse_error('abc (1 2', 'Tuple incomplete before "(1 2"')
 
+    def test_incomplete_juxtaposed_tuples(self):
+        self._test_parse_error('(1 2)(3 4',
+                               'Juxtaposed tuples incomplete before "(1 2)(3 4"')
 
     def test_bad_literal(self):
         self._test_parse_error([('{99}', 'abc')],
@@ -177,12 +186,15 @@ class TestParseResponse(unittest.TestCase):
                      format_error(to_parse, output, expected))
 
     def _test_parse_error(self, to_parse, expected_msg):
+        if not isinstance(to_parse, list):
+            to_parse = [to_parse]
         try:
             parse_response(to_parse)
             self.fail("didn't raise an exception")
         except ParseError, err:
-            self.assert_(expected_msg == str(err)[:len(expected_msg)],
-                         'got ParseError with wrong msg: %r' % str(err))
+            actual_msg = str(err)
+            self.assert_(expected_msg == actual_msg[:len(expected_msg)],
+                         'got ParseError with wrong msg: %r' % actual_msg)