-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.py
199 lines (144 loc) · 3.56 KB
/
tests.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""Unit tests for ``yield From()``."""
import pytest
from yieldfrom import yieldfrom, From, Return
def test_basic_operation():
"""Test basic ``yield From()`` operation."""
def subgen():
yield 2
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
assert list(gen()) == [1, 2, 3, 4]
def test_return_value():
"""Test subgenerator return value."""
def subgen():
yield 2
yield 3
Return(100)
@yieldfrom
def gen():
yield 1
ret = (yield From(subgen()))
yield 4
yield ret
assert list(gen()) == [1, 2, 3, 4, 100]
def test_nesting():
"""Test subgenerator nesting."""
def subsubgen():
yield 2
@yieldfrom
def subgen():
yield From(subsubgen())
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
assert list(gen()) == [1, 2, 3, 4]
def test_throwing():
"""Test throwing exceptions into subgenerator and its handling."""
def subsubgen():
try:
yield 2
except ValueError:
yield 200
@yieldfrom
def subgen():
yield From(subsubgen())
yield 3
@yieldfrom
def gen():
yield 1
yield From(subgen())
yield 4
ret = []
try:
gen_obj = gen()
while True:
i = next(gen_obj)
if i == 2:
i = gen_obj.throw(ValueError())
ret.append(i)
except StopIteration:
pass
assert ret == [1, 200, 3, 4]
def test_iterable():
"""Test regular iterable in ``yield From``."""
@yieldfrom
def gen():
yield From([1, 2 ,3])
yield [4, 5, 6]
assert list(gen()) == [1, 2, 3, [4, 5, 6]]
def test_non_iterable():
"""Test non iterable passed to ``yield From``."""
@yieldfrom
def gen():
yield 1
yield From(ValueError)
yield From(None)
yield 2
assert list(gen()) == [1, 2]
def test_throwing_handling():
"""Test throwing exceptions into subgenerator and handling in main."""
def subgen():
yield 1
yield 2
@yieldfrom
def gen():
try:
yield From(subgen())
except ValueError:
yield 3
ret = []
try:
gen_obj = gen()
while True:
i = next(gen_obj)
if i == 1:
i = gen_obj.throw(ValueError())
ret.append(i)
except StopIteration:
pass
assert ret == [3]
def test_exception_flow():
"""Test raising and catching exceptions between generators."""
def subgen():
yield 1
raise ValueError()
yield 100
@yieldfrom
def gen():
try:
yield From(subgen())
except ValueError:
yield 2
yield 3
assert list(gen()) == [1, 2, 3]
def test_exception_before_first_yield():
"""Test an exception raised before the first `yield`."""
def raise_value_error():
raise ValueError()
def subgen():
yield raise_value_error()
@yieldfrom
def gen():
try:
yield From(subgen())
except ValueError:
yield 1
assert list(gen()) == [1]
def test_non_generator_wrapping():
"""Test wrapping a non-generator function with ``yieldfrom``."""
@yieldfrom
def non_gen():
return 42
@yieldfrom
def gen():
yield From(non_gen())
with pytest.raises(TypeError) as excinfo:
list(gen())
assert 'int' in str(excinfo.value)