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 from lepl.support.lib import fmt
32 from lepl._test.base import BaseTest
33 from lepl.stream.core import s_empty, s_fmt, s_line, s_next, s_stream
34 from lepl.stream.factory import DEFAULT_STREAM_FACTORY
35
36
38
40 f = DEFAULT_STREAM_FACTORY
41 for (constructor, data) in ((f.from_sequence, ''),
42 (f.from_sequence, []),
43 (f.from_sequence, ()),
44 (f.from_string, ''),
45 (f.from_list, [])):
46 s = constructor(data)
47 assert s_empty(s)
48 try:
49 s_next(s)
50 assert False, fmt('expected error: {0}', s)
51 except StopIteration:
52 pass
53 try:
54 s_line(s, False)
55 assert False, fmt('expected error: {0}', s)
56 except StopIteration:
57 pass
58
60 f = DEFAULT_STREAM_FACTORY
61 for (constructor, data) in ((f.from_sequence, 'a'),
62 (f.from_sequence, [1]),
63 (f.from_sequence, (2,)),
64 (f.from_string, 'b'),
65 (f.from_list, ['c'])):
66 s = constructor(data)
67 assert not s_empty(s)
68 (value, n) = s_next(s)
69 assert value == data
70 assert s_empty(n)
71 (line, n) = s_line(s, False)
72 assert line == data
73 assert s_empty(n)
74
76 f = DEFAULT_STREAM_FACTORY
77 for (constructor, data) in ((f.from_sequence, 'ab'),
78 (f.from_sequence, [1, 2]),
79 (f.from_sequence, (2,3)),
80 (f.from_string, 'bc'),
81 (f.from_list, ['c', 6])):
82 s = constructor(data)
83 assert not s_empty(s)
84 (value, n) = s_next(s)
85 assert value == data[0:1]
86 (value, n) = s_next(n)
87 assert value == data[1:2]
88 assert s_empty(n)
89 (line, n) = s_line(s, False)
90 assert line == data
91 assert s_empty(n)
92
94 f = DEFAULT_STREAM_FACTORY
95 s = f.from_string('line 1\nline 2\nline 3\n')
96 (l, s) = s_line(s, False)
97 assert l == 'line 1\n', l
98 (l, _) = s_line(s, False)
99 assert l == 'line 2\n', repr(l)
100 locn = s_fmt(s, '{location}')
101 assert locn == 'line 2, character 1', locn
102 sl = s_stream(s, l)
103 (_, sl) = s_next(sl, count=2)
104 locn = s_fmt(sl, '{location}')
105 assert locn == 'line 2, character 3', locn
106