Skip to content

Commit 3b64cc4

Browse files
committed
Add tests
1 parent 92ab89b commit 3b64cc4

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

mypyc/test-data/run-async.test

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# async test cases (compile and run)
22

3-
[case testRunAsync]
3+
[case testRunAsyncBasics]
44
import asyncio
55

66
async def h() -> int:
@@ -34,6 +34,73 @@ def run(x: object) -> object: ...
3434

3535
[typing fixtures/typing-full.pyi]
3636

37+
[case testRunAsyncAwaitInVariousPositions]
38+
import asyncio
39+
40+
async def one() -> int:
41+
return int() + 1
42+
43+
async def true() -> bool:
44+
return bool(int() + 1)
45+
46+
async def branch_await() -> int:
47+
if await true():
48+
return 3
49+
return 2
50+
51+
async def branch_await_not() -> int:
52+
if not await true():
53+
return 3
54+
return 2
55+
56+
def test_branch() -> None:
57+
assert asyncio.run(branch_await()) == 3
58+
assert asyncio.run(branch_await_not()) == 2
59+
60+
async def assign_local() -> int:
61+
x = await one()
62+
return x + 1
63+
64+
def test_assign_local() -> None:
65+
assert asyncio.run(assign_local()) == 2
66+
67+
class C:
68+
def __init__(self, s: str) -> None:
69+
self.s = s
70+
71+
async def make_c(s: str) -> C:
72+
await one()
73+
return C(s)
74+
75+
async def get_attr(s: str) -> str:
76+
return (await make_c(s)).s
77+
78+
def test_get_attr() -> None:
79+
assert asyncio.run(get_attr("foo")) == "foo"
80+
81+
async def concat(s: str, t: str) -> str:
82+
await one()
83+
return s + t
84+
85+
async def set_attr1(s: str) -> str:
86+
c = await make_c("xyz")
87+
c.s = await concat(s, "!")
88+
return c.s
89+
90+
async def set_attr2(s: str) -> None:
91+
(await make_c("xyz")).s = s
92+
93+
def test_set_attr() -> None:
94+
assert asyncio.run(set_attr1("foo")) == "foo!"
95+
asyncio.run(set_attr2("foo")) # Just check that it compiles and runs
96+
97+
[file asyncio/__init__.pyi]
98+
async def sleep(t: float) -> None: ...
99+
# eh, we could use the real type but it doesn't seem important
100+
def run(x: object) -> object: ...
101+
102+
[typing fixtures/typing-full.pyi]
103+
37104

38105
[case testAsyncWith]
39106
from testutil import async_val
@@ -78,7 +145,6 @@ yields, val = run_generator(async_return())
78145
assert yields == ('foo',)
79146
assert val == 'test', val
80147

81-
82148
[case testAsyncFor]
83149
from typing import AsyncIterable, List, Set, Dict
84150

0 commit comments

Comments
 (0)