2019-03-09 16:52:42 +00:00
|
|
|
|
|
|
|
import mock
|
|
|
|
import unittest2
|
|
|
|
|
|
|
|
import mitogen.core
|
|
|
|
|
|
|
|
import testlib
|
|
|
|
|
2019-07-24 14:14:08 +00:00
|
|
|
try:
|
|
|
|
next
|
|
|
|
except NameError:
|
|
|
|
def next(it):
|
|
|
|
return it.next()
|
|
|
|
|
2019-03-09 16:52:42 +00:00
|
|
|
|
|
|
|
class IterSplitTest(unittest2.TestCase):
|
|
|
|
func = staticmethod(mitogen.core.iter_split)
|
|
|
|
|
|
|
|
def test_empty_buffer(self):
|
|
|
|
lst = []
|
2019-03-12 01:08:11 +00:00
|
|
|
trailer, cont = self.func(buf='', delim='\n', func=lst.append)
|
|
|
|
self.assertTrue(cont)
|
2019-03-09 16:52:42 +00:00
|
|
|
self.assertEquals('', trailer)
|
|
|
|
self.assertEquals([], lst)
|
|
|
|
|
|
|
|
def test_empty_line(self):
|
|
|
|
lst = []
|
2019-03-12 01:08:11 +00:00
|
|
|
trailer, cont = self.func(buf='\n', delim='\n', func=lst.append)
|
|
|
|
self.assertTrue(cont)
|
2019-03-09 16:52:42 +00:00
|
|
|
self.assertEquals('', trailer)
|
|
|
|
self.assertEquals([''], lst)
|
|
|
|
|
|
|
|
def test_one_line(self):
|
|
|
|
buf = 'xxxx\n'
|
|
|
|
lst = []
|
2019-03-12 01:08:11 +00:00
|
|
|
trailer, cont = self.func(buf=buf, delim='\n', func=lst.append)
|
|
|
|
self.assertTrue(cont)
|
2019-03-09 16:52:42 +00:00
|
|
|
self.assertEquals('', trailer)
|
|
|
|
self.assertEquals(lst, ['xxxx'])
|
|
|
|
|
|
|
|
def test_one_incomplete(self):
|
|
|
|
buf = 'xxxx\nyy'
|
|
|
|
lst = []
|
2019-03-12 01:08:11 +00:00
|
|
|
trailer, cont = self.func(buf=buf, delim='\n', func=lst.append)
|
|
|
|
self.assertTrue(cont)
|
2019-03-09 16:52:42 +00:00
|
|
|
self.assertEquals('yy', trailer)
|
|
|
|
self.assertEquals(lst, ['xxxx'])
|
|
|
|
|
2019-03-12 01:08:11 +00:00
|
|
|
def test_returns_false_immediately(self):
|
|
|
|
buf = 'xxxx\nyy'
|
|
|
|
func = lambda buf: False
|
|
|
|
trailer, cont = self.func(buf=buf, delim='\n', func=func)
|
|
|
|
self.assertFalse(cont)
|
|
|
|
self.assertEquals('yy', trailer)
|
|
|
|
|
|
|
|
def test_returns_false_second_call(self):
|
|
|
|
buf = 'xxxx\nyy\nzz'
|
|
|
|
it = iter([True, False])
|
|
|
|
func = lambda buf: next(it)
|
|
|
|
trailer, cont = self.func(buf=buf, delim='\n', func=func)
|
|
|
|
self.assertFalse(cont)
|
|
|
|
self.assertEquals('zz', trailer)
|
|
|
|
|
2019-03-09 16:52:42 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest2.main()
|