add .clear() to StatefulDecoder too (and tests for .clear() on both statefuls)

This commit is contained in:
jesopo 2020-03-11 17:09:54 +00:00
parent 4b9ff45921
commit 53d056c3e0
3 changed files with 20 additions and 0 deletions

View File

@ -4,8 +4,14 @@ from .protocol import Line, tokenise
class StatefulDecoder(object):
def __init__(self, fallback: str="iso-8859"):
self._fallback = fallback
self.clear()
def clear(self):
self._buffer = b""
def pending(self) -> bytes:
return self._buffer
def push(self, data: bytes) -> typing.Optional[typing.List[Line]]:
if not data:
return None

View File

@ -43,3 +43,10 @@ class TestEmpty(unittest.TestCase):
d.push(b"PRIVMSG #channel hello")
lines = d.push(b"")
self.assertIsNone(lines)
class TestClear(unittest.TestCase):
def test(self):
d = irctokens.StatefulDecoder()
d.push(b"PRIVMSG ")
d.clear()
self.assertEqual(d.pending(), b"")

View File

@ -31,3 +31,10 @@ class TestPop(unittest.TestCase):
e.push(line)
lines = e.pop(1)
self.assertEqual(len(lines), 0)
class TestClear(unittest.TetsCase):
def test(self):
e = irctokens.StatefulEncoder()
e.push(irctokens.tokenise("PRIVMSG #channel hello")
e.clear()
self.assertEqual(e.pending(), b"")