test_old_fast_parser.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """
  2. These tests test the cases that the old fast parser tested with the normal
  3. parser.
  4. The old fast parser doesn't exist anymore and was replaced with a diff parser.
  5. However the tests might still be relevant for the parser.
  6. """
  7. from textwrap import dedent
  8. from parso._compatibility import u
  9. from parso import parse
  10. def test_carriage_return_splitting():
  11. source = u(dedent('''
  12. "string"
  13. class Foo():
  14. pass
  15. '''))
  16. source = source.replace('\n', '\r\n')
  17. module = parse(source)
  18. assert [n.value for lst in module.get_used_names().values() for n in lst] == ['Foo']
  19. def check_p(src, number_parsers_used, number_of_splits=None, number_of_misses=0):
  20. if number_of_splits is None:
  21. number_of_splits = number_parsers_used
  22. module_node = parse(src)
  23. assert src == module_node.get_code()
  24. return module_node
  25. def test_for():
  26. src = dedent("""\
  27. for a in [1,2]:
  28. a
  29. for a1 in 1,"":
  30. a1
  31. """)
  32. check_p(src, 1)
  33. def test_class_with_class_var():
  34. src = dedent("""\
  35. class SuperClass:
  36. class_super = 3
  37. def __init__(self):
  38. self.foo = 4
  39. pass
  40. """)
  41. check_p(src, 3)
  42. def test_func_with_if():
  43. src = dedent("""\
  44. def recursion(a):
  45. if foo:
  46. return recursion(a)
  47. else:
  48. if bar:
  49. return inexistent
  50. else:
  51. return a
  52. """)
  53. check_p(src, 1)
  54. def test_decorator():
  55. src = dedent("""\
  56. class Decorator():
  57. @memoize
  58. def dec(self, a):
  59. return a
  60. """)
  61. check_p(src, 2)
  62. def test_nested_funcs():
  63. src = dedent("""\
  64. def memoize(func):
  65. def wrapper(*args, **kwargs):
  66. return func(*args, **kwargs)
  67. return wrapper
  68. """)
  69. check_p(src, 3)
  70. def test_multi_line_params():
  71. src = dedent("""\
  72. def x(a,
  73. b):
  74. pass
  75. foo = 1
  76. """)
  77. check_p(src, 2)
  78. def test_class_func_if():
  79. src = dedent("""\
  80. class Class:
  81. def func(self):
  82. if 1:
  83. a
  84. else:
  85. b
  86. pass
  87. """)
  88. check_p(src, 3)
  89. def test_multi_line_for():
  90. src = dedent("""\
  91. for x in [1,
  92. 2]:
  93. pass
  94. pass
  95. """)
  96. check_p(src, 1)
  97. def test_wrong_indentation():
  98. src = dedent("""\
  99. def func():
  100. a
  101. b
  102. a
  103. """)
  104. #check_p(src, 1)
  105. src = dedent("""\
  106. def complex():
  107. def nested():
  108. a
  109. b
  110. a
  111. def other():
  112. pass
  113. """)
  114. check_p(src, 3)
  115. def test_strange_parentheses():
  116. src = dedent("""
  117. class X():
  118. a = (1
  119. if 1 else 2)
  120. def x():
  121. pass
  122. """)
  123. check_p(src, 2)
  124. def test_fake_parentheses():
  125. """
  126. The fast parser splitting counts parentheses, but not as correct tokens.
  127. Therefore parentheses in string tokens are included as well. This needs to
  128. be accounted for.
  129. """
  130. src = dedent(r"""
  131. def x():
  132. a = (')'
  133. if 1 else 2)
  134. def y():
  135. pass
  136. def z():
  137. pass
  138. """)
  139. check_p(src, 3, 2, 1)
  140. def test_additional_indent():
  141. source = dedent('''\
  142. int(
  143. def x():
  144. pass
  145. ''')
  146. check_p(source, 2)
  147. def test_round_trip():
  148. code = dedent('''
  149. def x():
  150. """hahaha"""
  151. func''')
  152. assert parse(code).get_code() == code
  153. def test_parentheses_in_string():
  154. code = dedent('''
  155. def x():
  156. '('
  157. import abc
  158. abc.''')
  159. check_p(code, 2, 1, 1)