Skip to content

Commit

Permalink
cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeier committed Sep 5, 2024
1 parent 6332673 commit aec9957
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions tests/test_gen_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ def test_non_async_syntax_error():
_parse_code("foo = None\n bar = None", src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS)


def test_no_async_roundtrip():
code = "None"
assert not _needs_async_handling(code, src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS)

code_unparsed = ast.unparse(_parse_code(code, src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS))

assert code_unparsed == code


@needs_ast_unparse
@pytest.mark.parametrize(
("code", "needs"),
"code",
[
pytest.param("None", False, id="no_async"),
pytest.param(
dedent(
"""
Expand All @@ -42,7 +50,6 @@ async def afn():
assert await afn()
"""
),
True,
id="await",
),
pytest.param(
Expand All @@ -55,7 +62,6 @@ async def agen():
assert item
"""
),
True,
id="async_for",
),
pytest.param(
Expand All @@ -67,7 +73,6 @@ async def agen():
assert [item async for item in agen()] == [True]
"""
),
True,
id="async_comprehension",
),
pytest.param(
Expand All @@ -83,23 +88,21 @@ async def acm():
assert ctx
"""
),
True,
id="async_context_manager",
),
],
)
def test_async_handling(code, needs):
assert _needs_async_handling(code, src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS) is needs
def test_async_handling(code):
assert _needs_async_handling(code, src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS)

# Since AST objects are quite involved to compare, we unparse again and check that nothing has changed. Note that
# since we are dealing with AST and not CST here, all whitespace is eliminated in the process and this needs to be
# reflected in the input as well.
code_stripped = "\n".join(line for line in code.splitlines() if line)
code_unparsed = ast.unparse(_parse_code(code, src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS))
assert (code_unparsed == code_stripped) ^ needs
assert code_unparsed != code_stripped

if needs:
assert not _needs_async_handling(code_unparsed, src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS)
assert not _needs_async_handling(code_unparsed, src_file=SRC_FILE, compiler_flags=COMPILER_FLAGS)

exec(COMPILER(code_unparsed, SRC_FILE, "exec"), make_globals())

Expand Down

0 comments on commit aec9957

Please sign in to comment.