test_error_recovery.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from textwrap import dedent
  2. from parso import parse, load_grammar
  3. def test_with_stmt():
  4. module = parse('with x: f.\na')
  5. assert module.children[0].type == 'with_stmt'
  6. w, with_item, colon, f = module.children[0].children
  7. assert f.type == 'error_node'
  8. assert f.get_code(include_prefix=False) == 'f.'
  9. assert module.children[2].type == 'name'
  10. def test_one_line_function(each_version):
  11. module = parse('def x(): f.', version=each_version)
  12. assert module.children[0].type == 'funcdef'
  13. def_, name, parameters, colon, f = module.children[0].children
  14. assert f.type == 'error_node'
  15. module = parse('def x(a:', version=each_version)
  16. func = module.children[0]
  17. assert func.type == 'error_node'
  18. if each_version.startswith('2'):
  19. assert func.children[-1].value == 'a'
  20. else:
  21. assert func.children[-1] == ':'
  22. def test_if_else():
  23. module = parse('if x:\n f.\nelse:\n g(')
  24. if_stmt = module.children[0]
  25. if_, test, colon, suite1, else_, colon, suite2 = if_stmt.children
  26. f = suite1.children[1]
  27. assert f.type == 'error_node'
  28. assert f.children[0].value == 'f'
  29. assert f.children[1].value == '.'
  30. g = suite2.children[1]
  31. assert g.children[0].value == 'g'
  32. assert g.children[1].value == '('
  33. def test_if_stmt():
  34. module = parse('if x: f.\nelse: g(')
  35. if_stmt = module.children[0]
  36. assert if_stmt.type == 'if_stmt'
  37. if_, test, colon, f = if_stmt.children
  38. assert f.type == 'error_node'
  39. assert f.children[0].value == 'f'
  40. assert f.children[1].value == '.'
  41. assert module.children[1].type == 'newline'
  42. assert module.children[1].value == '\n'
  43. assert module.children[2].type == 'error_leaf'
  44. assert module.children[2].value == 'else'
  45. assert module.children[3].type == 'error_leaf'
  46. assert module.children[3].value == ':'
  47. in_else_stmt = module.children[4]
  48. assert in_else_stmt.type == 'error_node'
  49. assert in_else_stmt.children[0].value == 'g'
  50. assert in_else_stmt.children[1].value == '('
  51. def test_invalid_token():
  52. module = parse('a + ? + b')
  53. error_node, q, plus_b, endmarker = module.children
  54. assert error_node.get_code() == 'a +'
  55. assert q.value == '?'
  56. assert q.type == 'error_leaf'
  57. assert plus_b.type == 'factor'
  58. assert plus_b.get_code() == ' + b'
  59. def test_invalid_token_in_fstr():
  60. module = load_grammar(version='3.6').parse('f"{a + ? + b}"')
  61. error_node, q, plus_b, error1, error2, endmarker = module.children
  62. assert error_node.get_code() == 'f"{a +'
  63. assert q.value == '?'
  64. assert q.type == 'error_leaf'
  65. assert plus_b.type == 'error_node'
  66. assert plus_b.get_code() == ' + b'
  67. assert error1.value == '}'
  68. assert error1.type == 'error_leaf'
  69. assert error2.value == '"'
  70. assert error2.type == 'error_leaf'
  71. def test_dedent_issues1():
  72. code = dedent('''\
  73. class C:
  74. @property
  75. f
  76. g
  77. end
  78. ''')
  79. module = load_grammar(version='3.8').parse(code)
  80. klass, endmarker = module.children
  81. suite = klass.children[-1]
  82. assert suite.children[2].type == 'error_leaf'
  83. assert suite.children[3].get_code(include_prefix=False) == 'f\n'
  84. assert suite.children[5].get_code(include_prefix=False) == 'g\n'
  85. assert suite.type == 'suite'
  86. def test_dedent_issues2():
  87. code = dedent('''\
  88. class C:
  89. @property
  90. if 1:
  91. g
  92. else:
  93. h
  94. end
  95. ''')
  96. module = load_grammar(version='3.8').parse(code)
  97. klass, endmarker = module.children
  98. suite = klass.children[-1]
  99. assert suite.children[2].type == 'error_leaf'
  100. if_ = suite.children[3]
  101. assert if_.children[0] == 'if'
  102. assert if_.children[3].type == 'suite'
  103. assert if_.children[3].get_code() == '\n g\n'
  104. assert if_.children[4] == 'else'
  105. assert if_.children[6].type == 'suite'
  106. assert if_.children[6].get_code() == '\n h\n'
  107. assert suite.children[4].get_code(include_prefix=False) == 'end\n'
  108. assert suite.type == 'suite'
  109. def test_dedent_issues3():
  110. code = dedent('''\
  111. class C:
  112. f
  113. g
  114. ''')
  115. module = load_grammar(version='3.8').parse(code)
  116. klass, endmarker = module.children
  117. suite = klass.children[-1]
  118. assert len(suite.children) == 4
  119. assert suite.children[1].get_code() == ' f\n'
  120. assert suite.children[1].type == 'simple_stmt'
  121. assert suite.children[2].get_code() == ''
  122. assert suite.children[2].type == 'error_leaf'
  123. assert suite.children[2].token_type == 'ERROR_DEDENT'
  124. assert suite.children[3].get_code() == ' g\n'
  125. assert suite.children[3].type == 'simple_stmt'