test_yank_nth_arg.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import annotations
  2. import pytest
  3. from prompt_toolkit.buffer import Buffer
  4. from prompt_toolkit.history import InMemoryHistory
  5. @pytest.fixture
  6. def _history():
  7. "Prefilled history."
  8. history = InMemoryHistory()
  9. history.append_string("alpha beta gamma delta")
  10. history.append_string("one two three four")
  11. return history
  12. # Test yank_last_arg.
  13. def test_empty_history():
  14. buf = Buffer()
  15. buf.yank_last_arg()
  16. assert buf.document.current_line == ""
  17. def test_simple_search(_history):
  18. buff = Buffer(history=_history)
  19. buff.yank_last_arg()
  20. assert buff.document.current_line == "four"
  21. def test_simple_search_with_quotes(_history):
  22. _history.append_string("""one two "three 'x' four"\n""")
  23. buff = Buffer(history=_history)
  24. buff.yank_last_arg()
  25. assert buff.document.current_line == '''"three 'x' four"'''
  26. def test_simple_search_with_arg(_history):
  27. buff = Buffer(history=_history)
  28. buff.yank_last_arg(n=2)
  29. assert buff.document.current_line == "three"
  30. def test_simple_search_with_arg_out_of_bounds(_history):
  31. buff = Buffer(history=_history)
  32. buff.yank_last_arg(n=8)
  33. assert buff.document.current_line == ""
  34. def test_repeated_search(_history):
  35. buff = Buffer(history=_history)
  36. buff.yank_last_arg()
  37. buff.yank_last_arg()
  38. assert buff.document.current_line == "delta"
  39. def test_repeated_search_with_wraparound(_history):
  40. buff = Buffer(history=_history)
  41. buff.yank_last_arg()
  42. buff.yank_last_arg()
  43. buff.yank_last_arg()
  44. assert buff.document.current_line == "four"
  45. # Test yank_last_arg.
  46. def test_yank_nth_arg(_history):
  47. buff = Buffer(history=_history)
  48. buff.yank_nth_arg()
  49. assert buff.document.current_line == "two"
  50. def test_repeated_yank_nth_arg(_history):
  51. buff = Buffer(history=_history)
  52. buff.yank_nth_arg()
  53. buff.yank_nth_arg()
  54. assert buff.document.current_line == "beta"
  55. def test_yank_nth_arg_with_arg(_history):
  56. buff = Buffer(history=_history)
  57. buff.yank_nth_arg(n=2)
  58. assert buff.document.current_line == "three"