Skip to content

Commit

Permalink
Add tests for uses_top_level_await
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffersGlass committed Nov 16, 2022
1 parent 9914237 commit 6390536
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pyscriptjs/tests/py-unit/test_pyscript.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import textwrap
from unittest.mock import Mock

import pyscript
Expand Down Expand Up @@ -48,3 +49,71 @@ def test_format_mime_HTML():
out, mime = pyscript.format_mime(obj)
assert out == "<p>hello</p>"
assert mime == "text/html"


def test_uses_top_level_await():
# Basic Case
src = "x = 1"
assert pyscript.uses_top_level_await(src) is False

# Comments are not top-level await
src = textwrap.dedent(
"""
#await async for async with asyncio
"""
)

assert pyscript.uses_top_level_await(src) is False

# Top-level-await cases
src = textwrap.dedent(
"""
async def foo():
pass
await foo
"""
)
assert pyscript.uses_top_level_await(src) is True

src = textwrap.dedent(
"""
async with object():
pass
"""
)
assert pyscript.uses_top_level_await(src) is True

src = textwrap.dedent(
"""
async for _ in range(10):
pass
"""
)
assert pyscript.uses_top_level_await(src) is True

# Acceptable await/async for/async with cases
src = textwrap.dedent(
"""
async def foo():
await foo()
"""
)
assert pyscript.uses_top_level_await(src) is False

src = textwrap.dedent(
"""
async def foo():
async with object():
pass
"""
)
assert pyscript.uses_top_level_await(src) is False

src = textwrap.dedent(
"""
async def foo():
async for _ in range(10):
pass
"""
)
assert pyscript.uses_top_level_await(src) is False

0 comments on commit 6390536

Please sign in to comment.