test_prefix.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. try:
  2. from itertools import zip_longest
  3. except ImportError:
  4. # Python 2
  5. from itertools import izip_longest as zip_longest
  6. from codecs import BOM_UTF8
  7. import pytest
  8. import parso
  9. unicode_bom = BOM_UTF8.decode('utf-8')
  10. @pytest.mark.parametrize(('string', 'tokens'), [
  11. ('', ['']),
  12. ('#', ['#', '']),
  13. (' # ', ['# ', '']),
  14. (' # \n', ['# ', '\n', '']),
  15. (' # \f\n', ['# ', '\f', '\n', '']),
  16. (' \n', ['\n', '']),
  17. (' \n ', ['\n', ' ']),
  18. (' \f ', ['\f', ' ']),
  19. (' \f ', ['\f', ' ']),
  20. (' \r\n', ['\r\n', '']),
  21. ('\\\n', ['\\\n', '']),
  22. ('\\\r\n', ['\\\r\n', '']),
  23. ('\t\t\n\t', ['\n', '\t']),
  24. ])
  25. def test_simple_prefix_splitting(string, tokens):
  26. tree = parso.parse(string)
  27. leaf = tree.children[0]
  28. assert leaf.type == 'endmarker'
  29. parsed_tokens = list(leaf._split_prefix())
  30. start_pos = (1, 0)
  31. for pt, expected in zip_longest(parsed_tokens, tokens):
  32. assert pt.value == expected
  33. # Calculate the estimated end_pos
  34. if expected.endswith('\n'):
  35. end_pos = start_pos[0] + 1, 0
  36. else:
  37. end_pos = start_pos[0], start_pos[1] + len(expected) + len(pt.spacing)
  38. #assert start_pos == pt.start_pos
  39. assert end_pos == pt.end_pos
  40. start_pos = end_pos
  41. @pytest.mark.parametrize(('string', 'types'), [
  42. ('# ', ['comment', 'spacing']),
  43. ('\r\n', ['newline', 'spacing']),
  44. ('\f', ['formfeed', 'spacing']),
  45. ('\\\n', ['backslash', 'spacing']),
  46. (' \t', ['spacing']),
  47. (' \t ', ['spacing']),
  48. (unicode_bom + ' # ', ['bom', 'comment', 'spacing']),
  49. ])
  50. def test_prefix_splitting_types(string, types):
  51. tree = parso.parse(string)
  52. leaf = tree.children[0]
  53. assert leaf.type == 'endmarker'
  54. parsed_tokens = list(leaf._split_prefix())
  55. assert [t.type for t in parsed_tokens] == types
  56. def test_utf8_bom():
  57. tree = parso.parse(unicode_bom + 'a = 1')
  58. expr_stmt = tree.children[0]
  59. assert expr_stmt.start_pos == (1, 0)
  60. tree = parso.parse(unicode_bom + '\n')
  61. endmarker = tree.children[0]
  62. parts = list(endmarker._split_prefix())
  63. assert [p.type for p in parts] == ['bom', 'newline', 'spacing']
  64. assert [p.start_pos for p in parts] == [(1, 0), (1, 0), (2, 0)]
  65. assert [p.end_pos for p in parts] == [(1, 0), (2, 0), (2, 0)]