Skip to content

Commit c33df19

Browse files
committed
tests: Add 6 tests for async await/for/with.
1 parent eacbd7a commit c33df19

6 files changed

Lines changed: 175 additions & 0 deletions

File tree

tests/basics/async_await.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# test basic await expression
2+
# adapted from PEP0492
3+
4+
async def abinary(n):
5+
print(n)
6+
if n <= 0:
7+
return 1
8+
l = await abinary(n - 1)
9+
r = await abinary(n - 1)
10+
return l + 1 + r
11+
12+
o = abinary(4)
13+
try:
14+
while True:
15+
o.send(None)
16+
except StopIteration:
17+
print('finished')

tests/basics/async_await2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# test await expression
2+
3+
import sys
4+
if sys.implementation.name == 'micropython':
5+
# uPy allows normal generators to be awaitables
6+
coroutine = lambda f: f
7+
else:
8+
import types
9+
coroutine = types.coroutine
10+
11+
@coroutine
12+
def wait(value):
13+
print('wait value:', value)
14+
msg = yield 'message from wait(%u)' % value
15+
print('wait got back:', msg)
16+
return 10
17+
18+
async def f():
19+
x = await wait(1)**2
20+
print('x =', x)
21+
22+
coro = f()
23+
print('return from send:', coro.send(None))
24+
try:
25+
coro.send('message from main')
26+
except StopIteration:
27+
print('got StopIteration')

tests/basics/async_for.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# test basic async for execution
2+
# example taken from PEP0492
3+
4+
class AsyncIteratorWrapper:
5+
def __init__(self, obj):
6+
print('init')
7+
self._it = iter(obj)
8+
9+
async def __aiter__(self):
10+
print('aiter')
11+
return self
12+
13+
async def __anext__(self):
14+
print('anext')
15+
try:
16+
value = next(self._it)
17+
except StopIteration:
18+
raise StopAsyncIteration
19+
return value
20+
21+
async def coro():
22+
async for letter in AsyncIteratorWrapper('abc'):
23+
print(letter)
24+
25+
o = coro()
26+
try:
27+
o.send(None)
28+
except StopIteration:
29+
print('finished')

tests/basics/async_for2.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# test waiting within "async for" aiter/anext functions
2+
3+
import sys
4+
if sys.implementation.name == 'micropython':
5+
# uPy allows normal generators to be awaitables
6+
coroutine = lambda f: f
7+
else:
8+
import types
9+
coroutine = types.coroutine
10+
11+
@coroutine
12+
def f(x):
13+
print('f start:', x)
14+
yield x + 1
15+
yield x + 2
16+
return x + 3
17+
18+
class ARange:
19+
def __init__(self, high):
20+
print('init')
21+
self.cur = 0
22+
self.high = high
23+
24+
async def __aiter__(self):
25+
print('aiter')
26+
print('f returned:', await f(10))
27+
return self
28+
29+
async def __anext__(self):
30+
print('anext')
31+
print('f returned:', await f(20))
32+
if self.cur < self.high:
33+
val = self.cur
34+
self.cur += 1
35+
return val
36+
else:
37+
raise StopAsyncIteration
38+
39+
async def coro():
40+
async for x in ARange(4):
41+
print('x', x)
42+
43+
o = coro()
44+
try:
45+
while True:
46+
print('coro yielded:', o.send(None))
47+
except StopIteration:
48+
print('finished')

tests/basics/async_with.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# test simple async with execution
2+
3+
class AContext:
4+
async def __aenter__(self):
5+
print('enter')
6+
async def __aexit__(self, exc_type, exc, tb):
7+
print('exit')
8+
9+
async def f():
10+
async with AContext():
11+
print('body')
12+
13+
o = f()
14+
try:
15+
o.send(None)
16+
except StopIteration:
17+
print('finished')

tests/basics/async_with2.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# test waiting within async with enter/exit functions
2+
3+
import sys
4+
if sys.implementation.name == 'micropython':
5+
# uPy allows normal generators to be awaitables
6+
coroutine = lambda f: f
7+
else:
8+
import types
9+
coroutine = types.coroutine
10+
11+
@coroutine
12+
def f(x):
13+
print('f start:', x)
14+
yield x + 1
15+
yield x + 2
16+
return x + 3
17+
18+
class AContext:
19+
async def __aenter__(self):
20+
print('enter')
21+
print('f returned:', await f(10))
22+
async def __aexit__(self, exc_type, exc, tb):
23+
print('exit')
24+
print('f returned:', await f(20))
25+
26+
async def coro():
27+
async with AContext():
28+
print('body start')
29+
print('body f returned:', await f(30))
30+
print('body end')
31+
32+
o = coro()
33+
try:
34+
while True:
35+
print('coro yielded:', o.send(None))
36+
except StopIteration:
37+
print('finished')

0 commit comments

Comments
 (0)