File: iter_split_test.py

package info (click to toggle)
python-mitogen 0.3.3-9%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,816 kB
  • sloc: python: 22,086; sh: 171; makefile: 74; perl: 19; ansic: 18; javascript: 5
file content (58 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import unittest

import mitogen.core

try:
    next
except NameError:
    def next(it):
        return it.next()


class IterSplitTest(unittest.TestCase):
    func = staticmethod(mitogen.core.iter_split)

    def test_empty_buffer(self):
        lst = []
        trailer, cont = self.func(buf='', delim='\n', func=lst.append)
        self.assertTrue(cont)
        self.assertEqual('', trailer)
        self.assertEqual([], lst)

    def test_empty_line(self):
        lst = []
        trailer, cont = self.func(buf='\n', delim='\n', func=lst.append)
        self.assertTrue(cont)
        self.assertEqual('', trailer)
        self.assertEqual([''], lst)

    def test_one_line(self):
        buf = 'xxxx\n'
        lst = []
        trailer, cont = self.func(buf=buf, delim='\n', func=lst.append)
        self.assertTrue(cont)
        self.assertEqual('', trailer)
        self.assertEqual(lst, ['xxxx'])

    def test_one_incomplete(self):
        buf = 'xxxx\nyy'
        lst = []
        trailer, cont = self.func(buf=buf, delim='\n', func=lst.append)
        self.assertTrue(cont)
        self.assertEqual('yy', trailer)
        self.assertEqual(lst, ['xxxx'])

    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.assertEqual('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.assertEqual('zz', trailer)