def test_parse_prepared(self): prepared1 = MagicMock(PreparedMessage) prepared2 = MagicMock(PreparedMessage) state = ParsingState( continuations=[v(b'test test! {14}\n'), v(b'second message\n ')], prepared_messages=[]) with self.assertRaises(ParsingInterrupt) as raised: AppendCommand.parse(b' inbox {10}\n', Params(state)) expected = raised.exception.expected self.assertIsInstance(expected, ExpectPreparedMessage) self.assertEqual('INBOX', expected.mailbox) self.assertEqual(b'test test!', expected.message.literal) self.assertFalse(expected.message.flag_set) state = ParsingState( continuations=[v(b'test test! {14}\n'), v(b'second message\n ')], prepared_messages=[prepared1]) with self.assertRaises(ParsingInterrupt) as raised: AppendCommand.parse(b' inbox {10}\n', Params(state)) expected = raised.exception.expected self.assertIsInstance(expected, ExpectPreparedMessage) self.assertEqual('INBOX', expected.mailbox) self.assertEqual(b'second message', expected.message.literal) self.assertFalse(expected.message.flag_set) state = ParsingState( continuations=[v(b'test test! {14}\n'), v(b'second message\n ')], prepared_messages=[prepared1, prepared2]) ret, buf = AppendCommand.parse(b' inbox {10}\n', Params(state)) self.assertIsInstance(ret, AppendCommand) self.assertEqual('INBOX', ret.mailbox) self.assertEqual([prepared1, prepared2], ret.messages) self.assertEqual(b' ', buf)
def test_literal_parse(self): state = ParsingState(continuations=[v(b'test\x01abc')]) ret, buf = String.parse(b'{5}\r\n', Params(state)) self.assertIsInstance(ret, LiteralString) self.assertEqual(b'test\x01', ret.value) self.assertFalse(ret.binary) self.assertEqual(b'abc', buf)
def test_literal_binary(self): state = ParsingState(continuations=[v(b'\x00\x01\02abc')]) ret, buf = String.parse(b'~{3}\r\n', Params(state)) self.assertIsInstance(ret, LiteralString) self.assertEqual(b'\x00\x01\x02', ret.value) self.assertTrue(ret.binary) self.assertEqual(b'abc', buf)
def test_parse(self): state = ParsingState(continuations=[v(b'test test!\n ')]) with self.assertRaises(ParsingInterrupt) as raised: AppendCommand.parse( b' inbox (\\Seen) "01-Jan-1970 01:01:00 +0000" {10}\n', Params(state)) expected = raised.exception.expected self.assertIsInstance(expected, ExpectPreparedMessage) self.assertEqual('INBOX', expected.mailbox) self.assertEqual(b'test test!', expected.message.literal) self.assertEqual({self._seen}, expected.message.flag_set) self.assertEqual(self._epoch, expected.message.when)
async def read_command(self, state: ConnectionState) -> Command: line = await self.readline() conts: List[memoryview] = [] preps: List[PreparedMessage] = [] while True: parsing_state = ParsingState(continuations=conts, prepared_messages=preps) params = self.params.copy(parsing_state) try: cmd, _ = self.commands.parse(line, params) except ParsingInterrupt as interrupt: await self._interrupt(state, interrupt, conts, preps) else: return cmd
def test_literal_parse_failure(self): with self.assertRaises(NotParseable): String.parse(b'{}\r\n', Params()) with self.assertRaises(NotParseable): String.parse(b'{10}', Params()) with self.assertRaises(NotParseable): String.parse(b'{10}\r\nabc', Params()) with self.assertRaises(NotParseable): state = ParsingState(continuations=[v(b'a' * 9)]) String.parse(b'{10}\r\n', Params(state)) with self.assertRaises(NotParseable): String.parse(b'{10+}\r\n' + (b'a' * 9), Params()) with self.assertRaises(ParsingInterrupt) as raised1: String.parse(b'{10}\r\n', Params()) self.assertIsInstance(raised1.exception.expected, ExpectContinuation) with self.assertRaises(NotParseable) as raised2: String.parse(b'{4097}\r\n', Params()) self.assertEqual(b'[TOOBIG]', bytes(raised2.exception.code))
def test_parse_literal(self): state = ParsingState(continuations=[memoryview(b'abcd ')]) ret, buf = AString.parse(b' {4}\r\n', Params(state)) self.assertIsInstance(ret, AString) self.assertEqual(b'abcd', ret.value) self.assertEqual(b' ', buf)
def test_literal_parse_empty(self): state = ParsingState(continuations=[v(b'abc')]) ret, buf = String.parse(b'{0}\r\n', Params(state)) self.assertIsInstance(ret, LiteralString) self.assertEqual(b'', ret.value) self.assertEqual(b'abc', buf)