test_buffer.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from __future__ import unicode_literals
  2. from prompt_toolkit.buffer import Buffer
  3. import pytest
  4. @pytest.fixture
  5. def _buffer():
  6. return Buffer()
  7. def test_initial(_buffer):
  8. assert _buffer.text == ''
  9. assert _buffer.cursor_position == 0
  10. def test_insert_text(_buffer):
  11. _buffer.insert_text('some_text')
  12. assert _buffer.text == 'some_text'
  13. assert _buffer.cursor_position == len('some_text')
  14. def test_cursor_movement(_buffer):
  15. _buffer.insert_text('some_text')
  16. _buffer.cursor_left()
  17. _buffer.cursor_left()
  18. _buffer.cursor_left()
  19. _buffer.cursor_right()
  20. _buffer.insert_text('A')
  21. assert _buffer.text == 'some_teAxt'
  22. assert _buffer.cursor_position == len('some_teA')
  23. def test_backspace(_buffer):
  24. _buffer.insert_text('some_text')
  25. _buffer.cursor_left()
  26. _buffer.cursor_left()
  27. _buffer.delete_before_cursor()
  28. assert _buffer.text == 'some_txt'
  29. assert _buffer.cursor_position == len('some_t')
  30. def test_cursor_up(_buffer):
  31. # Cursor up to a line thats longer.
  32. _buffer.insert_text('long line1\nline2')
  33. _buffer.cursor_up()
  34. assert _buffer.document.cursor_position == 5
  35. # Going up when already at the top.
  36. _buffer.cursor_up()
  37. assert _buffer.document.cursor_position == 5
  38. # Going up to a line that's shorter.
  39. _buffer.reset()
  40. _buffer.insert_text('line1\nlong line2')
  41. _buffer.cursor_up()
  42. assert _buffer.document.cursor_position == 5
  43. def test_cursor_down(_buffer):
  44. _buffer.insert_text('line1\nline2')
  45. _buffer.cursor_position = 3
  46. # Normally going down
  47. _buffer.cursor_down()
  48. assert _buffer.document.cursor_position == len('line1\nlin')
  49. # Going down to a line that's storter.
  50. _buffer.reset()
  51. _buffer.insert_text('long line1\na\nb')
  52. _buffer.cursor_position = 3
  53. _buffer.cursor_down()
  54. assert _buffer.document.cursor_position == len('long line1\na')
  55. def test_join_next_line(_buffer):
  56. _buffer.insert_text('line1\nline2\nline3')
  57. _buffer.cursor_up()
  58. _buffer.join_next_line()
  59. assert _buffer.text == 'line1\nline2 line3'
  60. # Test when there is no '\n' in the text
  61. _buffer.reset()
  62. _buffer.insert_text('line1')
  63. _buffer.cursor_position = 0
  64. _buffer.join_next_line()
  65. assert _buffer.text == 'line1'
  66. def test_newline(_buffer):
  67. _buffer.insert_text('hello world')
  68. _buffer.newline()
  69. assert _buffer.text == 'hello world\n'
  70. def test_swap_characters_before_cursor(_buffer):
  71. _buffer.insert_text('hello world')
  72. _buffer.cursor_left()
  73. _buffer.cursor_left()
  74. _buffer.swap_characters_before_cursor()
  75. assert _buffer.text == 'hello wrold'