blob: 0b2b0a5c2f4c2aa274258e92b9eb9e4ec7bcc0d7 [file] [log] [blame]
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001"""
2Very minimal unittests for parts of the readline module.
Ronald Oussoren2efd9242009-09-20 14:53:22 +00003"""
Victor Stinnera3c80ce2014-07-24 12:23:56 +02004import os
Ronald Oussoren2efd9242009-09-20 14:53:22 +00005import unittest
Mark Dickinson2d7062e2009-10-26 12:01:06 +00006from test.support import run_unittest, import_module
Victor Stinnera3c80ce2014-07-24 12:23:56 +02007from test.script_helper import assert_python_ok
Ronald Oussoren2efd9242009-09-20 14:53:22 +00008
Mark Dickinson2d7062e2009-10-26 12:01:06 +00009# Skip tests if there is no readline module
10readline = import_module('readline')
Ronald Oussoren2efd9242009-09-20 14:53:22 +000011
12class TestHistoryManipulation (unittest.TestCase):
Victor Stinnera3c80ce2014-07-24 12:23:56 +020013 """
14 These tests were added to check that the libedit emulation on OSX and the
15 "real" readline have the same interface for history manipulation. That's
16 why the tests cover only a small subset of the interface.
17 """
R David Murrayf8b9dfd2011-03-14 17:10:22 -040018
Benjamin Peterson5b535072014-11-26 14:35:56 -060019 @unittest.skipUnless(hasattr(readline, "clear_history"),
20 "The history update test cannot be run because the "
21 "clear_history method is not available.")
Ronald Oussoren2efd9242009-09-20 14:53:22 +000022 def testHistoryUpdates(self):
Georg Brandl7b250a52012-08-11 11:02:14 +020023 readline.clear_history()
Ronald Oussoren2efd9242009-09-20 14:53:22 +000024
25 readline.add_history("first line")
26 readline.add_history("second line")
27
28 self.assertEqual(readline.get_history_item(0), None)
29 self.assertEqual(readline.get_history_item(1), "first line")
30 self.assertEqual(readline.get_history_item(2), "second line")
31
32 readline.replace_history_item(0, "replaced line")
33 self.assertEqual(readline.get_history_item(0), None)
34 self.assertEqual(readline.get_history_item(1), "replaced line")
35 self.assertEqual(readline.get_history_item(2), "second line")
36
37 self.assertEqual(readline.get_current_history_length(), 2)
38
39 readline.remove_history_item(0)
40 self.assertEqual(readline.get_history_item(0), None)
41 self.assertEqual(readline.get_history_item(1), "second line")
42
43 self.assertEqual(readline.get_current_history_length(), 1)
44
45
Victor Stinnera3c80ce2014-07-24 12:23:56 +020046class TestReadline(unittest.TestCase):
Antoine Pitrou7e8b8672014-11-04 14:52:10 +010047
48 @unittest.skipIf(readline._READLINE_VERSION < 0x0600
49 and "libedit" not in readline.__doc__,
50 "not supported in this library version")
Victor Stinnera3c80ce2014-07-24 12:23:56 +020051 def test_init(self):
52 # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
53 # written into stdout when the readline module is imported and stdout
54 # is redirected to a pipe.
55 rc, stdout, stderr = assert_python_ok('-c', 'import readline',
56 TERM='xterm-256color')
57 self.assertEqual(stdout, b'')
58
59
Ronald Oussoren2efd9242009-09-20 14:53:22 +000060def test_main():
Victor Stinnera3c80ce2014-07-24 12:23:56 +020061 run_unittest(TestHistoryManipulation, TestReadline)
Ronald Oussoren2efd9242009-09-20 14:53:22 +000062
63if __name__ == "__main__":
64 test_main()