test_readline.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Very minimal unittests for parts of the readline module.
  3. """
  4. import os
  5. import unittest
  6. from test.test_support import run_unittest, import_module
  7. from test.script_helper import assert_python_ok
  8. # Skip tests if there is no readline module
  9. readline = import_module('readline')
  10. class TestHistoryManipulation (unittest.TestCase):
  11. """These tests were added to check that the libedit emulation on OSX and
  12. the "real" readline have the same interface for history manipulation.
  13. That's why the tests cover only a small subset of the interface.
  14. """
  15. @unittest.skipUnless(hasattr(readline, "clear_history"),
  16. "The history update test cannot be run because the "
  17. "clear_history method is not available.")
  18. def testHistoryUpdates(self):
  19. readline.clear_history()
  20. readline.add_history("first line")
  21. readline.add_history("second line")
  22. self.assertEqual(readline.get_history_item(0), None)
  23. self.assertEqual(readline.get_history_item(1), "first line")
  24. self.assertEqual(readline.get_history_item(2), "second line")
  25. readline.replace_history_item(0, "replaced line")
  26. self.assertEqual(readline.get_history_item(0), None)
  27. self.assertEqual(readline.get_history_item(1), "replaced line")
  28. self.assertEqual(readline.get_history_item(2), "second line")
  29. self.assertEqual(readline.get_current_history_length(), 2)
  30. readline.remove_history_item(0)
  31. self.assertEqual(readline.get_history_item(0), None)
  32. self.assertEqual(readline.get_history_item(1), "second line")
  33. self.assertEqual(readline.get_current_history_length(), 1)
  34. class TestReadline(unittest.TestCase):
  35. @unittest.skipIf(readline._READLINE_VERSION < 0x0600
  36. and "libedit" not in readline.__doc__,
  37. "not supported in this library version")
  38. def test_init(self):
  39. # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
  40. # written into stdout when the readline module is imported and stdout
  41. # is redirected to a pipe.
  42. rc, stdout, stderr = assert_python_ok('-c', 'import readline',
  43. TERM='xterm-256color')
  44. self.assertEqual(stdout, b'')
  45. def test_main():
  46. run_unittest(TestHistoryManipulation, TestReadline)
  47. if __name__ == "__main__":
  48. test_main()