Skip to content

Commit d0e6d2f

Browse files
committed
add tests for custom iters
1 parent 4e277cf commit d0e6d2f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/snippets/list.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,55 @@ def must_assign_iter_to_slice():
399399
x[::2] = 42
400400

401401
assert_raises(TypeError, must_assign_iter_to_slice)
402+
403+
# other iterables?
404+
a = list(range(10))
405+
406+
# string
407+
x = a[:]
408+
x[3:8] = "abcdefghi"
409+
assert x == [0, 1, 2, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 8, 9]
410+
411+
# tuple
412+
x = a[:]
413+
x[3:8] = (11, 12, 13, 14, 15)
414+
assert x == [0, 1, 2, 11, 12, 13, 14, 15, 8, 9]
415+
416+
# class
417+
class CIterNext:
418+
def __init__(self, sec=(1, 2, 3)):
419+
self.sec = sec
420+
self.index = 0
421+
def __iter__(self):
422+
return self
423+
def __next__(self):
424+
if self.index >= len(self.sec):
425+
raise StopIteration
426+
v = self.sec[self.index]
427+
self.index += 1
428+
return v
429+
430+
x = list(range(10))
431+
x[3:8] = CIterNext()
432+
assert x == [0, 1, 2, 1, 2, 3, 8, 9]
433+
434+
class CIter:
435+
def __init__(self, sec=(1, 2, 3)):
436+
self.sec = sec
437+
def __iter__(self):
438+
for n in self.sec:
439+
yield n
440+
441+
x = list(range(10))
442+
x[3:8] = CIter()
443+
assert x == [0, 1, 2, 1, 2, 3, 8, 9]
444+
445+
class CGetItem:
446+
def __init__(self, sec=(1, 2, 3)):
447+
self.sec = sec
448+
def __getitem__(self, sub):
449+
return self.sec[sub]
450+
451+
x = list(range(10))
452+
x[3:8] = CGetItem()
453+
assert x == [0, 1, 2, 1, 2, 3, 8, 9]

0 commit comments

Comments
 (0)