test_old_fast_parser.py 3.5 KB

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