Skip to content

Commit

Permalink
Split out test for token in f string for py312
Browse files Browse the repository at this point in the history
See gristlabs/asttokens#109 for example of
Python 3.12's change.
  • Loading branch information
jamescooke committed Sep 25, 2023
1 parent 594e00e commit f66439b
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions tests/helpers/test_get_first_token.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import pytest
from asttokens.util import Token

Expand All @@ -10,7 +12,7 @@ def f():
do_thing()
''',
])
def test(first_node_with_tokens):
def test(first_node_with_tokens) -> None:
result = get_first_token(first_node_with_tokens)

assert isinstance(result, Token)
Expand All @@ -25,13 +27,39 @@ def test(first_node_with_tokens):
('''
r'hello world'
''', 'r\'hello world\''),
('''
f"hello world"
''', 'f"hello world"'),
]
)
def test_strings(first_node_with_tokens, expected):
def test_strings(first_node_with_tokens, expected: str) -> None:
result = get_first_token(first_node_with_tokens)

assert isinstance(result, Token)
assert result.string == expected


@pytest.mark.skipif(sys.version_info >= (3, 12), reason='Requires Python 3.11 or lower')
@pytest.mark.parametrize('code_str', ['''
f"hello world"
'''])
def test_f_string(first_node_with_tokens) -> None:
result = get_first_token(first_node_with_tokens)

assert isinstance(result, Token)
assert result.string == 'f"hello world"'


@pytest.mark.skipif(sys.version_info < (3, 12), reason="Requires Python 3.12 or higher")
@pytest.mark.parametrize('code_str', ['''
f"hello world"
'''])
def test_f_string_tokens(first_node_with_tokens, expected: str) -> None:
"""
Python 3.12 has tokens for f-strings, where previous versions didn't.
This test added while upgrading to py312, but there were no integration
tests that this affected, just internal checks. More info on the f-string
change: https://github.com/gristlabs/asttokens/issues/109
"""
result = get_first_token(first_node_with_tokens)

assert isinstance(result, Token)
assert result.string == 'f"' # FSTRING_START token

0 comments on commit f66439b

Please sign in to comment.