test_buffer.py 2.6 KB

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