Skip to content

Commit 2f2a843

Browse files
committed
Add iterable tests
1 parent de5b1c4 commit 2f2a843

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tests/snippets/iterable.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from testutils import assert_raises
2+
3+
def test_container(x):
4+
assert 3 in x
5+
assert 4 not in x
6+
assert list(x) == list(iter(x))
7+
assert list(x) == [0, 1, 2, 3]
8+
assert [*x] == [0, 1, 2, 3]
9+
lst = []
10+
lst.extend(x)
11+
assert lst == [0, 1, 2, 3]
12+
13+
class C:
14+
def __iter__(self):
15+
return iter([0, 1, 2, 3])
16+
test_container(C())
17+
18+
class C:
19+
def __getitem__(self, x):
20+
return (0, 1, 2, 3)[x] # raises IndexError on x==4
21+
test_container(C())
22+
23+
class C:
24+
def __getitem__(self, x):
25+
if x > 3:
26+
raise StopIteration
27+
return x
28+
test_container(C())
29+
30+
class C: pass
31+
assert_raises(TypeError, lambda: 5 in C())
32+
assert_raises(TypeError, lambda: iter(C))

0 commit comments

Comments
 (0)