test_history.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from __future__ import annotations
  2. from asyncio import run
  3. from prompt_toolkit.history import FileHistory, InMemoryHistory, ThreadedHistory
  4. def _call_history_load(history):
  5. """
  6. Helper: Call the history "load" method and return the result as a list of strings.
  7. """
  8. result = []
  9. async def call_load():
  10. async for item in history.load():
  11. result.append(item)
  12. run(call_load())
  13. return result
  14. def test_in_memory_history():
  15. history = InMemoryHistory()
  16. history.append_string("hello")
  17. history.append_string("world")
  18. # Newest should yield first.
  19. assert _call_history_load(history) == ["world", "hello"]
  20. # Test another call.
  21. assert _call_history_load(history) == ["world", "hello"]
  22. history.append_string("test3")
  23. assert _call_history_load(history) == ["test3", "world", "hello"]
  24. # Passing history as a parameter.
  25. history2 = InMemoryHistory(["abc", "def"])
  26. assert _call_history_load(history2) == ["def", "abc"]
  27. def test_file_history(tmpdir):
  28. histfile = tmpdir.join("history")
  29. history = FileHistory(histfile)
  30. history.append_string("hello")
  31. history.append_string("world")
  32. # Newest should yield first.
  33. assert _call_history_load(history) == ["world", "hello"]
  34. # Test another call.
  35. assert _call_history_load(history) == ["world", "hello"]
  36. history.append_string("test3")
  37. assert _call_history_load(history) == ["test3", "world", "hello"]
  38. # Create another history instance pointing to the same file.
  39. history2 = FileHistory(histfile)
  40. assert _call_history_load(history2) == ["test3", "world", "hello"]
  41. def test_threaded_file_history(tmpdir):
  42. histfile = tmpdir.join("history")
  43. history = ThreadedHistory(FileHistory(histfile))
  44. history.append_string("hello")
  45. history.append_string("world")
  46. # Newest should yield first.
  47. assert _call_history_load(history) == ["world", "hello"]
  48. # Test another call.
  49. assert _call_history_load(history) == ["world", "hello"]
  50. history.append_string("test3")
  51. assert _call_history_load(history) == ["test3", "world", "hello"]
  52. # Create another history instance pointing to the same file.
  53. history2 = ThreadedHistory(FileHistory(histfile))
  54. assert _call_history_load(history2) == ["test3", "world", "hello"]
  55. def test_threaded_in_memory_history():
  56. # Threaded in memory history is not useful. But testing it anyway, just to
  57. # see whether everything plays nicely together.
  58. history = ThreadedHistory(InMemoryHistory())
  59. history.append_string("hello")
  60. history.append_string("world")
  61. # Newest should yield first.
  62. assert _call_history_load(history) == ["world", "hello"]
  63. # Test another call.
  64. assert _call_history_load(history) == ["world", "hello"]
  65. history.append_string("test3")
  66. assert _call_history_load(history) == ["test3", "world", "hello"]
  67. # Passing history as a parameter.
  68. history2 = ThreadedHistory(InMemoryHistory(["abc", "def"]))
  69. assert _call_history_load(history2) == ["def", "abc"]