test_document.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import unicode_literals
  2. import pytest
  3. from prompt_toolkit.document import Document
  4. @pytest.fixture
  5. def document():
  6. return Document(
  7. 'line 1\n' +
  8. 'line 2\n' +
  9. 'line 3\n' +
  10. 'line 4\n',
  11. len('line 1\n' + 'lin')
  12. )
  13. def test_current_char(document):
  14. assert document.current_char == 'e'
  15. def test_text_before_cursor(document):
  16. assert document.text_before_cursor == 'line 1\nlin'
  17. def test_text_after_cursor(document):
  18. assert document.text_after_cursor == 'e 2\n' + \
  19. 'line 3\n' + \
  20. 'line 4\n'
  21. def test_lines(document):
  22. assert document.lines == [
  23. 'line 1',
  24. 'line 2',
  25. 'line 3',
  26. 'line 4', '']
  27. def test_line_count(document):
  28. assert document.line_count == 5
  29. def test_current_line_before_cursor(document):
  30. assert document.current_line_before_cursor == 'lin'
  31. def test_current_line_after_cursor(document):
  32. assert document.current_line_after_cursor == 'e 2'
  33. def test_current_line(document):
  34. assert document.current_line == 'line 2'
  35. def test_cursor_position(document):
  36. assert document.cursor_position_row == 1
  37. assert document.cursor_position_col == 3
  38. d = Document('', 0)
  39. assert d.cursor_position_row == 0
  40. assert d.cursor_position_col == 0
  41. def test_translate_index_to_position(document):
  42. pos = document.translate_index_to_position(
  43. len('line 1\nline 2\nlin'))
  44. assert pos[0] == 2
  45. assert pos[1] == 3
  46. pos = document.translate_index_to_position(0)
  47. assert pos == (0, 0)