Skip to content

Commit 4347b2c

Browse files
committed
extend multi-shot generator tests
1 parent f6cdf20 commit 4347b2c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

unpythonic/syntax/tests/test_conts_multishot.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,32 @@ def g():
562562
with testset("MultishotIterator: adapting @multishot to Python's generator API"):
563563
# basic use
564564
test[[x for x in MultishotIterator(g())] == [1, 2, 3]]
565-
# TODO: advanced example, exercise all features
565+
566+
# Re-using `g` from above:
567+
mig = MultishotIterator(g())
568+
test[next(mig) == 1]
569+
k = mig.k # stash the current continuation tracked by the `MultishotIterator`
570+
test[next(mig) == 2]
571+
test[next(mig) == 3]
572+
mig.k = k # multi-shot: rewind to the point we stashed
573+
test[next(mig) == 2]
574+
test[next(mig) == 3]
575+
576+
# Re-using `f` from above:
577+
mif = MultishotIterator(f())
578+
test[next(mif) is None]
579+
k = mif.k
580+
test[next(mif) == 42]
581+
test[next(mif) is None]
582+
test[mif.send(23) == 42]
583+
test_raises[StopIteration, mif.send(17)]
584+
mif.k = k # rewind
585+
test[next(mif) == 42]
586+
test[next(mif) is None]
587+
test[mif.send(23) == 42]
588+
test_raises[StopIteration, mif.send(17)]
589+
590+
# TODO: advanced examples, exercise all features
566591

567592
if __name__ == '__main__': # pragma: no cover
568593
with session(__file__):

0 commit comments

Comments
 (0)