async def test_zip(): async for va, vb in a.zip(asyncify(range(5)), range(5)): assert va == vb async for idx, vs in a.enumerate(a.zip(asyncify(range(5)), range(5))): assert vs[0] == vs[1] == idx async for _ in a.zip(): assert False
async def test_zip_strict(): async for va, vb in a.zip(asyncify(range(5)), range(5), strict=True): assert va == vb with pytest.raises(ValueError): async for _ in a.zip(asyncify(range(5)), range(6), strict=True): pass with pytest.raises(ValueError): async for _ in a.zip(asyncify(range(6)), range(5), strict=True): pass with pytest.raises(ValueError): async for _ in a.zip(*[range(5)] * 6, range(6), strict=True): pass
async def test_collect_all(count): activities = [ ping_pong(idx, delay=count - idx) for idx in range(count) ] async for winner, expected in a.zip( first(*activities, count=None), reversed(range(count))): assert winner == expected assert (time == count)
async def test_types(): assert await a.list(asyncify(range(5))) == list(range(5)) assert await a.list(asyncify(range(0))) == list(range(0)) assert await a.list() == list() assert await a.tuple(asyncify(range(5))) == tuple(range(5)) assert await a.tuple(asyncify(range(0))) == tuple(range(0)) assert await a.tuple() == tuple() assert await a.set(asyncify(range(5))) == set(range(5)) assert await a.set(asyncify(range(0))) == set(range(0)) assert await a.set() == set() assert await a.dict(a.zip((str(i) for i in range(5)), range(5))) == dict( zip((str(i) for i in range(5)), range(5)) ) assert await a.dict(a.zip((str(i) for i in range(0)), range(0))) == dict( zip((str(i) for i in range(0)), range(0)) ) assert await a.dict(b=3) == dict(b=3) assert await a.dict(a.zip((str(i) for i in range(5)), range(5)), b=3) == dict( zip((str(i) for i in range(5)), range(5)), b=3 ) assert await a.dict() == dict()
async def test_zip_close_immediately(): closed = False class SomeIterable: async def __aiter__(self): try: while True: yield 1 finally: nonlocal closed if await inside_loop(): closed = True zip_iter = a.zip(asyncify(range(-5, 0)), SomeIterable()) async for va, vb in zip_iter: assert va < 0 assert vb == 1 assert closed is True