From: Menno Smits Date: Sun, 31 Oct 2010 17:23:32 +0000 (+0000) Subject: More tests and better error reporting for juxtaposed tuple handling X-Git-Tag: 0.7~8^2~2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=5706f1a2de091c5dba8a0f73d262953023025971;p=imapclient.git More tests and better error reporting for juxtaposed tuple handling --- diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 00ed5fe..faef1c9 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -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) + diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index 3565fa1..951e227 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -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)