From b47b5beb3df7577f2de402d1648c2dc9ea6b5ea7 Mon Sep 17 00:00:00 2001 From: Daniil Kalinin Date: Wed, 7 May 2025 15:20:46 +0200 Subject: [PATCH] PY-79967 Add a couple of tests for template strings to PythonParsingTest GitOrigin-RevId: df2ec2ac92e21fb307cb17f446c2e5eca8e09eb3 --- python/testData/psi/EmptyTemplateStrings.py | 5 ++ python/testData/psi/EmptyTemplateStrings.txt | 35 ++++++++++++ .../psi/FStringInsideTemplateString.py | 1 + .../psi/FStringInsideTemplateString.txt | 18 ++++++ python/testData/psi/NestedTemplateStrings.py | 1 + python/testData/psi/NestedTemplateStrings.txt | 36 ++++++++++++ .../psi/TemplateStringInsideFString.py | 1 + .../psi/TemplateStringInsideFString.txt | 18 ++++++ .../psi/TemplateStringWithFragment.py | 1 + .../psi/TemplateStringWithFragment.txt | 18 ++++++ python/testData/psi/TemplateStrings.py | 5 ++ python/testData/psi/TemplateStrings.txt | 55 +++++++++++++++++++ .../python/parsing/PythonParsingTest.java | 30 ++++++++++ 13 files changed, 224 insertions(+) create mode 100644 python/testData/psi/EmptyTemplateStrings.py create mode 100644 python/testData/psi/EmptyTemplateStrings.txt create mode 100644 python/testData/psi/FStringInsideTemplateString.py create mode 100644 python/testData/psi/FStringInsideTemplateString.txt create mode 100644 python/testData/psi/NestedTemplateStrings.py create mode 100644 python/testData/psi/NestedTemplateStrings.txt create mode 100644 python/testData/psi/TemplateStringInsideFString.py create mode 100644 python/testData/psi/TemplateStringInsideFString.txt create mode 100644 python/testData/psi/TemplateStringWithFragment.py create mode 100644 python/testData/psi/TemplateStringWithFragment.txt create mode 100644 python/testData/psi/TemplateStrings.py create mode 100644 python/testData/psi/TemplateStrings.txt diff --git a/python/testData/psi/EmptyTemplateStrings.py b/python/testData/psi/EmptyTemplateStrings.py new file mode 100644 index 000000000000..a1829832b200 --- /dev/null +++ b/python/testData/psi/EmptyTemplateStrings.py @@ -0,0 +1,5 @@ +s = (t'', + T"", + rt"""""", + TR'''''', + ) \ No newline at end of file diff --git a/python/testData/psi/EmptyTemplateStrings.txt b/python/testData/psi/EmptyTemplateStrings.txt new file mode 100644 index 000000000000..891b51c83f0f --- /dev/null +++ b/python/testData/psi/EmptyTemplateStrings.txt @@ -0,0 +1,35 @@ +PyFile:EmptyTemplateStrings.py + PyAssignmentStatement + PyTargetExpression: s + PsiElement(Py:IDENTIFIER)('s') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyParenthesizedExpression + PsiElement(Py:LPAR)('(') + PyTupleExpression + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t'') + PsiElement(Py:FSTRING_END)(''') + PsiElement(Py:COMMA)(',') + PsiWhiteSpace('\n ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('T"') + PsiElement(Py:FSTRING_END)('"') + PsiElement(Py:COMMA)(',') + PsiWhiteSpace('\n ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('rt"""') + PsiElement(Py:FSTRING_END)('"""') + PsiElement(Py:COMMA)(',') + PsiWhiteSpace('\n ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('TR'''') + PsiElement(Py:FSTRING_END)(''''') + PsiElement(Py:COMMA)(',') + PsiWhiteSpace('\n ') + PsiElement(Py:RPAR)(')') \ No newline at end of file diff --git a/python/testData/psi/FStringInsideTemplateString.py b/python/testData/psi/FStringInsideTemplateString.py new file mode 100644 index 000000000000..472972ca21b9 --- /dev/null +++ b/python/testData/psi/FStringInsideTemplateString.py @@ -0,0 +1 @@ +s = t"{f''}" \ No newline at end of file diff --git a/python/testData/psi/FStringInsideTemplateString.txt b/python/testData/psi/FStringInsideTemplateString.txt new file mode 100644 index 000000000000..7023651fb4c1 --- /dev/null +++ b/python/testData/psi/FStringInsideTemplateString.txt @@ -0,0 +1,18 @@ +PyFile:FStringInsideTemplateString.py + PyAssignmentStatement + PyTargetExpression: s + PsiElement(Py:IDENTIFIER)('s') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: {f''} + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t"') + PyFStringFragment + PsiElement(Py:FSTRING_FRAGMENT_START)('{') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('f'') + PsiElement(Py:FSTRING_END)(''') + PsiElement(Py:FSTRING_FRAGMENT_END)('}') + PsiElement(Py:FSTRING_END)('"') \ No newline at end of file diff --git a/python/testData/psi/NestedTemplateStrings.py b/python/testData/psi/NestedTemplateStrings.py new file mode 100644 index 000000000000..d0027ee5a72d --- /dev/null +++ b/python/testData/psi/NestedTemplateStrings.py @@ -0,0 +1 @@ +s = t"""1{t'''2{t"3{t'4'}"}'''}""" \ No newline at end of file diff --git a/python/testData/psi/NestedTemplateStrings.txt b/python/testData/psi/NestedTemplateStrings.txt new file mode 100644 index 000000000000..e74519d6d353 --- /dev/null +++ b/python/testData/psi/NestedTemplateStrings.txt @@ -0,0 +1,36 @@ +PyFile:NestedTemplateStrings.py + PyAssignmentStatement + PyTargetExpression: s + PsiElement(Py:IDENTIFIER)('s') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: 1{t'''2{t"3{t'4'}"}'''} + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t"""') + PsiElement(Py:FSTRING_TEXT)('1') + PyFStringFragment + PsiElement(Py:FSTRING_FRAGMENT_START)('{') + PyStringLiteralExpression: 2{t"3{t'4'}"} + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t'''') + PsiElement(Py:FSTRING_TEXT)('2') + PyFStringFragment + PsiElement(Py:FSTRING_FRAGMENT_START)('{') + PyStringLiteralExpression: 3{t'4'} + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t"') + PsiElement(Py:FSTRING_TEXT)('3') + PyFStringFragment + PsiElement(Py:FSTRING_FRAGMENT_START)('{') + PyStringLiteralExpression: 4 + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t'') + PsiElement(Py:FSTRING_TEXT)('4') + PsiElement(Py:FSTRING_END)(''') + PsiElement(Py:FSTRING_FRAGMENT_END)('}') + PsiElement(Py:FSTRING_END)('"') + PsiElement(Py:FSTRING_FRAGMENT_END)('}') + PsiElement(Py:FSTRING_END)(''''') + PsiElement(Py:FSTRING_FRAGMENT_END)('}') + PsiElement(Py:FSTRING_END)('"""') \ No newline at end of file diff --git a/python/testData/psi/TemplateStringInsideFString.py b/python/testData/psi/TemplateStringInsideFString.py new file mode 100644 index 000000000000..195e5e79ebc3 --- /dev/null +++ b/python/testData/psi/TemplateStringInsideFString.py @@ -0,0 +1 @@ +s = f"{t''}" \ No newline at end of file diff --git a/python/testData/psi/TemplateStringInsideFString.txt b/python/testData/psi/TemplateStringInsideFString.txt new file mode 100644 index 000000000000..d284116b7886 --- /dev/null +++ b/python/testData/psi/TemplateStringInsideFString.txt @@ -0,0 +1,18 @@ +PyFile:TemplateStringInsideFString.py + PyAssignmentStatement + PyTargetExpression: s + PsiElement(Py:IDENTIFIER)('s') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: {t''} + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('f"') + PyFStringFragment + PsiElement(Py:FSTRING_FRAGMENT_START)('{') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t'') + PsiElement(Py:FSTRING_END)(''') + PsiElement(Py:FSTRING_FRAGMENT_END)('}') + PsiElement(Py:FSTRING_END)('"') \ No newline at end of file diff --git a/python/testData/psi/TemplateStringWithFragment.py b/python/testData/psi/TemplateStringWithFragment.py new file mode 100644 index 000000000000..0ef58b11dcc0 --- /dev/null +++ b/python/testData/psi/TemplateStringWithFragment.py @@ -0,0 +1 @@ +s = t'foo{42}bar' \ No newline at end of file diff --git a/python/testData/psi/TemplateStringWithFragment.txt b/python/testData/psi/TemplateStringWithFragment.txt new file mode 100644 index 000000000000..cd3e3f0977b0 --- /dev/null +++ b/python/testData/psi/TemplateStringWithFragment.txt @@ -0,0 +1,18 @@ +PyFile:TemplateStringWithFragment.py + PyAssignmentStatement + PyTargetExpression: s + PsiElement(Py:IDENTIFIER)('s') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: foo{42}bar + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t'') + PsiElement(Py:FSTRING_TEXT)('foo') + PyFStringFragment + PsiElement(Py:FSTRING_FRAGMENT_START)('{') + PyNumericLiteralExpression + PsiElement(Py:INTEGER_LITERAL)('42') + PsiElement(Py:FSTRING_FRAGMENT_END)('}') + PsiElement(Py:FSTRING_TEXT)('bar') + PsiElement(Py:FSTRING_END)(''') \ No newline at end of file diff --git a/python/testData/psi/TemplateStrings.py b/python/testData/psi/TemplateStrings.py new file mode 100644 index 000000000000..79a79682997b --- /dev/null +++ b/python/testData/psi/TemplateStrings.py @@ -0,0 +1,5 @@ +s1 = t'' +s2 = tr'' +s3 = rt'' +s4 = T'' +s5 = RT'' \ No newline at end of file diff --git a/python/testData/psi/TemplateStrings.txt b/python/testData/psi/TemplateStrings.txt new file mode 100644 index 000000000000..2a6e1e9a5c9f --- /dev/null +++ b/python/testData/psi/TemplateStrings.txt @@ -0,0 +1,55 @@ +PyFile:TemplateStrings.py + PyAssignmentStatement + PyTargetExpression: s1 + PsiElement(Py:IDENTIFIER)('s1') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('t'') + PsiElement(Py:FSTRING_END)(''') + PsiWhiteSpace('\n') + PyAssignmentStatement + PyTargetExpression: s2 + PsiElement(Py:IDENTIFIER)('s2') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('tr'') + PsiElement(Py:FSTRING_END)(''') + PsiWhiteSpace('\n') + PyAssignmentStatement + PyTargetExpression: s3 + PsiElement(Py:IDENTIFIER)('s3') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('rt'') + PsiElement(Py:FSTRING_END)(''') + PsiWhiteSpace('\n') + PyAssignmentStatement + PyTargetExpression: s4 + PsiElement(Py:IDENTIFIER)('s4') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('T'') + PsiElement(Py:FSTRING_END)(''') + PsiWhiteSpace('\n') + PyAssignmentStatement + PyTargetExpression: s5 + PsiElement(Py:IDENTIFIER)('s5') + PsiWhiteSpace(' ') + PsiElement(Py:EQ)('=') + PsiWhiteSpace(' ') + PyStringLiteralExpression: + PyFormattedStringElement + PsiElement(Py:FSTRING_START)('RT'') + PsiElement(Py:FSTRING_END)(''') \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/parsing/PythonParsingTest.java b/python/testSrc/com/jetbrains/python/parsing/PythonParsingTest.java index b7fa3ba654ff..98fe8bdc5c7e 100644 --- a/python/testSrc/com/jetbrains/python/parsing/PythonParsingTest.java +++ b/python/testSrc/com/jetbrains/python/parsing/PythonParsingTest.java @@ -1383,6 +1383,36 @@ public class PythonParsingTest extends ParsingTestCase { doTest(LanguageLevel.PYTHON312); } + // PY-79967 + public void testTemplateStrings() { + doTest(LanguageLevel.PYTHON314); + } + + // PY-79967 + public void testTemplateStringWithFragment() { + doTest(LanguageLevel.PYTHON314); + } + + // PY-79967 + public void testEmptyTemplateStrings() { + doTest(LanguageLevel.PYTHON314); + } + + // PY-79967 + public void testNestedTemplateStrings() { + doTest(LanguageLevel.PYTHON314); + } + + // PY-79967 + public void testTemplateStringInsideFString() { + doTest(LanguageLevel.PYTHON314); + } + + // PY-79967 + public void testFStringInsideTemplateString() { + doTest(LanguageLevel.PYTHON314); + } + public void doTest() { doTest(LanguageLevel.PYTHON26); }