test_fstring.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import pytest
  2. from textwrap import dedent
  3. from parso import load_grammar, ParserSyntaxError
  4. from parso.python.tokenize import tokenize
  5. @pytest.fixture
  6. def grammar():
  7. return load_grammar(version='3.8')
  8. @pytest.mark.parametrize(
  9. 'code', [
  10. # simple cases
  11. 'f"{1}"',
  12. 'f"""{1}"""',
  13. 'f"{foo} {bar}"',
  14. # empty string
  15. 'f""',
  16. 'f""""""',
  17. # empty format specifier is okay
  18. 'f"{1:}"',
  19. # use of conversion options
  20. 'f"{1!a}"',
  21. 'f"{1!a:1}"',
  22. # format specifiers
  23. 'f"{1:1}"',
  24. 'f"{1:1.{32}}"',
  25. 'f"{1::>4}"',
  26. 'f"{x:{y}}"',
  27. 'f"{x:{y:}}"',
  28. 'f"{x:{y:1}}"',
  29. # Escapes
  30. 'f"{{}}"',
  31. 'f"{{{1}}}"',
  32. 'f"{{{1}"',
  33. 'f"1{{2{{3"',
  34. 'f"}}"',
  35. # New Python 3.8 syntax f'{a=}'
  36. 'f"{a=}"',
  37. 'f"{a()=}"',
  38. # multiline f-string
  39. 'f"""abc\ndef"""',
  40. 'f"""abc{\n123}def"""',
  41. # a line continuation inside of an fstring_string
  42. 'f"abc\\\ndef"',
  43. 'f"\\\n{123}\\\n"',
  44. # a line continuation inside of an fstring_expr
  45. 'f"{\\\n123}"',
  46. # a line continuation inside of an format spec
  47. 'f"{123:.2\\\nf}"',
  48. ]
  49. )
  50. def test_valid(code, grammar):
  51. module = grammar.parse(code, error_recovery=False)
  52. fstring = module.children[0]
  53. assert fstring.type == 'fstring'
  54. assert fstring.get_code() == code
  55. @pytest.mark.parametrize(
  56. 'code', [
  57. # an f-string can't contain unmatched curly braces
  58. 'f"}"',
  59. 'f"{"',
  60. 'f"""}"""',
  61. 'f"""{"""',
  62. # invalid conversion characters
  63. 'f"{1!{a}}"',
  64. 'f"{!{a}}"',
  65. # The curly braces must contain an expression
  66. 'f"{}"',
  67. 'f"{:}"',
  68. 'f"{:}}}"',
  69. 'f"{:1}"',
  70. 'f"{!:}"',
  71. 'f"{!}"',
  72. 'f"{!a}"',
  73. # invalid (empty) format specifiers
  74. 'f"{1:{}}"',
  75. 'f"{1:{:}}"',
  76. # a newline without a line continuation inside a single-line string
  77. 'f"abc\ndef"',
  78. ]
  79. )
  80. def test_invalid(code, grammar):
  81. with pytest.raises(ParserSyntaxError):
  82. grammar.parse(code, error_recovery=False)
  83. # It should work with error recovery.
  84. grammar.parse(code, error_recovery=True)
  85. @pytest.mark.parametrize(
  86. ('code', 'positions'), [
  87. # 2 times 2, 5 because python expr and endmarker.
  88. ('f"}{"', [(1, 0), (1, 2), (1, 3), (1, 4), (1, 5)]),
  89. ('f" :{ 1 : } "', [(1, 0), (1, 2), (1, 4), (1, 6), (1, 8), (1, 9),
  90. (1, 10), (1, 11), (1, 12), (1, 13)]),
  91. ('f"""\n {\nfoo\n }"""', [(1, 0), (1, 4), (2, 1), (3, 0), (4, 1),
  92. (4, 2), (4, 5)]),
  93. ]
  94. )
  95. def test_tokenize_start_pos(code, positions):
  96. tokens = list(tokenize(code, version_info=(3, 6)))
  97. assert positions == [p.start_pos for p in tokens]
  98. @pytest.mark.parametrize(
  99. 'code', [
  100. dedent("""\
  101. f'''s{
  102. str.uppe
  103. '''
  104. """),
  105. 'f"foo',
  106. 'f"""foo',
  107. 'f"abc\ndef"',
  108. ]
  109. )
  110. def test_roundtrip(grammar, code):
  111. tree = grammar.parse(code)
  112. assert tree.get_code() == code