forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol_iterable.py
More file actions
50 lines (31 loc) · 807 Bytes
/
protocol_iterable.py
File metadata and controls
50 lines (31 loc) · 807 Bytes
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
from testutils import assert_raises
def test_container(x):
assert 3 in x
assert 4 not in x
assert list(x) == list(iter(x))
assert list(x) == [0, 1, 2, 3]
assert [*x] == [0, 1, 2, 3]
lst = []
lst.extend(x)
assert lst == [0, 1, 2, 3]
class C:
def __iter__(self):
return iter([0, 1, 2, 3])
test_container(C())
class C:
def __getitem__(self, x):
return (0, 1, 2, 3)[x] # raises IndexError on x==4
test_container(C())
class C:
def __getitem__(self, x):
if x > 3:
raise StopIteration
return x
test_container(C())
class C:
pass
assert_raises(TypeError, lambda: 5 in C())
assert_raises(TypeError, iter, C)
it = iter([1, 2, 3, 4, 5])
call_it = iter(lambda: next(it), 4)
assert list(call_it) == [1, 2, 3]