run-async.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # async test cases (compile and run)
  2. [case testAsync]
  3. import asyncio
  4. async def h() -> int:
  5. return 1
  6. async def g() -> int:
  7. await asyncio.sleep(0)
  8. return await h()
  9. async def f() -> int:
  10. return await g()
  11. [file asyncio/__init__.pyi]
  12. async def sleep(t: float) -> None: ...
  13. [typing fixtures/typing-full.pyi]
  14. [file driver.py]
  15. from native import f
  16. import asyncio
  17. result = asyncio.run(f())
  18. assert result == 1
  19. [case testAsyncWith]
  20. from testutil import async_val
  21. class async_ctx:
  22. async def __aenter__(self) -> str:
  23. await async_val("enter")
  24. return "test"
  25. async def __aexit__(self, x, y, z) -> None:
  26. await async_val("exit")
  27. async def async_with() -> str:
  28. async with async_ctx() as x:
  29. return await async_val("body")
  30. [file driver.py]
  31. from native import async_with
  32. from testutil import run_generator
  33. yields, val = run_generator(async_with(), [None, 'x', None])
  34. assert yields == ('enter', 'body', 'exit'), yields
  35. assert val == 'x', val
  36. [case testAsyncReturn]
  37. from testutil import async_val
  38. async def async_return() -> str:
  39. try:
  40. return 'test'
  41. finally:
  42. await async_val('foo')
  43. [file driver.py]
  44. from native import async_return
  45. from testutil import run_generator
  46. yields, val = run_generator(async_return())
  47. assert yields == ('foo',)
  48. assert val == 'test', val
  49. [case testAsyncFor]
  50. from typing import AsyncIterable, List, Set, Dict
  51. async def async_iter(xs: AsyncIterable[int]) -> List[int]:
  52. ys = []
  53. async for x in xs:
  54. ys.append(x)
  55. return ys
  56. async def async_comp(xs: AsyncIterable[int]) -> List[int]:
  57. ys = [x async for x in xs]
  58. return ys
  59. async def async_comp_set(xs: AsyncIterable[int]) -> Set[int]:
  60. return {x async for x in xs}
  61. async def async_comp_dict(xs: AsyncIterable[int]) -> Dict[int, str]:
  62. return {x: str(x) async for x in xs}
  63. [typing fixtures/typing-full.pyi]
  64. [file driver.py]
  65. from native import async_iter, async_comp, async_comp_set, async_comp_dict
  66. from testutil import run_generator, async_val
  67. from typing import AsyncIterable, List
  68. # defined here since we couldn't do it inside the test yet...
  69. async def foo() -> AsyncIterable[int]:
  70. for x in range(3):
  71. await async_val(x)
  72. yield x
  73. yields, val = run_generator(async_iter(foo()))
  74. assert val == [0,1,2], val
  75. assert yields == (0,1,2), yields
  76. yields, val = run_generator(async_comp(foo()))
  77. assert val == [0,1,2], val
  78. assert yields == (0,1,2), yields
  79. yields, val = run_generator(async_comp_set(foo()))
  80. assert val == {0,1,2}, val
  81. assert yields == (0,1,2), yields
  82. yields, val = run_generator(async_comp_dict(foo()))
  83. assert val == {0: '0',1: '1', 2: '2'}, val
  84. assert yields == (0,1,2), yields
  85. [case testAsyncFor2]
  86. from typing import AsyncIterable, List
  87. async def async_iter(xs: AsyncIterable[int]) -> List[int]:
  88. ys = []
  89. async for x in xs:
  90. ys.append(x)
  91. return ys
  92. [typing fixtures/typing-full.pyi]
  93. [file driver.py]
  94. from native import async_iter
  95. from testutil import run_generator, async_val
  96. from typing import AsyncIterable, List
  97. # defined here since we couldn't do it inside the test yet...
  98. async def foo() -> AsyncIterable[int]:
  99. for x in range(3):
  100. await async_val(x)
  101. yield x
  102. raise Exception('lol no')
  103. yields, val = run_generator(async_iter(foo()))
  104. assert yields == (0,1,2), yields
  105. assert val == 'lol no', val